packages/sql-semantics-postgres/src/Statement/Expression/Operator/UnaryOperation.php

1<?php
2
3declare(strict_types=1);
4
5namespace SqlSemantics\Platform\PostgreSql\Statement\Expression\Operator;
6
7use SqlSemantics\Platform\PostgreSql\Model\Construction\Derivation;
8use SqlSemantics\Platform\PostgreSql\Model\Diagnostic\Check;
9use SqlSemantics\Platform\PostgreSql\Model\Rendering\Output;
10use SqlSemantics\Platform\PostgreSql\Model\Resolution\Environment;
11use SqlSemantics\Platform\PostgreSql\Model\Statement\Fact\ScalarFact;
12use SqlSemantics\Platform\PostgreSql\Model\Statement\Scalar;
13use SqlSemantics\Platform\PostgreSql\Model\Statement\Snapshot;
14use SqlSemantics\Platform\PostgreSql\Rules\Expression\Precedence;
15use SqlSemantics\Platform\PostgreSql\Rules\Expression\Typing\PrefixTyping;
16use SqlSemantics\Platform\PostgreSql\Statement\Name\OperatorName;
17
18/**
19 * A prefix operator applied to one operand: `- a`, `+ a`, `@ a`, `OPERATOR(s.!!) a`.
20 *
21 * Mirrors PostgreSQL's `A_Expr` node of kind `AEXPR_OP` without a left
22 * operand. The parser folds a minus before a numeric constant into a negative
23 * constant; the model keeps the operator and types the pair as that constant.
24 *
25 * Rule: PG-UNARY-OPERATION-001. Facts follow PG-OPERATOR-TYPING-001. Only
26 * `+`, `-`, an operator the scanner reads as a general operator, or the
27 * `OPERATOR(...)` syntax can be written before an operand. The operand must
28 * keep its place without parentheses (PG-PRECEDENCE-001).
29 * Source: https://www.postgresql.org/docs/17/sql-expressions.html#SQL-EXPRESSIONS-OPERATOR-CALLS,
30 * https://www.postgresql.org/docs/17/sql-syntax-lexical.html#SQL-SYNTAX-OPERATORS. Status: Implemented.
31 *
32 * @visibility public
33 * @example Typing a negated constant as the constant it folds to
34 *     (new \SqlSemantics\Platform\PostgreSql\Semantics())->analyze('SELECT -2147483648')->field(0)->type->descriptor->name() // => 'integer'
35 * @example Rejecting an operator that cannot be written before an operand
36 *     new \SqlSemantics\Platform\PostgreSql\Statement\Expression\Operator\UnaryOperation(new \SqlSemantics\Platform\PostgreSql\Statement\Name\OperatorName(new \SqlSemantics\Platform\PostgreSql\Model\Statement\Identifier\Name('*')), new \SqlSemantics\Platform\PostgreSql\Statement\Literal\NullLiteral()) // throws \SqlSemantics\Platform\PostgreSql\Model\Diagnostic\InvalidConstruction
37 */
38final class UnaryOperation implements Scalar
39{
40    use Snapshot;
41
42    /**
43     * The operators the scanner returns as their own tokens, of which only + and - can be written before an operand.
44     */
45    private const SELF = ['*', '/', '%', '^', '<', '>', '=', '<=', '>=', '<>'];
46
47    /**
48     * @param OperatorName $operator The operator
49     * @param Scalar $operand The operand
50     */
51    public function __construct(public readonly OperatorName $operator, public readonly Scalar $operand)
52    {
53        Check::input($operator->explicit || $operator->qualifiers === [], 'An expression writes a qualified operator with OPERATOR(...).');
54        Check::input($operator->explicit || !in_array($operator->name->value, self::SELF, true), 'This operator cannot be written before an operand without OPERATOR(...).');
55        $precedence = new Precedence();
56        Check::input($precedence->after($operand, $precedence->operator($operator, true)), 'The operand needs parentheses to keep its place.');
57    }
58
59    /**
60     * Derives the operand and the result of the operator over its type.
61     */
62    public function deriveScalar(Derivation $derivation, Environment $environment): ScalarFact
63    {
64        $operand = $derivation->scalar($this->operand, $environment);
65
66        return (new PrefixTyping())->prefix($derivation->context, $this->operator, $operand, $this->operand);
67    }
68
69    /**
70     * Writes the operator and the operand.
71     */
72    public function render(Output $out): void
73    {
74        $out->node($this->operator)->node($this->operand);
75    }
76}
77