Skip to content
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

Add support for EXECUTE IMMEDIATE statement #17341

Merged
merged 4 commits into from
May 16, 2023

Conversation

aalbu
Copy link
Member

@aalbu aalbu commented May 4, 2023

Description

Fixes #17353

Syntax:

EXECUTE IMMEDIATE
'SELECT * FROM t WHERE a = ? and b = ?'
USING 'foo', 42

The semantics are the same as:

PREPARE stmt1 FROM SELECT * FROM t WHERE a = ? and b = ?
EXECUTE stmt1 USING 'foo', 42
DEALLOCATE PREPARE stmt1

Additional context and related issues

Release notes

(x) Release notes are required, with the following suggested text:

# Section
* Add support for `EXECUTE IMMEDIATE`. ({issue}`17341`)

@findepi
Copy link
Member

findepi commented May 5, 2023

I posed some syntax comments in the issue: #17353

Copy link
Member

@electrum electrum left a comment

Choose a reason for hiding this comment

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

Looks good. The last two commits should be squashed.

@martint should also review

Copy link
Member

@kasiafi kasiafi left a comment

Choose a reason for hiding this comment

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

just skimmed

@aalbu aalbu force-pushed the execute-immediate branch 3 times, most recently from 6d80305 to f1e0f26 Compare May 11, 2023 01:59
catch (ParsingException e) {
int embeddedStatementLine = executeImmediateStatement.getStatementLocation().getLineNumber();
int embeddedStatementColumn = executeImmediateStatement.getStatementLocation().getColumnNumber();
ParsingException exceptionWithUpdatedLocation = new ParsingException(
Copy link
Member

Choose a reason for hiding this comment

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

No need for a separate variable for this. Inline it below.

Copy link
Member

Choose a reason for hiding this comment

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

Also, this won't work for a test such as this, as it only handles parsing errors:

        assertTrinoExceptionThrownBy(() -> assertions.query("EXECUTE IMMEDIATE 'SELECT * FROM tiny.tpch.orders'"))
                .hasMessageMatching("line 1:34: Catalog 'tiny' does not exist");

Copy link
Member

Choose a reason for hiding this comment

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

If the SqlParser could take a "relative start position", it would solve both issues:

  • parsing exceptions would have the correct location,
  • the created AST would have the correct locations, so the Analyzer errors would be positioned correctly, too.

Copy link
Member

Choose a reason for hiding this comment

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

Also, this won't work for a test such as this, as it only handles parsing errors:

BTW @martint we have the same problem with EXECUTE:

trino> prepare test from select * from foo;
PREPARE
trino> execute test;
Query 20230510_184850_00010_fczhd failed: line 3:3: Schema must be specified when session schema is not set

It's even worse, as the line/column in error message refer to how the statement was formatter by the SqlFormatter, not to how it was originally written by the user.

Probably in this case we should skip the position altogether, or place it at the beginning of the EXECUTE statement, or maybe include the executed statement in the message to gie context for the error.

catch (ParsingException e) {
int embeddedStatementLine = executeImmediateStatement.getStatementLocation().getLineNumber();
int embeddedStatementColumn = executeImmediateStatement.getStatementLocation().getColumnNumber();
ParsingException exceptionWithUpdatedLocation = new ParsingException(
Copy link
Member

Choose a reason for hiding this comment

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

If the SqlParser could take a "relative start position", it would solve both issues:

  • parsing exceptions would have the correct location,
  • the created AST would have the correct locations, so the Analyzer errors would be positioned correctly, too.

catch (ParsingException e) {
int embeddedStatementLine = executeImmediateStatement.getStatementLocation().getLineNumber();
int embeddedStatementColumn = executeImmediateStatement.getStatementLocation().getColumnNumber();
ParsingException exceptionWithUpdatedLocation = new ParsingException(
Copy link
Member

Choose a reason for hiding this comment

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

Also, this won't work for a test such as this, as it only handles parsing errors:

BTW @martint we have the same problem with EXECUTE:

trino> prepare test from select * from foo;
PREPARE
trino> execute test;
Query 20230510_184850_00010_fczhd failed: line 3:3: Schema must be specified when session schema is not set

It's even worse, as the line/column in error message refer to how the statement was formatter by the SqlFormatter, not to how it was originally written by the user.

Probably in this case we should skip the position altogether, or place it at the beginning of the EXECUTE statement, or maybe include the executed statement in the message to gie context for the error.

aalbu added 3 commits May 15, 2023 19:07
Syntax:
EXECUTE IMMEDIATE
'SELECT * FROM t WHERE a = ? and b = ?'
USING 'foo', 42

The semantics are the same as:
PREPARE stmt1 FROM SELECT * FROM t WHERE a = ? and b = ?
EXECUTE stmt1 USING 'foo', 42
DEALLOCATE PREPARE stmt1
@colebow
Copy link
Member

colebow commented May 17, 2023

I really don't like that we're merging new SQL syntax without docs the day before our normal release day...

@aalbu could you please try to follow-up with a docs PR? I think you can likely just add a blurb to execute.rst
cc @martint

@aalbu
Copy link
Member Author

aalbu commented May 17, 2023

@aalbu could you please try to follow-up with a docs PR? I think you can likely just add a blurb to execute.rst cc @martint

Sorry, my bad. I'll write something up.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Development

Successfully merging this pull request may close these issues.

Add support for EXECUTE IMMEDIATE statement
6 participants