packages/sql-semantics-mysql/src/Rules/Call/WindowResults.php
1<?php
2
3declare(strict_types=1);
4
5namespace SqlSemantics\Platform\MySql\Rules\Call;
6
7use SqlSemantics\Platform\MySql\Model\Construction\Derivation;
8use SqlSemantics\Platform\MySql\Model\Statement\Fact\ScalarFact;
9use SqlSemantics\Platform\MySql\Model\Statement\Type\Known;
10use SqlSemantics\Platform\MySql\Model\Statement\Type\Nullability;
11use SqlSemantics\Platform\MySql\Model\Statement\Type\NullOnly;
12use SqlSemantics\Platform\MySql\Rules\Expression\TypeAggregation;
13use SqlSemantics\Platform\MySql\Statement\Call\Problem\UnsupportedWindowing;
14use SqlSemantics\Platform\MySql\Statement\Call\Problem\WindowingLimit;
15use SqlSemantics\Platform\MySql\Statement\Call\Window\CountingEdge;
16use SqlSemantics\Platform\MySql\Statement\Call\Window\NullTreatment;
17use SqlSemantics\Platform\MySql\Statement\Call\Window\WindowFunction;
18use SqlSemantics\Platform\MySql\Statement\Call\Window\WindowFunctionKind;
19
20/**
21 * The result of a function that is only a window function.
22 *
23 * Rule: MYSQL-WINDOW-RESULT-001. ROW_NUMBER, RANK, DENSE_RANK and NTILE are
24 * BIGINT UNSIGNED, CUME_DIST and PERCENT_RANK DOUBLE; none of them is NULL.
25 * LEAD and LAG have the aggregated type of the value and the default
26 * (MYSQL-TYPE-AGGREGATION-001); FIRST_VALUE, LAST_VALUE and NTH_VALUE
27 * the type of the value; each is NULL when the row is outside the frame or
28 * partition. IGNORE NULLS and FROM LAST are rejected by the server
29 * (ER_NOT_SUPPORTED_YET). Terminates: a fixed number of tests on one call.
30 * Source: https://dev.mysql.com/doc/refman/8.4/en/window-function-descriptions.html.
31 * Status: Implemented.
32 *
33 * @visibility SqlSemantics\Platform\MySql
34 */
35final class WindowResults
36{
37 /**
38 * Answers the facts of a window function call and reports what the server rejects.
39 *
40 * @param list<ScalarFact> $arguments
41 */
42 public function result(WindowFunction $call, array $arguments, Derivation $derivation): ScalarFact
43 {
44 if ($call->nulls === NullTreatment::Ignore) {
45 $derivation->report(new UnsupportedWindowing(WindowingLimit::IgnoreNulls));
46 }
47 if ($call->edge === CountingEdge::Last) {
48 $derivation->report(new UnsupportedWindowing(WindowingLimit::FromLast));
49 }
50
51 return match ($call->kind) {
52 WindowFunctionKind::RowNumber, WindowFunctionKind::Rank, WindowFunctionKind::DenseRank,
53 WindowFunctionKind::Tile => new ScalarFact(new Known(TypeClass::Unsigned->descriptor()), Nullability::NotNull),
54 WindowFunctionKind::CumulativeDistribution,
55 WindowFunctionKind::PercentRank => new ScalarFact(new Known(TypeClass::Floating->descriptor()), Nullability::NotNull),
56 WindowFunctionKind::Lead, WindowFunctionKind::Lag => new ScalarFact(
57 (new TypeAggregation())->aggregate([$arguments[0]->type, ...(isset($arguments[2]) ? [$arguments[2]->type] : [])]),
58 Nullability::Nullable,
59 ),
60 WindowFunctionKind::FirstValue, WindowFunctionKind::LastValue,
61 WindowFunctionKind::NthValue => new ScalarFact($arguments[0]->type ?? new NullOnly(), Nullability::Nullable),
62 };
63 }
64}
65