packages/sql-semantics-mysql/src/Statement/Expression/Operator/IntervalArithmetic.php
1<?php
2
3declare(strict_types=1);
4
5namespace SqlSemantics\Platform\MySql\Statement\Expression\Operator;
6
7use SqlSemantics\Platform\MySql\Model\Construction\Derivation;
8use SqlSemantics\Platform\MySql\Model\Diagnostic\Check;
9use SqlSemantics\Platform\MySql\Model\Rendering\Output;
10use SqlSemantics\Platform\MySql\Model\Resolution\Environment;
11use SqlSemantics\Platform\MySql\Model\Statement\Fact\ScalarFact;
12use SqlSemantics\Platform\MySql\Model\Statement\Scalar;
13use SqlSemantics\Platform\MySql\Model\Statement\Snapshot;
14use SqlSemantics\Platform\MySql\Model\Statement\Type\Nullability;
15use SqlSemantics\Platform\MySql\Rules\Expression\Operands;
16use SqlSemantics\Platform\MySql\Rules\Expression\Precedence;
17use SqlSemantics\Platform\MySql\Rules\Expression\TemporalResult;
18
19/**
20 * Date arithmetic with a trailing interval: `date + INTERVAL n unit` or `date - INTERVAL n unit` (`Item_date_add_interval`).
21 *
22 * It belongs to the additive level of bit_expr and associates to the left
23 * with `+` and `-` (MYSQL-PRECEDENCE-001).
24 *
25 * Rule: MYSQL-INTERVAL-ARITHMETIC-001. Facts: the type follows
26 * MYSQL-TEMPORAL-RESULT-001; the result is NULL for an invalid date, so it
27 * can always be NULL. Terminates: the operand and the quantity are strict parts.
28 * Source: https://dev.mysql.com/doc/refman/8.4/en/date-and-time-functions.html#function_date-add.
29 * Status: Implemented.
30 *
31 * @visibility public
32 * @example Subtracting an interval
33 * $query = (new \SqlSemantics\Platform\MySql\Semantics())->analyze("SELECT a FROM t WHERE DATE '2024-01-31' - INTERVAL 1 MONTH");
34 * [$query->statement->where->subtract, $query->facts->scalar($query->statement->where)->type->descriptor->name()] // => [true, 'DATE']
35 */
36final class IntervalArithmetic implements Scalar
37{
38 use Snapshot;
39
40 /**
41 * @param Scalar $operand The date or time value
42 * @param Interval $interval The interval added or subtracted
43 * @param bool $subtract Whether the interval is subtracted
44 */
45 public function __construct(public readonly Scalar $operand, public readonly Interval $interval, public readonly bool $subtract = false)
46 {
47 Check::input((new Precedence())->fits($operand, Precedence::ADDITIVE, Precedence::ADDITIVE), 'The operand of interval arithmetic needs a grouping to keep its place.');
48 }
49
50 /**
51 * Derives the operand and the quantity and the temporal type of the result.
52 */
53 public function deriveScalar(Derivation $derivation, Environment $environment): ScalarFact
54 {
55 $operands = new Operands();
56 $operand = $operands->single($derivation->scalar($this->operand, $environment), $derivation);
57 $operands->single($derivation->scalar($this->interval->quantity, $environment), $derivation);
58
59 return new ScalarFact((new TemporalResult())->interval($operand->type, $this->interval->unit, $derivation->context->profile->grammar), Nullability::Nullable);
60 }
61
62 /**
63 * Writes the operand, the sign and the interval.
64 */
65 public function render(Output $out): void
66 {
67 $out->node($this->operand)->symbol($this->subtract ? '-' : '+')->node($this->interval);
68 }
69}
70