Skip to content

Commit 51452d6

Browse files
brayanjulsalamb
andauthored
Fix Infer prepare statement type tests (#15743)
* draft commit to rolledback changes on function naming and include prepare clause on the infer types tests * include data types in plan when it is not included in the prepare statement * fix: prepare statement error * Update datafusion/sql/src/statement.rs Co-authored-by: Andrew Lamb <[email protected]> * remove infer types from prepare statement the infer data type changes in statement will be introduced in a new PR * fix to show correct output message * remove white space * Restore the original tests too --------- Co-authored-by: Andrew Lamb <[email protected]>
1 parent d5ecaca commit 51452d6

File tree

1 file changed

+136
-0
lines changed

1 file changed

+136
-0
lines changed

datafusion/sql/tests/sql_integration.rs

Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4641,6 +4641,28 @@ fn test_infer_types_from_join() {
46414641
);
46424642
}
46434643

4644+
#[test]
4645+
fn test_prepare_statement_infer_types_from_join() {
4646+
let sql =
4647+
"PREPARE my_plan AS SELECT id, order_id FROM person JOIN orders ON id = customer_id and age = $1";
4648+
4649+
let plan = logical_plan(sql).unwrap();
4650+
assert_snapshot!(
4651+
plan,
4652+
@r#"
4653+
Prepare: "my_plan" []
4654+
Projection: person.id, orders.order_id
4655+
Inner Join: Filter: person.id = orders.customer_id AND person.age = $1
4656+
TableScan: person
4657+
TableScan: orders
4658+
"#
4659+
);
4660+
4661+
let actual_types = plan.get_parameter_types().unwrap();
4662+
let expected_types = HashMap::from([("$1".to_string(), Some(DataType::Int32))]);
4663+
assert_eq!(actual_types, expected_types);
4664+
}
4665+
46444666
#[test]
46454667
fn test_infer_types_from_predicate() {
46464668
let sql = "SELECT id, age FROM person WHERE age = $1";
@@ -4672,6 +4694,25 @@ fn test_infer_types_from_predicate() {
46724694
);
46734695
}
46744696

4697+
#[test]
4698+
fn test_prepare_statement_infer_types_from_predicate() {
4699+
let sql = "PREPARE my_plan AS SELECT id, age FROM person WHERE age = $1";
4700+
let plan = logical_plan(sql).unwrap();
4701+
assert_snapshot!(
4702+
plan,
4703+
@r#"
4704+
Prepare: "my_plan" []
4705+
Projection: person.id, person.age
4706+
Filter: person.age = $1
4707+
TableScan: person
4708+
"#
4709+
);
4710+
4711+
let actual_types = plan.get_parameter_types().unwrap();
4712+
let expected_types = HashMap::from([("$1".to_string(), Some(DataType::Int32))]);
4713+
assert_eq!(actual_types, expected_types);
4714+
}
4715+
46754716
#[test]
46764717
fn test_infer_types_from_between_predicate() {
46774718
let sql = "SELECT id, age FROM person WHERE age BETWEEN $1 AND $2";
@@ -4707,6 +4748,29 @@ fn test_infer_types_from_between_predicate() {
47074748
);
47084749
}
47094750

4751+
#[test]
4752+
fn test_prepare_statement_infer_types_from_between_predicate() {
4753+
let sql = "PREPARE my_plan AS SELECT id, age FROM person WHERE age BETWEEN $1 AND $2";
4754+
4755+
let plan = logical_plan(sql).unwrap();
4756+
assert_snapshot!(
4757+
plan,
4758+
@r#"
4759+
Prepare: "my_plan" []
4760+
Projection: person.id, person.age
4761+
Filter: person.age BETWEEN $1 AND $2
4762+
TableScan: person
4763+
"#
4764+
);
4765+
4766+
let actual_types = plan.get_parameter_types().unwrap();
4767+
let expected_types = HashMap::from([
4768+
("$1".to_string(), Some(DataType::Int32)),
4769+
("$2".to_string(), Some(DataType::Int32)),
4770+
]);
4771+
assert_eq!(actual_types, expected_types);
4772+
}
4773+
47104774
#[test]
47114775
fn test_infer_types_subquery() {
47124776
let sql = "SELECT id, age FROM person WHERE age = (select max(age) from person where id = $1)";
@@ -4749,6 +4813,31 @@ fn test_infer_types_subquery() {
47494813
);
47504814
}
47514815

4816+
#[test]
4817+
fn test_prepare_statement_infer_types_subquery() {
4818+
let sql = "PREPARE my_plan AS SELECT id, age FROM person WHERE age = (select max(age) from person where id = $1)";
4819+
4820+
let plan = logical_plan(sql).unwrap();
4821+
assert_snapshot!(
4822+
plan,
4823+
@r#"
4824+
Prepare: "my_plan" []
4825+
Projection: person.id, person.age
4826+
Filter: person.age = (<subquery>)
4827+
Subquery:
4828+
Projection: max(person.age)
4829+
Aggregate: groupBy=[[]], aggr=[[max(person.age)]]
4830+
Filter: person.id = $1
4831+
TableScan: person
4832+
TableScan: person
4833+
"#
4834+
);
4835+
4836+
let actual_types = plan.get_parameter_types().unwrap();
4837+
let expected_types = HashMap::from([("$1".to_string(), Some(DataType::UInt32))]);
4838+
assert_eq!(actual_types, expected_types);
4839+
}
4840+
47524841
#[test]
47534842
fn test_update_infer() {
47544843
let sql = "update person set age=$1 where id=$2";
@@ -4786,6 +4875,30 @@ fn test_update_infer() {
47864875
);
47874876
}
47884877

4878+
#[test]
4879+
fn test_prepare_statement_update_infer() {
4880+
let sql = "PREPARE my_plan AS update person set age=$1 where id=$2";
4881+
4882+
let plan = logical_plan(sql).unwrap();
4883+
assert_snapshot!(
4884+
plan,
4885+
@r#"
4886+
Prepare: "my_plan" []
4887+
Dml: op=[Update] table=[person]
4888+
Projection: person.id AS id, person.first_name AS first_name, person.last_name AS last_name, $1 AS age, person.state AS state, person.salary AS salary, person.birth_date AS birth_date, person.😀 AS 😀
4889+
Filter: person.id = $2
4890+
TableScan: person
4891+
"#
4892+
);
4893+
4894+
let actual_types = plan.get_parameter_types().unwrap();
4895+
let expected_types = HashMap::from([
4896+
("$1".to_string(), Some(DataType::Int32)),
4897+
("$2".to_string(), Some(DataType::UInt32)),
4898+
]);
4899+
assert_eq!(actual_types, expected_types);
4900+
}
4901+
47894902
#[test]
47904903
fn test_insert_infer() {
47914904
let sql = "insert into person (id, first_name, last_name) values ($1, $2, $3)";
@@ -4824,6 +4937,29 @@ fn test_insert_infer() {
48244937
);
48254938
}
48264939

4940+
#[test]
4941+
fn test_prepare_statement_insert_infer() {
4942+
let sql = "PREPARE my_plan AS insert into person (id, first_name, last_name) values ($1, $2, $3)";
4943+
let plan = logical_plan(sql).unwrap();
4944+
assert_snapshot!(
4945+
plan,
4946+
@r#"
4947+
Prepare: "my_plan" []
4948+
Dml: op=[Insert Into] table=[person]
4949+
Projection: column1 AS id, column2 AS first_name, column3 AS last_name, CAST(NULL AS Int32) AS age, CAST(NULL AS Utf8) AS state, CAST(NULL AS Float64) AS salary, CAST(NULL AS Timestamp(Nanosecond, None)) AS birth_date, CAST(NULL AS Int32) AS 😀
4950+
Values: ($1, $2, $3)
4951+
"#
4952+
);
4953+
4954+
let actual_types = plan.get_parameter_types().unwrap();
4955+
let expected_types = HashMap::from([
4956+
("$1".to_string(), Some(DataType::UInt32)),
4957+
("$2".to_string(), Some(DataType::Utf8)),
4958+
("$3".to_string(), Some(DataType::Utf8)),
4959+
]);
4960+
assert_eq!(actual_types, expected_types);
4961+
}
4962+
48274963
#[test]
48284964
fn test_prepare_statement_to_plan_one_param() {
48294965
let sql = "PREPARE my_plan(INT) AS SELECT id, age FROM person WHERE age = $1";

0 commit comments

Comments
 (0)