Skip to content
This repository was archived by the owner on Jan 9, 2020. It is now read-only.

Commit ef10f45

Browse files
committed
[SPARK-21652][SQL][FOLLOW-UP] Fix rule conflict caused by InferFiltersFromConstraints
## What changes were proposed in this pull request? The optimizer rule `InferFiltersFromConstraints` could trigger our batch `Operator Optimizations` exceeds the max iteration limit (i.e., 100) so that the final plan might not be properly optimized. The rule `InferFiltersFromConstraints` could conflict with the other Filter/Join predicate reduction rules. Thus, we need to separate `InferFiltersFromConstraints` from the other rules. This PR is to separate `InferFiltersFromConstraints ` from the main batch `Operator Optimizations` . ## How was this patch tested? The existing test cases. Author: gatorsmile <[email protected]> Closes apache#19149 from gatorsmile/inferFilterRule.
1 parent ee56fc3 commit ef10f45

File tree

1 file changed

+64
-51
lines changed
  • sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/optimizer

1 file changed

+64
-51
lines changed

sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/optimizer/Optimizer.scala

Lines changed: 64 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,62 @@ abstract class Optimizer(sessionCatalog: SessionCatalog)
4747
protected def fixedPoint = FixedPoint(SQLConf.get.optimizerMaxIterations)
4848

4949
def batches: Seq[Batch] = {
50-
Batch("Eliminate Distinct", Once, EliminateDistinct) ::
50+
val operatorOptimizationRuleSet =
51+
Seq(
52+
// Operator push down
53+
PushProjectionThroughUnion,
54+
ReorderJoin,
55+
EliminateOuterJoin,
56+
PushPredicateThroughJoin,
57+
PushDownPredicate,
58+
LimitPushDown,
59+
ColumnPruning,
60+
InferFiltersFromConstraints,
61+
// Operator combine
62+
CollapseRepartition,
63+
CollapseProject,
64+
CollapseWindow,
65+
CombineFilters,
66+
CombineLimits,
67+
CombineUnions,
68+
// Constant folding and strength reduction
69+
NullPropagation,
70+
ConstantPropagation,
71+
FoldablePropagation,
72+
OptimizeIn,
73+
ConstantFolding,
74+
ReorderAssociativeOperator,
75+
LikeSimplification,
76+
BooleanSimplification,
77+
SimplifyConditionals,
78+
RemoveDispensableExpressions,
79+
SimplifyBinaryComparison,
80+
PruneFilters,
81+
EliminateSorts,
82+
SimplifyCasts,
83+
SimplifyCaseConversionExpressions,
84+
RewriteCorrelatedScalarSubquery,
85+
EliminateSerialization,
86+
RemoveRedundantAliases,
87+
RemoveRedundantProject,
88+
SimplifyCreateStructOps,
89+
SimplifyCreateArrayOps,
90+
SimplifyCreateMapOps,
91+
CombineConcats) ++
92+
extendedOperatorOptimizationRules
93+
94+
val operatorOptimizationBatch: Seq[Batch] = {
95+
val rulesWithoutInferFiltersFromConstraints =
96+
operatorOptimizationRuleSet.filterNot(_ == InferFiltersFromConstraints)
97+
Batch("Operator Optimization before Inferring Filters", fixedPoint,
98+
rulesWithoutInferFiltersFromConstraints: _*) ::
99+
Batch("Infer Filters", Once,
100+
InferFiltersFromConstraints) ::
101+
Batch("Operator Optimization after Inferring Filters", fixedPoint,
102+
rulesWithoutInferFiltersFromConstraints: _*) :: Nil
103+
}
104+
105+
(Batch("Eliminate Distinct", Once, EliminateDistinct) ::
51106
// Technically some of the rules in Finish Analysis are not optimizer rules and belong more
52107
// in the analyzer, because they are needed for correctness (e.g. ComputeCurrentTime).
53108
// However, because we also use the analyzer to canonicalized queries (for view definition),
@@ -81,68 +136,26 @@ abstract class Optimizer(sessionCatalog: SessionCatalog)
81136
ReplaceDistinctWithAggregate) ::
82137
Batch("Aggregate", fixedPoint,
83138
RemoveLiteralFromGroupExpressions,
84-
RemoveRepetitionFromGroupExpressions) ::
85-
Batch("Operator Optimizations", fixedPoint, Seq(
86-
// Operator push down
87-
PushProjectionThroughUnion,
88-
ReorderJoin,
89-
EliminateOuterJoin,
90-
InferFiltersFromConstraints,
91-
BooleanSimplification,
92-
PushPredicateThroughJoin,
93-
PushDownPredicate,
94-
LimitPushDown,
95-
ColumnPruning,
96-
// Operator combine
97-
CollapseRepartition,
98-
CollapseProject,
99-
CollapseWindow,
100-
CombineFilters,
101-
CombineLimits,
102-
CombineUnions,
103-
// Constant folding and strength reduction
104-
NullPropagation,
105-
ConstantPropagation,
106-
FoldablePropagation,
107-
OptimizeIn,
108-
ConstantFolding,
109-
ReorderAssociativeOperator,
110-
LikeSimplification,
111-
BooleanSimplification,
112-
SimplifyConditionals,
113-
RemoveDispensableExpressions,
114-
SimplifyBinaryComparison,
115-
PruneFilters,
116-
EliminateSorts,
117-
SimplifyCasts,
118-
SimplifyCaseConversionExpressions,
119-
RewriteCorrelatedScalarSubquery,
120-
EliminateSerialization,
121-
RemoveRedundantAliases,
122-
RemoveRedundantProject,
123-
SimplifyCreateStructOps,
124-
SimplifyCreateArrayOps,
125-
SimplifyCreateMapOps,
126-
CombineConcats) ++
127-
extendedOperatorOptimizationRules: _*) ::
139+
RemoveRepetitionFromGroupExpressions) :: Nil ++
140+
operatorOptimizationBatch) :+
128141
Batch("Join Reorder", Once,
129-
CostBasedJoinReorder) ::
142+
CostBasedJoinReorder) :+
130143
Batch("Decimal Optimizations", fixedPoint,
131-
DecimalAggregates) ::
144+
DecimalAggregates) :+
132145
Batch("Object Expressions Optimization", fixedPoint,
133146
EliminateMapObjects,
134-
CombineTypedFilters) ::
147+
CombineTypedFilters) :+
135148
Batch("LocalRelation", fixedPoint,
136149
ConvertToLocalRelation,
137-
PropagateEmptyRelation) ::
150+
PropagateEmptyRelation) :+
138151
// The following batch should be executed after batch "Join Reorder" and "LocalRelation".
139152
Batch("Check Cartesian Products", Once,
140-
CheckCartesianProducts) ::
153+
CheckCartesianProducts) :+
141154
Batch("RewriteSubquery", Once,
142155
RewritePredicateSubquery,
143156
ColumnPruning,
144157
CollapseProject,
145-
RemoveRedundantProject) :: Nil
158+
RemoveRedundantProject)
146159
}
147160

148161
/**

0 commit comments

Comments
 (0)