Skip to content

Commit f1ca44b

Browse files
committed
Reject INTERVAL on a partial operand
The grammar nests "op bitExpr" to the right, regardless of operator precedence. In "1 + d + INTERVAL 1 DAY", the interval was applied to "d" only, while MySQL applies it to "1 + d". Reject an interval in such a nested operand, and suggest parentheses. The rewritten fold also no longer duplicates translate_sequence(), and the docblock no longer claims that INTERVAL is the only "bitExpr" form that SQLite can't run.
1 parent d71ed83 commit f1ca44b

3 files changed

Lines changed: 67 additions & 35 deletions

File tree

‎packages/mysql-on-sqlite/src/sqlite/class-wp-mysql-on-sqlite.php‎

Lines changed: 42 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -4936,51 +4936,58 @@ private function translate_simple_expr_body( WP_Parser_Node $node ): string {
49364936
/**
49374937
* Translate a "bitExpr" AST node.
49384938
*
4939-
* Date arithmetic is the only "bitExpr" form that SQLite cannot run as
4940-
* written: "expr + INTERVAL n unit" and "expr - INTERVAL n unit". Each
4941-
* interval is folded into the expression on its left, so a chain such as
4942-
* "expr + INTERVAL 5 HOUR + INTERVAL 1 DAY" becomes nested DATETIME() calls
4943-
* applied in the same order as in MySQL.
4939+
* SQLite can't run MySQL date arithmetic as written: "expr + INTERVAL n unit"
4940+
* and "expr - INTERVAL n unit". Each interval is folded into the expression
4941+
* on its left, so a chain such as "expr + INTERVAL 5 HOUR + INTERVAL 1 DAY"
4942+
* becomes nested DATETIME() calls applied in the same order as in MySQL.
49444943
*
49454944
* @param WP_Parser_Node $node The "bitExpr" AST node.
49464945
* @return string|null The translated value.
49474946
* @throws WP_MySQL_On_SQLite_Exception When the translation fails.
49484947
*/
49494948
private function translate_bit_expr( WP_Parser_Node $node ): ?string {
4950-
if ( ! $node->has_child_token( WP_MySQL_Lexer::INTERVAL_SYMBOL ) ) {
4951-
return $this->translate_sequence( $node->get_children() );
4952-
}
4953-
4949+
/*
4950+
* The children are a "simpleExpr", any number of "op INTERVAL expr interval"
4951+
* sequences, and an optional "op bitExpr" that holds the rest of the chain.
4952+
*/
49544953
$children = $node->get_children();
49554954
$count = count( $children );
4956-
$parts = array();
4957-
for ( $i = 0; $i < $count; $i++ ) {
4958-
$child = $children[ $i ];
4959-
if (
4960-
$child instanceof WP_Parser_Token
4961-
&& ( WP_MySQL_Lexer::PLUS_OPERATOR === $child->id || WP_MySQL_Lexer::MINUS_OPERATOR === $child->id )
4962-
&& isset( $children[ $i + 1 ] )
4963-
&& $children[ $i + 1 ] instanceof WP_Parser_Token
4964-
&& WP_MySQL_Lexer::INTERVAL_SYMBOL === $children[ $i + 1 ]->id
4965-
) {
4966-
$parts = array(
4967-
$this->translate_interval_arithmetic(
4968-
implode( ' ', $parts ),
4969-
WP_MySQL_Lexer::MINUS_OPERATOR === $child->id,
4970-
$children[ $i + 2 ],
4971-
$children[ $i + 3 ]
4972-
),
4973-
);
4974-
$i += 3;
4975-
continue;
4976-
}
4955+
$sql = $this->translate( $children[0] );
4956+
$i = 1;
4957+
while (
4958+
isset( $children[ $i + 1 ] )
4959+
&& $children[ $i + 1 ] instanceof WP_Parser_Token
4960+
&& WP_MySQL_Lexer::INTERVAL_SYMBOL === $children[ $i + 1 ]->id
4961+
) {
4962+
$sql = $this->translate_interval_arithmetic(
4963+
$sql,
4964+
WP_MySQL_Lexer::MINUS_OPERATOR === $children[ $i ]->id,
4965+
$children[ $i + 2 ],
4966+
$children[ $i + 3 ]
4967+
);
4968+
$i += 4;
4969+
}
49774970

4978-
$translated = $this->translate( $child );
4979-
if ( null !== $translated ) {
4980-
$parts[] = $translated;
4981-
}
4971+
if ( $i === $count ) {
4972+
return $sql;
4973+
}
4974+
4975+
/*
4976+
* The grammar nests "op bitExpr" to the right, regardless of precedence.
4977+
* In "a + b + INTERVAL 1 DAY", the interval would apply to "b" only, while
4978+
* MySQL applies it to "a + b". Reject intervals in such nested operands.
4979+
*/
4980+
$operator = $children[ $i ];
4981+
$operand = $children[ $i + 1 ];
4982+
if ( $operand->has_child_token( WP_MySQL_Lexer::INTERVAL_SYMBOL ) ) {
4983+
throw $this->new_not_supported_exception(
4984+
sprintf(
4985+
'INTERVAL after the "%s" operator; wrap the expression before INTERVAL in parentheses',
4986+
$operator->get_value()
4987+
)
4988+
);
49824989
}
4983-
return implode( ' ', $parts );
4990+
return sprintf( '%s %s %s', $sql, $this->translate( $operator ), $this->translate( $operand ) );
49844991
}
49854992

49864993
/**

‎packages/mysql-on-sqlite/tests/WP_MySQL_On_SQLite_Tests.php‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1060,6 +1060,25 @@ public function testIntervalArithmeticPrefixForm() {
10601060
$this->assertEquals( 2, $result[0]->count );
10611061
}
10621062

1063+
public function testIntervalArithmeticOnPartialOperand() {
1064+
// MySQL applies the interval to "1 + '2024-01-01'", but the parser nests
1065+
// the interval with "'2024-01-01'" only. Such expressions are rejected.
1066+
$this->assertQueryError(
1067+
"SELECT 1 + '2024-01-01' + INTERVAL 1 DAY",
1068+
'MySQL query not supported. Cause: INTERVAL after the "+" operator; wrap the expression before INTERVAL in parentheses'
1069+
);
1070+
1071+
$this->assertQueryError(
1072+
"SELECT 2 * '2024-01-01' - INTERVAL 1 DAY",
1073+
'MySQL query not supported. Cause: INTERVAL after the "*" operator; wrap the expression before INTERVAL in parentheses'
1074+
);
1075+
1076+
$this->assertQueryError(
1077+
"SELECT 0 | '2024-01-01' + INTERVAL 1 DAY",
1078+
'MySQL query not supported. Cause: INTERVAL after the "|" operator; wrap the expression before INTERVAL in parentheses'
1079+
);
1080+
}
1081+
10631082
public function testLeftFunction1Char() {
10641083
$result = $this->assertQuery(
10651084
'SELECT LEFT("abc", 1) as output'

‎packages/mysql-on-sqlite/tests/WP_MySQL_On_SQLite_Translation_Tests.php‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1741,6 +1741,12 @@ public function testIntervalArithmeticInParentheses(): void {
17411741
"SELECT ( DATETIME('2024-01-01', (1) || ' DAY') ) + 0 AS `a`",
17421742
"SELECT ('2024-01-01' + INTERVAL 1 DAY) + 0 AS a"
17431743
);
1744+
1745+
// Without parentheses, the interval would apply to a part of the expression.
1746+
$this->assertQuery(
1747+
"SELECT DATETIME(( 1 + '2024-01-01' ), (1) || ' DAY') AS `a`",
1748+
"SELECT (1 + '2024-01-01') + INTERVAL 1 DAY AS a"
1749+
);
17441750
}
17451751

17461752
public function testIndexHints(): void {

0 commit comments

Comments
 (0)