Skip to content

Allow empty options for BigQuery #1657

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

Merged
merged 2 commits into from
Jan 16, 2025

Conversation

MartinSahlen
Copy link
Contributor

@MartinSahlen MartinSahlen commented Jan 15, 2025

In BigQuery, it's possible to have statements like these (added tests for them):

CREATE TABLE foo (x INT64) OPTIONS()
CREATE TABLE db.schema.test (x INT64 OPTIONS(description = 'An optional INTEGER field')) OPTIONS()

While this may look silly (and clearly is), it's very common that companies are using tools (like dbt) to generate the SQL based on a certain configuration defined at runtime using macros and other abominations (but that is off-topic). If there is no config/options, the clause is still generated, with no contents. I am not sure about the semantics in other platforms, but PR, with tests, allows specifying empty options. Seeing that all tests run, this can hopefully be merged quickly. Also ran fmt and clippy.

Comment on lines 7344 to 7349
if self.peek_token() == Token::RParen {
self.next_token();
return Ok(vec![]);
}
let options = self.parse_comma_separated(Parser::parse_sql_option)?;
self.expect_token(&Token::RParen)?;
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
if self.peek_token() == Token::RParen {
self.next_token();
return Ok(vec![]);
}
let options = self.parse_comma_separated(Parser::parse_sql_option)?;
self.expect_token(&Token::RParen)?;
let options = self.parse_comma_separated0(Parser::parse_sql_option, &Token::RParen)?;

Would something like this work to simplify the condition?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I was not aware of this one, but it seems to break here:

thread 'test_bigquery_create_function' panicked at src/test_utils.rs:155:61:
CREATE OR REPLACE TEMPORARY FUNCTION project1.mydataset.myfunction(x FLOAT64) RETURNS FLOAT64 OPTIONS(x = 'y') AS 42: ParserError("Expected: AS, found: )")

when it looks like this

  pub fn parse_options(&mut self, keyword: Keyword) -> Result<Vec<SqlOption>, ParserError> {
        if self.parse_keyword(keyword) {
            self.expect_token(&Token::LParen)?;
            let options = self.parse_comma_separated0(Parser::parse_sql_option, Token::RParen)?;
            Ok(options)
        } else {
            Ok(vec![])
        }
    }

I will work a bit to see what is going wrong

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The logic in the function parse_comma_separated0 should be equivalent to what I did already

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I tried changing both
parse_options and parse_options_with_keywords as they excibit similar behaviour but I cannot get the tests to pass, although my original use case is working. It could be that I've stumbled upon some deeper issue or bug though (or I dont know enough about the code base which is clearly the case). The question is whether this potential bug should be addressed in this PR or not. Based on the expected bevahour of the function you suggested this seems like a bug.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ok, I think I found the bug, if I do this:

  pub fn parse_options(&mut self, keyword: Keyword) -> Result<Vec<SqlOption>, ParserError> {
        if self.parse_keyword(keyword) {
            self.expect_token(&Token::LParen)?;
            let options = self.parse_comma_separated0(Parser::parse_sql_option, Token::RParen)?;
            self.expect_token(&Token::RParen)?;
            Ok(options)
        } else {
            Ok(vec![])
        }
    }

its is working. And this leads me to look at the parse_comma_separated0 implementation. Should this not also consume the end_token parameter?

 pub fn parse_comma_separated0<T, F>(
        &mut self,
        f: F,
        end_token: Token,
    ) -> Result<Vec<T>, ParserError>

Ok I tried making this function consume the end_token but it seems to cause more issues. I also saw that there is a parse_options_with_keywords that at the moment will not accept an empty options, but I will not change or touch that in this PR.

Comment on lines 2248 to 2258
#[test]
fn parse_create_table_with_empty_table_options() {
let sql = "CREATE TABLE foo (x INT64) OPTIONS()";
bigquery().verified_stmt(sql);
}

#[test]
fn parse_create_table_with_empty_table_options_and_column_options() {
let sql = "CREATE TABLE db.schema.test (x INT64 OPTIONS(description = 'An optional INTEGER field')) OPTIONS()";
bigquery().verified_stmt(sql);
}
Copy link
Contributor

Choose a reason for hiding this comment

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

Can we add these as scenarios into the existing tests for the feature vs having them as dedicated tests?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It shall be done! How did I miss that, I was searching but my bad!

Copy link
Contributor

@iffyio iffyio left a comment

Choose a reason for hiding this comment

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

LGTM! Thanks @MartinSahlen!
cc @alamb

@iffyio iffyio merged commit 9105cae into apache:main Jan 16, 2025
9 checks passed
Vedin pushed a commit to Embucket/datafusion-sqlparser-rs that referenced this pull request Feb 3, 2025
Vedin pushed a commit to Embucket/datafusion-sqlparser-rs that referenced this pull request Feb 3, 2025
Vedin added a commit to Embucket/datafusion-sqlparser-rs that referenced this pull request Feb 3, 2025
ayman-sigma pushed a commit to sigmacomputing/sqlparser-rs that referenced this pull request Apr 10, 2025
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.

2 participants