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

support PIVOT table syntax #836

Merged
merged 1 commit into from Mar 26, 2023

Conversation

pawel-big-lebowski
Copy link
Contributor

PIVOT TABLE syntax is available in Snowflake.
This PR introduces PIVOT support within sqlparser-rs.

@coveralls
Copy link

coveralls commented Mar 15, 2023

Pull Request Test Coverage Report for Build 4466498674

  • 68 of 75 (90.67%) changed or added relevant lines in 3 files are covered.
  • 12 unchanged lines in 1 file lost coverage.
  • Overall coverage increased (+0.01%) to 86.138%

Changes Missing Coverage Covered Lines Changed/Added Lines %
src/parser.rs 19 20 95.0%
tests/sqlparser_common.rs 30 31 96.77%
src/ast/query.rs 19 24 79.17%
Files with Coverage Reduction New Missed Lines %
src/parser.rs 12 82.95%
Totals Coverage Status
Change from base Build 4456045946: 0.01%
Covered Lines: 13646
Relevant Lines: 15842

💛 - Coveralls

@pawel-big-lebowski
Copy link
Contributor Author

Failing lint should be resolved within #835

@@ -670,6 +670,17 @@ pub enum TableFactor {
table_with_joins: Box<TableWithJoins>,
alias: Option<TableAlias>,
},
/// Represents PIVOT operation on a table.
/// For example `FROM monthly_sales PIVOT(sum(amount) FOR MONTH IN ('JAN', 'FEB'))`
Copy link
Contributor

Choose a reason for hiding this comment

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

Could you add a link to the Snowflake docs (https://docs.snowflake.com/en/sql-reference/constructs/pivot) in here?

#[test]
fn parse_pivot_table() {
let sql = concat!(
"SELECT * FROM monthly_sales AS a ",
Copy link
Contributor

Choose a reason for hiding this comment

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

  • can you add a test case without the AS component?
  • can you add a roundtrip test (so we can verify the Display() implementation works too)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

  • There is below a variable sql_without_table_alias that tests this.
  • Will add the roundtrip as it is missing.

src/parser.rs Outdated
self.expect_token(&Token::LParen)?;
let function_name = match self.next_token().token {
Token::Word(w) => Ok(w.value),
_ => self.expected("a function name", self.peek_token()),
Copy link
Contributor

Choose a reason for hiding this comment

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

maybe "an aggregate function" in the error message?

src/parser.rs Outdated
}?;
let function = self
.parse_function(ObjectName(vec![Ident::new(function_name)]))
.unwrap();
Copy link
Contributor

Choose a reason for hiding this comment

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

shouldn't we propagate the error here instead of unwrapping?

src/parser.rs Outdated
.parse_function(ObjectName(vec![Ident::new(function_name)]))
.unwrap();
self.expect_keyword(Keyword::FOR)?;
let mut value_column = vec![self.parse_identifier()?];
Copy link
Contributor

Choose a reason for hiding this comment

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

why not use self.parse_object_name()?

Signed-off-by: Pawel Leszczynski <leszczynski.pawel@gmail.com>
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.

Thank you @pawel-big-lebowski -- looks good to me. Thanks for the assist @ankrgyl

@alamb alamb merged commit 29dea5d into sqlparser-rs:main Mar 26, 2023
9 checks passed
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

4 participants