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

fix: unnest join constraint with alias parsing for BigQuery dialect #732

Merged
merged 2 commits into from Dec 13, 2022

Conversation

Ziinc
Copy link
Contributor

@Ziinc Ziinc commented Nov 29, 2022

This PR fixes a parsing bug for the BigQuery Dialect.
The expected outcome is for the following statement to be parsed with join constraints:

select * from t1 join unnest(t1.col) as f1 on  c1 = 123

However, this statement would result in the following error:

ParserError("Expected end of statement, found: c1")

Closer inspection revealed that the following ast was produced for the join table factor

UNNEST { 
  alias: Some(TableAlias { name: Ident { value: "f", quote_style: None }, columns: [] }), 
  array_expr: CompoundIdentifier([Ident { value: "t1", quote_style: None }, Ident { value: "a", quote_style: None }]), 
  with_offset: false, 
  with_offset_alias: Some(Ident { value: "ON", quote_style: None }) 
}

As you can see, the with_offset_alias parses the ON keyword despite the with_offset being false.

To replicate this behaviour, I added a failing test that verifies this behaviour difference

thread 'parse_join_constraint_unnest' panicked at 'called `Result::unwrap()` on an `Err` value: ParserError("Expected end of statement, found: c1")', src/test_utils.rs:86:61
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
test parse_join_constraint_unnest ... FAILED

This fix uses the boolean check to determine if the subsequent optional alias should be parsed.

Copy link
Collaborator

@alamb alamb left a comment

Choose a reason for hiding this comment

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

Looks great -- thank you @Ziinc

@alamb
Copy link
Collaborator

alamb commented Nov 30, 2022

@Ziinc there are some CI failures on this PR -- can you please resolve them?

@Ziinc
Copy link
Contributor Author

Ziinc commented Dec 5, 2022

@alamb I've fixed the lint error. For the test case, it seems like the inline comments for the tested cases were off, and the last case that I removed should actually raise a ParserError, since an alias + WITH OFFSET alias without a WITH OFFSET clause does not make sense.

Copy link
Collaborator

@alamb alamb left a comment

Choose a reason for hiding this comment

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

Makes sense to me -- thank you @Ziinc

@coveralls
Copy link

Pull Request Test Coverage Report for Build 3623351580

  • 18 of 19 (94.74%) changed or added relevant lines in 2 files are covered.
  • 1044 unchanged lines in 8 files lost coverage.
  • Overall coverage increased (+0.03%) to 86.385%

Changes Missing Coverage Covered Lines Changed/Added Lines %
src/parser.rs 3 4 75.0%
Files with Coverage Reduction New Missed Lines %
src/tokenizer.rs 1 90.28%
tests/sqlparser_hive.rs 8 96.55%
tests/sqlparser_postgres.rs 25 97.41%
src/ast/ddl.rs 34 81.25%
src/ast/query.rs 47 84.23%
tests/sqlparser_common.rs 86 97.24%
src/ast/mod.rs 314 77.7%
src/parser.rs 529 83.44%
Totals Coverage Status
Change from base Build 3568725328: 0.03%
Covered Lines: 12449
Relevant Lines: 14411

💛 - Coveralls

@alamb alamb merged commit d420001 into sqlparser-rs:main Dec 13, 2022
@alamb
Copy link
Collaborator

alamb commented Dec 13, 2022

Thanks again @Ziinc

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.

None yet

3 participants