|
| 1 | +# Licensed to the Apache Software Foundation (ASF) under one |
| 2 | +# or more contributor license agreements. See the NOTICE file |
| 3 | +# distributed with this work for additional information |
| 4 | +# regarding copyright ownership. The ASF licenses this file |
| 5 | +# to you under the Apache License, Version 2.0 (the |
| 6 | +# "License"); you may not use this file except in compliance |
| 7 | +# with the License. You may obtain a copy of the License at |
| 8 | + |
| 9 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + |
| 11 | +# Unless required by applicable law or agreed to in writing, |
| 12 | +# software distributed under the License is distributed on an |
| 13 | +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 14 | +# KIND, either express or implied. See the License for the |
| 15 | +# specific language governing permissions and limitations |
| 16 | +# under the License. |
| 17 | + |
| 18 | +# Test cases for predicate simplification feature |
| 19 | +# Basic redundant comparison simplification |
| 20 | + |
| 21 | +statement ok |
| 22 | +set datafusion.explain.logical_plan_only=true; |
| 23 | + |
| 24 | +statement ok |
| 25 | +CREATE TABLE test_data ( |
| 26 | + int_col INT, |
| 27 | + float_col FLOAT, |
| 28 | + str_col VARCHAR, |
| 29 | + date_col DATE, |
| 30 | + bool_col BOOLEAN |
| 31 | +); |
| 32 | + |
| 33 | +# x > 5 AND x > 6 should simplify to x > 6 |
| 34 | +query TT |
| 35 | +EXPLAIN SELECT * FROM test_data WHERE int_col > 5 AND int_col > 6; |
| 36 | +---- |
| 37 | +logical_plan |
| 38 | +01)Filter: test_data.int_col > Int32(6) |
| 39 | +02)--TableScan: test_data projection=[int_col, float_col, str_col, date_col, bool_col] |
| 40 | + |
| 41 | +# x > 5 AND x >= 6 should simplify to x >= 6 |
| 42 | +query TT |
| 43 | +EXPLAIN SELECT * FROM test_data WHERE int_col > 5 AND int_col >= 6; |
| 44 | +---- |
| 45 | +logical_plan |
| 46 | +01)Filter: test_data.int_col >= Int32(6) |
| 47 | +02)--TableScan: test_data projection=[int_col, float_col, str_col, date_col, bool_col] |
| 48 | + |
| 49 | +# x < 10 AND x <= 8 should simplify to x <= 8 |
| 50 | +query TT |
| 51 | +EXPLAIN SELECT * FROM test_data WHERE int_col < 10 AND int_col <= 8; |
| 52 | +---- |
| 53 | +logical_plan |
| 54 | +01)Filter: test_data.int_col <= Int32(8) |
| 55 | +02)--TableScan: test_data projection=[int_col, float_col, str_col, date_col, bool_col] |
| 56 | + |
| 57 | +# x > 5 AND x > 6 AND x > 7 should simplify to x > 7 |
| 58 | +query TT |
| 59 | +EXPLAIN SELECT * FROM test_data WHERE int_col > 5 AND int_col > 6 AND int_col > 7; |
| 60 | +---- |
| 61 | +logical_plan |
| 62 | +01)Filter: test_data.int_col > Int32(7) |
| 63 | +02)--TableScan: test_data projection=[int_col, float_col, str_col, date_col, bool_col] |
| 64 | + |
| 65 | +# x > 5 AND y < 10 AND x > 6 AND y < 8 should simplify to x > 6 AND y < 8 |
| 66 | +query TT |
| 67 | +EXPLAIN SELECT * FROM test_data WHERE int_col > 5 AND float_col < 10 AND int_col > 6 AND float_col < 8; |
| 68 | +---- |
| 69 | +logical_plan |
| 70 | +01)Filter: test_data.float_col < Float32(8) AND test_data.int_col > Int32(6) |
| 71 | +02)--TableScan: test_data projection=[int_col, float_col, str_col, date_col, bool_col] |
| 72 | + |
| 73 | + |
| 74 | +# x = 7 AND x > 5 should simplify to x = 7 |
| 75 | +query TT |
| 76 | +EXPLAIN SELECT * FROM test_data WHERE int_col = 7 AND int_col > 5; |
| 77 | +---- |
| 78 | +logical_plan |
| 79 | +01)Filter: test_data.int_col = Int32(7) |
| 80 | +02)--TableScan: test_data projection=[int_col, float_col, str_col, date_col, bool_col] |
| 81 | + |
| 82 | +# str_col > 'apple' AND str_col > 'banana' should simplify to str_col > 'banana' |
| 83 | +query TT |
| 84 | +EXPLAIN SELECT * FROM test_data WHERE str_col > 'apple' AND str_col > 'banana'; |
| 85 | +---- |
| 86 | +logical_plan |
| 87 | +01)Filter: test_data.str_col > Utf8("banana") |
| 88 | +02)--TableScan: test_data projection=[int_col, float_col, str_col, date_col, bool_col] |
| 89 | + |
| 90 | +# date_col > '2023-01-01' AND date_col > '2023-02-01' should simplify to date_col > '2023-02-01' |
| 91 | +query TT |
| 92 | +EXPLAIN SELECT * FROM test_data WHERE date_col > '2023-01-01' AND date_col > '2023-02-01'; |
| 93 | +---- |
| 94 | +logical_plan |
| 95 | +01)Filter: test_data.date_col > Date32("2023-02-01") |
| 96 | +02)--TableScan: test_data projection=[int_col, float_col, str_col, date_col, bool_col] |
| 97 | + |
| 98 | +query TT |
| 99 | +EXPLAIN SELECT * FROM test_data WHERE bool_col = true AND bool_col = false; |
| 100 | +---- |
| 101 | +logical_plan |
| 102 | +01)Filter: test_data.bool_col AND NOT test_data.bool_col |
| 103 | +02)--TableScan: test_data projection=[int_col, float_col, str_col, date_col, bool_col] |
| 104 | + |
| 105 | + |
| 106 | +# This shouldn't be simplified since they're different relationships |
| 107 | +query TT |
| 108 | +EXPLAIN SELECT * FROM test_data WHERE int_col > float_col AND int_col > 5; |
| 109 | +---- |
| 110 | +logical_plan |
| 111 | +01)Filter: CAST(test_data.int_col AS Float32) > test_data.float_col AND test_data.int_col > Int32(5) |
| 112 | +02)--TableScan: test_data projection=[int_col, float_col, str_col, date_col, bool_col] |
| 113 | + |
| 114 | +# Should simplify the int_col predicates but preserve the others |
| 115 | +query TT |
| 116 | +EXPLAIN SELECT * FROM test_data |
| 117 | +WHERE int_col > 5 |
| 118 | + AND int_col > 10 |
| 119 | + AND str_col LIKE 'A%' |
| 120 | + AND float_col BETWEEN 1 AND 100; |
| 121 | +---- |
| 122 | +logical_plan |
| 123 | +01)Filter: test_data.str_col LIKE Utf8("A%") AND test_data.float_col >= Float32(1) AND test_data.float_col <= Float32(100) AND test_data.int_col > Int32(10) |
| 124 | +02)--TableScan: test_data projection=[int_col, float_col, str_col, date_col, bool_col] |
| 125 | + |
| 126 | +statement ok |
| 127 | +CREATE TABLE test_data2 ( |
| 128 | + id INT, |
| 129 | + value INT |
| 130 | +); |
| 131 | + |
| 132 | +query TT |
| 133 | +EXPLAIN SELECT t1.int_col, t2.value |
| 134 | +FROM test_data t1 |
| 135 | +JOIN test_data2 t2 ON t1.int_col = t2.id |
| 136 | +WHERE t1.int_col > 5 |
| 137 | + AND t1.int_col > 10 |
| 138 | + AND t2.value < 100 |
| 139 | + AND t2.value < 50; |
| 140 | +---- |
| 141 | +logical_plan |
| 142 | +01)Projection: t1.int_col, t2.value |
| 143 | +02)--Inner Join: t1.int_col = t2.id |
| 144 | +03)----SubqueryAlias: t1 |
| 145 | +04)------Filter: test_data.int_col > Int32(10) |
| 146 | +05)--------TableScan: test_data projection=[int_col] |
| 147 | +06)----SubqueryAlias: t2 |
| 148 | +07)------Filter: test_data2.value < Int32(50) AND test_data2.id > Int32(10) |
| 149 | +08)--------TableScan: test_data2 projection=[id, value] |
| 150 | + |
| 151 | +# Case 13: Handling negated predicates |
| 152 | +# NOT (x < 10) AND NOT (x < 5) should simplify to NOT (x < 10) |
| 153 | +query TT |
| 154 | +EXPLAIN SELECT * FROM test_data WHERE NOT (int_col < 10) AND NOT (int_col < 5); |
| 155 | +---- |
| 156 | +logical_plan |
| 157 | +01)Filter: test_data.int_col >= Int32(10) |
| 158 | +02)--TableScan: test_data projection=[int_col, float_col, str_col, date_col, bool_col] |
| 159 | + |
| 160 | +# x > 5 AND x < 10 should be preserved (can't be simplified) |
| 161 | +query TT |
| 162 | +EXPLAIN SELECT * FROM test_data WHERE int_col > 5 AND int_col < 10; |
| 163 | +---- |
| 164 | +logical_plan |
| 165 | +01)Filter: test_data.int_col > Int32(5) AND test_data.int_col < Int32(10) |
| 166 | +02)--TableScan: test_data projection=[int_col, float_col, str_col, date_col, bool_col] |
| 167 | + |
| 168 | +# 5 < x AND 3 < x should simplify to 5 < x |
| 169 | +query TT |
| 170 | +EXPLAIN SELECT * FROM test_data WHERE 5 < int_col AND 3 < int_col; |
| 171 | +---- |
| 172 | +logical_plan |
| 173 | +01)Filter: test_data.int_col > Int32(5) |
| 174 | +02)--TableScan: test_data projection=[int_col, float_col, str_col, date_col, bool_col] |
| 175 | + |
| 176 | +# CAST(x AS FLOAT) > 5.0 AND CAST(x AS FLOAT) > 6.0 should simplify |
| 177 | +query TT |
| 178 | +EXPLAIN SELECT * FROM test_data WHERE CAST(int_col AS FLOAT) > 5.0 AND CAST(int_col AS FLOAT) > 6.0; |
| 179 | +---- |
| 180 | +logical_plan |
| 181 | +01)Filter: CAST(CAST(test_data.int_col AS Float32) AS Float64) > Float64(6) |
| 182 | +02)--TableScan: test_data projection=[int_col, float_col, str_col, date_col, bool_col] |
| 183 | + |
| 184 | +# x = 5 AND x = 6 (logically impossible) |
| 185 | +query TT |
| 186 | +EXPLAIN SELECT * FROM test_data WHERE int_col = 5 AND int_col = 6; |
| 187 | +---- |
| 188 | +logical_plan EmptyRelation |
| 189 | + |
| 190 | +# (x > 5 OR y < 10) AND (x > 6 OR y < 8) |
| 191 | +# This is more complex but could still benefit from some simplification |
| 192 | +query TT |
| 193 | +EXPLAIN SELECT * FROM test_data |
| 194 | +WHERE (int_col > 5 OR float_col < 10) |
| 195 | + AND (int_col > 6 OR float_col < 8); |
| 196 | +---- |
| 197 | +logical_plan |
| 198 | +01)Filter: (test_data.int_col > Int32(5) OR test_data.float_col < Float32(10)) AND (test_data.int_col > Int32(6) OR test_data.float_col < Float32(8)) |
| 199 | +02)--TableScan: test_data projection=[int_col, float_col, str_col, date_col, bool_col] |
| 200 | + |
| 201 | +# Case 20: Combination of AND and OR with simplifiable predicates |
| 202 | +query TT |
| 203 | +EXPLAIN SELECT * FROM test_data |
| 204 | +WHERE (int_col > 5 AND int_col > 6) |
| 205 | + OR (float_col < 10 AND float_col < 8); |
| 206 | +---- |
| 207 | +logical_plan |
| 208 | +01)Filter: test_data.int_col > Int32(5) AND test_data.int_col > Int32(6) OR test_data.float_col < Float32(10) AND test_data.float_col < Float32(8) |
| 209 | +02)--TableScan: test_data projection=[int_col, float_col, str_col, date_col, bool_col] |
| 210 | + |
| 211 | +statement ok |
| 212 | +set datafusion.explain.logical_plan_only=false; |
0 commit comments