packages/sql-semantics-mysql/src/Rules/Call/AggregateResults.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\Model\Statement\Type\TypeFact;
13use SqlSemantics\Platform\MySql\Statement\Call\Aggregate\Aggregate;
14use SqlSemantics\Platform\MySql\Statement\Call\Aggregate\AggregateFunction;
15use SqlSemantics\Platform\MySql\Statement\Call\Problem\UnsupportedWindowing;
16use SqlSemantics\Platform\MySql\Statement\Call\Problem\WindowingLimit;
17
18/**
19 * The result of an aggregate function with one argument, COUNT(*) or COUNT(DISTINCT ...).
20 *
21 * Rule: MYSQL-AGGREGATE-RESULT-001. COUNT is a BIGINT and never NULL. SUM
22 * and AVG of an exact number (an integer, DECIMAL, YEAR or BIT) are DECIMAL,
23 * of anything else DOUBLE. MIN and MAX have the type of their argument. The
24 * BIT_AND, BIT_OR and BIT_XOR of a binary string are a binary string, of
25 * anything else a BIGINT UNSIGNED; they are never NULL. STD, VARIANCE,
26 * STDDEV_SAMP and VAR_SAMP are DOUBLE, JSON_ARRAYAGG is JSON, ST_COLLECT a
27 * geometry. Every other aggregate is NULL for a group without non-NULL
28 * values. DISTINCT in a windowed aggregate is not supported 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/aggregate-functions.html,
31 * https://dev.mysql.com/doc/refman/8.4/en/bit-functions.html#function_bit-and,
32 * https://dev.mysql.com/doc/refman/8.4/en/window-function-restrictions.html.
33 * Status: Implemented.
34 *
35 * @visibility SqlSemantics\Platform\MySql
36 */
37final class AggregateResults
38{
39    /**
40     * Answers the facts of an aggregate call and reports what the server rejects.
41     *
42     * @param list<ScalarFact> $arguments
43     */
44    public function aggregate(Aggregate $call, array $arguments, Derivation $derivation): ScalarFact
45    {
46        if ($call->distinct && $call->over !== null) {
47            $derivation->report(new UnsupportedWindowing(WindowingLimit::DistinctAggregate));
48        }
49        $type = $arguments[0]->type ?? new NullOnly();
50        $aggregation = new TypeClasses();
51
52        return match ($call->function) {
53            AggregateFunction::Count => new ScalarFact(new Known(TypeClass::Integer->descriptor()), Nullability::NotNull),
54            AggregateFunction::Sum, AggregateFunction::Average => new ScalarFact($this->sum($type), Nullability::Nullable),
55            AggregateFunction::Minimum, AggregateFunction::Maximum => new ScalarFact($type, Nullability::Nullable),
56            AggregateFunction::BitAnd, AggregateFunction::BitOr, AggregateFunction::BitXor => new ScalarFact($this->bits($type), Nullability::NotNull),
57            AggregateFunction::StandardDeviation, AggregateFunction::Variance, AggregateFunction::SampleStandardDeviation,
58            AggregateFunction::SampleVariance => new ScalarFact(new Known(TypeClass::Floating->descriptor()), Nullability::Nullable),
59            AggregateFunction::JsonArray => new ScalarFact(new Known(TypeClass::Json->descriptor()), Nullability::Nullable),
60            AggregateFunction::Collect => new ScalarFact($aggregation->fact([TypeClass::Spatial]), Nullability::Nullable),
61        };
62    }
63
64    /**
65     * Answers the type of SUM and AVG.
66     */
67    public function sum(TypeFact $argument): TypeFact
68    {
69        $classes = (new TypeClasses())->classes($argument);
70        if ($classes === []) {
71            return (new TypeClasses())->fact([TypeClass::Decimal, TypeClass::Floating]);
72        }
73        $results = [];
74        foreach ($classes as $class) {
75            $results[] = in_array($class, [TypeClass::Integer, TypeClass::Unsigned, TypeClass::Decimal, TypeClass::Year, TypeClass::Bit], true) ? TypeClass::Decimal : TypeClass::Floating;
76        }
77
78        return (new TypeClasses())->fact($results);
79    }
80
81    /**
82     * Answers the type of BIT_AND, BIT_OR and BIT_XOR.
83     */
84    public function bits(TypeFact $argument): TypeFact
85    {
86        $classes = (new TypeClasses())->classes($argument);
87        if ($argument instanceof NullOnly) {
88            return new Known(TypeClass::Unsigned->descriptor());
89        }
90        if ($classes === []) {
91            return (new TypeClasses())->fact([TypeClass::Unsigned, TypeClass::Binary]);
92        }
93        $results = [];
94        foreach ($classes as $class) {
95            $results[] = $class === TypeClass::Binary ? TypeClass::Binary : TypeClass::Unsigned;
96        }
97
98        return (new TypeClasses())->fact($results);
99    }
100}
101