Skip to content

Added support for unpivot in Redshift with expression and bracketsless - #2375

Merged
iffyio merged 11 commits into
apache:mainfrom
SatoriCyber:upivot_expression_support
Aug 4, 2026
Merged

Added support for unpivot in Redshift with expression and bracketsless#2375
iffyio merged 11 commits into
apache:mainfrom
SatoriCyber:upivot_expression_support

Conversation

@kfirSatori

Copy link
Copy Markdown
Contributor

Added support for unpivot in Redshift with expression and bracketsless:
SELECT t.id, k, v FROM test_colors as t, UNPIVOT t.count_by_color AS v AT k;

@kfirSatori

Copy link
Copy Markdown
Contributor Author

@iffyio Can you help me with the PR, it's a while here.

Comment thread src/ast/query.rs
/// Syntax:
/// ```sql
/// UNPIVOT expression AS value_alias [AT attribute_alias]
/// ```

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Can we add a link to the docs describing the syntax?

@kfirSatori kfirSatori Jul 21, 2026

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

@iffyio , Added link to docs

Comment thread src/dialect/mod.rs Outdated
/// ```sql
/// UNPIVOT expression AS value_alias [AT attribute_alias]
/// ```
fn supports_unpivot_expr_in_from(&self) -> bool {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Suggested change
fn supports_unpivot_expr_in_from(&self) -> bool {
fn supports_unpivot_expr(&self) -> bool {

Comment thread tests/sqlparser_redshift.rs Outdated
Comment on lines +547 to +561
fn parse_unpivot_expression() {
let sql = r#"SELECT t.id, k, v FROM test_colors as t, UNPIVOT t.count_by_color AS v AT k;
"#;

redshift().parse_sql_statements(sql).unwrap();

}

#[test]
fn parse_unpivot_no_brackets() {
let sql = r#"SELECT t.id, k, v FROM test_colors as t, UNPIVOT t AS v AT k;
"#;

redshift().parse_sql_statements(sql).unwrap();

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

let's use verified_stmt also we can merge the test cases into the same function

Comment thread src/parser/mod.rs Outdated
with_ordinality,
})
} else if self.dialect.supports_unpivot_expr_in_from()
&& self.parse_keyword(Keyword::UNPIVOT)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

can we change this to self.peek(UNPIVOT) so that the parse_unpivot_expr_table_factor is standalone since we're making it a public function?

@kfirSatori kfirSatori Jul 21, 2026

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

@iffyio , Can you please explain it?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I meant essentially that the condition becomes else if self.dialect.supports_unpivot_expr_in_from() && self.peek_keyword(Keyword::UNPIVOT) { parse_unpivot_expr_table_factor() } - then parse_unpivot_expr_table_factor is updated to expect the UNPIVOT keyword

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

@iffyio added peek_keyword()

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

the parse_keyword still remains, the goal of introducing peek_keyword is that parse_keyword moves into the parse_unpivot_expr_table_factor function as an expect call instead

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Sorry for the confusion, fixed. @iffyio

Comment thread src/dialect/redshift.rs Outdated
true
}

fn supports_unpivot_expr_in_from(&self) -> bool {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

can we add a link to the redshift docs?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I have added the doc link on the top, here as well?

@kfirSatori

Copy link
Copy Markdown
Contributor Author

Hi @iffyio ,

I've fixed the comments, please tell me if there's anything else

Comment thread src/dialect/mod.rs Outdated
///
/// Syntax:
/// ```sql
/// UNPIVOT expression AS value_alias [AT attribute_alias]

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Suggested change
/// UNPIVOT expression AS value_alias [AT attribute_alias]
/// SELECT * FROM T UNPIVOT expression AS value_alias [AT attribute_alias]

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

@iffyio done.

Comment thread src/parser/mod.rs Outdated
with_ordinality,
})
} else if self.dialect.supports_unpivot_expr_in_from()
&& self.parse_keyword(Keyword::UNPIVOT)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

the parse_keyword still remains, the goal of introducing peek_keyword is that parse_keyword moves into the parse_unpivot_expr_table_factor function as an expect call instead

Comment thread tests/sqlparser_redshift.rs Outdated
Comment on lines +548 to +549
redshift().verified_stmt("SELECT t.id, k, v FROM test_colors AS t, UNPIVOT t.count_by_color AS v AT k",);
redshift().verified_stmt("SELECT t.id, k, v FROM test_colors AS t, UNPIVOT t AS v AT k");

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

can we change the dialect selection to use all_dialects_where(|d| d.supports...)?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

@iffyio done.

@kfirSatori

Copy link
Copy Markdown
Contributor Author

Hi @iffyio ,
I think we're ready to merge.

@iffyio iffyio left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Thanks @kfirSatori!

@iffyio
iffyio added this pull request to the merge queue Aug 3, 2026
@github-merge-queue
github-merge-queue Bot removed this pull request from the merge queue due to no response for status checks Aug 3, 2026
@iffyio
iffyio enabled auto-merge August 4, 2026 11:27
@iffyio
iffyio added this pull request to the merge queue Aug 4, 2026
@github-merge-queue
github-merge-queue Bot removed this pull request from the merge queue due to no response for status checks Aug 4, 2026
@iffyio
iffyio added this pull request to the merge queue Aug 4, 2026
Merged via the queue into apache:main with commit 30d0836 Aug 4, 2026
10 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.

2 participants