Skip to content

Commit 3960975

Browse files
committed
Extend supported quote identifiers for ranking order unaliasing
1 parent ed53ef2 commit 3960975

File tree

2 files changed

+23
-2
lines changed

2 files changed

+23
-2
lines changed

core/RankingQuery.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -737,14 +737,14 @@ private function prepareColumnsForWindowOrderBy(?string $expr, string $selectCol
737737

738738
$column = str_replace('`', '', trim($columnParts[0]));
739739

740-
if (preg_match('/`?' . $column . '`? AS `?(\w+)`?(?:,|$)/is', $selectColumns, $matches)) {
740+
if (preg_match('/`?' . $column . '`? AS [`"\']?(\w+)[`"\']?(?:,|$)/is', $selectColumns, $matches)) {
741741
// unalias the column to allow usage in window
742742
$column = trim($matches[1]);
743743
$columnParts[0] = '`' . $column . '`';
744744
$exprColumn = implode(' ', $columnParts);
745745
}
746746

747-
if (!preg_match('/`?' . $column . '`?(?:,|$)/is', $selectColumns)) {
747+
if (!preg_match('/[`"\']?' . $column . '[`"\']?(?:,|$)/is', $selectColumns)) {
748748
// the column was not found as "column," or "column<end of select>" in the SELECT part
749749
// we remove it from the window because it otherwise break the query
750750
unset($exprColumns[$i]);

tests/PHPUnit/Unit/RankingQueryTest.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -749,6 +749,27 @@ public function getGenerateWindowOrderByStringTestData(): iterable
749749
'`column_new`',
750750
];
751751

752+
yield 'column aliases are resolved - backtick quoted' => [
753+
$rankingQuery,
754+
'SELECT column_one AS `column_new` FROM my_table ORDER BY column_one',
755+
false,
756+
'`column_new`',
757+
];
758+
759+
yield 'column aliases are resolved - double quoted' => [
760+
$rankingQuery,
761+
'SELECT column_one AS "column_new" FROM my_table ORDER BY column_one',
762+
false,
763+
'`column_new`',
764+
];
765+
766+
yield 'column aliases are resolved - single quoted' => [
767+
$rankingQuery,
768+
"SELECT column_one AS 'column_new' FROM my_table ORDER BY column_one",
769+
false,
770+
'`column_new`',
771+
];
772+
752773
yield 'column aliases are resolved - rollup' => [
753774
$rankingQuery,
754775
'

0 commit comments

Comments
 (0)