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

Implement prettier SQL unparsing (more human readable) #11186

Merged
merged 17 commits into from
Jul 11, 2024
Merged
Show file tree
Hide file tree
Changes from 16 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion datafusion-examples/examples/parse_sql_expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,9 @@ async fn query_parquet_demo() -> Result<()> {

/// DataFusion can parse a SQL text and convert it back to SQL using [`Unparser`].
async fn round_trip_parse_sql_expr_demo() -> Result<()> {
let sql = "((int_col < 5) OR (double_col = 8))";
// unparser can also remove extra parentheses,
// so `((int_col < 5) OR (double_col = 8))` will also produce the same SQL
Copy link
Contributor

Choose a reason for hiding this comment

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

👍

let sql = "int_col < 5 OR double_col = 8";

let ctx = SessionContext::new();
let testdata = datafusion::test_util::parquet_test_data();
Expand Down
6 changes: 3 additions & 3 deletions datafusion-examples/examples/plan_to_sql.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ async fn main() -> Result<()> {
fn simple_expr_to_sql_demo() -> Result<()> {
let expr = col("a").lt(lit(5)).or(col("a").eq(lit(8)));
let sql = expr_to_sql(&expr)?.to_string();
assert_eq!(sql, r#"((a < 5) OR (a = 8))"#);
assert_eq!(sql, r#"a < 5 OR a = 8"#);
Ok(())
}

Expand All @@ -71,7 +71,7 @@ fn simple_expr_to_sql_demo_escape_mysql_style() -> Result<()> {
let dialect = CustomDialect::new(Some('`'));
let unparser = Unparser::new(&dialect);
let sql = unparser.expr_to_sql(&expr)?.to_string();
assert_eq!(sql, r#"((`a` < 5) OR (`a` = 8))"#);
assert_eq!(sql, r#"`a` < 5 OR `a` = 8"#);
Ok(())
}

Expand Down Expand Up @@ -133,7 +133,7 @@ async fn round_trip_plan_to_sql_demo() -> Result<()> {
let sql = plan_to_sql(df.logical_plan())?.to_string();
assert_eq!(
sql,
r#"SELECT alltypes_plain.int_col, alltypes_plain.double_col, CAST(alltypes_plain.date_string_col AS VARCHAR) FROM alltypes_plain WHERE ((alltypes_plain.id > 1) AND (alltypes_plain.tinyint_col < alltypes_plain.double_col))"#
r#"SELECT alltypes_plain.int_col, alltypes_plain.double_col, CAST(alltypes_plain.date_string_col AS VARCHAR) FROM alltypes_plain WHERE alltypes_plain.id > 1 AND alltypes_plain.tinyint_col < alltypes_plain.double_col"#
);

Ok(())
Expand Down
26 changes: 14 additions & 12 deletions datafusion/core/tests/expr_api/parse_sql_expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,17 +39,17 @@ fn schema() -> DFSchemaRef {
#[tokio::test]
async fn round_trip_parse_sql_expr() -> Result<()> {
let tests = vec![
"(a = 10)",
"((a = 10) AND (b <> 20))",
"((a = 10) OR (b <> 20))",
"(((a = 10) AND (b <> 20)) OR (c = a))",
"((a = 10) AND b IN (20, 30))",
"((a = 10) AND b NOT IN (20, 30))",
"a = 10",
"(a = 10) AND (b <> 20)",
"(a = 10) OR (b <> 20)",
"((a = 10) AND (b <> 20)) OR (c = a)",
"(a = 10) AND b IN (20, 30)",
"(a = 10) AND b NOT IN (20, 30)",
"sum(a)",
"(sum(a) + 1)",
"(MIN(a) + MAX(b))",
"(MIN(a) + (MAX(b) * sum(c)))",
"(MIN(a) + ((MAX(b) * sum(c)) / 10))",
"sum(a) + 1",
"MIN(a) + MAX(b)",
"MIN(a) + (MAX(b) * sum(c))",
"MIN(a) + ((MAX(b) * sum(c)) / 10)",
];

for test in tests {
Expand All @@ -65,7 +65,8 @@ fn round_trip_session_context(sql: &str) -> Result<()> {
let df_schema = schema();
let expr = ctx.parse_sql_expr(sql, &df_schema)?;
let sql2 = unparse_sql_expr(&expr)?;
assert_eq!(sql, sql2);
let expr2 = ctx.parse_sql_expr(&sql2, &df_schema)?;
assert_eq!(expr.to_string(), expr2.to_string());

Ok(())
}
Expand All @@ -80,7 +81,8 @@ async fn round_trip_dataframe(sql: &str) -> Result<()> {
.await?;
let expr = df.parse_sql_expr(sql)?;
let sql2 = unparse_sql_expr(&expr)?;
assert_eq!(sql, sql2);
let roundtrip = df.parse_sql_expr(&sql2)?;
assert_eq!(expr, roundtrip);

Ok(())
}
Expand Down
24 changes: 10 additions & 14 deletions datafusion/expr/src/operator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -218,37 +218,33 @@ impl Operator {
}

/// Get the operator precedence
/// use <https://www.postgresql.org/docs/7.0/operators.htm#AEN2026> as a reference
/// use <https://www.postgresql.org/docs/7.2/sql-precedence.html> as a reference
pub fn precedence(&self) -> u8 {
Copy link
Contributor

Choose a reason for hiding this comment

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

It looks like this is only used in a few places for formatting the parenthesis in a Display trait on BinaryExpr. I don't think that will have any major impact on library users.

match self {
Operator::Or => 5,
Operator::And => 10,
Operator::NotEq
| Operator::Eq
| Operator::Lt
| Operator::LtEq
| Operator::Gt
| Operator::GtEq => 20,
Operator::Plus | Operator::Minus => 30,
Operator::Multiply | Operator::Divide | Operator::Modulo => 40,
Operator::Eq | Operator::NotEq | Operator::LtEq | Operator::GtEq => 15,
Operator::Lt | Operator::Gt => 20,
Operator::LikeMatch
| Operator::NotLikeMatch
| Operator::ILikeMatch
| Operator::NotILikeMatch => 25,
Operator::IsDistinctFrom
| Operator::IsNotDistinctFrom
| Operator::RegexMatch
| Operator::RegexNotMatch
| Operator::RegexIMatch
| Operator::RegexNotIMatch
| Operator::LikeMatch
| Operator::ILikeMatch
| Operator::NotLikeMatch
| Operator::NotILikeMatch
| Operator::BitwiseAnd
| Operator::BitwiseOr
| Operator::BitwiseShiftLeft
| Operator::BitwiseShiftRight
| Operator::BitwiseXor
| Operator::StringConcat
| Operator::AtArrow
| Operator::ArrowAt => 0,
| Operator::ArrowAt => 30,
Operator::Plus | Operator::Minus => 40,
Operator::Multiply | Operator::Divide | Operator::Modulo => 45,
}
}
}
Expand Down
Loading