Skip to content

Fix Window function handling that failed because of "OVER" keyword case-sensitive comparison. #623

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: 5.11.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/Components/Expression.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use function is_array;
use function rtrim;
use function strlen;
use function strtoupper;
use function trim;

/**
Expand Down Expand Up @@ -372,7 +373,7 @@ public static function parse(Parser $parser, TokensList $list, array $options =
&& ! ($prev[1]->flags & Token::FLAG_SYMBOL_VARIABLE)
&& ! ($prev[1]->flags & Token::FLAG_SYMBOL_PARAMETER))
|| ($prev[1]->type === Token::TYPE_NONE
&& $prev[1]->token !== 'OVER'))
&& strtoupper($prev[1]->token) !== 'OVER'))
) {
if (! empty($ret->alias)) {
$parser->error('An alias was previously found.', $token);
Expand Down
36 changes: 36 additions & 0 deletions tests/Builder/SelectStatementTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -414,4 +414,40 @@ public function testBuilderSelectAllFormsOfIndexHints(): void

self::assertSame($query, $stmt->build());
}

public function testBuilderSelectRowNumberOverAlias(): void
{
$query = 'SELECT id, pid, appid, appname, row_number() over (partition by pid, appid) as `group_row_number`'
. ' FROM game group by appname';
$expected = 'SELECT id, pid, appid, appname, row_number() over (partition by pid, appid) AS `group_row_number`'
. ' FROM game GROUP BY appname';

$parser = new Parser($query);
$stmt = $parser->statements[0];

self::assertSame($expected, $stmt->build());
}

public function testBuilderSelectWindowFunctions(): void
{
$queryVsExpected = [
'SELECT row_number() over (ORDER BY NULL) x' => 'SELECT row_number() over (ORDER BY NULL) AS `x`',
'SELECT rank() over (ORDER BY NULL) x' => 'SELECT rank() over (ORDER BY NULL) AS `x`',
'SELECT dense_rank() over (ORDER BY NULL) x' => 'SELECT dense_rank() over (ORDER BY NULL) AS `x`',
'SELECT cume_dist() over (ORDER BY NULL) x' => 'SELECT cume_dist() over (ORDER BY NULL) AS `x`',
'SELECT ntile(3) over (ORDER BY NULL) x' => 'SELECT ntile(3) over (ORDER BY NULL) AS `x`',
'SELECT ROW_NUMBER() OVER(ORDER BY NULL) x' => 'SELECT ROW_NUMBER() OVER(ORDER BY NULL) AS `x`',
'SELECT RANK()OVER(ORDER BY NULL) x' => 'SELECT RANK()OVER(ORDER BY NULL) AS `x`',
'SELECT DENSE_RANK()OVER(ORDER BY NULL) x' => 'SELECT DENSE_RANK()OVER(ORDER BY NULL) AS `x`',
'SELECT CUME_DIST()OVER(ORDER BY NULL) x' => 'SELECT CUME_DIST()OVER(ORDER BY NULL) AS `x`',
'SELECT NTILE(3)OVER(ORDER BY NULL) x' => 'SELECT NTILE(3)OVER(ORDER BY NULL) AS `x`',
];

foreach ($queryVsExpected as $query => $expected) {
$parser = new Parser($query);
$stmt = $parser->statements[0];

self::assertSame($expected, $stmt->build());
}
}
}
Loading