packages/sql-semantics-mysql/src/Statement/Call/Window/WindowSpec.php
1<?php
2
3declare(strict_types=1);
4
5namespace SqlSemantics\Platform\MySql\Statement\Call\Window;
6
7use SqlSemantics\Platform\MySql\Model\Construction\Derivation;
8use SqlSemantics\Platform\MySql\Model\Contract\NameUse;
9use SqlSemantics\Platform\MySql\Model\Diagnostic\Check;
10use SqlSemantics\Platform\MySql\Model\Rendering\Output;
11use SqlSemantics\Platform\MySql\Model\Resolution\Environment;
12use SqlSemantics\Platform\MySql\Model\Statement\Identifier\Name;
13use SqlSemantics\Platform\MySql\Model\Statement\Snapshot;
14use SqlSemantics\Platform\MySql\Rules\Call\Arguments;
15use SqlSemantics\Platform\MySql\Statement\Call\Problem\UnsupportedWindowing;
16use SqlSemantics\Platform\MySql\Statement\Call\Problem\WindowingLimit;
17use SqlSemantics\Platform\MySql\Statement\Call\WindowSpecification;
18use SqlSemantics\Platform\MySql\Statement\Query\OrderItem;
19
20/**
21 * A parenthesized window specification: an optional window it refines, a partitioning, an ordering and a frame.
22 *
23 * Rule: MYSQL-WINDOW-SPEC-001. The expressions are derived at the position
24 * of the window. The base window is a window of the WINDOW clause, which
25 * the query family resolves. A GROUPS frame and an EXCLUDE clause are
26 * accepted by the grammar and rejected by the server (ER_NOT_SUPPORTED_YET).
27 * Terminates: the parts are strict parts.
28 * Source: https://dev.mysql.com/doc/refman/8.4/en/window-functions-usage.html,
29 * https://dev.mysql.com/doc/refman/8.4/en/window-functions-named-windows.html.
30 * Status: Implemented.
31 *
32 * @visibility public
33 * @example Holding the parts of a window
34 * $order = [new \SqlSemantics\Platform\MySql\Statement\Query\OrderItem(new \SqlSemantics\Platform\MySql\Statement\Name\ColumnUse(new \SqlSemantics\Platform\MySql\Model\Statement\Identifier\Name('b')))];
35 * $window = new \SqlSemantics\Platform\MySql\Statement\Call\Window\WindowSpec(null, [], $order, new \SqlSemantics\Platform\MySql\Statement\Call\Window\Frame(\SqlSemantics\Platform\MySql\Statement\Call\Window\FrameUnit::Rows, new \SqlSemantics\Platform\MySql\Statement\Call\Window\FrameBound(\SqlSemantics\Platform\MySql\Statement\Call\Window\FrameBoundKind::UnboundedPreceding)));
36 * [count($window->partition), count($window->order), $window->frame?->unit] // => [0, 1, \SqlSemantics\Platform\MySql\Statement\Call\Window\FrameUnit::Rows]
37 */
38final class WindowSpec implements WindowSpecification
39{
40 use Snapshot;
41
42 /**
43 * @var list<OrderItem> The partitioning expressions
44 */
45 public readonly array $partition;
46
47 /**
48 * @var list<OrderItem> The ordering items
49 */
50 public readonly array $order;
51
52 /**
53 * @param Name|null $base The window this one refines
54 * @param list<OrderItem> $partition The partitioning expressions
55 * @param list<OrderItem> $order The ordering items
56 * @param Frame|null $frame The frame
57 */
58 public function __construct(public readonly ?Name $base = null, array $partition = [], array $order = [], public readonly ?Frame $frame = null)
59 {
60 $this->partition = Check::listOf($partition, OrderItem::class, 'A window partition is a list of ordering items.');
61 $this->order = Check::listOf($order, OrderItem::class, 'A window ordering is a list of ordering items.');
62 }
63
64 /**
65 * Derives the partitioning, the ordering and the frame offsets, and reports what the server does not support.
66 */
67 public function deriveWindow(Derivation $derivation, Environment $environment): void
68 {
69 foreach ([...$this->partition, ...$this->order] as $item) {
70 (new Arguments())->one($item->expression, $derivation, $environment);
71 }
72 foreach ($this->frame?->offsets() ?? [] as $offset) {
73 (new Arguments())->one($offset, $derivation, $environment);
74 }
75 if ($this->frame?->unit === FrameUnit::Groups) {
76 $derivation->report(new UnsupportedWindowing(WindowingLimit::GroupsUnit));
77 }
78 if ($this->frame?->exclusion !== null) {
79 $derivation->report(new UnsupportedWindowing(WindowingLimit::Exclusion));
80 }
81 }
82
83 /**
84 * Writes the parenthesized specification.
85 */
86 public function render(Output $out): void
87 {
88 $out->symbol('(');
89 if ($this->base !== null) {
90 $out->name($this->base, NameUse::Label);
91 }
92 if ($this->partition !== []) {
93 $out->keyword('PARTITION', 'BY')->list($this->partition);
94 }
95 if ($this->order !== []) {
96 $out->keyword('ORDER', 'BY')->list($this->order);
97 }
98 $out->node($this->frame)->symbol(')');
99 }
100}
101