packages/sql-semantics-mysql/src/Statement/Call/Aggregate/GroupConcat.php
1<?php
2
3declare(strict_types=1);
4
5namespace SqlSemantics\Platform\MySql\Statement\Call\Aggregate;
6
7use SqlSemantics\Platform\MySql\Model\Construction\Derivation;
8use SqlSemantics\Platform\MySql\Model\Diagnostic\Check;
9use SqlSemantics\Platform\MySql\Model\Rendering\Output;
10use SqlSemantics\Platform\MySql\Model\Resolution\Environment;
11use SqlSemantics\Platform\MySql\Model\Statement\Fact\ScalarFact;
12use SqlSemantics\Platform\MySql\Model\Statement\Identifier\Name;
13use SqlSemantics\Platform\MySql\Model\Statement\Scalar;
14use SqlSemantics\Platform\MySql\Model\Statement\Snapshot;
15use SqlSemantics\Platform\MySql\Model\Statement\Type\Nullability;
16use SqlSemantics\Platform\MySql\Rules\Call\Arguments;
17use SqlSemantics\Platform\MySql\Rules\Call\ResultTyping;
18use SqlSemantics\Platform\MySql\Rules\Call\Windows;
19use SqlSemantics\Platform\MySql\Rules\Query\Having\HavingScope;
20use SqlSemantics\Platform\MySql\Statement\Call\Problem\UnsupportedWindowing;
21use SqlSemantics\Platform\MySql\Statement\Call\Problem\WindowingLimit;
22use SqlSemantics\Platform\MySql\Statement\Call\SetFunction;
23use SqlSemantics\Platform\MySql\Statement\Call\WindowSpecification;
24use SqlSemantics\Platform\MySql\Statement\Literal\Text;
25use SqlSemantics\Platform\MySql\Statement\Query\OrderItem;
26
27/**
28 * A call of GROUP_CONCAT(): the concatenated non-NULL values of a group, with DISTINCT, an ordering and a separator.
29 *
30 * Rule: MYSQL-GROUP-CONCAT-001. The arguments of one row are concatenated,
31 * the rows in the written order, separated by the separator (a comma by
32 * default). The result is a string in the character set of the arguments,
33 * binary when one is binary; it is NULL when a group has no non-NULL row.
34 * Its length is cut to group_concat_max_len. The grammar of MySQL 8.0 and
35 * later accepts OVER, which the server rejects (ER_NOT_SUPPORTED_YET).
36 * Terminates: the parts are strict parts.
37 * Source: https://dev.mysql.com/doc/refman/8.4/en/aggregate-functions.html#function_group-concat.
38 * Status: Implemented.
39 *
40 * @visibility public
41 * @example Typing GROUP_CONCAT()
42 * $query = (new \SqlSemantics\Platform\MySql\Semantics())->analyze("SELECT GROUP_CONCAT(DISTINCT 'a' SEPARATOR ';')");
43 * [$query->field(0)->type->descriptor->name(), $query->field(0)->nullability] // => ['VARCHAR', \SqlSemantics\Platform\MySql\Model\Statement\Type\Nullability::Nullable]
44 */
45final class GroupConcat implements SetFunction
46{
47 use Snapshot;
48
49 /**
50 * @var list<Scalar> The concatenated expressions in order
51 */
52 public readonly array $arguments;
53
54 /**
55 * @var list<OrderItem> The ordering of the rows
56 */
57 public readonly array $order;
58
59 /**
60 * @param list<Scalar> $arguments The concatenated expressions in order; at least one
61 * @param bool $distinct Whether DISTINCT is written
62 * @param list<OrderItem> $order The ordering of the rows
63 * @param Text|null $separator The separator, when written
64 * @param Name|WindowSpecification|null $over The window written after OVER
65 */
66 public function __construct(
67 array $arguments,
68 public readonly bool $distinct = false,
69 array $order = [],
70 public readonly ?Text $separator = null,
71 public readonly Name|WindowSpecification|null $over = null,
72 ) {
73 $this->arguments = Check::listOf($arguments, Scalar::class, 'GROUP_CONCAT takes at least one expression.', 1);
74 $this->order = Check::listOf($order, OrderItem::class, 'An ordering is a list of ordering items.');
75 }
76
77 /**
78 * Tells whether the call aggregates its query block: it has no window.
79 */
80 public function aggregates(): bool
81 {
82 return $this->over === null;
83 }
84
85 /**
86 * Derives the parts and the result, and reports a window.
87 */
88 public function deriveScalar(Derivation $derivation, Environment $environment): ScalarFact
89 {
90 $environment = $this->aggregates() ? (new HavingScope())->leave($environment) : $environment;
91 $facts = [];
92 foreach ($this->arguments as $argument) {
93 $facts[] = (new Arguments())->one($argument, $derivation, $environment);
94 }
95 foreach ($this->order as $item) {
96 (new Arguments())->one($item->expression, $derivation, $environment);
97 }
98 (new Windows())->derive($this->over, $derivation, $environment);
99 if ($this->over !== null) {
100 $derivation->report(new UnsupportedWindowing(WindowingLimit::GroupConcat));
101 }
102
103 return new ScalarFact((new ResultTyping())->type('S', $facts), Nullability::Nullable);
104 }
105
106 /**
107 * Writes the call and its window.
108 */
109 public function render(Output $out): void
110 {
111 $out->keyword('GROUP_CONCAT')->glue()->symbol('(');
112 if ($this->distinct) {
113 $out->keyword('DISTINCT');
114 }
115 $out->list($this->arguments);
116 if ($this->order !== []) {
117 $out->keyword('ORDER', 'BY')->list($this->order);
118 }
119 if ($this->separator !== null) {
120 $out->keyword('SEPARATOR')->node($this->separator);
121 }
122 $out->symbol(')');
123 (new Windows())->render($this->over, $out);
124 }
125}
126