|
| 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 | +# Tests for ExpressionAnalyzerRegistry end-to-end integration. |
| 19 | +# |
| 20 | +# t_ndv is a StatisticsTable with controlled statistics: |
| 21 | +# 1000 rows, column a (Int64, NDV=10), column b (Int64, NDV=5). |
| 22 | +# |
| 23 | +# OR predicates are not expressible as a single interval, so the built-in |
| 24 | +# interval-arithmetic path always falls back to the default selectivity (20%). |
| 25 | +# ExpressionAnalyzerRegistry applies inclusion-exclusion instead: |
| 26 | +# P(a=42 OR b=5) = P(a=42) + P(b=5) - P(a=42)*P(b=5) |
| 27 | +# = 1/10 + 1/5 - 1/50 |
| 28 | +# = 0.1 + 0.2 - 0.02 = 0.28 |
| 29 | +# Expected rows = round(1000 * 0.28) = 280. |
| 30 | + |
| 31 | +statement ok |
| 32 | +SET datafusion.execution.target_partitions = 1; |
| 33 | + |
| 34 | +statement ok |
| 35 | +SET datafusion.explain.show_statistics = true; |
| 36 | + |
| 37 | +statement ok |
| 38 | +SET datafusion.explain.physical_plan_only = true; |
| 39 | + |
| 40 | +# Without ExpressionAnalyzerRegistry: OR predicate falls back to 20% default selectivity |
| 41 | +# → FilterExec estimated rows = 1000 * 0.20 = 200 |
| 42 | +query TT |
| 43 | +EXPLAIN SELECT * FROM t_ndv WHERE a = 42 OR b = 5; |
| 44 | +---- |
| 45 | +physical_plan |
| 46 | +01)FilterExec: a@0 = 42 OR b@1 = 5, statistics=[Rows=Inexact(200), Bytes=Absent, [(Col[0]: Distinct=Inexact(10)),(Col[1]: Distinct=Inexact(5))]] |
| 47 | +02)--CooperativeExec, statistics=[Rows=Inexact(1000), Bytes=Absent, [(Col[0]: Distinct=Inexact(10)),(Col[1]: Distinct=Inexact(5))]] |
| 48 | +03)----StatisticsTable, statistics=[Rows=Inexact(1000), Bytes=Absent, [(Col[0]: Distinct=Inexact(10)),(Col[1]: Distinct=Inexact(5))]] |
| 49 | + |
| 50 | +# Enable ExpressionAnalyzerRegistry so inclusion-exclusion applies to OR predicates |
| 51 | +statement ok |
| 52 | +SET datafusion.optimizer.use_expression_analyzer = true; |
| 53 | + |
| 54 | +# With ExpressionAnalyzerRegistry: OR uses inclusion-exclusion |
| 55 | +# P(a=42 OR b=5) = 1/10 + 1/5 - (1/10 * 1/5) = 0.28 → 280 rows |
| 56 | +query TT |
| 57 | +EXPLAIN SELECT * FROM t_ndv WHERE a = 42 OR b = 5; |
| 58 | +---- |
| 59 | +physical_plan |
| 60 | +01)FilterExec: a@0 = 42 OR b@1 = 5, statistics=[Rows=Inexact(280), Bytes=Absent, [(Col[0]: Distinct=Inexact(10)),(Col[1]: Distinct=Inexact(5))]] |
| 61 | +02)--CooperativeExec, statistics=[Rows=Inexact(1000), Bytes=Absent, [(Col[0]: Distinct=Inexact(10)),(Col[1]: Distinct=Inexact(5))]] |
| 62 | +03)----StatisticsTable, statistics=[Rows=Inexact(1000), Bytes=Absent, [(Col[0]: Distinct=Inexact(10)),(Col[1]: Distinct=Inexact(5))]] |
| 63 | + |
| 64 | +# Verify the registry survives physical optimizer rules: ORDER BY + LIMIT triggers the |
| 65 | +# TopK sort rule which rewrites the plan above FilterExec. The FilterExec row estimate |
| 66 | +# must still reflect inclusion-exclusion (280), not the 20% default. |
| 67 | +query TT |
| 68 | +EXPLAIN SELECT * FROM t_ndv WHERE a = 42 OR b = 5 ORDER BY a LIMIT 100; |
| 69 | +---- |
| 70 | +physical_plan |
| 71 | +01)SortExec: TopK(fetch=100), expr=[a@0 ASC NULLS LAST], preserve_partitioning=[false], statistics=[Rows=Inexact(100), Bytes=Absent, [(Col[0]: Distinct=Inexact(10)),(Col[1]: Distinct=Inexact(5))]] |
| 72 | +02)--FilterExec: a@0 = 42 OR b@1 = 5, statistics=[Rows=Inexact(280), Bytes=Absent, [(Col[0]: Distinct=Inexact(10)),(Col[1]: Distinct=Inexact(5))]] |
| 73 | +03)----CooperativeExec, statistics=[Rows=Inexact(1000), Bytes=Absent, [(Col[0]: Distinct=Inexact(10)),(Col[1]: Distinct=Inexact(5))]] |
| 74 | +04)------StatisticsTable, statistics=[Rows=Inexact(1000), Bytes=Absent, [(Col[0]: Distinct=Inexact(10)),(Col[1]: Distinct=Inexact(5))]] |
| 75 | + |
| 76 | +# Verify the registry reaches FilterExec nodes created by optimizer rules: the |
| 77 | +# filter_pushdown rule running on UnionExec creates fresh FilterExec nodes (one per |
| 78 | +# branch) that never existed when the registry was first injected. Both must show |
| 79 | +# 280 rows, confirming re-injection after each rule reaches newly created nodes. |
| 80 | +# The UnionExec row count (560 = 2 * 280) and doubled NDVs (20, 10) also confirm |
| 81 | +# that distinct-count propagation through UnionExec is correct. |
| 82 | +query TT |
| 83 | +EXPLAIN SELECT * FROM (SELECT * FROM t_ndv UNION ALL SELECT * FROM t_ndv) WHERE a = 42 OR b = 5; |
| 84 | +---- |
| 85 | +physical_plan |
| 86 | +01)UnionExec, statistics=[Rows=Inexact(560), Bytes=Absent, [(Col[0]: Distinct=Inexact(20)),(Col[1]: Distinct=Inexact(10))]] |
| 87 | +02)--FilterExec: a@0 = 42 OR b@1 = 5, statistics=[Rows=Inexact(280), Bytes=Absent, [(Col[0]: Distinct=Inexact(10)),(Col[1]: Distinct=Inexact(5))]] |
| 88 | +03)----CooperativeExec, statistics=[Rows=Inexact(1000), Bytes=Absent, [(Col[0]: Distinct=Inexact(10)),(Col[1]: Distinct=Inexact(5))]] |
| 89 | +04)------StatisticsTable, statistics=[Rows=Inexact(1000), Bytes=Absent, [(Col[0]: Distinct=Inexact(10)),(Col[1]: Distinct=Inexact(5))]] |
| 90 | +05)--FilterExec: a@0 = 42 OR b@1 = 5, statistics=[Rows=Inexact(280), Bytes=Absent, [(Col[0]: Distinct=Inexact(10)),(Col[1]: Distinct=Inexact(5))]] |
| 91 | +06)----CooperativeExec, statistics=[Rows=Inexact(1000), Bytes=Absent, [(Col[0]: Distinct=Inexact(10)),(Col[1]: Distinct=Inexact(5))]] |
| 92 | +07)------StatisticsTable, statistics=[Rows=Inexact(1000), Bytes=Absent, [(Col[0]: Distinct=Inexact(10)),(Col[1]: Distinct=Inexact(5))]] |
| 93 | + |
| 94 | +# Verify the registry reaches a FilterExec pushed through a join: filter_pushdown |
| 95 | +# moves the WHERE clause filter to the left side of the HashJoinExec, creating a |
| 96 | +# FilterExec that was not present in the plan at initial injection time. |
| 97 | +query TT |
| 98 | +EXPLAIN SELECT l.a, r.b FROM t_ndv l JOIN t_ndv r ON l.a = r.a WHERE l.a = 42 OR l.b = 5; |
| 99 | +---- |
| 100 | +physical_plan |
| 101 | +01)HashJoinExec: mode=CollectLeft, join_type=Inner, on=[(a@0, a@0)], projection=[a@0, b@2], statistics=[Rows=Inexact(28000), Bytes=Absent, [(Col[0]: Distinct=Inexact(10)),(Col[1]: Distinct=Inexact(5))]] |
| 102 | +02)--FilterExec: a@0 = 42 OR b@1 = 5, projection=[a@0], statistics=[Rows=Inexact(280), Bytes=Absent, [(Col[0]: Distinct=Inexact(10))]] |
| 103 | +03)----CooperativeExec, statistics=[Rows=Inexact(1000), Bytes=Absent, [(Col[0]: Distinct=Inexact(10)),(Col[1]: Distinct=Inexact(5))]] |
| 104 | +04)------StatisticsTable, statistics=[Rows=Inexact(1000), Bytes=Absent, [(Col[0]: Distinct=Inexact(10)),(Col[1]: Distinct=Inexact(5))]] |
| 105 | +05)--CooperativeExec, statistics=[Rows=Inexact(1000), Bytes=Absent, [(Col[0]: Distinct=Inexact(10)),(Col[1]: Distinct=Inexact(5))]] |
| 106 | +06)----StatisticsTable, statistics=[Rows=Inexact(1000), Bytes=Absent, [(Col[0]: Distinct=Inexact(10)),(Col[1]: Distinct=Inexact(5))]] |
| 107 | + |
| 108 | +statement ok |
| 109 | +SET datafusion.optimizer.use_expression_analyzer = false; |
| 110 | + |
| 111 | +statement ok |
| 112 | +SET datafusion.explain.show_statistics = false; |
| 113 | + |
| 114 | +statement ok |
| 115 | +SET datafusion.explain.physical_plan_only = false; |
| 116 | + |
| 117 | +statement ok |
| 118 | +SET datafusion.execution.target_partitions = 4; |
0 commit comments