packages/sql-semantics-mysql/src/Rules/Expression/NumericMerge.php

1<?php
2
3declare(strict_types=1);
4
5namespace SqlSemantics\Platform\MySql\Rules\Expression;
6
7use SqlSemantics\Platform\MySql\Model\Statement\Type\TypeDescriptor;
8use SqlSemantics\Platform\MySql\Statement\Type\Decimal;
9use SqlSemantics\Platform\MySql\Statement\Type\Elementary;
10use SqlSemantics\Platform\MySql\Statement\Type\Floating;
11use SqlSemantics\Platform\MySql\Statement\Type\Integral;
12use SqlSemantics\Platform\MySql\Statement\Type\Kind\ElementaryKind;
13use SqlSemantics\Platform\MySql\Statement\Type\Kind\FloatingKind;
14use SqlSemantics\Platform\MySql\Statement\Type\Kind\IntegralKind;
15use SqlSemantics\Platform\MySql\Statement\Type\Kind\NumericModifier;
16
17/**
18 * Merges two numeric types.
19 *
20 * Rule: MYSQL-NUMERIC-MERGE-001, a part of MYSQL-TYPE-AGGREGATION-001. BOOL
21 * is TINYINT, SERIAL and BIT are BIGINT UNSIGNED. Two integers merge to the
22 * wider kind, unsigned when both are; an unsigned and a signed integer need
23 * the next wider signed kind, and BIGINT with BIGINT UNSIGNED needs
24 * DECIMAL. An integer with DECIMAL gives DECIMAL. Two FLOATs stay FLOAT; any
25 * other mixture with FLOAT, DOUBLE or REAL gives DOUBLE. Terminates: no recursion.
26 * Source: https://dev.mysql.com/doc/refman/8.4/en/union.html,
27 * https://dev.mysql.com/doc/refman/8.4/en/numeric-type-syntax.html.
28 * Status: Implemented.
29 *
30 * @visibility SqlSemantics\Platform\MySql
31 */
32final class NumericMerge
33{
34    /**
35     * The integer kinds in order of width.
36     */
37    private const WIDTHS = ['TINYINT' => 1, 'SMALLINT' => 2, 'MEDIUMINT' => 3, 'INT' => 4, 'BIGINT' => 5];
38
39    /**
40     * The integer kinds by width.
41     */
42    private const KINDS = [1 => IntegralKind::TinyInt, 2 => IntegralKind::SmallInt, 3 => IntegralKind::MediumInt, 4 => IntegralKind::Int, 5 => IntegralKind::BigInt];
43
44    /**
45     * Answers the common type of two numeric types.
46     */
47    public function merge(TypeDescriptor $left, TypeDescriptor $right): TypeDescriptor
48    {
49        $left = $this->integral($left);
50        $right = $this->integral($right);
51        if ($left instanceof Integral && $right instanceof Integral) {
52            return $this->integers($left, $right);
53        }
54        if ($left instanceof Floating || $right instanceof Floating) {
55            $float = $left instanceof Floating && $right instanceof Floating && $left->kind === FloatingKind::Float && $right->kind === FloatingKind::Float;
56
57            return new Floating($float ? FloatingKind::Float : FloatingKind::Double);
58        }
59
60        return new Decimal();
61    }
62
63    /**
64     * Answers the integer type BOOL, SERIAL and BIT stand for, and every other type unchanged.
65     */
66    public function integral(TypeDescriptor $type): TypeDescriptor
67    {
68        if (!$type instanceof Elementary) {
69            return $type;
70        }
71
72        return match ($type->kind) {
73            ElementaryKind::Boolean => new Integral(IntegralKind::TinyInt),
74            ElementaryKind::Serial, ElementaryKind::Bit => new Integral(IntegralKind::BigInt, null, [NumericModifier::Unsigned]),
75            ElementaryKind::Json, ElementaryKind::Vector => new Floating(FloatingKind::Double),
76        };
77    }
78
79    /**
80     * Answers the common type of two integer types.
81     */
82    public function integers(Integral $left, Integral $right): TypeDescriptor
83    {
84        $width = max(self::WIDTHS[$left->kind->value], self::WIDTHS[$right->kind->value]);
85        if ($left->unsigned() === $right->unsigned()) {
86            return new Integral(self::KINDS[$width], null, $left->unsigned() ? [NumericModifier::Unsigned] : []);
87        }
88        $unsigned = $left->unsigned() ? $left : $right;
89        $needed = max($width, self::WIDTHS[$unsigned->kind->value] + 1);
90
91        return $needed > 5 ? new Decimal() : new Integral(self::KINDS[$needed]);
92    }
93}
94