You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Make SQL strings generated from Exprs "prettier" #10557
Currently, expressions from the DataFusion SQL unparser (aka expr --> String) are somewhat ugly
For example the expression col("a").eq(lit(5)) would be rendered as a = 5 by most poeple if they did it manaully, but DataFusion's unparser currently renders it like "a" = 5 (with extra quotes).
DataFusion also puts in quotes to make the order of operations explicit -- so instead of a < 5 AND b < 10 it would render ("a" < 5) AND ("b" < 10)
The current unparser is conservative and likely works well for when generating SQL for consumptions by other database systems. However, the SQL is not as nice for human consumption
/// Note: this trait will eventually be replaced by the Dialect in the SQLparser package
I think we need to use the Dialect in sqlparser-rs instead and extract identifier_needs_quote in #10573 to sqlparser-rs. Just like apache/datafusion-sqlparser-rs#1170
Yes, these are basically the same object. The one in DataFusion was put there temporarily until the trait extension in the sqlparser repo is landed and pushed to crates.io. This may have happened in the meantime.
I'm not sure but I think we can merge #10573 first because it also fix many unpasring tests. Then, I'll create PR for sqlparser to add the check rule in dialect.
Activity
goldmedal commentedon May 18, 2024
Provide something I surveyed.
I think we can follow how Calcite handles the quoted issue. The
SqlDialectof Calcite has a check ruleidentifierNeedsQuote.https://github.com/apache/calcite/blob/aba64f0b217093b500629fe07a0befdc68293fbc/core/src/main/java/org/apache/calcite/sql/SqlDialect.java#L413-L415
It can be overridden according to the specific data source, such as BigQuery:
https://github.com/apache/calcite/blob/50a20824c4536450dcae963b5e757cf4bbc7e406/core/src/main/java/org/apache/calcite/sql/dialect/BigQuerySqlDialect.java#L106-L109
They implement some rules, such as regex patterns or reserved word lists. I think a dialect-specific rule is a nice choice.
backkem commentedon May 18, 2024
Indeed, there is already a function on the sqlparser::dialect trait that takes this into account:
https://github.com/sqlparser-rs/sqlparser-rs/blob/54184460b5d873a67c2801e8b7c6e4f145bc65df/src/dialect/mod.rs#L113-L116
The dialect specific implementations just need to be expanded on. For now they just always return a conservative quote character.
goldmedal commentedon May 18, 2024
I think it's same as the dialect in datafusion::unparser::dialect
datafusion/datafusion/sql/src/unparser/dialect.rs
Line 28 in e7858ff
However, what we need is a checker to check if the identifier needs to be quoted.
I think I can make a PR for
DefaultDialectfirst.goldmedal commentedon May 19, 2024
As the mentioned in
dialect.rsdatafusion/datafusion/sql/src/unparser/dialect.rs
Line 19 in e7858ff
I think we need to use the Dialect in sqlparser-rs instead and extract
identifier_needs_quotein #10573 to sqlparser-rs. Just like apache/datafusion-sqlparser-rs#1170backkem commentedon May 19, 2024
Yes, these are basically the same object. The one in DataFusion was put there temporarily until the trait extension in the sqlparser repo is landed and pushed to crates.io. This may have happened in the meantime.
alamb commentedon May 20, 2024
#10392 is the upgrade to sqlparser -- I think it is pretty close but @tisonkun hit an issue during upgrade.
tisonkun commentedon May 20, 2024
We may need a 0.46.1 for resolving the regressions:
valuesas table name datafusion-sqlparser-rs#1278I've locally confirmed that
array.sltis last failure forcargo test --test sqllogictests.goldmedal commentedon May 21, 2024
I'm not sure but I think we can merge #10573 first because it also fix many unpasring tests. Then, I'll create PR for sqlparser to add the check rule in dialect.
alamb commentedon May 22, 2024
FWI #10573 is merged!
backkem commentedon May 23, 2024
Do we split off a ticket reduce the nr of brackets emitted?
alamb commentedon May 23, 2024
Excellent call -- I filed #10633