Skip to content

Apache DataFusion SQL Query Engine

Migrate datafusion/sql tests to insta, part6 (#15578)

Tommy shu4ccd4551dddb
diff --git a/datafusion/sql/tests/cases/diagnostic.rs b/datafusion/sql/tests/cases/diagnostic.rs
index ebb21e9cde..d08fe78794 100644
--- a/datafusion/sql/tests/cases/diagnostic.rs
+++ b/datafusion/sql/tests/cases/diagnostic.rs
@@ -16,6 +16,7 @@
// under the License.
use datafusion_functions::string;
+use insta::assert_snapshot;
use std::{collections::HashMap, sync::Arc};
use datafusion_common::{Diagnostic, Location, Result, Span};
@@ -136,7 +137,7 @@ fn test_table_not_found() -> Result<()> {
let query = "SELECT * FROM /*a*/personx/*a*/";
let spans = get_spans(query);
let diag = do_query(query);
- assert_eq!(diag.message, "table 'personx' not found");
+ assert_snapshot!(diag.message, @"table 'personx' not found");
assert_eq!(diag.span, Some(spans["a"]));
Ok(())
}
@@ -146,7 +147,7 @@ fn test_unqualified_column_not_found() -> Result<()> {
let query = "SELECT /*a*/first_namex/*a*/ FROM person";
let spans = get_spans(query);
let diag = do_query(query);
- assert_eq!(diag.message, "column 'first_namex' not found");
+ assert_snapshot!(diag.message, @"column 'first_namex' not found");
assert_eq!(diag.span, Some(spans["a"]));
Ok(())
}
@@ -156,7 +157,7 @@ fn test_qualified_column_not_found() -> Result<()> {
let query = "SELECT /*a*/person.first_namex/*a*/ FROM person";
let spans = get_spans(query);
let diag = do_query(query);
- assert_eq!(diag.message, "column 'first_namex' not found in 'person'");
+ assert_snapshot!(diag.message, @"column 'first_namex' not found in 'person'");
assert_eq!(diag.span, Some(spans["a"]));
Ok(())
}
@@ -166,14 +167,11 @@ fn test_union_wrong_number_of_columns() -> Result<()> {
let query = "/*whole+left*/SELECT first_name FROM person/*left*/ UNION ALL /*right*/SELECT first_name, last_name FROM person/*right+whole*/";
let spans = get_spans(query);
let diag = do_query(query);
- assert_eq!(
- diag.message,
- "UNION queries have different number of columns"
- );
+ assert_snapshot!(diag.message, @"UNION queries have different number of columns");
assert_eq!(diag.span, Some(spans["whole"]));
- assert_eq!(diag.notes[0].message, "this side has 1 fields");
+ assert_snapshot!(diag.notes[0].message, @"this side has 1 fields");
assert_eq!(diag.notes[0].span, Some(spans["left"]));
- assert_eq!(diag.notes[1].message, "this side has 2 fields");
+ assert_snapshot!(diag.notes[1].message, @"this side has 2 fields");
assert_eq!(diag.notes[1].span, Some(spans["right"]));
Ok(())
}
@@ -183,15 +181,9 @@ fn test_missing_non_aggregate_in_group_by() -> Result<()> {
let query = "SELECT id, /*a*/first_name/*a*/ FROM person GROUP BY id";
let spans = get_spans(query);
let diag = do_query(query);
- assert_eq!(
- diag.message,
- "'person.first_name' must appear in GROUP BY clause because it's not an aggregate expression"
- );
+ assert_snapshot!(diag.message, @"'person.first_name' must appear in GROUP BY clause because it's not an aggregate expression");
assert_eq!(diag.span, Some(spans["a"]));
- assert_eq!(
- diag.helps[0].message,
- "Either add 'person.first_name' to GROUP BY clause, or use an aggregare function like ANY_VALUE(person.first_name)"
- );
+ assert_snapshot!(diag.helps[0].message, @"Either add 'person.first_name' to GROUP BY clause, or use an aggregare function like ANY_VALUE(person.first_name)");
Ok(())
}
@@ -200,10 +192,10 @@ fn test_ambiguous_reference() -> Result<()> {
let query = "SELECT /*a*/first_name/*a*/ FROM person a, person b";
let spans = get_spans(query);
let diag = do_query(query);
- assert_eq!(diag.message, "column 'first_name' is ambiguous");
+ assert_snapshot!(diag.message, @"column 'first_name' is ambiguous");
assert_eq!(diag.span, Some(spans["a"]));
- assert_eq!(diag.notes[0].message, "possible column a.first_name");
- assert_eq!(diag.notes[1].message, "possible column b.first_name");
+ assert_snapshot!(diag.notes[0].message, @"possible column a.first_name");
+ assert_snapshot!(diag.notes[1].message, @"possible column b.first_name");
Ok(())
}
@@ -213,11 +205,11 @@ fn test_incompatible_types_binary_arithmetic() -> Result<()> {
"SELECT /*whole+left*/id/*left*/ + /*right*/first_name/*right+whole*/ FROM person";
let spans = get_spans(query);
let diag = do_query(query);
- assert_eq!(diag.message, "expressions have incompatible types");
+ assert_snapshot!(diag.message, @"expressions have incompatible types");
assert_eq!(diag.span, Some(spans["whole"]));
- assert_eq!(diag.notes[0].message, "has type UInt32");
+ assert_snapshot!(diag.notes[0].message, @"has type UInt32");
assert_eq!(diag.notes[0].span, Some(spans["left"]));
- assert_eq!(diag.notes[1].message, "has type Utf8");
+ assert_snapshot!(diag.notes[1].message, @"has type Utf8");
assert_eq!(diag.notes[1].span, Some(spans["right"]));
Ok(())
}
@@ -227,7 +219,7 @@ fn test_field_not_found_suggestion() -> Result<()> {
let query = "SELECT /*whole*/first_na/*whole*/ FROM person";
let spans = get_spans(query);
let diag = do_query(query);
- assert_eq!(diag.message, "column 'first_na' not found");
+ assert_snapshot!(diag.message, @"column 'first_na' not found");
assert_eq!(diag.span, Some(spans["whole"]));
assert_eq!(diag.notes.len(), 1);
@@ -243,7 +235,7 @@ fn test_field_not_found_suggestion() -> Result<()> {
})
.collect();
suggested_fields.sort();
- assert_eq!(suggested_fields[0], "person.first_name");
+ assert_snapshot!(suggested_fields[0], @"person.first_name");
Ok(())
}
@@ -253,7 +245,7 @@ fn test_ambiguous_column_suggestion() -> Result<()> {
let spans = get_spans(query);
let diag = do_query(query);
- assert_eq!(diag.message, "column 'id' is ambiguous");
+ assert_snapshot!(diag.message, @"column 'id' is ambiguous");
assert_eq!(diag.span, Some(spans["whole"]));
assert_eq!(diag.notes.len(), 2);
@@ -281,8 +273,8 @@ fn test_invalid_function() -> Result<()> {
let query = "SELECT /*whole*/concat_not_exist/*whole*/()";
let spans = get_spans(query);
let diag = do_query(query);
- assert_eq!(diag.message, "Invalid function 'concat_not_exist'");
- assert_eq!(diag.notes[0].message, "Possible function 'concat'");
+ assert_snapshot!(diag.message, @"Invalid function 'concat_not_exist'");
+ assert_snapshot!(diag.notes[0].message, @"Possible function 'concat'");
assert_eq!(diag.span, Some(spans["whole"]));
Ok(())
}
@@ -292,10 +284,7 @@ fn test_scalar_subquery_multiple_columns() -> Result<(), Box<dyn std::error::Err
let spans = get_spans(query);
let diag = do_query(query);
- assert_eq!(
- diag.message,
- "Too many columns! The subquery should only return one column"
- );
+ assert_snapshot!(diag.message, @"Too many columns! The subquery should only return one column");
let expected_span = Some(Span {
start: spans["x"].start,
@@ -327,10 +316,7 @@ fn test_in_subquery_multiple_columns() -> Result<(), Box<dyn std::error::Error>>
let spans = get_spans(query);
let diag = do_query(query);
- assert_eq!(
- diag.message,
- "Too many columns! The subquery should only return one column"
- );
+ assert_snapshot!(diag.message, @"Too many columns! The subquery should only return one column");
let expected_span = Some(Span {
start: spans["id"].start,
@@ -360,16 +346,10 @@ fn test_unary_op_plus_with_column() -> Result<()> {
let query = "SELECT +/*whole*/first_name/*whole*/ FROM person";
let spans = get_spans(query);
let diag = do_query(query);
- assert_eq!(diag.message, "+ cannot be used with Utf8");
+ assert_snapshot!(diag.message, @"+ cannot be used with Utf8");
assert_eq!(diag.span, Some(spans["whole"]));
- assert_eq!(
- diag.notes[0].message,
- "+ can only be used with numbers, intervals, and timestamps"
- );
- assert_eq!(
- diag.helps[0].message,
- "perhaps you need to cast person.first_name"
- );
+ assert_snapshot!(diag.notes[0].message, @"+ can only be used with numbers, intervals, and timestamps");
+ assert_snapshot!(diag.helps[0].message, @"perhaps you need to cast person.first_name");
Ok(())
}
@@ -379,15 +359,9 @@ fn test_unary_op_plus_with_non_column() -> Result<()> {
let query = "SELECT +'a'";
let diag = do_query(query);
assert_eq!(diag.message, "+ cannot be used with Utf8");
- assert_eq!(
- diag.notes[0].message,
- "+ can only be used with numbers, intervals, and timestamps"
- );
+ assert_snapshot!(diag.notes[0].message, @"+ can only be used with numbers, intervals, and timestamps");
assert_eq!(diag.notes[0].span, None);
- assert_eq!(
- diag.helps[0].message,
- "perhaps you need to cast Utf8(\"a\")"
- );
+ assert_snapshot!(diag.helps[0].message, @r#"perhaps you need to cast Utf8("a")"#);
assert_eq!(diag.helps[0].span, None);
assert_eq!(diag.span, None);
Ok(())
diff --git a/datafusion/sql/tests/cases/plan_to_sql.rs b/datafusion/sql/tests/cases/plan_to_sql.rs
index b7185c2d50..52759c9099 100644
--- a/datafusion/sql/tests/cases/plan_to_sql.rs
+++ b/datafusion/sql/tests/cases/plan_to_sql.rs
@@ -17,7 +17,8 @@
use arrow::datatypes::{DataType, Field, Schema};
use datafusion_common::{
- assert_contains, Column, DFSchema, DFSchemaRef, Result, TableReference,
+ assert_contains, Column, DFSchema, DFSchemaRef, DataFusionError, Result,
+ TableReference,
};
use datafusion_expr::test::function_stub::{
count_udaf, max_udaf, min_udaf, sum, sum_udaf,
@@ -38,6 +39,7 @@ use datafusion_sql::unparser::dialect::{
PostgreSqlDialect as UnparserPostgreSqlDialect, SqliteDialect,
};
use datafusion_sql::unparser::{expr_to_sql, plan_to_sql, Unparser};
+use insta::assert_snapshot;
use sqlparser::ast::Statement;
use std::hash::Hash;
use std::ops::Add;
@@ -62,46 +64,44 @@ use sqlparser::dialect::{Dialect, GenericDialect, MySqlDialect};
use sqlparser::parser::Parser;
#[test]
-fn roundtrip_expr() {
- let tests: Vec<(TableReference, &str, &str)> = vec![
- (TableReference::bare("person"), "age > 35", r#"(age > 35)"#),
- (
- TableReference::bare("person"),
- "id = '10'",
- r#"(id = '10')"#,
- ),
- (
- TableReference::bare("person"),
- "CAST(id AS VARCHAR)",
- r#"CAST(id AS VARCHAR)"#,
- ),
- (
- TableReference::bare("person"),
- "sum((age * 2))",
- r#"sum((age * 2))"#,
- ),
- ];
+fn test_roundtrip_expr_1() {
+ let expr = roundtrip_expr(TableReference::bare("person"), "age > 35").unwrap();
+ assert_snapshot!(expr, @r#"(age > 35)"#);
+}
- let roundtrip = |table, sql: &str| -> Result<String> {
- let dialect = GenericDialect {};
- let sql_expr = Parser::new(&dialect).try_with_sql(sql)?.parse_expr()?;
- let state = MockSessionState::default().with_aggregate_function(sum_udaf());
- let context = MockContextProvider { state };
- let schema = context.get_table_source(table)?.schema();
- let df_schema = DFSchema::try_from(schema.as_ref().clone())?;
- let sql_to_rel = SqlToRel::new(&context);
- let expr =
- sql_to_rel.sql_to_expr(sql_expr, &df_schema, &mut PlannerContext::new())?;
+#[test]
+fn test_roundtrip_expr_2() {
+ let expr = roundtrip_expr(TableReference::bare("person"), "id = '10'").unwrap();
+ assert_snapshot!(expr, @r#"(id = '10')"#);
+}
- let ast = expr_to_sql(&expr)?;
+#[test]
+fn test_roundtrip_expr_3() {
+ let expr =
+ roundtrip_expr(TableReference::bare("person"), "CAST(id AS VARCHAR)").unwrap();
+ assert_snapshot!(expr, @r#"CAST(id AS VARCHAR)"#);
+}
- Ok(ast.to_string())
- };
+#[test]
+fn test_roundtrip_expr_4() {
+ let expr = roundtrip_expr(TableReference::bare("person"), "sum((age * 2))").unwrap();
+ assert_snapshot!(expr, @r#"sum((age * 2))"#);
+}
- for (table, query, expected) in tests {
- let actual = roundtrip(table, query).unwrap();
- assert_eq!(actual, expected);
- }
+fn roundtrip_expr(table: TableReference, sql: &str) -> Result<String> {
+ let dialect = GenericDialect {};
+ let sql_expr = Parser::new(&dialect).try_with_sql(sql)?.parse_expr()?;
+ let state = MockSessionState::default().with_aggregate_function(sum_udaf());
+ let context = MockContextProvider { state };
+ let schema = context.get_table_source(table)?.schema();
+ let df_schema = DFSchema::try_from(schema.as_ref().clone())?;
+ let sql_to_rel = SqlToRel::new(&context);
+ let expr =
+ sql_to_rel.sql_to_expr(sql_expr, &df_schema, &mut PlannerContext::new())?;
+
+ let ast = expr_to_sql(&expr)?;
+
+ Ok(ast.to_string())
}
#[test]
@@ -236,10 +236,6 @@ fn roundtrip_statement() -> Result<()> {
let roundtrip_statement = plan_to_sql(&plan)?;
- let actual = &roundtrip_statement.to_string();
- println!("roundtrip sql: {actual}");
- println!("plan {}", plan.display_indent());
-
let plan_roundtrip = sql_to_rel
.sql_statement_to_plan(roundtrip_statement.clone())
.unwrap();
@@ -275,14 +271,59 @@ fn roundtrip_crossjoin() -> Result<()> {
let plan_roundtrip = sql_to_rel
.sql_statement_to_plan(roundtrip_statement)
.unwrap();
+ assert_snapshot!(
+ plan_roundtrip,
+ @r"
+ Projection: j1.j1_id, j2.j2_string
+ Cross Join:
+ TableScan: j1
+ TableScan: j2
+ "
+ );
+
+ Ok(())
+}
+
+#[macro_export]
+macro_rules! roundtrip_statement_with_dialect_helper {
+ (
+ query: $sql:expr,
+ parser_dialect: $parser_dialect:expr,
+ unparser_dialect: $unparser_dialect:expr,
+ expected: @ $expected:literal $(,)?
+ ) => {{
+ let statement = Parser::new(&$parser_dialect)
+ .try_with_sql($sql)?
+ .parse_statement()?;
+
+ let state = MockSessionState::default()
+ .with_aggregate_function(max_udaf())
+ .with_aggregate_function(min_udaf())
+ .with_expr_planner(Arc::new(CoreFunctionPlanner::default()))
+ .with_expr_planner(Arc::new(NestedFunctionPlanner));
+
+ let context = MockContextProvider { state };
+ let sql_to_rel = SqlToRel::new(&context);
+ let plan = sql_to_rel
+ .sql_statement_to_plan(statement)
+ .unwrap_or_else(|e| panic!("Failed to parse sql: {}\n{e}", $sql));
- let expected = "Projection: j1.j1_id, j2.j2_string\
- \n Cross Join: \
- \n TableScan: j1\
- \n TableScan: j2";
+ let unparser = Unparser::new(&$unparser_dialect);
+ let roundtrip_statement = unparser.plan_to_sql(&plan)?;
- assert_eq!(plan_roundtrip.to_string(), expected);
+ let actual = &roundtrip_statement.to_string();
+ insta::assert_snapshot!(actual, @ $expected);
+ }};
+}
+#[test]
+fn roundtrip_statement_with_dialect_1() -> Result<(), DataFusionError> {
+ roundtrip_statement_with_dialect_helper!(
+ query: "select min(ta.j1_id) as j1_min from j1 ta order by min(ta.j1_id) limit 10;",
+ parser_dialect: MySqlDialect {},
+ unparser_dialect: UnparserMySqlDialect {},
+ expected: @"SELECT `j1_min` FROM (SELECT min(`ta`.`j1_id`) AS `j1_min`, min(`ta`.`j1_id`) FROM `j1` AS `ta` ORDER BY min(`ta`.`j1_id`) ASC) AS `derived_sort` LIMIT 10",
+ );
Ok(())
}
@@ -677,8 +718,6 @@ fn roundtrip_statement_with_dialect() -> Result<()> {
let roundtrip_statement = unparser.plan_to_sql(&plan)?;
let actual = &roundtrip_statement.to_string();
- println!("roundtrip sql: {actual}");
- println!("plan {}", plan.display_indent());
assert_eq!(query.expected, actual);
}
@@ -700,13 +739,14 @@ fn test_unnest_logical_plan() -> Result<()> {
};
let sql_to_rel = SqlToRel::new(&context);
let plan = sql_to_rel.sql_statement_to_plan(statement).unwrap();
- let expected = r#"
+ assert_snapshot!(
+ plan,
+ @r#"
Projection: __unnest_placeholder(unnest_table.struct_col).field1, __unnest_placeholder(unnest_table.struct_col).field2, __unnest_placeholder(unnest_table.array_col,depth=1) AS UNNEST(unnest_table.array_col), unnest_table.struct_col, unnest_table.array_col
Unnest: lists[__unnest_placeholder(unnest_table.array_col)|depth=1] structs[__unnest_placeholder(unnest_table.struct_col)]
Projection: unnest_table.struct_col AS __unnest_placeholder(unnest_table.struct_col), unnest_table.array_col AS __unnest_placeholder(unnest_table.array_col), unnest_table.struct_col, unnest_table.array_col
- TableScan: unnest_table"#.trim_start();
-
- assert_eq!(plan.to_string(), expected);
+ TableScan: unnest_table"#
+ );
Ok(())
}
@@ -726,121 +766,248 @@ fn test_aggregation_without_projection() -> Result<()> {
let unparser = Unparser::default();
let statement = unparser.plan_to_sql(&plan)?;
-
- let actual = &statement.to_string();
-
- assert_eq!(
- actual,
- r#"SELECT sum(users.age), users."name" FROM users GROUP BY users."name""#
+ assert_snapshot!(
+ statement,
+ @r#"SELECT sum(users.age), users."name" FROM users GROUP BY users."name""#
);
Ok(())
}
-#[test]
-fn test_table_references_in_plan_to_sql() {
- fn test(table_name: &str, expected_sql: &str, dialect: &impl UnparserDialect) {
- let schema = Schema::new(vec![
- Field::new("id", DataType::Utf8, false),
- Field::new("value", DataType::Utf8, false),
- ]);
- let plan = table_scan(Some(table_name), &schema, None)
- .unwrap()
- .project(vec![col("id"), col("value")])
- .unwrap()
- .build()
- .unwrap();
-
- let unparser = Unparser::new(dialect);
- let sql = unparser.plan_to_sql(&plan).unwrap();
-
- assert_eq!(sql.to_string(), expected_sql)
- }
+/// return a schema with two string columns: "id" and "value"
+fn test_schema() -> Schema {
+ Schema::new(vec![
+ Field::new("id", DataType::Utf8, false),
+ Field::new("value", DataType::Utf8, false),
+ ])
+}
- test(
- "catalog.schema.table",
- r#"SELECT "table".id, "table"."value" FROM "catalog"."schema"."table""#,
+#[test]
+fn test_table_references_in_plan_to_sql_1() {
+ let table_name = "catalog.schema.table";
+ let schema = test_schema();
+ let sql = table_references_in_plan_helper(
+ table_name,
+ schema,
+ vec![col("id"), col("value")],
&DefaultDialect {},
);
- test(
- "schema.table",
- r#"SELECT "table".id, "table"."value" FROM "schema"."table""#,
+ assert_snapshot!(
+ sql,
+ @r#"SELECT "table".id, "table"."value" FROM "catalog"."schema"."table""#
+ );
+}
+
+#[test]
+fn test_table_references_in_plan_to_sql_2() {
+ let table_name = "schema.table";
+ let schema = test_schema();
+ let sql = table_references_in_plan_helper(
+ table_name,
+ schema,
+ vec![col("id"), col("value")],
&DefaultDialect {},
);
- test(
- "table",
- r#"SELECT "table".id, "table"."value" FROM "table""#,
+ assert_snapshot!(
+ sql,
+ @r#"SELECT "table".id, "table"."value" FROM "schema"."table""#
+ );
+}
+
+#[test]
+fn test_table_references_in_plan_to_sql_3() {
+ let table_name = "table";
+ let schema = test_schema();
+ let sql = table_references_in_plan_helper(
+ table_name,
+ schema,
+ vec![col("id"), col("value")],
&DefaultDialect {},
);
+ assert_snapshot!(
+ sql,
+ @r#"SELECT "table".id, "table"."value" FROM "table""#
+ );
+}
+#[test]
+fn test_table_references_in_plan_to_sql_4() {
+ let table_name = "catalog.schema.table";
+ let schema = test_schema();
let custom_dialect = CustomDialectBuilder::default()
.with_full_qualified_col(true)
.with_identifier_quote_style('"')
.build();
- test(
- "catalog.schema.table",
- r#"SELECT "catalog"."schema"."table"."id", "catalog"."schema"."table"."value" FROM "catalog"."schema"."table""#,
+ let sql = table_references_in_plan_helper(
+ table_name,
+ schema,
+ vec![col("id"), col("value")],
&custom_dialect,
);
- test(
- "schema.table",
- r#"SELECT "schema"."table"."id", "schema"."table"."value" FROM "schema"."table""#,
+ assert_snapshot!(
+ sql,
+ @r#"SELECT "catalog"."schema"."table"."id", "catalog"."schema"."table"."value" FROM "catalog"."schema"."table""#
+ );
+}
+
+#[test]
+fn test_table_references_in_plan_to_sql_5() {
+ let table_name = "schema.table";
+ let schema = test_schema();
+ let custom_dialect = CustomDialectBuilder::default()
+ .with_full_qualified_col(true)
+ .with_identifier_quote_style('"')
+ .build();
+
+ let sql = table_references_in_plan_helper(
+ table_name,
+ schema,
+ vec![col("id"), col("value")],
&custom_dialect,
);
- test(
- "table",
- r#"SELECT "table"."id", "table"."value" FROM "table""#,
+ assert_snapshot!(
+ sql,
+ @r#"SELECT "schema"."table"."id", "schema"."table"."value" FROM "schema"."table""#
+ );
+}
+
+#[test]
+fn test_table_references_in_plan_to_sql_6() {
+ let table_name = "table";
+ let schema = test_schema();
+ let custom_dialect = CustomDialectBuilder::default()
+ .with_full_qualified_col(true)
+ .with_identifier_quote_style('"')
+ .build();
+
+ let sql = table_references_in_plan_helper(
+ table_name,
+ schema,
+ vec![col("id"), col("value")],
&custom_dialect,
);
+ assert_snapshot!(
+ sql,
+ @r#"SELECT "table"."id", "table"."value" FROM "table""#
+ );
+}
+
+fn table_references_in_plan_helper(
+ table_name: &str,
+ table_schema: Schema,
+ expr: impl IntoIterator<Item = impl Into<datafusion_expr::select_expr::SelectExpr>>,
+ dialect: &impl UnparserDialect,
+) -> Statement {
+ let plan = table_scan(Some(table_name), &table_schema, None)
+ .unwrap()
+ .project(expr)
+ .unwrap()
+ .build()
+ .unwrap();
+ let unparser = Unparser::new(dialect);
+ unparser.plan_to_sql(&plan).unwrap()
}
#[test]
-fn test_table_scan_with_none_projection_in_plan_to_sql() {
- fn test(table_name: &str, expected_sql: &str) {
- let schema = Schema::new(vec![
- Field::new("id", DataType::Utf8, false),
- Field::new("value", DataType::Utf8, false),
- ]);
-
- let plan = table_scan(Some(table_name), &schema, None)
- .unwrap()
- .build()
- .unwrap();
- let sql = plan_to_sql(&plan).unwrap();
- assert_eq!(sql.to_string(), expected_sql)
- }
+fn test_table_scan_with_none_projection_in_plan_to_sql_1() {
+ let schema = test_schema();
+ let table_name = "catalog.schema.table";
+ let plan = table_scan_with_empty_projection_and_none_projection_helper(
+ table_name, schema, None,
+ );
+ let sql = plan_to_sql(&plan).unwrap();
+ assert_snapshot!(
+ sql,
+ @r#"SELECT * FROM "catalog"."schema"."table""#
+ );
+}
- test(
- "catalog.schema.table",
- r#"SELECT * FROM "catalog"."schema"."table""#,
+#[test]
+fn test_table_scan_with_none_projection_in_plan_to_sql_2() {
+ let schema = test_schema();
+ let table_name = "schema.table";
+ let plan = table_scan_with_empty_projection_and_none_projection_helper(
+ table_name, schema, None,
+ );
+ let sql = plan_to_sql(&plan).unwrap();
+ assert_snapshot!(
+ sql,
+ @r#"SELECT * FROM "schema"."table""#
);
- test("schema.table", r#"SELECT * FROM "schema"."table""#);
- test("table", r#"SELECT * FROM "table""#);
}
#[test]
-fn test_table_scan_with_empty_projection_in_plan_to_sql() {
- fn test(table_name: &str, expected_sql: &str) {
- let schema = Schema::new(vec![
- Field::new("id", DataType::Utf8, false),
- Field::new("value", DataType::Utf8, false),
- ]);
-
- let plan = table_scan(Some(table_name), &schema, Some(vec![]))
- .unwrap()
- .build()
- .unwrap();
- let sql = plan_to_sql(&plan).unwrap();
- assert_eq!(sql.to_string(), expected_sql)
- }
+fn test_table_scan_with_none_projection_in_plan_to_sql_3() {
+ let schema = test_schema();
+ let table_name = "table";
+ let plan = table_scan_with_empty_projection_and_none_projection_helper(
+ table_name, schema, None,
+ );
+ let sql = plan_to_sql(&plan).unwrap();
+ assert_snapshot!(
+ sql,
+ @r#"SELECT * FROM "table""#
+ );
+}
- test(
- "catalog.schema.table",
- r#"SELECT 1 FROM "catalog"."schema"."table""#,
+#[test]
+fn test_table_scan_with_empty_projection_in_plan_to_sql_1() {
+ let schema = test_schema();
+ let table_name = "catalog.schema.table";
+ let plan = table_scan_with_empty_projection_and_none_projection_helper(
+ table_name,
+ schema,
+ Some(vec![]),
+ );
+ let sql = plan_to_sql(&plan).unwrap();
+ assert_snapshot!(
+ sql,
+ @r#"SELECT 1 FROM "catalog"."schema"."table""#
);
- test("schema.table", r#"SELECT 1 FROM "schema"."table""#);
- test("table", r#"SELECT 1 FROM "table""#);
+}
+
+#[test]
+fn test_table_scan_with_empty_projection_in_plan_to_sql_2() {
+ let schema = test_schema();
+ let table_name = "schema.table";
+ let plan = table_scan_with_empty_projection_and_none_projection_helper(
+ table_name,
+ schema,
+ Some(vec![]),
+ );
+ let sql = plan_to_sql(&plan).unwrap();
+ assert_snapshot!(
+ sql,
+ @r#"SELECT 1 FROM "schema"."table""#
+ );
+}
+
+#[test]
+fn test_table_scan_with_empty_projection_in_plan_to_sql_3() {
+ let schema = test_schema();
+ let table_name = "table";
+ let plan = table_scan_with_empty_projection_and_none_projection_helper(
+ table_name,
+ schema,
+ Some(vec![]),
+ );
+ let sql = plan_to_sql(&plan).unwrap();
+ assert_snapshot!(
+ sql,
+ @r#"SELECT 1 FROM "table""#
+ );
+}
+
+fn table_scan_with_empty_projection_and_none_projection_helper(
+ table_name: &str,
+ table_schema: Schema,
+ projection: Option<Vec<usize>>,
+) -> LogicalPlan {
+ table_scan(Some(table_name), &table_schema, projection)
+ .unwrap()
+ .build()
+ .unwrap()
}
#[test]
@@ -920,12 +1087,12 @@ fn test_pretty_roundtrip() -> Result<()> {
Ok(())
}
-fn sql_round_trip<D>(dialect: D, query: &str, expect: &str)
+fn generate_round_trip_statement<D>(dialect: D, sql: &str) -> Statement
where
D: Dialect,
{
let statement = Parser::new(&dialect)
- .try_with_sql(query)
+ .try_with_sql(sql)
.unwrap()
.parse_statement()
.unwrap();
@@ -942,8 +1109,7 @@ where
let sql_to_rel = SqlToRel::new(&context);
let plan = sql_to_rel.sql_statement_to_plan(statement).unwrap();
- let roundtrip_statement = plan_to_sql(&plan).unwrap();
- assert_eq!(roundtrip_statement.to_string(), expect);
+ plan_to_sql(&plan).unwrap()
}
#[test]
@@ -958,7 +1124,10 @@ fn test_table_scan_alias() -> Result<()> {
.alias("a")?
.build()?;
let sql = plan_to_sql(&plan)?;
- assert_eq!(sql.to_string(), "SELECT * FROM (SELECT t1.id FROM t1) AS a");
+ assert_snapshot!(
+ sql,
+ @"SELECT * FROM (SELECT t1.id FROM t1) AS a"
+ );
let plan = table_scan(Some("t1"), &schema, None)?
.project(vec![col("id")])?
@@ -966,7 +1135,10 @@ fn test_table_scan_alias() -> Result<()> {
.build()?;
let sql = plan_to_sql(&plan)?;
- assert_eq!(sql.to_string(), "SELECT * FROM (SELECT t1.id FROM t1) AS a");
+ assert_snapshot!(
+ sql,
+ @"SELECT * FROM (SELECT t1.id FROM t1) AS a"
+ );
let plan = table_scan(Some("t1"), &schema, None)?
.filter(col("id").gt(lit(5)))?
@@ -974,9 +1146,9 @@ fn test_table_scan_alias() -> Result<()> {
.alias("a")?
.build()?;
let sql = plan_to_sql(&plan)?;
- assert_eq!(
- sql.to_string(),
- "SELECT * FROM (SELECT t1.id FROM t1 WHERE (t1.id > 5)) AS a"
+ assert_snapshot!(
+ sql,
+ @r#"SELECT * FROM (SELECT t1.id FROM t1 WHERE (t1.id > 5)) AS a"#
);
let table_scan_with_two_filter = table_scan_with_filters(
@@ -989,9 +1161,9 @@ fn test_table_scan_alias() -> Result<()> {
.alias("a")?
.build()?;
let table_scan_with_two_filter = plan_to_sql(&table_scan_with_two_filter)?;
- assert_eq!(
- table_scan_with_two_filter.to_string(),
- "SELECT a.id FROM t1 AS a WHERE ((a.id > 1) AND (a.age < 2))"
+ assert_snapshot!(
+ table_scan_with_two_filter,
+ @r#"SELECT a.id FROM t1 AS a WHERE ((a.id > 1) AND (a.age < 2))"#
);
let table_scan_with_fetch =
@@ -1000,9 +1172,9 @@ fn test_table_scan_alias() -> Result<()> {
.alias("a")?
.build()?;
let table_scan_with_fetch = plan_to_sql(&table_scan_with_fetch)?;
- assert_eq!(
- table_scan_with_fetch.to_string(),
- "SELECT a.id FROM (SELECT * FROM t1 LIMIT 10) AS a"
+ assert_snapshot!(
+ table_scan_with_fetch,
+ @r#"SELECT a.id FROM (SELECT * FROM t1 LIMIT 10) AS a"#
);
let table_scan_with_pushdown_all = table_scan_with_filter_and_fetch(
@@ -1016,9 +1188,9 @@ fn test_table_scan_alias() -> Result<()> {
.alias("a")?
.build()?;
let table_scan_with_pushdown_all = plan_to_sql(&table_scan_with_pushdown_all)?;
- assert_eq!(
- table_scan_with_pushdown_all.to_string(),
- "SELECT a.id FROM (SELECT a.id, a.age FROM t1 AS a WHERE (a.id > 1) LIMIT 10) AS a"
+ assert_snapshot!(
+ table_scan_with_pushdown_all,
+ @r#"SELECT a.id FROM (SELECT a.id, a.age FROM t1 AS a WHERE (a.id > 1) LIMIT 10) AS a"#
);
Ok(())
}
@@ -1032,18 +1204,24 @@ fn test_table_scan_pushdown() -> Result<()> {
let scan_with_projection =
table_scan(Some("t1"), &schema, Some(vec![0, 1]))?.build()?;
let scan_with_projection = plan_to_sql(&scan_with_projection)?;
- assert_eq!(
- scan_with_projection.to_string(),
- "SELECT t1.id, t1.age FROM t1"
+ assert_snapshot!(
+ scan_with_projection,
+ @r#"SELECT t1.id, t1.age FROM t1"#
);
let scan_with_projection = table_scan(Some("t1"), &schema, Some(vec![1]))?.build()?;
let scan_with_projection = plan_to_sql(&scan_with_projection)?;
- assert_eq!(scan_with_projection.to_string(), "SELECT t1.age FROM t1");
+ assert_snapshot!(
+ scan_with_projection,
+ @r#"SELECT t1.age FROM t1"#
+ );
let scan_with_no_projection = table_scan(Some("t1"), &schema, None)?.build()?;
let scan_with_no_projection = plan_to_sql(&scan_with_no_projection)?;
- assert_eq!(scan_with_no_projection.to_string(), "SELECT * FROM t1");
+ assert_snapshot!(
+ scan_with_no_projection,
+ @r#"SELECT * FROM t1"#
+ );
let table_scan_with_projection_alias =
table_scan(Some("t1"), &schema, Some(vec![0, 1]))?
@@ -1051,9 +1229,9 @@ fn test_table_scan_pushdown() -> Result<()> {
.build()?;
let table_scan_with_projection_alias =
plan_to_sql(&table_scan_with_projection_alias)?;
- assert_eq!(
- table_scan_with_projection_alias.to_string(),
- "SELECT ta.id, ta.age FROM t1 AS ta"
+ assert_snapshot!(
+ table_scan_with_projection_alias,
+ @r#"SELECT ta.id, ta.age FROM t1 AS ta"#
);
let table_scan_with_projection_alias =
@@ -1062,9 +1240,9 @@ fn test_table_scan_pushdown() -> Result<()> {
.build()?;
let table_scan_with_projection_alias =
plan_to_sql(&table_scan_with_projection_alias)?;
- assert_eq!(
- table_scan_with_projection_alias.to_string(),
- "SELECT ta.age FROM t1 AS ta"
+ assert_snapshot!(
+ table_scan_with_projection_alias,
+ @r#"SELECT ta.age FROM t1 AS ta"#
);
let table_scan_with_no_projection_alias = table_scan(Some("t1"), &schema, None)?
@@ -1072,9 +1250,9 @@ fn test_table_scan_pushdown() -> Result<()> {
.build()?;
let table_scan_with_no_projection_alias =
plan_to_sql(&table_scan_with_no_projection_alias)?;
- assert_eq!(
- table_scan_with_no_projection_alias.to_string(),
- "SELECT * FROM t1 AS ta"
+ assert_snapshot!(
+ table_scan_with_no_projection_alias,
+ @r#"SELECT * FROM t1 AS ta"#
);
let query_from_table_scan_with_projection = LogicalPlanBuilder::from(
@@ -1084,9 +1262,9 @@ fn test_table_scan_pushdown() -> Result<()> {
.build()?;
let query_from_table_scan_with_projection =
plan_to_sql(&query_from_table_scan_with_projection)?;
- assert_eq!(
- query_from_table_scan_with_projection.to_string(),
- "SELECT t1.id, t1.age FROM t1"
+ assert_snapshot!(
+ query_from_table_scan_with_projection,
+ @r#"SELECT t1.id, t1.age FROM t1"#
);
let query_from_table_scan_with_two_projections = LogicalPlanBuilder::from(
@@ -1097,9 +1275,9 @@ fn test_table_scan_pushdown() -> Result<()> {
.build()?;
let query_from_table_scan_with_two_projections =
plan_to_sql(&query_from_table_scan_with_two_projections)?;
- assert_eq!(
- query_from_table_scan_with_two_projections.to_string(),
- "SELECT t1.id, t1.age FROM (SELECT t1.id, t1.age FROM t1)"
+ assert_snapshot!(
+ query_from_table_scan_with_two_projections,
+ @r#"SELECT t1.id, t1.age FROM (SELECT t1.id, t1.age FROM t1)"#
);
let table_scan_with_filter = table_scan_with_filters(
@@ -1110,9 +1288,9 @@ fn test_table_scan_pushdown() -> Result<()> {
)?
.build()?;
let table_scan_with_filter = plan_to_sql(&table_scan_with_filter)?;
- assert_eq!(
- table_scan_with_filter.to_string(),
- "SELECT * FROM t1 WHERE (t1.id > t1.age)"
+ assert_snapshot!(
+ table_scan_with_filter,
+ @r#"SELECT * FROM t1 WHERE (t1.id > t1.age)"#
);
let table_scan_with_two_filter = table_scan_with_filters(
@@ -1123,9 +1301,9 @@ fn test_table_scan_pushdown() -> Result<()> {
)?
.build()?;
let table_scan_with_two_filter = plan_to_sql(&table_scan_with_two_filter)?;
- assert_eq!(
- table_scan_with_two_filter.to_string(),
- "SELECT * FROM t1 WHERE ((t1.id > 1) AND (t1.age < 2))"
+ assert_snapshot!(
+ table_scan_with_two_filter,
+ @r#"SELECT * FROM t1 WHERE ((t1.id > 1) AND (t1.age < 2))"#
);
let table_scan_with_filter_alias = table_scan_with_filters(
@@ -1137,9 +1315,9 @@ fn test_table_scan_pushdown() -> Result<()> {
.alias("ta")?
.build()?;
let table_scan_with_filter_alias = plan_to_sql(&table_scan_with_filter_alias)?;
- assert_eq!(
- table_scan_with_filter_alias.to_string(),
- "SELECT * FROM t1 AS ta WHERE (ta.id > ta.age)"
+ assert_snapshot!(
+ table_scan_with_filter_alias,
+ @r#"SELECT * FROM t1 AS ta WHERE (ta.id > ta.age)"#
);
let table_scan_with_projection_and_filter = table_scan_with_filters(
@@ -1151,9 +1329,9 @@ fn test_table_scan_pushdown() -> Result<()> {
.build()?;
let table_scan_with_projection_and_filter =
plan_to_sql(&table_scan_with_projection_and_filter)?;
- assert_eq!(
- table_scan_with_projection_and_filter.to_string(),
- "SELECT t1.id, t1.age FROM t1 WHERE (t1.id > t1.age)"
+ assert_snapshot!(
+ table_scan_with_projection_and_filter,
+ @r#"SELECT t1.id, t1.age FROM t1 WHERE (t1.id > t1.age)"#
);
let table_scan_with_projection_and_filter = table_scan_with_filters(
@@ -1165,18 +1343,18 @@ fn test_table_scan_pushdown() -> Result<()> {
.build()?;
let table_scan_with_projection_and_filter =
plan_to_sql(&table_scan_with_projection_and_filter)?;
- assert_eq!(
- table_scan_with_projection_and_filter.to_string(),
- "SELECT t1.age FROM t1 WHERE (t1.id > t1.age)"
+ assert_snapshot!(
+ table_scan_with_projection_and_filter,
+ @r#"SELECT t1.age FROM t1 WHERE (t1.id > t1.age)"#
);
let table_scan_with_inline_fetch =
table_scan_with_filter_and_fetch(Some("t1"), &schema, None, vec![], Some(10))?
.build()?;
let table_scan_with_inline_fetch = plan_to_sql(&table_scan_with_inline_fetch)?;
- assert_eq!(
- table_scan_with_inline_fetch.to_string(),
- "SELECT * FROM t1 LIMIT 10"
+ assert_snapshot!(
+ table_scan_with_inline_fetch,
+ @r#"SELECT * FROM t1 LIMIT 10"#
);
let table_scan_with_projection_and_inline_fetch = table_scan_with_filter_and_fetch(
@@ -1189,9 +1367,9 @@ fn test_table_scan_pushdown() -> Result<()> {
.build()?;
let table_scan_with_projection_and_inline_fetch =
plan_to_sql(&table_scan_with_projection_and_inline_fetch)?;
- assert_eq!(
- table_scan_with_projection_and_inline_fetch.to_string(),
- "SELECT t1.id, t1.age FROM t1 LIMIT 10"
+ assert_snapshot!(
+ table_scan_with_projection_and_inline_fetch,
+ @r#"SELECT t1.id, t1.age FROM t1 LIMIT 10"#
);
let table_scan_with_all = table_scan_with_filter_and_fetch(
@@ -1203,9 +1381,9 @@ fn test_table_scan_pushdown() -> Result<()> {
)?
.build()?;
let table_scan_with_all = plan_to_sql(&table_scan_with_all)?;
- assert_eq!(
- table_scan_with_all.to_string(),
- "SELECT t1.id, t1.age FROM t1 WHERE (t1.id > t1.age) LIMIT 10"
+ assert_snapshot!(
+ table_scan_with_all,
+ @r#"SELECT t1.id, t1.age FROM t1 WHERE (t1.id > t1.age) LIMIT 10"#
);
let table_scan_with_additional_filter = table_scan_with_filters(
@@ -1217,9 +1395,9 @@ fn test_table_scan_pushdown() -> Result<()> {
.filter(col("id").eq(lit(5)))?
.build()?;
let table_scan_with_filter = plan_to_sql(&table_scan_with_additional_filter)?;
- assert_eq!(
- table_scan_with_filter.to_string(),
- "SELECT * FROM t1 WHERE (t1.id = 5) AND (t1.id > t1.age)"
+ assert_snapshot!(
+ table_scan_with_filter,
+ @r#"SELECT * FROM t1 WHERE (t1.id = 5) AND (t1.id > t1.age)"#
);
Ok(())
@@ -1238,9 +1416,9 @@ fn test_sort_with_push_down_fetch() -> Result<()> {
.build()?;
let sql = plan_to_sql(&plan)?;
- assert_eq!(
- format!("{}", sql),
- "SELECT t1.id, t1.age FROM t1 ORDER BY t1.age ASC NULLS FIRST LIMIT 10"
+ assert_snapshot!(
+ sql,
+ @r#"SELECT t1.id, t1.age FROM t1 ORDER BY t1.age ASC NULLS FIRST LIMIT 10"#
);
Ok(())
}
@@ -1284,10 +1462,10 @@ fn test_join_with_table_scan_filters() -> Result<()> {
.build()?;
let sql = plan_to_sql(&join_plan_with_filter)?;
-
- let expected_sql = r#"SELECT * FROM left_table AS "left" INNER JOIN right_table ON "left".id = right_table.id AND (("left".id > 5) AND ("left"."name" LIKE 'some_name' AND (age > 10)))"#;
-
- assert_eq!(sql.to_string(), expected_sql);
+ assert_snapshot!(
+ sql,
+ @r#"SELECT * FROM left_table AS "left" INNER JOIN right_table ON "left".id = right_table.id AND (("left".id > 5) AND ("left"."name" LIKE 'some_name' AND (age > 10)))"#
+ );
let join_plan_no_filter = LogicalPlanBuilder::from(left_plan.clone())
.join(
@@ -1299,10 +1477,10 @@ fn test_join_with_table_scan_filters() -> Result<()> {
.build()?;
let sql = plan_to_sql(&join_plan_no_filter)?;
-
- let expected_sql = r#"SELECT * FROM left_table AS "left" INNER JOIN right_table ON "left".id = right_table.id AND ("left"."name" LIKE 'some_name' AND (age > 10))"#;
-
- assert_eq!(sql.to_string(), expected_sql);
+ assert_snapshot!(
+ sql,
+ @r#"SELECT * FROM left_table AS "left" INNER JOIN right_table ON "left".id = right_table.id AND ("left"."name" LIKE 'some_name' AND (age > 10))"#
+ );
let right_plan_with_filter = table_scan_with_filters(
Some("right_table"),
@@ -1324,10 +1502,10 @@ fn test_join_with_table_scan_filters() -> Result<()> {
.build()?;
let sql = plan_to_sql(&join_plan_multiple_filters)?;
-
- let expected_sql = r#"SELECT * FROM left_table AS "left" INNER JOIN right_table ON "left".id = right_table.id AND (("left".id > 5) AND (("left"."name" LIKE 'some_name' AND (right_table."name" = 'before_join_filter_val')) AND (age > 10))) WHERE ("left"."name" = 'after_join_filter_val')"#;
-
- assert_eq!(sql.to_string(), expected_sql);
+ assert_snapshot!(
+ sql,
+ @r#"SELECT * FROM left_table AS "left" INNER JOIN right_table ON "left".id = right_table.id AND (("left".id > 5) AND (("left"."name" LIKE 'some_name' AND (right_table."name" = 'before_join_filter_val')) AND (age > 10))) WHERE ("left"."name" = 'after_join_filter_val')"#
+ );
let right_plan_with_filter_schema = table_scan_with_filters(
Some("right_table"),
@@ -1354,114 +1532,153 @@ fn test_join_with_table_scan_filters() -> Result<()> {
.build()?;
let sql = plan_to_sql(&join_plan_duplicated_filter)?;
-
- let expected_sql = r#"SELECT * FROM left_table AS "left" INNER JOIN right_table ON "left".id = right_table.id AND (("left".id > 5) AND (("left"."name" LIKE 'some_name' AND (right_table.age > 10)) AND (right_table.age < 11)))"#;
-
- assert_eq!(sql.to_string(), expected_sql);
+ assert_snapshot!(
+ sql,
+ @r#"SELECT * FROM left_table AS "left" INNER JOIN right_table ON "left".id = right_table.id AND (("left".id > 5) AND (("left"."name" LIKE 'some_name' AND (right_table.age > 10)) AND (right_table.age < 11)))"#
+ );
Ok(())
}
#[test]
fn test_interval_lhs_eq() {
- sql_round_trip(
+ let statement = generate_round_trip_statement(
GenericDialect {},
"select interval '2 seconds' = interval '2 seconds'",
- "SELECT (INTERVAL '2.000000000 SECS' = INTERVAL '2.000000000 SECS')",
);
+ assert_snapshot!(
+ statement,
+ @r#"SELECT (INTERVAL '2.000000000 SECS' = INTERVAL '2.000000000 SECS')"#
+ )
}
#[test]
fn test_interval_lhs_lt() {
- sql_round_trip(
+ let statement = generate_round_trip_statement(
GenericDialect {},
"select interval '2 seconds' < interval '2 seconds'",
- "SELECT (INTERVAL '2.000000000 SECS' < INTERVAL '2.000000000 SECS')",
);
+ assert_snapshot!(
+ statement,
+ @r#"SELECT (INTERVAL '2.000000000 SECS' < INTERVAL '2.000000000 SECS')"#
+ )
}
#[test]
fn test_without_offset() {
- sql_round_trip(MySqlDialect {}, "select 1", "SELECT 1");
+ let statement = generate_round_trip_statement(MySqlDialect {}, "select 1");
+ assert_snapshot!(
+ statement,
+ @r#"SELECT 1"#
+ )
}
#[test]
fn test_with_offset0() {
- sql_round_trip(MySqlDialect {}, "select 1 offset 0", "SELECT 1 OFFSET 0");
+ let statement = generate_round_trip_statement(MySqlDialect {}, "select 1 offset 0");
+ assert_snapshot!(
+ statement,
+ @r#"SELECT 1 OFFSET 0"#
+ )
}
#[test]
fn test_with_offset95() {
- sql_round_trip(MySqlDialect {}, "select 1 offset 95", "SELECT 1 OFFSET 95");
+ let statement = generate_round_trip_statement(MySqlDialect {}, "select 1 offset 95");
+ assert_snapshot!(
+ statement,
+ @r#"SELECT 1 OFFSET 95"#
+ )
}
#[test]
-fn test_order_by_to_sql() {
+fn test_order_by_to_sql_1() {
// order by aggregation function
- sql_round_trip(
+ let statement = generate_round_trip_statement(
GenericDialect {},
r#"SELECT id, first_name, SUM(id) FROM person GROUP BY id, first_name ORDER BY SUM(id) ASC, first_name DESC, id, first_name LIMIT 10"#,
- r#"SELECT person.id, person.first_name, sum(person.id) FROM person GROUP BY person.id, person.first_name ORDER BY sum(person.id) ASC NULLS LAST, person.first_name DESC NULLS FIRST, person.id ASC NULLS LAST, person.first_name ASC NULLS LAST LIMIT 10"#,
);
+ assert_snapshot!(
+ statement,
+ @r#"SELECT person.id, person.first_name, sum(person.id) FROM person GROUP BY person.id, person.first_name ORDER BY sum(person.id) ASC NULLS LAST, person.first_name DESC NULLS FIRST, person.id ASC NULLS LAST, person.first_name ASC NULLS LAST LIMIT 10"#
+ );
+}
+#[test]
+fn test_order_by_to_sql_2() {
// order by aggregation function alias
- sql_round_trip(
+ let statement = generate_round_trip_statement(
GenericDialect {},
r#"SELECT id, first_name, SUM(id) as total_sum FROM person GROUP BY id, first_name ORDER BY total_sum ASC, first_name DESC, id, first_name LIMIT 10"#,
- r#"SELECT person.id, person.first_name, sum(person.id) AS total_sum FROM person GROUP BY person.id, person.first_name ORDER BY total_sum ASC NULLS LAST, person.first_name DESC NULLS FIRST, person.id ASC NULLS LAST, person.first_name ASC NULLS LAST LIMIT 10"#,
);
+ assert_snapshot!(
+ statement,
+ @r#"SELECT person.id, person.first_name, sum(person.id) AS total_sum FROM person GROUP BY person.id, person.first_name ORDER BY total_sum ASC NULLS LAST, person.first_name DESC NULLS FIRST, person.id ASC NULLS LAST, person.first_name ASC NULLS LAST LIMIT 10"#
+ );
+}
- // order by scalar function from projection
- sql_round_trip(
+#[test]
+fn test_order_by_to_sql_3() {
+ let statement = generate_round_trip_statement(
GenericDialect {},
r#"SELECT id, first_name, substr(first_name,0,5) FROM person ORDER BY id, substr(first_name,0,5)"#,
- r#"SELECT person.id, person.first_name, substr(person.first_name, 0, 5) FROM person ORDER BY person.id ASC NULLS LAST, substr(person.first_name, 0, 5) ASC NULLS LAST"#,
+ );
+ assert_snapshot!(
+ statement,
+ @r#"SELECT person.id, person.first_name, substr(person.first_name, 0, 5) FROM person ORDER BY person.id ASC NULLS LAST, substr(person.first_name, 0, 5) ASC NULLS LAST"#
);
}
#[test]
fn test_aggregation_to_sql() {
- sql_round_trip(
- GenericDialect {},
- r#"SELECT id, first_name,
+ let sql = r#"SELECT id, first_name,
SUM(id) AS total_sum,
SUM(id) OVER (PARTITION BY first_name ROWS BETWEEN 5 PRECEDING AND 2 FOLLOWING) AS moving_sum,
MAX(SUM(id)) OVER (PARTITION BY first_name ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING) AS max_total,
rank() OVER (PARTITION BY grouping(id) + grouping(age), CASE WHEN grouping(age) = 0 THEN id END ORDER BY sum(id) DESC) AS rank_within_parent_1,
rank() OVER (PARTITION BY grouping(age) + grouping(id), CASE WHEN (CAST(grouping(age) AS BIGINT) = 0) THEN id END ORDER BY sum(id) DESC) AS rank_within_parent_2
FROM person
- GROUP BY id, first_name;"#,
- r#"SELECT person.id, person.first_name,
-sum(person.id) AS total_sum, sum(person.id) OVER (PARTITION BY person.first_name ROWS BETWEEN 5 PRECEDING AND 2 FOLLOWING) AS moving_sum,
-max(sum(person.id)) OVER (PARTITION BY person.first_name ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING) AS max_total,
-rank() OVER (PARTITION BY (grouping(person.id) + grouping(person.age)), CASE WHEN (grouping(person.age) = 0) THEN person.id END ORDER BY sum(person.id) DESC NULLS FIRST RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) AS rank_within_parent_1,
-rank() OVER (PARTITION BY (grouping(person.age) + grouping(person.id)), CASE WHEN (CAST(grouping(person.age) AS BIGINT) = 0) THEN person.id END ORDER BY sum(person.id) DESC NULLS FIRST RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) AS rank_within_parent_2
-FROM person
-GROUP BY person.id, person.first_name"#.replace("\n", " ").as_str(),
+ GROUP BY id, first_name"#;
+ let statement = generate_round_trip_statement(GenericDialect {}, sql);
+ assert_snapshot!(
+ statement,
+ @"SELECT person.id, person.first_name, sum(person.id) AS total_sum, sum(person.id) OVER (PARTITION BY person.first_name ROWS BETWEEN 5 PRECEDING AND 2 FOLLOWING) AS moving_sum, max(sum(person.id)) OVER (PARTITION BY person.first_name ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING) AS max_total, rank() OVER (PARTITION BY (grouping(person.id) + grouping(person.age)), CASE WHEN (grouping(person.age) = 0) THEN person.id END ORDER BY sum(person.id) DESC NULLS FIRST RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) AS rank_within_parent_1, rank() OVER (PARTITION BY (grouping(person.age) + grouping(person.id)), CASE WHEN (CAST(grouping(person.age) AS BIGINT) = 0) THEN person.id END ORDER BY sum(person.id) DESC NULLS FIRST RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) AS rank_within_parent_2 FROM person GROUP BY person.id, person.first_name",
);
}
#[test]
-fn test_unnest_to_sql() {
- sql_round_trip(
+fn test_unnest_to_sql_1() {
+ let statement = generate_round_trip_statement(
GenericDialect {},
r#"SELECT unnest(array_col) as u1, struct_col, array_col FROM unnest_table WHERE array_col != NULL ORDER BY struct_col, array_col"#,
- r#"SELECT UNNEST(unnest_table.array_col) AS u1, unnest_table.struct_col, unnest_table.array_col FROM unnest_table WHERE (unnest_table.array_col <> NULL) ORDER BY unnest_table.struct_col ASC NULLS LAST, unnest_table.array_col ASC NULLS LAST"#,
);
+ assert_snapshot!(
+ statement,
+ @r#"SELECT UNNEST(unnest_table.array_col) AS u1, unnest_table.struct_col, unnest_table.array_col FROM unnest_table WHERE (unnest_table.array_col <> NULL) ORDER BY unnest_table.struct_col ASC NULLS LAST, unnest_table.array_col ASC NULLS LAST"#
+ );
+}
- sql_round_trip(
+#[test]
+fn test_unnest_to_sql_2() {
+ let statement = generate_round_trip_statement(
GenericDialect {},
r#"SELECT unnest(make_array(1, 2, 2, 5, NULL)) as u1"#,
- r#"SELECT UNNEST([1, 2, 2, 5, NULL]) AS u1"#,
+ );
+ assert_snapshot!(
+ statement,
+ @r#"SELECT UNNEST([1, 2, 2, 5, NULL]) AS u1"#
);
}
#[test]
fn test_join_with_no_conditions() {
- sql_round_trip(
+ let statement = generate_round_trip_statement(
GenericDialect {},
"SELECT j1.j1_id, j1.j1_string FROM j1 CROSS JOIN j2",
- "SELECT j1.j1_id, j1.j1_string FROM j1 CROSS JOIN j2",
+ );
+ assert_snapshot!(
+ statement,
+ @r#"SELECT j1.j1_id, j1.j1_string FROM j1 CROSS JOIN j2"#
);
}
@@ -1562,8 +1779,10 @@ fn test_unparse_extension_to_statement() -> Result<()> {
Arc::new(UnusedUnparser {}),
]);
let sql = unparser.plan_to_sql(&extension)?;
- let expected = "SELECT j1.j1_id, j1.j1_string FROM j1";
- assert_eq!(sql.to_string(), expected);
+ assert_snapshot!(
+ sql,
+ @r#"SELECT j1.j1_id, j1.j1_string FROM j1"#
+ );
if let Some(err) = plan_to_sql(&extension).err() {
assert_contains!(
@@ -1625,9 +1844,10 @@ fn test_unparse_extension_to_sql() -> Result<()> {
Arc::new(UnusedUnparser {}),
]);
let sql = unparser.plan_to_sql(&plan)?;
- let expected =
- "SELECT j1.j1_id AS user_id FROM (SELECT j1.j1_id, j1.j1_string FROM j1)";
- assert_eq!(sql.to_string(), expected);
+ assert_snapshot!(
+ sql,
+ @r#"SELECT j1.j1_id AS user_id FROM (SELECT j1.j1_id, j1.j1_string FROM j1)"#
+ );
if let Some(err) = plan_to_sql(&plan).err() {
assert_contains!(
@@ -1665,10 +1885,10 @@ fn test_unparse_optimized_multi_union() -> Result<()> {
],
schema: dfschema.clone(),
});
-
- let sql = "SELECT 1 AS x, 'a' AS y UNION ALL SELECT 1 AS x, 'b' AS y UNION ALL SELECT 2 AS x, 'a' AS y UNION ALL SELECT 2 AS x, 'c' AS y";
-
- assert_eq!(unparser.plan_to_sql(&plan)?.to_string(), sql);
+ assert_snapshot!(
+ unparser.plan_to_sql(&plan)?,
+ @r#"SELECT 1 AS x, 'a' AS y UNION ALL SELECT 1 AS x, 'b' AS y UNION ALL SELECT 2 AS x, 'a' AS y UNION ALL SELECT 2 AS x, 'c' AS y"#
+ );
let plan = LogicalPlan::Union(Union {
inputs: vec![project(
@@ -1746,8 +1966,10 @@ fn test_unparse_subquery_alias_with_table_pushdown() -> Result<()> {
let unparser = Unparser::default();
let sql = unparser.plan_to_sql(&plan)?;
- let expected = "SELECT customer_view.c_custkey, customer_view.c_name, customer_view.custkey_plus FROM (SELECT customer.c_custkey, (CAST(customer.c_custkey AS BIGINT) + 1) AS custkey_plus, customer.c_name FROM (SELECT customer.c_custkey, customer.c_name FROM customer AS customer) AS customer) AS customer_view";
- assert_eq!(sql.to_string(), expected);
+ assert_snapshot!(
+ sql,
+ @r#"SELECT customer_view.c_custkey, customer_view.c_name, customer_view.custkey_plus FROM (SELECT customer.c_custkey, (CAST(customer.c_custkey AS BIGINT) + 1) AS custkey_plus, customer.c_name FROM (SELECT customer.c_custkey, customer.c_name FROM customer AS customer) AS customer) AS customer_view"#
+ );
Ok(())
}
@@ -1778,7 +2000,10 @@ fn test_unparse_left_anti_join() -> Result<()> {
let unparser = Unparser::new(&UnparserPostgreSqlDialect {});
let sql = unparser.plan_to_sql(&plan)?;
- assert_eq!("SELECT \"t1\".\"d\" FROM \"t1\" WHERE NOT EXISTS (SELECT 1 FROM \"t2\" AS \"__correlated_sq_1\" WHERE (\"t1\".\"c\" = \"__correlated_sq_1\".\"c\"))", sql.to_string());
+ assert_snapshot!(
+ sql,
+ @r#"SELECT "t1"."d" FROM "t1" WHERE NOT EXISTS (SELECT 1 FROM "t2" AS "__correlated_sq_1" WHERE ("t1"."c" = "__correlated_sq_1"."c"))"#
+ );
Ok(())
}
@@ -1809,7 +2034,10 @@ fn test_unparse_left_semi_join() -> Result<()> {
let unparser = Unparser::new(&UnparserPostgreSqlDialect {});
let sql = unparser.plan_to_sql(&plan)?;
- assert_eq!("SELECT \"t1\".\"d\" FROM \"t1\" WHERE EXISTS (SELECT 1 FROM \"t2\" AS \"__correlated_sq_1\" WHERE (\"t1\".\"c\" = \"__correlated_sq_1\".\"c\"))", sql.to_string());
+ assert_snapshot!(
+ sql,
+ @r#"SELECT "t1"."d" FROM "t1" WHERE EXISTS (SELECT 1 FROM "t2" AS "__correlated_sq_1" WHERE ("t1"."c" = "__correlated_sq_1"."c"))"#
+ );
Ok(())
}
@@ -1841,7 +2069,10 @@ fn test_unparse_left_mark_join() -> Result<()> {
let unparser = Unparser::new(&UnparserPostgreSqlDialect {});
let sql = unparser.plan_to_sql(&plan)?;
- assert_eq!("SELECT \"t1\".\"d\" FROM \"t1\" WHERE (EXISTS (SELECT 1 FROM \"t2\" AS \"__correlated_sq_1\" WHERE (\"t1\".\"c\" = \"__correlated_sq_1\".\"c\")) OR (\"t1\".\"d\" < 0))", sql.to_string());
+ assert_snapshot!(
+ sql,
+ @r#"SELECT "t1"."d" FROM "t1" WHERE (EXISTS (SELECT 1 FROM "t2" AS "__correlated_sq_1" WHERE ("t1"."c" = "__correlated_sq_1"."c")) OR ("t1"."d" < 0))"#
+ );
Ok(())
}
@@ -1876,7 +2107,10 @@ fn test_unparse_right_semi_join() -> Result<()> {
.build()?;
let unparser = Unparser::new(&UnparserPostgreSqlDialect {});
let sql = unparser.plan_to_sql(&plan)?;
- assert_eq!("SELECT \"t2\".\"c\", \"t2\".\"d\" FROM \"t2\" WHERE (\"t2\".\"c\" <= 1) AND EXISTS (SELECT 1 FROM \"t1\" WHERE (\"t1\".\"c\" = \"t2\".\"c\"))", sql.to_string());
+ assert_snapshot!(
+ sql,
+ @r#"SELECT "t2"."c", "t2"."d" FROM "t2" WHERE ("t2"."c" <= 1) AND EXISTS (SELECT 1 FROM "t1" WHERE ("t1"."c" = "t2"."c"))"#
+ );
Ok(())
}
@@ -1911,6 +2145,9 @@ fn test_unparse_right_anti_join() -> Result<()> {
.build()?;
let unparser = Unparser::new(&UnparserPostgreSqlDialect {});
let sql = unparser.plan_to_sql(&plan)?;
- assert_eq!("SELECT \"t2\".\"c\", \"t2\".\"d\" FROM \"t2\" WHERE (\"t2\".\"c\" <= 1) AND NOT EXISTS (SELECT 1 FROM \"t1\" WHERE (\"t1\".\"c\" = \"t2\".\"c\"))", sql.to_string());
+ assert_snapshot!(
+ sql,
+ @r#"SELECT "t2"."c", "t2"."d" FROM "t2" WHERE ("t2"."c" <= 1) AND NOT EXISTS (SELECT 1 FROM "t1" WHERE ("t1"."c" = "t2"."c"))"#
+ );
Ok(())
}