Skip to content

Commit 52a90a5

Browse files
committed
planner: move rule_constant_propagation to rule pkg. (pingcap#55231)
1 parent 6df7aba commit 52a90a5

File tree

7 files changed

+49
-29
lines changed

7 files changed

+49
-29
lines changed

pkg/planner/core/BUILD.bazel

-1
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,6 @@ go_library(
6969
"rule_aggregation_skew_rewrite.go",
7070
"rule_collect_plan_stats.go",
7171
"rule_column_pruning.go",
72-
"rule_constant_propagation.go",
7372
"rule_decorrelate.go",
7473
"rule_derive_topn_from_window.go",
7574
"rule_eliminate_projection.go",

pkg/planner/core/logical_selection.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,7 @@ func (p *LogicalSelection) PullUpConstantPredicates() []expression.Expression {
190190
var result []expression.Expression
191191
for _, candidatePredicate := range p.Conditions {
192192
// the candidate predicate should be a constant and compare predicate
193-
match := validCompareConstantPredicate(p.SCtx().GetExprCtx().GetEvalCtx(), candidatePredicate)
193+
match := ruleutil.ValidCompareConstantPredicate(p.SCtx().GetExprCtx().GetEvalCtx(), candidatePredicate)
194194
if match {
195195
result = append(result, candidatePredicate)
196196
}

pkg/planner/core/optimizer.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ var optRuleList = []base.LogicalOptRule{
109109
&SkewDistinctAggRewriter{},
110110
&ProjectionEliminator{},
111111
&MaxMinEliminator{},
112-
&ConstantPropagationSolver{},
112+
&rule.ConstantPropagationSolver{},
113113
&ConvertOuterToInnerJoin{},
114114
&PPDSolver{},
115115
&OuterJoinEliminator{},

pkg/planner/core/rule/BUILD.bazel

+1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ go_library(
44
name = "rule",
55
srcs = [
66
"rule_build_key_info.go",
7+
"rule_constant_propagation.go",
78
"rule_init.go",
89
],
910
importpath = "github.com/pingcap/tidb/pkg/planner/core/rule",

pkg/planner/core/rule_constant_propagation.go pkg/planner/core/rule/rule_constant_propagation.go

+1-26
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,11 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15-
package core
15+
package rule
1616

1717
import (
1818
"context"
1919

20-
"github.com/pingcap/tidb/pkg/expression"
21-
"github.com/pingcap/tidb/pkg/parser/ast"
2220
"github.com/pingcap/tidb/pkg/planner/core/base"
2321
"github.com/pingcap/tidb/pkg/planner/util/optimizetrace"
2422
)
@@ -87,26 +85,3 @@ func (cp *ConstantPropagationSolver) execOptimize(currentPlan base.LogicalPlan,
8785
func (*ConstantPropagationSolver) Name() string {
8886
return "constant_propagation"
8987
}
90-
91-
// validComparePredicate checks if the predicate is an expression like [column '>'|'>='|'<'|'<='|'=' constant].
92-
// return param1: return true, if the predicate is a compare constant predicate.
93-
// return param2: return the column side of predicate.
94-
func validCompareConstantPredicate(ctx expression.EvalContext, candidatePredicate expression.Expression) bool {
95-
scalarFunction, ok := candidatePredicate.(*expression.ScalarFunction)
96-
if !ok {
97-
return false
98-
}
99-
if scalarFunction.FuncName.L != ast.GT && scalarFunction.FuncName.L != ast.GE &&
100-
scalarFunction.FuncName.L != ast.LT && scalarFunction.FuncName.L != ast.LE &&
101-
scalarFunction.FuncName.L != ast.EQ {
102-
return false
103-
}
104-
column, _ := expression.ValidCompareConstantPredicateHelper(ctx, scalarFunction, true)
105-
if column == nil {
106-
column, _ = expression.ValidCompareConstantPredicateHelper(ctx, scalarFunction, false)
107-
}
108-
if column == nil {
109-
return false
110-
}
111-
return true
112-
}

pkg/planner/core/rule/util/BUILD.bazel

+2
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,13 @@ go_library(
55
srcs = [
66
"build_key_info_misc.go",
77
"misc.go",
8+
"rule_constant_propagation.go",
89
],
910
importpath = "github.com/pingcap/tidb/pkg/planner/core/rule/util",
1011
visibility = ["//visibility:public"],
1112
deps = [
1213
"//pkg/expression",
14+
"//pkg/parser/ast",
1315
"//pkg/parser/model",
1416
"//pkg/parser/mysql",
1517
"//pkg/planner/core/base",
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
// Copyright 2023 PingCAP, Inc.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package util
16+
17+
import (
18+
"github.com/pingcap/tidb/pkg/expression"
19+
"github.com/pingcap/tidb/pkg/parser/ast"
20+
)
21+
22+
// ValidCompareConstantPredicate checks if the predicate is an expression like [column '>'|'>='|'<'|'<='|'=' constant].
23+
// return param1: return true, if the predicate is a compare constant predicate.
24+
// return param2: return the column side of predicate.
25+
func ValidCompareConstantPredicate(ctx expression.EvalContext, candidatePredicate expression.Expression) bool {
26+
scalarFunction, ok := candidatePredicate.(*expression.ScalarFunction)
27+
if !ok {
28+
return false
29+
}
30+
if scalarFunction.FuncName.L != ast.GT && scalarFunction.FuncName.L != ast.GE &&
31+
scalarFunction.FuncName.L != ast.LT && scalarFunction.FuncName.L != ast.LE &&
32+
scalarFunction.FuncName.L != ast.EQ {
33+
return false
34+
}
35+
column, _ := expression.ValidCompareConstantPredicateHelper(ctx, scalarFunction, true)
36+
if column == nil {
37+
column, _ = expression.ValidCompareConstantPredicateHelper(ctx, scalarFunction, false)
38+
}
39+
if column == nil {
40+
return false
41+
}
42+
return true
43+
}

0 commit comments

Comments
 (0)