This is an automated email from the ASF dual-hosted git repository.

englefly pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/doris.git


The following commit(s) were added to refs/heads/master by this push:
     new cfcb9d4a45b [fix](nereids) Use known keys in mixed-statistics join 
estimates (#70909)
cfcb9d4a45b is described below

commit cfcb9d4a45bb1681b70537010a3ab9795378975d
Author: minghong <[email protected]>
AuthorDate: Wed Dec 2 11:13:25 2026 +0800

    [fix](nereids) Use known keys in mixed-statistics join estimates (#70909)
    
    ### Background
    
    Nereids estimates the row count of an equijoin from statistics on its
    keys. For multiple keys it groups estimates by confidence and discounts
    additional predicates to account for possible correlation.
    
    ### What problem does this PR solve?
    
    Issue Number: None
    Related PR: #19046 (introduced the early fallback), #26086 (restored the
    max-row fallback)
    PR Type: Bugfix
    
    Problem Summary:
    
    A missing statistic for one equality sends the entire estimate directly
    to the max-input-row fallback. Two inputs estimated at 1,000 rows each
    with a known 10-value key and an unknown second key produce an estimate
    of 1,000 rows. Retaining the estimate from the known key gives 100,000
    rows in the same fixture.
    
    ### Details
    
    - **Change map:** 144 lines of production code, 90 lines of tests, 209
    lines of generated outputs. The estimator fix is covered by focused
    tests and regenerated snapshots.
    - **Frequency:** Always when mixed known and unknown equalities reach
    the inner estimate of an ordinary inner or outer join.
    - **Root cause:** JoinEstimation.estimateInnerJoin checked all
    equalities together before estimating any key. The early return also
    bypassed expansion and residual predicates when a known equality was
    available.
    - **Why existing tests missed it:** JoinEstimateTest covers join
    statistics with known keys. The invalid_stats_join_order regression
    exercises a different invalid-statistics policy. The new tests use known
    and unknown equijoin keys together and assert the mixed estimate
    directly.
    - **Fix:** Reuse the confidence and correlation estimation for usable
    equalities and give unknown equalities a neutral selectivity. Continue
    through expansion and residual estimation when a usable equality exists.
    Preserve the max-input-row fallback when all equalities are unknown. The
    generated TPC-DS snapshots change join order in Q44 and Q59, and
    build-side choice and runtime-filter placement in Q47 and Q57.
    - **Other paths and branches:** Logical and physical ordinary inner and
    outer joins share the changed estimator. Semi, anti and ASOF joins use
    specialized policies. The legacy fallback is present in branch-3.1,
    branch-4.0, branch-4.1 and branch-4.2.
    - **Behavior change:** Known equijoin keys continue to affect
    cardinality estimates when another key has missing statistics.
    
    ### Testing
    
    - The mixed_join_key_statistics regression fails at the join estimate on
    master and passes with this change. The new FE test also covers the
    all-unknown fallback, reversed key order, an outer join residual, and
    expansion. Local FE unit tests passed for JoinEstimateTest and
    StatsCalculatorTest.
    - Regenerated 1,204 shape snapshots and verified 1,205 shape_check
    suites with the regression runner. The generated diff updates 8
    snapshots.
    - Built the FE with `build.sh --fe`. Local SQL regression used the Doris
    4.1.5 BE with compatibility settings for data execution.
    
    ### Release note
    
    Nereids uses available key statistics for joins whose other keys have
    missing statistics.
    
    ### Check List (For Author)
    
    - Behavior changed: Yes (see Details)
    - Does this need documentation: No
    - Pre-submission status: Ready
    
    ### AI Disclosure
    
    - Tool: OpenAI Codex (GPT-6)
    - Generated: Code, tests, and PR description
    - Human review: Pending
---
 .../apache/doris/nereids/stats/JoinEstimation.java | 144 +++++++++++----------
 .../doris/nereids/stats/JoinEstimateTest.java      |  37 ++++++
 .../query_p0/stats/mixed_join_key_statistics.out   |   5 +
 .../tpcds_sf100/noStatsRfPrune/query47.out         |  12 +-
 .../tpcds_sf100/noStatsRfPrune/query57.out         |  12 +-
 .../tpcds_sf100/no_stats_shape/query47.out         |  12 +-
 .../tpcds_sf100/no_stats_shape/query57.out         |  12 +-
 .../shape_check/tpcds_sf10t_orc/shape/query44.out  | 102 +++++++--------
 .../shape_check/tpcds_sf10t_orc/shape/query47.out  |  12 +-
 .../shape_check/tpcds_sf10t_orc/shape/query57.out  |  12 +-
 .../shape_check/tpcds_sf10t_orc/shape/query59.out  |  30 ++---
 .../stats/mixed_join_key_statistics.groovy         |  53 ++++++++
 12 files changed, 272 insertions(+), 171 deletions(-)

diff --git 
a/fe/fe-core/src/main/java/org/apache/doris/nereids/stats/JoinEstimation.java 
b/fe/fe-core/src/main/java/org/apache/doris/nereids/stats/JoinEstimation.java
index 24008b3ddea..af1a5d9c620 100644
--- 
a/fe/fe-core/src/main/java/org/apache/doris/nereids/stats/JoinEstimation.java
+++ 
b/fe/fe-core/src/main/java/org/apache/doris/nereids/stats/JoinEstimation.java
@@ -36,6 +36,7 @@ import org.apache.doris.statistics.model.StatisticsBuilder;
 import com.google.common.collect.Lists;
 import com.google.common.collect.Maps;
 
+import java.util.Collections;
 import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
@@ -111,62 +112,65 @@ public class JoinEstimation {
 
     private static boolean joinConditionContainsUnknownColumnStats(Statistics 
leftStats,
             Statistics rightStats, Join join) {
-        for (Expression expr : join.getEqualPredicates()) {
-            for (Slot slot : expr.getInputSlots()) {
-                ColumnStatistic colStats = 
leftStats.findColumnStatistics(slot);
-                if (colStats == null) {
-                    colStats = rightStats.findColumnStatistics(slot);
-                }
-                if (colStats == null || colStats.isUnKnown) {
-                    return true;
-                }
+        return join.getEqualPredicates().stream()
+                .anyMatch(predicate -> equalityHasUnknownInputStats(leftStats, 
rightStats, predicate));
+    }
+
+    private static boolean equalityHasUnknownInputStats(Statistics leftStats, 
Statistics rightStats,
+            EqualPredicate predicate) {
+        for (Slot slot : predicate.getInputSlots()) {
+            ColumnStatistic columnStats = leftStats.findColumnStatistics(slot);
+            if (columnStats == null) {
+                columnStats = rightStats.findColumnStatistics(slot);
+            }
+            if (columnStats == null || columnStats.isUnKnown) {
+                return true;
             }
         }
         return false;
     }
 
-    private static Statistics estimateInnerJoinWithEqualPredicate(Statistics 
leftStats,
-            Statistics rightStats, Join join) {
-        /*
-         * When we estimate filter A=B,
-         * if any side of equation, A or B, is almost unique, the confidence 
level of estimation is high.
-         * But is both sides are not unique, the confidence level is very low.
-         * The equations, whose confidence level is low, are called 
unTrustEquation.
-         * In order to avoid error propagation, for unTrustEquations, we only 
use the biggest selectivity.
-         */
+    private static Optional<Statistics> 
estimateInnerJoinWithEqualPredicate(Statistics leftStats,
+            Statistics rightStats, List<EqualPredicate> equalPredicates, 
boolean allowPartialStats) {
         List<Double> unTrustEqualRatio = Lists.newArrayList();
-        List<EqualPredicate> unTrustableCondition = Lists.newArrayList();
+        List<EqualPredicate> trustableConditions = Lists.newArrayList();
         boolean leftBigger = leftStats.getRowCount() > 
rightStats.getRowCount();
         double rightStatsRowCount = 
StatsMathUtil.nonZeroDivisor(rightStats.getRowCount());
         double leftStatsRowCount = 
StatsMathUtil.nonZeroDivisor(leftStats.getRowCount());
-        List<EqualPredicate> trustableConditions = 
join.getEqualPredicates().stream()
-                .map(expression -> (EqualPredicate) expression)
-                .filter(
-                        expression -> {
-                            // since ndv is not accurate, if ndv/rowcount < 
TRUSTABLE_UNIQ_THRESHOLD,
-                            // this column is regarded as unique.
-                            EqualPredicate equal = 
normalizeEqualPredJoinCondition(expression, rightStats);
-                            ColumnStatistic eqLeftColStats = 
ExpressionEstimation.estimate(equal.left(), leftStats);
-                            ColumnStatistic eqRightColStats = 
ExpressionEstimation.estimate(equal.right(), rightStats);
-                            boolean trustable = eqRightColStats.ndv / 
rightStatsRowCount > TRUSTABLE_UNIQ_THRESHOLD
-                                    || eqLeftColStats.ndv / leftStatsRowCount 
> TRUSTABLE_UNIQ_THRESHOLD;
-                            if (!trustable) {
-                                double rNdv = 
StatsMathUtil.nonZeroDivisor(eqRightColStats.ndv);
-                                double lNdv = 
StatsMathUtil.nonZeroDivisor(eqLeftColStats.ndv);
-                                if (leftBigger) {
-                                    unTrustEqualRatio.add((rightStatsRowCount 
/ rNdv)
-                                            * Math.min(eqLeftColStats.ndv, 
eqRightColStats.ndv) / lNdv);
-                                } else {
-                                    unTrustEqualRatio.add((leftStatsRowCount / 
lNdv)
-                                            * Math.min(eqLeftColStats.ndv, 
eqRightColStats.ndv) / rNdv);
-                                }
-                                unTrustableCondition.add(equal);
-                            }
-                            return trustable;
-                        }
-                ).collect(Collectors.toList());
+        for (EqualPredicate predicate : equalPredicates) {
+            // With no evidence of additional filtering, treat the unknown 
equality as correlated.
+            if (equalityHasUnknownInputStats(leftStats, rightStats, 
predicate)) {
+                if (!allowPartialStats) {
+                    return Optional.empty();
+                }
+                continue;
+            }
+            EqualPredicate equal = normalizeEqualPredJoinCondition(predicate, 
rightStats);
+            ColumnStatistic eqLeftColStats = 
ExpressionEstimation.estimate(equal.left(), leftStats);
+            ColumnStatistic eqRightColStats = 
ExpressionEstimation.estimate(equal.right(), rightStats);
+            if (eqLeftColStats.isUnKnown || eqRightColStats.isUnKnown) {
+                if (!allowPartialStats) {
+                    return Optional.empty();
+                }
+                continue;
+            }
+            // A nearly unique key provides a high-confidence equality 
estimate.
+            if (eqRightColStats.ndv / rightStatsRowCount > 
TRUSTABLE_UNIQ_THRESHOLD
+                    || eqLeftColStats.ndv / leftStatsRowCount > 
TRUSTABLE_UNIQ_THRESHOLD) {
+                trustableConditions.add(equal);
+            } else {
+                double rightNdv = 
StatsMathUtil.nonZeroDivisor(eqRightColStats.ndv);
+                double leftNdv = 
StatsMathUtil.nonZeroDivisor(eqLeftColStats.ndv);
+                double commonNdv = Math.min(eqLeftColStats.ndv, 
eqRightColStats.ndv);
+                unTrustEqualRatio.add(leftBigger
+                        ? (rightStatsRowCount / rightNdv) * commonNdv / leftNdv
+                        : (leftStatsRowCount / leftNdv) * commonNdv / 
rightNdv);
+            }
+        }
+        if (trustableConditions.isEmpty() && unTrustEqualRatio.isEmpty()) {
+            return Optional.empty();
+        }
 
-        Statistics innerJoinStats;
         Statistics crossJoinStats = new StatisticsBuilder()
                 .setRowCount(Math.max(1, leftStats.getRowCount()) * 
Math.max(1, rightStats.getRowCount()))
                 .putColumnStatistics(leftStats.columnStatistics())
@@ -190,21 +194,16 @@ public class JoinEstimation {
             }
             outputRowCount = Math.max(1, crossJoinStats.getRowCount() * sel);
             outputRowCount = outputRowCount * 
Math.pow(UNTRUSTABLE_CONDITION_SELECTIVITY_LINEAR_FACTOR,
-                    unTrustableCondition.size());
+                    unTrustEqualRatio.size());
         } else {
             outputRowCount = Math.max(leftStats.getRowCount(), 
rightStats.getRowCount());
-            Optional<Double> ratio = 
unTrustEqualRatio.stream().min(Double::compareTo);
-            if (ratio.isPresent()) {
-                outputRowCount = Math.max(1, outputRowCount * ratio.get());
-                if (shouldDecayRemainingUntrustConditions()) {
-                    outputRowCount = Math.max(1, outputRowCount * Math.pow(
-                            UNTRUSTABLE_CONDITION_SELECTIVITY_LINEAR_FACTOR,
-                            Math.max(0, unTrustableCondition.size() - 1)));
-                }
+            outputRowCount = Math.max(1, outputRowCount * 
Collections.min(unTrustEqualRatio));
+            if (shouldDecayRemainingUntrustConditions()) {
+                outputRowCount = Math.max(1, outputRowCount * Math.pow(
+                        UNTRUSTABLE_CONDITION_SELECTIVITY_LINEAR_FACTOR, 
unTrustEqualRatio.size() - 1));
             }
         }
-        innerJoinStats = 
crossJoinStats.withRowCountAndEnforceValid(outputRowCount);
-        return innerJoinStats;
+        return 
Optional.of(crossJoinStats.withRowCountAndEnforceValid(outputRowCount));
     }
 
     private static Statistics 
estimateInnerJoinWithoutEqualPredicate(Statistics leftStats,
@@ -258,21 +257,20 @@ public class JoinEstimation {
 
     private static Statistics estimateInnerJoin(Statistics leftStats, 
Statistics rightStats, Join join,
             double expansion) {
-        if (joinConditionContainsUnknownColumnStats(leftStats, rightStats, 
join)) {
-            double rowCount = Math.max(leftStats.getRowCount(), 
rightStats.getRowCount());
-            rowCount = Math.max(1, rowCount);
-            return new StatisticsBuilder()
-                .setRowCount(rowCount)
-                .putColumnStatistics(leftStats.columnStatistics())
-                .putColumnStatistics(rightStats.columnStatistics())
-                .build();
-        }
-
+        List<EqualPredicate> equalPredicates = join.getEqualPredicates();
         Statistics innerJoinStats;
-        if (join.getEqualPredicates().isEmpty()) {
+        if (equalPredicates.isEmpty()) {
             innerJoinStats = estimateInnerJoinWithoutEqualPredicate(leftStats, 
rightStats, join);
         } else {
-            innerJoinStats = estimateInnerJoinWithEqualPredicate(leftStats, 
rightStats, join);
+            // Preserve the special join estimators' all-or-nothing fallback.
+            JoinType joinType = join.getJoinType();
+            boolean allowPartialStats = joinType.isInnerJoin() || 
joinType.isOuterJoin();
+            Optional<Statistics> estimate = 
estimateInnerJoinWithEqualPredicate(
+                    leftStats, rightStats, equalPredicates, allowPartialStats);
+            if (estimate.isEmpty()) {
+                return estimateJoinWithUnknownEqualities(leftStats, 
rightStats);
+            }
+            innerJoinStats = estimate.get();
         }
 
         if (expansion != 1.0) {
@@ -293,6 +291,14 @@ public class JoinEstimation {
         return innerJoinStats;
     }
 
+    private static Statistics estimateJoinWithUnknownEqualities(Statistics 
leftStats, Statistics rightStats) {
+        return new StatisticsBuilder()
+                .setRowCount(Math.max(1, Math.max(leftStats.getRowCount(), 
rightStats.getRowCount())))
+                .putColumnStatistics(leftStats.columnStatistics())
+                .putColumnStatistics(rightStats.columnStatistics())
+                .build();
+    }
+
     private static double estimateJoinConditionSel(Statistics crossJoinStats, 
Expression joinCond) {
         Statistics statistics = new FilterEstimation().estimate(joinCond, 
crossJoinStats);
         return statistics.getRowCount() / crossJoinStats.getRowCount();
diff --git 
a/fe/fe-core/src/test/java/org/apache/doris/nereids/stats/JoinEstimateTest.java 
b/fe/fe-core/src/test/java/org/apache/doris/nereids/stats/JoinEstimateTest.java
index 3948a344f48..edc34f8b578 100644
--- 
a/fe/fe-core/src/test/java/org/apache/doris/nereids/stats/JoinEstimateTest.java
+++ 
b/fe/fe-core/src/test/java/org/apache/doris/nereids/stats/JoinEstimateTest.java
@@ -22,11 +22,14 @@ import org.apache.doris.nereids.memo.Group;
 import org.apache.doris.nereids.memo.GroupId;
 import org.apache.doris.nereids.properties.DataTrait;
 import org.apache.doris.nereids.properties.LogicalProperties;
+import org.apache.doris.nereids.rules.exploration.join.JoinReorderContext;
 import org.apache.doris.nereids.trees.expressions.EqualTo;
 import org.apache.doris.nereids.trees.expressions.Slot;
 import org.apache.doris.nereids.trees.expressions.SlotReference;
+import org.apache.doris.nereids.trees.expressions.literal.IntegerLiteral;
 import org.apache.doris.nereids.trees.plans.GroupPlan;
 import org.apache.doris.nereids.trees.plans.JoinType;
+import org.apache.doris.nereids.trees.plans.Plan;
 import org.apache.doris.nereids.trees.plans.logical.LogicalJoin;
 import org.apache.doris.nereids.types.IntegerType;
 import org.apache.doris.statistics.model.ColumnStatistic;
@@ -91,6 +94,40 @@ public class JoinEstimateTest {
         Assertions.assertEquals(5, outBStats.ndv);
     }
 
+    @Test
+    public void testMixedJoinKeyStatistics() {
+        SlotReference leftKey = new SlotReference("leftKey", 
IntegerType.INSTANCE);
+        SlotReference rightKey = new SlotReference("rightKey", 
IntegerType.INSTANCE);
+        SlotReference leftApp = new SlotReference("leftApp", 
IntegerType.INSTANCE);
+        SlotReference rightApp = new SlotReference("rightApp", 
IntegerType.INSTANCE);
+        SlotReference flag = new SlotReference("flag", IntegerType.INSTANCE);
+        ColumnStatistic key = new ColumnStatisticBuilder(1_000)
+                .setNdv(10).setMinValue(0).setMaxValue(9).build();
+        Statistics left = new StatisticsBuilder().setRowCount(1_000)
+                .putColumnStatistics(leftKey, key)
+                .putColumnStatistics(leftApp, ColumnStatistic.UNKNOWN)
+                .putColumnStatistics(flag, new ColumnStatisticBuilder(1_000)
+                        .setNdv(2).setMinValue(0).setMaxValue(1).build())
+                .build();
+        Statistics right = new StatisticsBuilder().setRowCount(1_000)
+                .putColumnStatistics(rightKey, key).build();
+
+        EqualTo known = new EqualTo(rightKey, leftKey);
+        EqualTo unknown = new EqualTo(leftApp, rightApp);
+        LogicalJoin<Plan, Plan> mixed = new LogicalJoin<>(JoinType.INNER_JOIN, 
List.of(unknown, known),
+                new DummyPlan(), new DummyPlan(), JoinReorderContext.EMPTY);
+        LogicalJoin<Plan, Plan> allUnknown = new 
LogicalJoin<>(JoinType.INNER_JOIN, List.of(unknown),
+                new DummyPlan(), new DummyPlan(), JoinReorderContext.EMPTY);
+        LogicalJoin<Plan, Plan> residual = new 
LogicalJoin<>(JoinType.LEFT_OUTER_JOIN, List.of(known, unknown),
+                List.of(new EqualTo(flag, new IntegerLiteral(0))),
+                new DummyPlan(), new DummyPlan(), JoinReorderContext.EMPTY);
+
+        Assertions.assertEquals(100_000, JoinEstimation.estimate(left, right, 
mixed).getRowCount());
+        Assertions.assertEquals(1_000, JoinEstimation.estimate(left, right, 
allUnknown).getRowCount());
+        Assertions.assertEquals(50_000, JoinEstimation.estimate(left, right, 
residual).getRowCount());
+        Assertions.assertEquals(200_000, JoinEstimation.estimate(left, right, 
mixed, 2).getRowCount());
+    }
+
     @Test
     public void testOuterJoinStats() {
         SlotReference a = new SlotReference("a", IntegerType.INSTANCE);
diff --git a/regression-test/data/query_p0/stats/mixed_join_key_statistics.out 
b/regression-test/data/query_p0/stats/mixed_join_key_statistics.out
new file mode 100644
index 00000000000..aeedfd6287a
--- /dev/null
+++ b/regression-test/data/query_p0/stats/mixed_join_key_statistics.out
@@ -0,0 +1,5 @@
+-- This file is automatically generated. You should know what you did if you 
want to edit this
+-- !join_result --
+0      100
+1      100
+
diff --git 
a/regression-test/data/shape_check/tpcds_sf100/noStatsRfPrune/query47.out 
b/regression-test/data/shape_check/tpcds_sf100/noStatsRfPrune/query47.out
index 841d445a2c4..76d757af379 100644
--- a/regression-test/data/shape_check/tpcds_sf100/noStatsRfPrune/query47.out
+++ b/regression-test/data/shape_check/tpcds_sf100/noStatsRfPrune/query47.out
@@ -31,13 +31,13 @@ PhysicalCteAnchor ( cteId=CTEId#0 )
 --------PhysicalDistribute[DistributionSpecGather]
 ----------PhysicalTopN[LOCAL_SORT]
 ------------PhysicalProject
---------------hashJoin[INNER_JOIN shuffleBucket] hashCondition=((v1.i_brand = 
v1_lead.i_brand) and (v1.i_category = v1_lead.i_category) and (v1.rn = expr_(rn 
- 1)) and (v1.s_company_name = v1_lead.s_company_name) and (v1.s_store_name = 
v1_lead.s_store_name)) otherCondition=() build RFs:RF8 
i_category->i_category;RF9 i_brand->i_brand;RF10 
s_store_name->s_store_name;RF11 s_company_name->s_company_name;RF12 rn->(rn - 1)
+--------------hashJoin[INNER_JOIN bucketShuffle] hashCondition=((v1.i_brand = 
v1_lead.i_brand) and (v1.i_category = v1_lead.i_category) and (v1.rn = expr_(rn 
- 1)) and (v1.s_company_name = v1_lead.s_company_name) and (v1.s_store_name = 
v1_lead.s_store_name)) otherCondition=() build RFs:RF8 
i_category->i_category;RF9 i_category->i_category;RF10 i_brand->i_brand;RF11 
i_brand->i_brand;RF12 s_store_name->s_store_name;RF13 
s_store_name->s_store_name;RF14 s_company_name->s_company_name;RF15 s_ [...]
 ----------------PhysicalProject
-------------------PhysicalCteConsumer ( cteId=CTEId#0 ) apply RFs: RF8 RF9 
RF10 RF11 RF12
-----------------PhysicalProject
-------------------hashJoin[INNER_JOIN shuffle] hashCondition=((v1.i_brand = 
v1_lag.i_brand) and (v1.i_category = v1_lag.i_category) and (v1.rn = expr_(rn + 
1)) and (v1.s_company_name = v1_lag.s_company_name) and (v1.s_store_name = 
v1_lag.s_store_name)) otherCondition=() build RFs:RF3 
i_category->i_category;RF4 i_brand->i_brand;RF5 s_store_name->s_store_name;RF6 
s_company_name->s_company_name;RF7 rn->(rn + 1)
+------------------hashJoin[INNER_JOIN shuffle] hashCondition=((v1.i_brand = 
v1_lag.i_brand) and (v1.i_category = v1_lag.i_category) and (v1.rn = expr_(rn + 
1)) and (v1.s_company_name = v1_lag.s_company_name) and (v1.s_store_name = 
v1_lag.s_store_name)) otherCondition=() build RFs:RF3 
i_category->i_category;RF4 i_brand->i_brand;RF5 s_store_name->s_store_name;RF6 
s_company_name->s_company_name;RF7 rn->(rn + 1);RF18 
i_category->i_category;RF19 i_brand->i_brand;RF20 s_store_name->s_store_nam 
[...]
 --------------------PhysicalProject
-----------------------PhysicalCteConsumer ( cteId=CTEId#0 ) apply RFs: RF3 RF4 
RF5 RF6 RF7
+----------------------PhysicalCteConsumer ( cteId=CTEId#0 ) apply RFs: RF3 RF4 
RF5 RF6 RF7 RF9 RF11 RF13 RF15 RF17
 --------------------filter(((cast(abs((v2.sum_sales - 
cast(v2.avg_monthly_sales as DECIMALV3(38, 2)))) as DECIMALV3(38, 10)) / 
v2.avg_monthly_sales) > 0.100000) and (v2.avg_monthly_sales > 0.0000) and 
(v2.d_year = 2001))
-----------------------PhysicalCteConsumer ( cteId=CTEId#0 )
+----------------------PhysicalCteConsumer ( cteId=CTEId#0 ) apply RFs: RF8 
RF10 RF12 RF14 RF16
+----------------PhysicalProject
+------------------PhysicalCteConsumer ( cteId=CTEId#0 ) apply RFs: RF18 RF19 
RF20 RF21 RF22
 
diff --git 
a/regression-test/data/shape_check/tpcds_sf100/noStatsRfPrune/query57.out 
b/regression-test/data/shape_check/tpcds_sf100/noStatsRfPrune/query57.out
index 420bbdb18db..c5f5d890a41 100644
--- a/regression-test/data/shape_check/tpcds_sf100/noStatsRfPrune/query57.out
+++ b/regression-test/data/shape_check/tpcds_sf100/noStatsRfPrune/query57.out
@@ -31,13 +31,13 @@ PhysicalCteAnchor ( cteId=CTEId#0 )
 --------PhysicalDistribute[DistributionSpecGather]
 ----------PhysicalTopN[LOCAL_SORT]
 ------------PhysicalProject
---------------hashJoin[INNER_JOIN shuffleBucket] hashCondition=((v1.cc_name = 
v1_lead.cc_name) and (v1.i_brand = v1_lead.i_brand) and (v1.i_category = 
v1_lead.i_category) and (v1.rn = expr_(rn - 1))) otherCondition=() build 
RFs:RF7 i_category->i_category;RF8 i_brand->i_brand;RF9 cc_name->cc_name;RF10 
rn->(rn - 1)
+--------------hashJoin[INNER_JOIN bucketShuffle] hashCondition=((v1.cc_name = 
v1_lead.cc_name) and (v1.i_brand = v1_lead.i_brand) and (v1.i_category = 
v1_lead.i_category) and (v1.rn = expr_(rn - 1))) otherCondition=() build 
RFs:RF7 i_category->i_category;RF8 i_category->i_category;RF9 
i_brand->i_brand;RF10 i_brand->i_brand;RF11 cc_name->cc_name;RF12 
cc_name->cc_name;RF13 expr_(rn - 1)->rn;RF14 expr_(rn - 1)->(rn + 1)
 ----------------PhysicalProject
-------------------PhysicalCteConsumer ( cteId=CTEId#0 ) apply RFs: RF7 RF8 RF9 
RF10
-----------------PhysicalProject
-------------------hashJoin[INNER_JOIN shuffle] hashCondition=((v1.cc_name = 
v1_lag.cc_name) and (v1.i_brand = v1_lag.i_brand) and (v1.i_category = 
v1_lag.i_category) and (v1.rn = expr_(rn + 1))) otherCondition=() build RFs:RF3 
i_category->i_category;RF4 i_brand->i_brand;RF5 cc_name->cc_name;RF6 rn->(rn + 
1)
+------------------hashJoin[INNER_JOIN shuffle] hashCondition=((v1.cc_name = 
v1_lag.cc_name) and (v1.i_brand = v1_lag.i_brand) and (v1.i_category = 
v1_lag.i_category) and (v1.rn = expr_(rn + 1))) otherCondition=() build RFs:RF3 
i_category->i_category;RF4 i_brand->i_brand;RF5 cc_name->cc_name;RF6 rn->(rn + 
1);RF15 i_category->i_category;RF16 i_brand->i_brand;RF17 cc_name->cc_name;RF18 
rn->(rn - 1)
 --------------------PhysicalProject
-----------------------PhysicalCteConsumer ( cteId=CTEId#0 ) apply RFs: RF3 RF4 
RF5 RF6
+----------------------PhysicalCteConsumer ( cteId=CTEId#0 ) apply RFs: RF3 RF4 
RF5 RF6 RF8 RF10 RF12 RF14
 --------------------filter(((cast(abs((v2.sum_sales - 
cast(v2.avg_monthly_sales as DECIMALV3(38, 2)))) as DECIMALV3(38, 10)) / 
v2.avg_monthly_sales) > 0.100000) and (v2.avg_monthly_sales > 0.0000) and 
(v2.d_year = 1999))
-----------------------PhysicalCteConsumer ( cteId=CTEId#0 )
+----------------------PhysicalCteConsumer ( cteId=CTEId#0 ) apply RFs: RF7 RF9 
RF11 RF13
+----------------PhysicalProject
+------------------PhysicalCteConsumer ( cteId=CTEId#0 ) apply RFs: RF15 RF16 
RF17 RF18
 
diff --git 
a/regression-test/data/shape_check/tpcds_sf100/no_stats_shape/query47.out 
b/regression-test/data/shape_check/tpcds_sf100/no_stats_shape/query47.out
index 67652a224aa..7709dce562b 100644
--- a/regression-test/data/shape_check/tpcds_sf100/no_stats_shape/query47.out
+++ b/regression-test/data/shape_check/tpcds_sf100/no_stats_shape/query47.out
@@ -31,13 +31,13 @@ PhysicalCteAnchor ( cteId=CTEId#0 )
 --------PhysicalDistribute[DistributionSpecGather]
 ----------PhysicalTopN[LOCAL_SORT]
 ------------PhysicalProject
---------------hashJoin[INNER_JOIN shuffleBucket] hashCondition=((v1.i_brand = 
v1_lead.i_brand) and (v1.i_category = v1_lead.i_category) and (v1.rn = expr_(rn 
- 1)) and (v1.s_company_name = v1_lead.s_company_name) and (v1.s_store_name = 
v1_lead.s_store_name)) otherCondition=() build RFs:RF8 
i_category->i_category;RF9 i_brand->i_brand;RF10 
s_store_name->s_store_name;RF11 s_company_name->s_company_name;RF12 rn->(rn - 1)
+--------------hashJoin[INNER_JOIN bucketShuffle] hashCondition=((v1.i_brand = 
v1_lead.i_brand) and (v1.i_category = v1_lead.i_category) and (v1.rn = expr_(rn 
- 1)) and (v1.s_company_name = v1_lead.s_company_name) and (v1.s_store_name = 
v1_lead.s_store_name)) otherCondition=() build RFs:RF8 
i_category->i_category;RF9 i_category->i_category;RF10 i_brand->i_brand;RF11 
i_brand->i_brand;RF12 s_store_name->s_store_name;RF13 
s_store_name->s_store_name;RF14 s_company_name->s_company_name;RF15 s_ [...]
 ----------------PhysicalProject
-------------------PhysicalCteConsumer ( cteId=CTEId#0 ) apply RFs: RF8 RF9 
RF10 RF11 RF12
-----------------PhysicalProject
-------------------hashJoin[INNER_JOIN shuffle] hashCondition=((v1.i_brand = 
v1_lag.i_brand) and (v1.i_category = v1_lag.i_category) and (v1.rn = expr_(rn + 
1)) and (v1.s_company_name = v1_lag.s_company_name) and (v1.s_store_name = 
v1_lag.s_store_name)) otherCondition=() build RFs:RF3 
i_category->i_category;RF4 i_brand->i_brand;RF5 s_store_name->s_store_name;RF6 
s_company_name->s_company_name;RF7 rn->(rn + 1)
+------------------hashJoin[INNER_JOIN shuffle] hashCondition=((v1.i_brand = 
v1_lag.i_brand) and (v1.i_category = v1_lag.i_category) and (v1.rn = expr_(rn + 
1)) and (v1.s_company_name = v1_lag.s_company_name) and (v1.s_store_name = 
v1_lag.s_store_name)) otherCondition=() build RFs:RF3 
i_category->i_category;RF4 i_brand->i_brand;RF5 s_store_name->s_store_name;RF6 
s_company_name->s_company_name;RF7 rn->(rn + 1);RF18 
i_category->i_category;RF19 i_brand->i_brand;RF20 s_store_name->s_store_nam 
[...]
 --------------------PhysicalProject
-----------------------PhysicalCteConsumer ( cteId=CTEId#0 ) apply RFs: RF3 RF4 
RF5 RF6 RF7
+----------------------PhysicalCteConsumer ( cteId=CTEId#0 ) apply RFs: RF3 RF4 
RF5 RF6 RF7 RF9 RF11 RF13 RF15 RF17
 --------------------filter(((cast(abs((v2.sum_sales - 
cast(v2.avg_monthly_sales as DECIMALV3(38, 2)))) as DECIMALV3(38, 10)) / 
v2.avg_monthly_sales) > 0.100000) and (v2.avg_monthly_sales > 0.0000) and 
(v2.d_year = 2001))
-----------------------PhysicalCteConsumer ( cteId=CTEId#0 )
+----------------------PhysicalCteConsumer ( cteId=CTEId#0 ) apply RFs: RF8 
RF10 RF12 RF14 RF16
+----------------PhysicalProject
+------------------PhysicalCteConsumer ( cteId=CTEId#0 ) apply RFs: RF18 RF19 
RF20 RF21 RF22
 
diff --git 
a/regression-test/data/shape_check/tpcds_sf100/no_stats_shape/query57.out 
b/regression-test/data/shape_check/tpcds_sf100/no_stats_shape/query57.out
index cb8a114ce6a..69d60cffc49 100644
--- a/regression-test/data/shape_check/tpcds_sf100/no_stats_shape/query57.out
+++ b/regression-test/data/shape_check/tpcds_sf100/no_stats_shape/query57.out
@@ -31,13 +31,13 @@ PhysicalCteAnchor ( cteId=CTEId#0 )
 --------PhysicalDistribute[DistributionSpecGather]
 ----------PhysicalTopN[LOCAL_SORT]
 ------------PhysicalProject
---------------hashJoin[INNER_JOIN shuffleBucket] hashCondition=((v1.cc_name = 
v1_lead.cc_name) and (v1.i_brand = v1_lead.i_brand) and (v1.i_category = 
v1_lead.i_category) and (v1.rn = expr_(rn - 1))) otherCondition=() build 
RFs:RF7 i_category->i_category;RF8 i_brand->i_brand;RF9 cc_name->cc_name;RF10 
rn->(rn - 1)
+--------------hashJoin[INNER_JOIN bucketShuffle] hashCondition=((v1.cc_name = 
v1_lead.cc_name) and (v1.i_brand = v1_lead.i_brand) and (v1.i_category = 
v1_lead.i_category) and (v1.rn = expr_(rn - 1))) otherCondition=() build 
RFs:RF7 i_category->i_category;RF8 i_category->i_category;RF9 
i_brand->i_brand;RF10 i_brand->i_brand;RF11 cc_name->cc_name;RF12 
cc_name->cc_name;RF13 expr_(rn - 1)->rn;RF14 expr_(rn - 1)->(rn + 1)
 ----------------PhysicalProject
-------------------PhysicalCteConsumer ( cteId=CTEId#0 ) apply RFs: RF7 RF8 RF9 
RF10
-----------------PhysicalProject
-------------------hashJoin[INNER_JOIN shuffle] hashCondition=((v1.cc_name = 
v1_lag.cc_name) and (v1.i_brand = v1_lag.i_brand) and (v1.i_category = 
v1_lag.i_category) and (v1.rn = expr_(rn + 1))) otherCondition=() build RFs:RF3 
i_category->i_category;RF4 i_brand->i_brand;RF5 cc_name->cc_name;RF6 rn->(rn + 
1)
+------------------hashJoin[INNER_JOIN shuffle] hashCondition=((v1.cc_name = 
v1_lag.cc_name) and (v1.i_brand = v1_lag.i_brand) and (v1.i_category = 
v1_lag.i_category) and (v1.rn = expr_(rn + 1))) otherCondition=() build RFs:RF3 
i_category->i_category;RF4 i_brand->i_brand;RF5 cc_name->cc_name;RF6 rn->(rn + 
1);RF15 i_category->i_category;RF16 i_brand->i_brand;RF17 cc_name->cc_name;RF18 
rn->(rn - 1)
 --------------------PhysicalProject
-----------------------PhysicalCteConsumer ( cteId=CTEId#0 ) apply RFs: RF3 RF4 
RF5 RF6
+----------------------PhysicalCteConsumer ( cteId=CTEId#0 ) apply RFs: RF3 RF4 
RF5 RF6 RF8 RF10 RF12 RF14
 --------------------filter(((cast(abs((v2.sum_sales - 
cast(v2.avg_monthly_sales as DECIMALV3(38, 2)))) as DECIMALV3(38, 10)) / 
v2.avg_monthly_sales) > 0.100000) and (v2.avg_monthly_sales > 0.0000) and 
(v2.d_year = 1999))
-----------------------PhysicalCteConsumer ( cteId=CTEId#0 )
+----------------------PhysicalCteConsumer ( cteId=CTEId#0 ) apply RFs: RF7 RF9 
RF11 RF13
+----------------PhysicalProject
+------------------PhysicalCteConsumer ( cteId=CTEId#0 ) apply RFs: RF15 RF16 
RF17 RF18
 
diff --git a/regression-test/data/shape_check/tpcds_sf10t_orc/shape/query44.out 
b/regression-test/data/shape_check/tpcds_sf10t_orc/shape/query44.out
index 88d06115b6f..3e63336e9cb 100644
--- a/regression-test/data/shape_check/tpcds_sf10t_orc/shape/query44.out
+++ b/regression-test/data/shape_check/tpcds_sf10t_orc/shape/query44.out
@@ -7,63 +7,63 @@ PhysicalResultSink
 --------PhysicalDistribute[DistributionSpecGather]
 ----------PhysicalTopN[LOCAL_SORT]
 ------------PhysicalProject
---------------hashJoin[INNER_JOIN broadcast] hashCondition=((i2.i_item_sk = 
descending.item_sk)) otherCondition=()
+--------------hashJoin[INNER_JOIN shuffle] hashCondition=((asceding.rnk = 
descending.rnk)) otherCondition=()
 ----------------PhysicalProject
 ------------------hashJoin[INNER_JOIN broadcast] hashCondition=((i1.i_item_sk 
= asceding.item_sk)) otherCondition=()
 --------------------PhysicalProject
-----------------------hashJoin[INNER_JOIN shuffle] 
hashCondition=((asceding.rnk = descending.rnk)) otherCondition=()
-------------------------PhysicalProject
---------------------------filter((V11.rnk < 11))
-----------------------------PhysicalWindow
-------------------------------PhysicalQuickSort[MERGE_SORT]
---------------------------------PhysicalDistribute[DistributionSpecGather]
-----------------------------------PhysicalQuickSort[LOCAL_SORT]
+----------------------filter((V11.rnk < 11))
+------------------------PhysicalWindow
+--------------------------PhysicalQuickSort[MERGE_SORT]
+----------------------------PhysicalDistribute[DistributionSpecGather]
+------------------------------PhysicalQuickSort[LOCAL_SORT]
+--------------------------------PhysicalProject
+----------------------------------NestedLoopJoin[INNER_JOIN](cast(rank_col as 
DECIMALV3(38, 5)) > (0.9 * rank_col))
 ------------------------------------PhysicalProject
---------------------------------------NestedLoopJoin[INNER_JOIN](cast(rank_col 
as DECIMALV3(38, 5)) > (0.9 * rank_col))
-----------------------------------------PhysicalProject
-------------------------------------------hashAgg[GLOBAL]
---------------------------------------------PhysicalDistribute[DistributionSpecHash]
-----------------------------------------------hashAgg[LOCAL]
-------------------------------------------------PhysicalProject
---------------------------------------------------filter((ss1.ss_store_sk = 
366))
-----------------------------------------------------PhysicalOlapScan[store_sales(ss1)]
-----------------------------------------PhysicalProject
-------------------------------------------PhysicalAssertNumRows
---------------------------------------------PhysicalDistribute[DistributionSpecGather]
-----------------------------------------------PhysicalProject
-------------------------------------------------hashAgg[GLOBAL]
---------------------------------------------------PhysicalDistribute[DistributionSpecHash]
-----------------------------------------------------hashAgg[LOCAL]
-------------------------------------------------------PhysicalProject
---------------------------------------------------------filter((store_sales.ss_store_sk
 = 366) and store_sales.ss_cdemo_sk IS NULL)
-----------------------------------------------------------PhysicalOlapScan[store_sales]
-------------------------PhysicalProject
---------------------------filter((V21.rnk < 11))
-----------------------------PhysicalWindow
-------------------------------PhysicalQuickSort[MERGE_SORT]
---------------------------------PhysicalDistribute[DistributionSpecGather]
-----------------------------------PhysicalQuickSort[LOCAL_SORT]
+--------------------------------------hashAgg[GLOBAL]
+----------------------------------------PhysicalDistribute[DistributionSpecHash]
+------------------------------------------hashAgg[LOCAL]
+--------------------------------------------PhysicalProject
+----------------------------------------------filter((ss1.ss_store_sk = 366))
+------------------------------------------------PhysicalOlapScan[store_sales(ss1)]
 ------------------------------------PhysicalProject
---------------------------------------NestedLoopJoin[INNER_JOIN](cast(rank_col 
as DECIMALV3(38, 5)) > (0.9 * rank_col))
-----------------------------------------PhysicalProject
-------------------------------------------hashAgg[GLOBAL]
---------------------------------------------PhysicalDistribute[DistributionSpecHash]
-----------------------------------------------hashAgg[LOCAL]
-------------------------------------------------PhysicalProject
---------------------------------------------------filter((ss1.ss_store_sk = 
366))
-----------------------------------------------------PhysicalOlapScan[store_sales(ss1)]
-----------------------------------------PhysicalProject
-------------------------------------------PhysicalAssertNumRows
---------------------------------------------PhysicalDistribute[DistributionSpecGather]
-----------------------------------------------PhysicalProject
-------------------------------------------------hashAgg[GLOBAL]
---------------------------------------------------PhysicalDistribute[DistributionSpecHash]
-----------------------------------------------------hashAgg[LOCAL]
-------------------------------------------------------PhysicalProject
---------------------------------------------------------filter((store_sales.ss_store_sk
 = 366) and store_sales.ss_cdemo_sk IS NULL)
-----------------------------------------------------------PhysicalOlapScan[store_sales]
+--------------------------------------PhysicalAssertNumRows
+----------------------------------------PhysicalDistribute[DistributionSpecGather]
+------------------------------------------PhysicalProject
+--------------------------------------------hashAgg[GLOBAL]
+----------------------------------------------PhysicalDistribute[DistributionSpecHash]
+------------------------------------------------hashAgg[LOCAL]
+--------------------------------------------------PhysicalProject
+----------------------------------------------------filter((store_sales.ss_store_sk
 = 366) and store_sales.ss_cdemo_sk IS NULL)
+------------------------------------------------------PhysicalOlapScan[store_sales]
 --------------------PhysicalProject
 ----------------------PhysicalLazyMaterializeOlapScan[item 
lazySlots:(i1.i_product_name)]
 ----------------PhysicalProject
-------------------PhysicalLazyMaterializeOlapScan[item 
lazySlots:(i2.i_product_name)]
+------------------hashJoin[INNER_JOIN broadcast] hashCondition=((i2.i_item_sk 
= descending.item_sk)) otherCondition=()
+--------------------PhysicalProject
+----------------------filter((V21.rnk < 11))
+------------------------PhysicalWindow
+--------------------------PhysicalQuickSort[MERGE_SORT]
+----------------------------PhysicalDistribute[DistributionSpecGather]
+------------------------------PhysicalQuickSort[LOCAL_SORT]
+--------------------------------PhysicalProject
+----------------------------------NestedLoopJoin[INNER_JOIN](cast(rank_col as 
DECIMALV3(38, 5)) > (0.9 * rank_col))
+------------------------------------PhysicalProject
+--------------------------------------hashAgg[GLOBAL]
+----------------------------------------PhysicalDistribute[DistributionSpecHash]
+------------------------------------------hashAgg[LOCAL]
+--------------------------------------------PhysicalProject
+----------------------------------------------filter((ss1.ss_store_sk = 366))
+------------------------------------------------PhysicalOlapScan[store_sales(ss1)]
+------------------------------------PhysicalProject
+--------------------------------------PhysicalAssertNumRows
+----------------------------------------PhysicalDistribute[DistributionSpecGather]
+------------------------------------------PhysicalProject
+--------------------------------------------hashAgg[GLOBAL]
+----------------------------------------------PhysicalDistribute[DistributionSpecHash]
+------------------------------------------------hashAgg[LOCAL]
+--------------------------------------------------PhysicalProject
+----------------------------------------------------filter((store_sales.ss_store_sk
 = 366) and store_sales.ss_cdemo_sk IS NULL)
+------------------------------------------------------PhysicalOlapScan[store_sales]
+--------------------PhysicalProject
+----------------------PhysicalLazyMaterializeOlapScan[item 
lazySlots:(i2.i_product_name)]
 
diff --git a/regression-test/data/shape_check/tpcds_sf10t_orc/shape/query47.out 
b/regression-test/data/shape_check/tpcds_sf10t_orc/shape/query47.out
index 16f72062f96..96290937bff 100644
--- a/regression-test/data/shape_check/tpcds_sf10t_orc/shape/query47.out
+++ b/regression-test/data/shape_check/tpcds_sf10t_orc/shape/query47.out
@@ -30,13 +30,13 @@ PhysicalCteAnchor ( cteId=CTEId#0 )
 --------PhysicalDistribute[DistributionSpecGather]
 ----------PhysicalTopN[LOCAL_SORT]
 ------------PhysicalProject
---------------hashJoin[INNER_JOIN shuffleBucket] hashCondition=((v1.i_brand = 
v1_lead.i_brand) and (v1.i_category = v1_lead.i_category) and (v1.rn = expr_(rn 
- 1)) and (v1.s_company_name = v1_lead.s_company_name) and (v1.s_store_name = 
v1_lead.s_store_name)) otherCondition=() build RFs:RF8 
i_category->i_category;RF9 i_brand->i_brand;RF10 
s_store_name->s_store_name;RF11 s_company_name->s_company_name;RF12 rn->(rn - 1)
+--------------hashJoin[INNER_JOIN bucketShuffle] hashCondition=((v1.i_brand = 
v1_lead.i_brand) and (v1.i_category = v1_lead.i_category) and (v1.rn = expr_(rn 
- 1)) and (v1.s_company_name = v1_lead.s_company_name) and (v1.s_store_name = 
v1_lead.s_store_name)) otherCondition=() build RFs:RF8 
i_category->i_category;RF9 i_category->i_category;RF10 i_brand->i_brand;RF11 
i_brand->i_brand;RF12 s_store_name->s_store_name;RF13 
s_store_name->s_store_name;RF14 s_company_name->s_company_name;RF15 s_ [...]
 ----------------PhysicalProject
-------------------PhysicalCteConsumer ( cteId=CTEId#0 ) apply RFs: RF8 RF9 
RF10 RF11 RF12
-----------------PhysicalProject
-------------------hashJoin[INNER_JOIN shuffle] hashCondition=((v1.i_brand = 
v1_lag.i_brand) and (v1.i_category = v1_lag.i_category) and (v1.rn = expr_(rn + 
1)) and (v1.s_company_name = v1_lag.s_company_name) and (v1.s_store_name = 
v1_lag.s_store_name)) otherCondition=() build RFs:RF3 
i_category->i_category;RF4 i_brand->i_brand;RF5 s_store_name->s_store_name;RF6 
s_company_name->s_company_name;RF7 rn->(rn + 1)
+------------------hashJoin[INNER_JOIN shuffle] hashCondition=((v1.i_brand = 
v1_lag.i_brand) and (v1.i_category = v1_lag.i_category) and (v1.rn = expr_(rn + 
1)) and (v1.s_company_name = v1_lag.s_company_name) and (v1.s_store_name = 
v1_lag.s_store_name)) otherCondition=() build RFs:RF3 
i_category->i_category;RF4 i_brand->i_brand;RF5 s_store_name->s_store_name;RF6 
s_company_name->s_company_name;RF7 rn->(rn + 1);RF18 
i_category->i_category;RF19 i_brand->i_brand;RF20 s_store_name->s_store_nam 
[...]
 --------------------PhysicalProject
-----------------------PhysicalCteConsumer ( cteId=CTEId#0 ) apply RFs: RF3 RF4 
RF5 RF6 RF7
+----------------------PhysicalCteConsumer ( cteId=CTEId#0 ) apply RFs: RF3 RF4 
RF5 RF6 RF7 RF9 RF11 RF13 RF15 RF17
 --------------------filter(((cast(abs((v2.sum_sales - 
cast(v2.avg_monthly_sales as DECIMALV3(38, 2)))) as DECIMALV3(38, 10)) / 
v2.avg_monthly_sales) > 0.100000) and (v2.avg_monthly_sales > 0.0000) and 
(v2.d_year = 1999))
-----------------------PhysicalCteConsumer ( cteId=CTEId#0 )
+----------------------PhysicalCteConsumer ( cteId=CTEId#0 ) apply RFs: RF8 
RF10 RF12 RF14 RF16
+----------------PhysicalProject
+------------------PhysicalCteConsumer ( cteId=CTEId#0 ) apply RFs: RF18 RF19 
RF20 RF21 RF22
 
diff --git a/regression-test/data/shape_check/tpcds_sf10t_orc/shape/query57.out 
b/regression-test/data/shape_check/tpcds_sf10t_orc/shape/query57.out
index 0f7d30562cd..5f4b1713886 100644
--- a/regression-test/data/shape_check/tpcds_sf10t_orc/shape/query57.out
+++ b/regression-test/data/shape_check/tpcds_sf10t_orc/shape/query57.out
@@ -30,13 +30,13 @@ PhysicalCteAnchor ( cteId=CTEId#0 )
 --------PhysicalDistribute[DistributionSpecGather]
 ----------PhysicalTopN[LOCAL_SORT]
 ------------PhysicalProject
---------------hashJoin[INNER_JOIN shuffleBucket] hashCondition=((v1.cc_name = 
v1_lead.cc_name) and (v1.i_brand = v1_lead.i_brand) and (v1.i_category = 
v1_lead.i_category) and (v1.rn = expr_(rn - 1))) otherCondition=() build 
RFs:RF7 i_category->i_category;RF8 i_brand->i_brand;RF9 cc_name->cc_name;RF10 
rn->(rn - 1)
+--------------hashJoin[INNER_JOIN bucketShuffle] hashCondition=((v1.cc_name = 
v1_lead.cc_name) and (v1.i_brand = v1_lead.i_brand) and (v1.i_category = 
v1_lead.i_category) and (v1.rn = expr_(rn - 1))) otherCondition=() build 
RFs:RF7 i_category->i_category;RF8 i_category->i_category;RF9 
i_brand->i_brand;RF10 i_brand->i_brand;RF11 cc_name->cc_name;RF12 
cc_name->cc_name;RF13 expr_(rn - 1)->rn;RF14 expr_(rn - 1)->(rn + 1)
 ----------------PhysicalProject
-------------------PhysicalCteConsumer ( cteId=CTEId#0 ) apply RFs: RF7 RF8 RF9 
RF10
-----------------PhysicalProject
-------------------hashJoin[INNER_JOIN shuffle] hashCondition=((v1.cc_name = 
v1_lag.cc_name) and (v1.i_brand = v1_lag.i_brand) and (v1.i_category = 
v1_lag.i_category) and (v1.rn = expr_(rn + 1))) otherCondition=() build RFs:RF3 
i_category->i_category;RF4 i_brand->i_brand;RF5 cc_name->cc_name;RF6 rn->(rn + 
1)
+------------------hashJoin[INNER_JOIN shuffle] hashCondition=((v1.cc_name = 
v1_lag.cc_name) and (v1.i_brand = v1_lag.i_brand) and (v1.i_category = 
v1_lag.i_category) and (v1.rn = expr_(rn + 1))) otherCondition=() build RFs:RF3 
i_category->i_category;RF4 i_brand->i_brand;RF5 cc_name->cc_name;RF6 rn->(rn + 
1);RF15 i_category->i_category;RF16 i_brand->i_brand;RF17 cc_name->cc_name;RF18 
rn->(rn - 1)
 --------------------PhysicalProject
-----------------------PhysicalCteConsumer ( cteId=CTEId#0 ) apply RFs: RF3 RF4 
RF5 RF6
+----------------------PhysicalCteConsumer ( cteId=CTEId#0 ) apply RFs: RF3 RF4 
RF5 RF6 RF8 RF10 RF12 RF14
 --------------------filter(((cast(abs((v2.sum_sales - 
cast(v2.avg_monthly_sales as DECIMALV3(38, 2)))) as DECIMALV3(38, 10)) / 
v2.avg_monthly_sales) > 0.100000) and (v2.avg_monthly_sales > 0.0000) and 
(v2.d_year = 1999))
-----------------------PhysicalCteConsumer ( cteId=CTEId#0 )
+----------------------PhysicalCteConsumer ( cteId=CTEId#0 ) apply RFs: RF7 RF9 
RF11 RF13
+----------------PhysicalProject
+------------------PhysicalCteConsumer ( cteId=CTEId#0 ) apply RFs: RF15 RF16 
RF17 RF18
 
diff --git a/regression-test/data/shape_check/tpcds_sf10t_orc/shape/query59.out 
b/regression-test/data/shape_check/tpcds_sf10t_orc/shape/query59.out
index 0d36a9556da..72efa8d776d 100644
--- a/regression-test/data/shape_check/tpcds_sf10t_orc/shape/query59.out
+++ b/regression-test/data/shape_check/tpcds_sf10t_orc/shape/query59.out
@@ -17,27 +17,27 @@ PhysicalCteAnchor ( cteId=CTEId#0 )
 --------PhysicalDistribute[DistributionSpecGather]
 ----------PhysicalTopN[LOCAL_SORT]
 ------------PhysicalProject
---------------hashJoin[INNER_JOIN broadcast] hashCondition=((d.d_week_seq = 
d_week_seq1)) otherCondition=() build RFs:RF6 d_week_seq->d_week_seq
+--------------hashJoin[INNER_JOIN shuffle] 
hashCondition=((expr_cast(d_week_seq1 as BIGINT) = expr_(cast(d_week_seq2 as 
BIGINT) - 52)) and (y.s_store_id1 = x.s_store_id2)) otherCondition=() build 
RFs:RF5 s_store_id2->s_store_id;RF6 expr_(cast(d_week_seq2 as BIGINT) - 
52)->cast(d_week_seq as BIGINT)
 ----------------PhysicalProject
-------------------hashJoin[INNER_JOIN shuffle] 
hashCondition=((expr_cast(d_week_seq1 as BIGINT) = expr_(cast(d_week_seq2 as 
BIGINT) - 52)) and (y.s_store_id1 = x.s_store_id2)) otherCondition=() build 
RFs:RF4 s_store_id2->s_store_id;RF5 expr_(cast(d_week_seq2 as BIGINT) - 
52)->cast(d_week_seq as BIGINT)
+------------------hashJoin[INNER_JOIN broadcast] hashCondition=((d.d_week_seq 
= d_week_seq1)) otherCondition=() build RFs:RF4 d_week_seq->d_week_seq
 --------------------PhysicalProject
 ----------------------hashJoin[INNER_JOIN shuffle] 
hashCondition=((wss.ss_store_sk = store.s_store_sk)) otherCondition=() build 
RFs:RF3 s_store_sk->ss_store_sk
 ------------------------PhysicalProject
---------------------------PhysicalCteConsumer ( cteId=CTEId#0 ) apply RFs: RF3 
RF5 RF6
+--------------------------PhysicalCteConsumer ( cteId=CTEId#0 ) apply RFs: RF3 
RF4 RF6
 ------------------------PhysicalProject
---------------------------PhysicalOlapScan[store] apply RFs: RF4
+--------------------------PhysicalOlapScan[store] apply RFs: RF5
 --------------------PhysicalProject
-----------------------hashJoin[INNER_JOIN broadcast] 
hashCondition=((d.d_week_seq = d_week_seq2)) otherCondition=() build RFs:RF2 
d_week_seq->d_week_seq
+----------------------filter((d.d_month_seq <= 1216) and (d.d_month_seq >= 
1205))
+------------------------PhysicalOlapScan[date_dim(d)]
+----------------PhysicalProject
+------------------hashJoin[INNER_JOIN broadcast] hashCondition=((d.d_week_seq 
= d_week_seq2)) otherCondition=() build RFs:RF2 d_week_seq->d_week_seq
+--------------------PhysicalProject
+----------------------hashJoin[INNER_JOIN shuffle] 
hashCondition=((wss.ss_store_sk = store.s_store_sk)) otherCondition=() build 
RFs:RF1 s_store_sk->ss_store_sk
 ------------------------PhysicalProject
---------------------------hashJoin[INNER_JOIN shuffle] 
hashCondition=((wss.ss_store_sk = store.s_store_sk)) otherCondition=() build 
RFs:RF1 s_store_sk->ss_store_sk
-----------------------------PhysicalProject
-------------------------------PhysicalCteConsumer ( cteId=CTEId#0 ) apply RFs: 
RF1 RF2
-----------------------------PhysicalProject
-------------------------------PhysicalOlapScan[store]
+--------------------------PhysicalCteConsumer ( cteId=CTEId#0 ) apply RFs: RF1 
RF2
 ------------------------PhysicalProject
---------------------------filter((d.d_month_seq <= 1228) and (d.d_month_seq >= 
1217))
-----------------------------PhysicalOlapScan[date_dim(d)]
-----------------PhysicalProject
-------------------filter((d.d_month_seq <= 1216) and (d.d_month_seq >= 1205))
---------------------PhysicalOlapScan[date_dim(d)]
+--------------------------PhysicalOlapScan[store]
+--------------------PhysicalProject
+----------------------filter((d.d_month_seq <= 1228) and (d.d_month_seq >= 
1217))
+------------------------PhysicalOlapScan[date_dim(d)]
 
diff --git 
a/regression-test/suites/query_p0/stats/mixed_join_key_statistics.groovy 
b/regression-test/suites/query_p0/stats/mixed_join_key_statistics.groovy
new file mode 100644
index 00000000000..34b36ca38b1
--- /dev/null
+++ b/regression-test/suites/query_p0/stats/mixed_join_key_statistics.groovy
@@ -0,0 +1,53 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements.  See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership.  The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License.  You may obtain a copy of the License at
+//
+//   http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied.  See the License for the
+// specific language governing permissions and limitations
+// under the License.
+
+suite("mixed_join_key_statistics") {
+    sql "DROP TABLE IF EXISTS mixed_join_key_left"
+    sql "DROP TABLE IF EXISTS mixed_join_key_right"
+    sql """
+        CREATE TABLE mixed_join_key_left (k INT NOT NULL, app INT NOT NULL)
+        DUPLICATE KEY(k) DISTRIBUTED BY HASH(k) BUCKETS 1 
PROPERTIES("replication_num" = "1")
+    """
+    sql """
+        CREATE TABLE mixed_join_key_right (k INT NOT NULL, app INT NOT NULL)
+        DUPLICATE KEY(k) DISTRIBUTED BY HASH(k) BUCKETS 1 
PROPERTIES("replication_num" = "1")
+    """
+    sql 'INSERT INTO mixed_join_key_left SELECT number % 2, 1 FROM 
numbers("number" = "20")'
+    sql 'INSERT INTO mixed_join_key_right SELECT number % 2, 1 FROM 
numbers("number" = "20")'
+    sql """
+        ALTER TABLE mixed_join_key_left MODIFY COLUMN k SET STATS
+        ('row_count'='1000', 'ndv'='10', 'min_value'='0', 'max_value'='9', 
'num_nulls'='0')
+    """
+    sql """
+        ALTER TABLE mixed_join_key_right MODIFY COLUMN k SET STATS
+        ('row_count'='1000', 'ndv'='10', 'min_value'='0', 'max_value'='9', 
'num_nulls'='0')
+    """
+
+    explain {
+        sql """
+            OPTIMIZED PLAN
+            SELECT l.k FROM mixed_join_key_left l JOIN mixed_join_key_right r 
ON l.k = r.k AND l.app = r.app
+        """
+        contains "stats=100,000, type=INNER_JOIN"
+    }
+
+    order_qt_join_result """
+        SELECT l.k, COUNT(*)
+        FROM mixed_join_key_left l JOIN mixed_join_key_right r ON l.k = r.k 
AND l.app = r.app
+        GROUP BY l.k
+    """
+}


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to