Apache DataFusion SQL Query Engine
Tweak list of optimization rules (#3841)
Daniël Heres431a4127f5e5
diff --git a/benchmarks/expected-plans/q19.txt b/benchmarks/expected-plans/q19.txtindex cbf4f08b34..ff729db965 100644--- a/benchmarks/expected-plans/q19.txt+++ b/benchmarks/expected-plans/q19.txt@@ -1,6 +1,6 @@Projection: SUM(lineitem.l_extendedprice * Int64(1) - lineitem.l_discount) AS revenueAggregate: groupBy=[[]], aggr=[[SUM(CAST(lineitem.l_extendedprice AS Decimal128(38, 4)) * CAST(Decimal128(Some(100),23,2) - CAST(lineitem.l_discount AS Decimal128(23, 2)) AS Decimal128(38, 4))) AS SUM(lineitem.l_extendedprice * Int64(1) - lineitem.l_discount)]]- Projection: lineitem.l_shipinstruct = Utf8("DELIVER IN PERSON") AS lineitem.l_shipinstruct = Utf8("DELIVER IN PERSON")Utf8("DELIVER IN PERSON")lineitem.l_shipinstruct, lineitem.l_shipmode IN ([Utf8("AIR"), Utf8("AIR REG")]) AS lineitem.l_shipmode IN ([Utf8("AIR"), Utf8("AIR REG")])Utf8("AIR REG")Utf8("AIR")lineitem.l_shipmode, part.p_size >= Int32(1) AS part.p_size >= Int32(1)Int32(1)part.p_size, lineitem.l_quantity, lineitem.l_extendedprice, lineitem.l_discount, part.p_brand, part.p_size, part.p_container+ Projection: lineitem.l_extendedprice, lineitem.l_discountFilter: part.p_brand = Utf8("Brand#12") AND part.p_container IN ([Utf8("SM CASE"), Utf8("SM BOX"), Utf8("SM PACK"), Utf8("SM PKG")]) AND lineitem.l_quantity >= Decimal128(Some(100),15,2) AND lineitem.l_quantity <= Decimal128(Some(1100),15,2) AND part.p_size <= Int32(5) OR part.p_brand = Utf8("Brand#23") AND part.p_container IN ([Utf8("MED BAG"), Utf8("MED BOX"), Utf8("MED PKG"), Utf8("MED PACK")]) AND lineitem.l_quantity >= Decimal128(Some(1000),15,2) AND lineitem.l_quantity <= Decimal128(Some(2000),15,2) AND part.p_size <= Int32(10) OR part.p_brand = Utf8("Brand#34") AND part.p_container IN ([Utf8("LG CASE"), Utf8("LG BOX"), Utf8("LG PACK"), Utf8("LG PKG")]) AND lineitem.l_quantity >= Decimal128(Some(2000),15,2) AND lineitem.l_quantity <= Decimal128(Some(3000),15,2) AND part.p_size <= Int32(15)Inner Join: lineitem.l_partkey = part.p_partkeyFilter: lineitem.l_shipmode IN ([Utf8("AIR"), Utf8("AIR REG")]) AND lineitem.l_shipinstruct = Utf8("DELIVER IN PERSON") AND lineitem.l_quantity >= Decimal128(Some(100),15,2) AND lineitem.l_quantity <= Decimal128(Some(1100),15,2) OR lineitem.l_quantity >= Decimal128(Some(1000),15,2) AND lineitem.l_quantity <= Decimal128(Some(2000),15,2) OR lineitem.l_quantity >= Decimal128(Some(2000),15,2) AND lineitem.l_quantity <= Decimal128(Some(3000),15,2)diff --git a/benchmarks/expected-plans/q21.txt b/benchmarks/expected-plans/q21.txtindex 5689fafc2e..f5aa1dc84c 100644--- a/benchmarks/expected-plans/q21.txt+++ b/benchmarks/expected-plans/q21.txt@@ -7,12 +7,12 @@ Sort: numwait DESC NULLS FIRST, supplier.s_name ASC NULLS LASTInner Join: l1.l_orderkey = orders.o_orderkeyInner Join: supplier.s_suppkey = l1.l_suppkeyTableScan: supplier projection=[s_suppkey, s_name, s_nationkey]- Filter: l1.l_receiptdate > l1.l_commitdate AND l1.l_receiptdate > l1.l_commitdate+ Filter: l1.l_receiptdate > l1.l_commitdateSubqueryAlias: l1TableScan: lineitem projection=[l_orderkey, l_suppkey, l_commitdate, l_receiptdate]- Filter: orders.o_orderstatus = Utf8("F") AND orders.o_orderstatus = Utf8("F")+ Filter: orders.o_orderstatus = Utf8("F")TableScan: orders projection=[o_orderkey, o_orderstatus]- Filter: nation.n_name = Utf8("SAUDI ARABIA") AND nation.n_name = Utf8("SAUDI ARABIA")+ Filter: nation.n_name = Utf8("SAUDI ARABIA")TableScan: nation projection=[n_nationkey, n_name]SubqueryAlias: l2TableScan: lineitem projection=[l_orderkey, l_suppkey]diff --git a/benchmarks/expected-plans/q22.txt b/benchmarks/expected-plans/q22.txtindex 4b0c1a59c0..919372f04a 100644--- a/benchmarks/expected-plans/q22.txt+++ b/benchmarks/expected-plans/q22.txt@@ -11,5 +11,5 @@ Sort: custsale.cntrycode ASC NULLS LASTTableScan: orders projection=[o_custkey]Projection: AVG(customer.c_acctbal) AS __value, alias=__sq_1Aggregate: groupBy=[[]], aggr=[[AVG(customer.c_acctbal)]]- Filter: CAST(customer.c_acctbal AS Decimal128(30, 15)) > Decimal128(Some(0),30,15) AND substr(customer.c_phone, Int64(1), Int64(2)) IN ([Utf8("13"), Utf8("31"), Utf8("23"), Utf8("29"), Utf8("30"), Utf8("18"), Utf8("17")])+ Filter: customer.c_acctbal > Decimal128(Some(0),15,2) AND substr(customer.c_phone, Int64(1), Int64(2)) IN ([Utf8("13"), Utf8("31"), Utf8("23"), Utf8("29"), Utf8("30"), Utf8("18"), Utf8("17")])TableScan: customer projection=[c_phone, c_acctbal]\ No newline at end of filediff --git a/benchmarks/expected-plans/q6.txt b/benchmarks/expected-plans/q6.txtindex 55a6174abc..efc17a2724 100644--- a/benchmarks/expected-plans/q6.txt+++ b/benchmarks/expected-plans/q6.txt@@ -1,5 +1,6 @@Projection: SUM(lineitem.l_extendedprice * lineitem.l_discount) AS revenueAggregate: groupBy=[[]], aggr=[[SUM(lineitem.l_extendedprice * lineitem.l_discount)]]- Projection: CAST(lineitem.l_discount AS Decimal128(30, 15)) AS CAST(lineitem.l_discount AS Decimal128(30, 15))lineitem.l_discount, lineitem.l_quantity, lineitem.l_extendedprice, lineitem.l_discount, lineitem.l_shipdate- Filter: lineitem.l_shipdate >= Date32("8766") AND lineitem.l_shipdate < Date32("9131") AND CAST(lineitem.l_discount AS Decimal128(30, 15)) >= Decimal128(Some(49999999999999),30,15) AND CAST(lineitem.l_discount AS Decimal128(30, 15)) <= Decimal128(Some(69999999999999),30,15) AND lineitem.l_quantity < Decimal128(Some(2400),15,2)- TableScan: lineitem projection=[l_quantity, l_extendedprice, l_discount, l_shipdate]\ No newline at end of file+ Projection: lineitem.l_extendedprice, lineitem.l_discount+ Filter: lineitem.l_shipdate >= Date32("8766") AND lineitem.l_shipdate < Date32("9131") AND CAST(lineitem.l_discount AS Decimal128(30, 15))lineitem.l_discount >= Decimal128(Some(49999999999999),30,15) AND CAST(lineitem.l_discount AS Decimal128(30, 15))lineitem.l_discount <= Decimal128(Some(69999999999999),30,15) AND lineitem.l_quantity < Decimal128(Some(2400),15,2)+ Projection: CAST(lineitem.l_discount AS Decimal128(30, 15)) AS CAST(lineitem.l_discount AS Decimal128(30, 15))lineitem.l_discount, lineitem.l_quantity, lineitem.l_extendedprice, lineitem.l_discount, lineitem.l_shipdate+ TableScan: lineitem projection=[l_quantity, l_extendedprice, l_discount, l_shipdate]\ No newline at end of filediff --git a/datafusion/core/tests/sql/subqueries.rs b/datafusion/core/tests/sql/subqueries.rsindex a5b246be4f..fd25e95bb4 100644--- a/datafusion/core/tests/sql/subqueries.rs+++ b/datafusion/core/tests/sql/subqueries.rs@@ -393,7 +393,7 @@ order by cntrycode;"#;TableScan: orders projection=[o_custkey]Projection: AVG(customer.c_acctbal) AS __value, alias=__sq_1Aggregate: groupBy=[[]], aggr=[[AVG(customer.c_acctbal)]]- Filter: CAST(customer.c_acctbal AS Decimal128(30, 15)) > Decimal128(Some(0),30,15) AND substr(customer.c_phone, Int64(1), Int64(2)) IN ([Utf8("13"), Utf8("31"), Utf8("23"), Utf8("29"), Utf8("30"), Utf8("18"), Utf8("17")])+ Filter: customer.c_acctbal > Decimal128(Some(0),15,2) AND substr(customer.c_phone, Int64(1), Int64(2)) IN ([Utf8("13"), Utf8("31"), Utf8("23"), Utf8("29"), Utf8("30"), Utf8("18"), Utf8("17")])TableScan: customer projection=[c_phone, c_acctbal], partial_filters=[CAST(customer.c_acctbal AS Decimal128(30, 15)) > Decimal128(Some(0),30,15), substr(customer.c_phone, Int64(1), Int64(2)) IN ([Utf8("13"), Utf8("31"), Utf8("23"), Utf8("29"), Utf8("30"), Utf8("18"), Utf8("17")])]"#.to_string();assert_eq!(actual, expected);diff --git a/datafusion/optimizer/src/optimizer.rs b/datafusion/optimizer/src/optimizer.rsindex 5e61ccc5a4..5052dc67e2 100644--- a/datafusion/optimizer/src/optimizer.rs+++ b/datafusion/optimizer/src/optimizer.rs@@ -153,7 +153,6 @@ impl Optimizer {Arc::new(ReduceCrossJoin::new()),Arc::new(CommonSubexprEliminate::new()),Arc::new(EliminateLimit::new()),- Arc::new(ProjectionPushDown::new()),Arc::new(RewriteDisjunctivePredicate::new()),];if config.filter_null_keys {@@ -163,6 +162,14 @@ impl Optimizer {rules.push(Arc::new(FilterPushDown::new()));rules.push(Arc::new(LimitPushDown::new()));rules.push(Arc::new(SingleDistinctToGroupBy::new()));++ // The previous optimizations added expressions and projections,+ // that might benefit from the following rules+ rules.push(Arc::new(SimplifyExpressions::new()));+ rules.push(Arc::new(UnwrapCastInComparison::new()));+ rules.push(Arc::new(CommonSubexprEliminate::new()));+ rules.push(Arc::new(ProjectionPushDown::new()));+Self::with_rules(rules)}