Skip to content

Commit fc016f7

Browse files
committed
Support ORDER BY disambiguation also for parenthesised ORDER BY column names
1 parent 0e90582 commit fc016f7

File tree

2 files changed

+15
-0
lines changed

2 files changed

+15
-0
lines changed

tests/WP_SQLite_Driver_Translation_Tests.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1368,12 +1368,24 @@ public function testSelectOrderByAmbiguousColumnResolution(): void {
13681368
'SELECT (((t1.name))) FROM t1 JOIN t2 ON t2.id = t1.id ORDER BY name'
13691369
);
13701370

1371+
// When the ORDER BY item is nested in a simple parentheses expression, the disambiguation still works.
1372+
$this->assertQuery(
1373+
'SELECT `t1`.`name` FROM `t1` JOIN `t2` ON `t2`.`id` = `t1`.`id` ORDER BY `t1`.`name`',
1374+
'SELECT t1.name FROM t1 JOIN t2 ON t2.id = t1.id ORDER BY (((name)))'
1375+
);
1376+
13711377
// When the SELECT item is nested in a complex expression, the column is not disambiguated (like in MySQL).
13721378
$this->assertQuery(
13731379
"SELECT (`t1`.`name` || 'test') AS `CONCAT(t1.name, 'test')` FROM `t1` JOIN `t2` ON `t2`.`id` = `t1`.`id` ORDER BY `name`",
13741380
"SELECT CONCAT(t1.name, 'test') FROM t1 JOIN t2 ON t2.id = t1.id ORDER BY name"
13751381
);
13761382

1383+
// When the ORDER BY item is nested in a complex expression, the column is not disambiguated (like in MySQL).
1384+
$this->assertQuery(
1385+
"SELECT `t1`.`name` FROM `t1` JOIN `t2` ON `t2`.`id` = `t1`.`id` ORDER BY ( `name` || 'test' )",
1386+
"SELECT t1.name FROM t1 JOIN t2 ON t2.id = t1.id ORDER BY (name || 'test')"
1387+
);
1388+
13771389
// When the SELECT list item uses an alias, the column is not disambiguated (like in MySQL).
13781390
$this->assertQuery(
13791391
'SELECT `t1`.`name` AS `t1_name` FROM `t1` JOIN `t2` ON `t2`.`id` = `t1`.`id` ORDER BY `name` DESC',

wp-includes/sqlite-ast/class-wp-sqlite-driver.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3001,6 +3001,9 @@ private function translate_query_expression( WP_Parser_Node $node ): string {
30013001
continue;
30023002
}
30033003

3004+
// Support also parenthesized ORDER BY column references (e.g. "(id)").
3005+
$order_expr = $this->unnest_parenthesized_expression( $order_expr );
3006+
30043007
// Consider only simple and parenthesized column references.
30053008
$order_expr_value = $this->translate( $order_expr );
30063009
$order_column_value = $this->translate( $order_column_ref );

0 commit comments

Comments
 (0)