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: invalid sqls when unparsing derived table with columns contains calculations, limit/order/distinct #11756

Conversation

y-f-u
Copy link
Contributor

@y-f-u y-f-u commented Jul 31, 2024

Which issue does this PR close?

This further addressed the unparser issues when derived table contains match operator, cast, limit, order, etc.

Rationale for this change

These are common SQL patterns which doesn't work now. See test cases for details

What changes are included in this PR?

Rewrite more bits of logical plan during unparsing. The key change is to use field name to match the outer layer projection field to detect if the logical plan can be corrected for unparsing purpose, which is the reverse path of parsing for these sql during the first leg of the round trip.

Are these changes tested?

Yes

Are there any user-facing changes?

No

…rder/distinct (#24)

* compare format output to make sure the two level of projects match

* add method to find inner projection that could be nested under limit/order/distinct

* use format! for matching in unparser sort optimization too

* refactor

* use to_string and also put comments in
@github-actions github-actions bot added the sql SQL Planner label Jul 31, 2024
* fix unparser derived table contains cast

* remove dbg
Copy link
Contributor

@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 @y-f-u -- this looks like a nice set of improvements to me

@@ -373,6 +373,38 @@ fn roundtrip_statement_with_dialect() -> Result<()> {
parser_dialect: Box::new(GenericDialect {}),
unparser_dialect: Box::new(UnparserDefaultDialect {}),
},
// Test query that has calculation in derived table with columns
Copy link
Contributor

Choose a reason for hiding this comment

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

These look like improvements to me

@alamb alamb merged commit 86030a1 into apache:main Aug 8, 2024
24 checks passed
@alamb
Copy link
Contributor

alamb commented Aug 8, 2024

Thanks again @y-f-u -- sorry for the delay in merging

@phillipleblanc phillipleblanc deleted the unparser-fix-for-derived-table-with-column-contains-math-and-limit branch August 14, 2024 07:01
phillipleblanc pushed a commit to spiceai/datafusion that referenced this pull request Aug 14, 2024
…calculations, limit/order/distinct (apache#11756)

* Fix unparser derived table with columns include calculations, limit/order/distinct (#24)

* compare format output to make sure the two level of projects match

* add method to find inner projection that could be nested under limit/order/distinct

* use format! for matching in unparser sort optimization too

* refactor

* use to_string and also put comments in

* clippy

* fix unparser derived table contains cast (#25)

* fix unparser derived table contains cast

* remove dbg
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
sql SQL Planner
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants