Skip to content

Fix parsing INSERT with a SELECT wrapped in brackets - #691

Open
dualfroz wants to merge 2 commits into
phpmyadmin:masterfrom
dualfroz:fix/484-insert-parenthesized-select
Open

dualfroz wants to merge 2 commits into
phpmyadmin:masterfrom
dualfroz:fix/484-insert-parenthesized-select

Conversation

@dualfroz

Copy link
Copy Markdown

the INSERT parser expected a keyword right after the column list, so INSERT INTO A (b) (SELECT 1) ended with "Unexpected token." and the select got split off into a second statement. an opening bracket there now goes into $parser->brackets the same way as brackets around a top-level statement, so the select should pick up the closing one itself. added a fixture for it. Fixes #484

`INSERT INTO a (b) (SELECT 1)` failed with "Unexpected token." because
the INSERT parser only accepted a keyword after the column list. An
opening bracket there is now counted like the brackets around a
top-level statement, so the SELECT parser consumes the closing one.

Signed-off-by: Michał Fox <me@dualfroz.com>
} elseif ($state === 1) {
// The `SELECT` might be wrapped in brackets, the closing one is
// handled by the statement through the bracket counter.
if ($token->type === TokenType::Operator && $token->value === '(') {

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

After this bracket every branch below is still open, not only SELECT. So some broken queries now parse with no error, where master reported one:

query master this PR
INSERT INTO a (b) (SET b=1) Unexpected token. no errors, $parser->brackets left at 1
INSERT INTO a (b) (SET b=1); SELECT 1 ) Unexpected token. no errors (the leftover bracket also hides the stray ) in the next statement)
INSERT INTO a (b) ( Unexpected token. no errors

SET and VALUES never look at $parser->brackets; only Statement::parse (so SELECT) takes the closing one back.

Letting only SELECT follow the bracket fixes the two SET rows, and the suite still passes (1032 tests):

++$parser->brackets;
++$openBrackets; // = 0 before the loop
continue;
}

if ($openBrackets > 0 && $token->keyword !== 'SELECT') {
    $parser->error('Unexpected token.', $token);
    break;
}

The unclosed ( at the end still needs its own check after the loop.

Also, REPLACE INTO a (b) (SELECT 1) has the same error as #484 and is valid in MySQL too. ReplaceStatement has the same keyword check, so it may be worth doing it here as well.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good catch, thanks. only SELECT can follow the bracket now, anything else (and the unclosed one) errors at the bracket like on master, with the counter put back. did REPLACE too and added fixtures for all of these

Anything other than SELECT after the opening bracket, or a bracket left
unclosed, now fails the same way as before, with the bracket counter
restored. REPLACE INTO a (b) (SELECT ...) is parsed the same way.

Signed-off-by: Michał Fox <me@dualfroz.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Error with parentheses around select statement

2 participants