From a2e8b6189f80e723d95782b8a89fcfae797367fa Mon Sep 17 00:00:00 2001 From: Devin D'Angelo Date: Wed, 13 Mar 2024 19:20:11 -0400 Subject: [PATCH] add roundtrip tests --- README.md | 1 + datafusion/sql/src/unparser/expr.rs | 4 ++-- datafusion/sql/tests/sql_integration.rs | 17 ++++++++++------- 3 files changed, 13 insertions(+), 9 deletions(-) diff --git a/README.md b/README.md index e5ac9503be44..abd727672aca 100644 --- a/README.md +++ b/README.md @@ -83,6 +83,7 @@ Default features: - `parquet`: support for reading the [Apache Parquet] format - `regex_expressions`: regular expression functions, such as `regexp_match` - `unicode_expressions`: Include unicode aware functions such as `character_length` +- `unparser` : enables support to reverse LogicalPlans back into SQL Optional features: diff --git a/datafusion/sql/src/unparser/expr.rs b/datafusion/sql/src/unparser/expr.rs index 0edcf1a3e3ee..403a7c6193d0 100644 --- a/datafusion/sql/src/unparser/expr.rs +++ b/datafusion/sql/src/unparser/expr.rs @@ -461,8 +461,8 @@ impl Unparser<'_> { DataType::Binary => todo!(), DataType::FixedSizeBinary(_) => todo!(), DataType::LargeBinary => todo!(), - DataType::Utf8 => todo!(), - DataType::LargeUtf8 => todo!(), + DataType::Utf8 => Ok(ast::DataType::Varchar(None)), + DataType::LargeUtf8 => Ok(ast::DataType::Text), DataType::List(_) => todo!(), DataType::FixedSizeList(_, _) => todo!(), DataType::LargeList(_) => todo!(), diff --git a/datafusion/sql/tests/sql_integration.rs b/datafusion/sql/tests/sql_integration.rs index fdf7ab8c3d28..396eebfdd767 100644 --- a/datafusion/sql/tests/sql_integration.rs +++ b/datafusion/sql/tests/sql_integration.rs @@ -4493,8 +4493,11 @@ impl TableSource for EmptyTable { #[test] fn roundtrip_expr() { let tests: Vec<(TableReference, &str, &str)> = vec![ - (TableReference::bare("person"), "age > 35", "age > 35"), - (TableReference::bare("person"), "id = '10'", "id = '10'"), + (TableReference::bare("person"), "age > 35", "(age > 35)"), + (TableReference::bare("person"), "id = '10'", "(id = '10')"), + (TableReference::bare("person"), "CAST(id AS VARCHAR)", "CAST(id AS VARCHAR)"), + (TableReference::bare("person"), "SUM((age * 2))", "SUM((age * 2))"), + ]; let roundtrip = |table, sql: &str| -> Result { @@ -4540,15 +4543,15 @@ fn roundtrip_statement() { ), ( "select ta.j1_id from j1 ta where ta.j1_id > 1;", - r#"SELECT ta.j1_id FROM j1 AS ta WHERE ta.j1_id > 1"#, + r#"SELECT ta.j1_id FROM j1 AS ta WHERE (ta.j1_id > 1)"#, ), ( - "select ta.j1_id, tb.j2_string from j1 ta join j2 tb on ta.j1_id = tb.j2_id;", - r#"SELECT ta.j1_id, tb.j2_string FROM j1 AS ta JOIN j2 AS tb ON ta.j1_id = tb.j2_id"#, + "select ta.j1_id, tb.j2_string from j1 ta join j2 tb on (ta.j1_id = tb.j2_id);", + r#"SELECT ta.j1_id, tb.j2_string FROM j1 AS ta JOIN j2 AS tb ON (ta.j1_id = tb.j2_id)"#, ), ( - "select ta.j1_id, tb.j2_string, tc.j3_string from j1 ta join j2 tb on ta.j1_id = tb.j2_id join j3 tc on ta.j1_id = tc.j3_id;", - r#"SELECT ta.j1_id, tb.j2_string, tc.j3_string FROM j1 AS ta JOIN j2 AS tb ON ta.j1_id = tb.j2_id JOIN j3 AS tc ON ta.j1_id = tc.j3_id"#, + "select ta.j1_id, tb.j2_string, tc.j3_string from j1 ta join j2 tb on (ta.j1_id = tb.j2_id) join j3 tc on (ta.j1_id = tc.j3_id);", + r#"SELECT ta.j1_id, tb.j2_string, tc.j3_string FROM j1 AS ta JOIN j2 AS tb ON (ta.j1_id = tb.j2_id) JOIN j3 AS tc ON (ta.j1_id = tc.j3_id)"#, ), ];