Skip to content

Commit dd2ddda

Browse files
committed
Map INTERVAL units by token type
The unit was taken from the token text, so units SQLite doesn't support produced an invalid DATETIME() modifier, and the result was NULL. With the INTERVAL operators, "UPDATE t SET d = d + INTERVAL 1 QUARTER" wrote NULL into the column, while MySQL adds 3 months. The same happened with the SQL_TSI_* synonyms, whose text differs from the unit they map to. Units are now looked up by token type. QUARTER becomes 3 months, and MICROSECOND and the composite units, such as HOUR_MINUTE, are rejected.
1 parent f1ca44b commit dd2ddda

2 files changed

Lines changed: 89 additions & 5 deletions

File tree

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

Lines changed: 31 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -330,6 +330,24 @@ class WP_MySQL_On_SQLite extends PDO {
330330
'%y' => '%y',
331331
);
332332

333+
/**
334+
* A map of MySQL INTERVAL units to SQLite DATETIME() modifier units.
335+
*
336+
* Each MySQL unit maps to a SQLite unit and a multiplier for the value.
337+
* MICROSECOND has no equivalent, as SQLite DATETIME() drops fractional
338+
* seconds, and the composite units, such as HOUR_MINUTE, aren't supported.
339+
*/
340+
private const INTERVAL_UNIT_MAP = array(
341+
WP_MySQL_Lexer::SECOND_SYMBOL => array( 'SECOND', 1 ),
342+
WP_MySQL_Lexer::MINUTE_SYMBOL => array( 'MINUTE', 1 ),
343+
WP_MySQL_Lexer::HOUR_SYMBOL => array( 'HOUR', 1 ),
344+
WP_MySQL_Lexer::DAY_SYMBOL => array( 'DAY', 1 ),
345+
WP_MySQL_Lexer::WEEK_SYMBOL => array( 'DAY', 7 ),
346+
WP_MySQL_Lexer::MONTH_SYMBOL => array( 'MONTH', 1 ),
347+
WP_MySQL_Lexer::QUARTER_SYMBOL => array( 'MONTH', 3 ),
348+
WP_MySQL_Lexer::YEAR_SYMBOL => array( 'YEAR', 1 ),
349+
);
350+
333351
/**
334352
* A map of MySQL data types to implicit default values for non-strict mode.
335353
*
@@ -5148,7 +5166,7 @@ private function translate_runtime_function_call( WP_Parser_Node $node ): string
51485166
* the value is parenthesized before the unit is appended. Subtraction
51495167
* negates the value instead of prefixing "-", which keeps a negative
51505168
* interval such as "INTERVAL -5 HOUR" valid: SQLite rejects "+-5 HOUR".
5151-
* A WEEK interval becomes seven days, as SQLite has no week modifier.
5169+
* Units without a SQLite equivalent are converted, e.g., WEEK to 7 days.
51525170
*
51535171
* @param string $expr The translated date expression.
51545172
* @param bool $subtract Whether the interval is subtracted.
@@ -5163,11 +5181,19 @@ private function translate_interval_arithmetic(
51635181
WP_Parser_Node $value,
51645182
WP_Parser_Node $interval
51655183
): string {
5184+
// Use the token ID, as the token value can be a synonym, e.g., "SQL_TSI_HOUR".
5185+
$unit_token = $interval->get_first_descendant_token();
5186+
if ( ! isset( self::INTERVAL_UNIT_MAP[ $unit_token->id ] ) ) {
5187+
throw $this->new_not_supported_exception(
5188+
sprintf( 'INTERVAL unit: %s', $unit_token->get_value() )
5189+
);
5190+
}
5191+
5192+
list( $unit, $multiplier ) = self::INTERVAL_UNIT_MAP[ $unit_token->id ];
5193+
51665194
$value_sql = sprintf( '(%s)', $this->translate( $value ) );
5167-
$unit = strtoupper( $this->translate( $interval ) );
5168-
if ( 'WEEK' === $unit ) {
5169-
$value_sql = sprintf( '(%s * 7)', $value_sql );
5170-
$unit = 'DAY';
5195+
if ( 1 !== $multiplier ) {
5196+
$value_sql = sprintf( '(%s * %d)', $value_sql, $multiplier );
51715197
}
51725198
if ( $subtract ) {
51735199
$value_sql = '-' . $value_sql;

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

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1079,6 +1079,64 @@ public function testIntervalArithmeticOnPartialOperand() {
10791079
);
10801080
}
10811081

1082+
public function testIntervalArithmeticUnits() {
1083+
$this->assertQuery( 'CREATE TABLE _events (id INT, occurred_at DATETIME)' );
1084+
$this->assertQuery(
1085+
"INSERT INTO _events VALUES (1, '2024-01-01 10:00:00'), (2, '2024-01-03 10:00:00'), (3, NULL)"
1086+
);
1087+
1088+
// Expected values were taken from MySQL 8.4.
1089+
$result = $this->assertQuery(
1090+
"SELECT '2024-01-01 10:00:00' + INTERVAL 1 QUARTER as output"
1091+
);
1092+
$this->assertEquals( '2024-04-01 10:00:00', $result[0]->output );
1093+
1094+
$result = $this->assertQuery(
1095+
"SELECT '2024-01-01 10:00:00' + INTERVAL 2 * 2 QUARTER as output"
1096+
);
1097+
$this->assertEquals( '2025-01-01 10:00:00', $result[0]->output );
1098+
1099+
$result = $this->assertQuery(
1100+
"SELECT DATE_SUB('2024-01-01 10:00:00', INTERVAL -2 QUARTER) as output"
1101+
);
1102+
$this->assertEquals( '2024-07-01 10:00:00', $result[0]->output );
1103+
1104+
// The SQL_TSI_* units are synonyms for the plain units.
1105+
$result = $this->assertQuery(
1106+
"SELECT '2024-01-01 10:00:00' + INTERVAL 1 SQL_TSI_HOUR as output"
1107+
);
1108+
$this->assertEquals( '2024-01-01 11:00:00', $result[0]->output );
1109+
1110+
$result = $this->assertQuery(
1111+
"SELECT '2024-01-01 10:00:00' - INTERVAL 1 SQL_TSI_QUARTER as output"
1112+
);
1113+
$this->assertEquals( '2023-10-01 10:00:00', $result[0]->output );
1114+
1115+
$result = $this->assertQuery(
1116+
"SELECT DATE_ADD('2024-01-01 10:00:00', INTERVAL 2 SQL_TSI_WEEK) as output"
1117+
);
1118+
$this->assertEquals( '2024-01-15 10:00:00', $result[0]->output );
1119+
1120+
$this->assertQuery( 'UPDATE _events SET occurred_at = occurred_at + INTERVAL 1 QUARTER' );
1121+
$result = $this->assertQuery( 'SELECT occurred_at FROM _events ORDER BY id' );
1122+
$this->assertEquals( '2024-04-01 10:00:00', $result[0]->occurred_at );
1123+
$this->assertEquals( '2024-04-03 10:00:00', $result[1]->occurred_at );
1124+
$this->assertNull( $result[2]->occurred_at );
1125+
}
1126+
1127+
public function testIntervalArithmeticWithUnsupportedUnits() {
1128+
// SQLite DATETIME() drops fractional seconds.
1129+
$this->assertQueryError(
1130+
"SELECT '2024-01-01 10:00:00' + INTERVAL 1 MICROSECOND",
1131+
'MySQL query not supported. Cause: INTERVAL unit: MICROSECOND'
1132+
);
1133+
1134+
$this->assertQueryError(
1135+
"SELECT DATE_ADD('2024-01-01 10:00:00', INTERVAL '1:30' HOUR_MINUTE)",
1136+
'MySQL query not supported. Cause: INTERVAL unit: HOUR_MINUTE'
1137+
);
1138+
}
1139+
10821140
public function testLeftFunction1Char() {
10831141
$result = $this->assertQuery(
10841142
'SELECT LEFT("abc", 1) as output'

0 commit comments

Comments
 (0)