-
Notifications
You must be signed in to change notification settings - Fork 524
/
Copy pathconstraint_propagation.go
174 lines (153 loc) · 5.03 KB
/
constraint_propagation.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
// Copyright 2018 PingCAP, Inc.
//
// Licensed 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,
// See the License for the specific language governing permissions and
// limitations under the License.
package expression
import (
"bytes"
"github.com/pingcap/tidb/parser/mysql"
"github.com/pingcap/tidb/sessionctx"
"github.com/pingcap/tidb/sessionctx/stmtctx"
"github.com/pingcap/tidb/types"
"github.com/pingcap/tidb/util/chunk"
"github.com/pingcap/tidb/util/logutil"
"go.uber.org/zap"
)
// exprSet is a Set container for expressions, each expression in it is unique.
// `tombstone` is deleted mark, if tombstone[i] is true, data[i] is invalid.
// `index` use expr.HashCode() as key, to implement the unique property.
type exprSet struct {
data []Expression
tombstone []bool
exists map[string]struct{}
constfalse bool
}
func (s *exprSet) Append(sc *stmtctx.StatementContext, e Expression) bool {
if _, ok := s.exists[string(e.HashCode(sc))]; ok {
return false
}
s.data = append(s.data, e)
s.tombstone = append(s.tombstone, false)
s.exists[string(e.HashCode(sc))] = struct{}{}
return true
}
// Slice returns the valid expressions in the exprSet, this function has side effect.
func (s *exprSet) Slice() []Expression {
if s.constfalse {
return []Expression{&Constant{
Value: types.NewDatum(false),
RetType: types.NewFieldType(mysql.TypeTiny),
}}
}
idx := 0
for i := 0; i < len(s.data); i++ {
if !s.tombstone[i] {
s.data[idx] = s.data[i]
idx++
}
}
return s.data[:idx]
}
func (s *exprSet) SetConstFalse() {
s.constfalse = true
}
func newExprSet(ctx sessionctx.Context, conditions []Expression) *exprSet {
var exprs exprSet
exprs.data = make([]Expression, 0, len(conditions))
exprs.tombstone = make([]bool, 0, len(conditions))
exprs.exists = make(map[string]struct{}, len(conditions))
sc := ctx.GetSessionVars().StmtCtx
for _, v := range conditions {
exprs.Append(sc, v)
}
return &exprs
}
type constraintSolver []constraintPropagateRule
func newConstraintSolver(rules ...constraintPropagateRule) constraintSolver {
return constraintSolver(rules)
}
type pgSolver2 struct{}
func (s pgSolver2) PropagateConstant(ctx sessionctx.Context, conditions []Expression) []Expression {
solver := newConstraintSolver(ruleConstantFalse, ruleColumnEQConst)
return solver.Solve(ctx, conditions)
}
// Solve propagate constraint according to the rules in the constraintSolver.
func (s constraintSolver) Solve(ctx sessionctx.Context, conditions []Expression) []Expression {
exprs := newExprSet(ctx, conditions)
s.fixPoint(ctx, exprs)
return exprs.Slice()
}
// fixPoint is the core of the constraint propagation algorithm.
// It will iterate the expression set over and over again, pick two expressions,
// apply one to another.
// If new conditions can be inferred, they will be append into the expression set.
// Until no more conditions can be inferred from the set, the algorithm finish.
func (s constraintSolver) fixPoint(ctx sessionctx.Context, exprs *exprSet) {
for {
saveLen := len(exprs.data)
s.iterOnce(ctx, exprs)
if saveLen == len(exprs.data) {
break
}
}
}
// iterOnce picks two expressions from the set, try to propagate new conditions from them.
func (s constraintSolver) iterOnce(ctx sessionctx.Context, exprs *exprSet) {
for i := 0; i < len(exprs.data); i++ {
if exprs.tombstone[i] {
continue
}
for j := 0; j < len(exprs.data); j++ {
if exprs.tombstone[j] {
continue
}
if i == j {
continue
}
s.solve(ctx, i, j, exprs)
}
}
}
// solve uses exprs[i] exprs[j] to propagate new conditions.
func (s constraintSolver) solve(ctx sessionctx.Context, i, j int, exprs *exprSet) {
for _, rule := range s {
rule(ctx, i, j, exprs)
}
}
type constraintPropagateRule func(ctx sessionctx.Context, i, j int, exprs *exprSet)
// ruleConstantFalse propagates from CNF condition that false plus anything returns false.
// false, a = 1, b = c ... => false
func ruleConstantFalse(ctx sessionctx.Context, i, j int, exprs *exprSet) {
cond := exprs.data[i]
if cons, ok := cond.(*Constant); ok {
v, isNull, err := cons.EvalInt(ctx, chunk.Row{})
if err != nil {
logutil.BgLogger().Warn("eval constant", zap.Error(err))
return
}
if !isNull && v == 0 {
exprs.SetConstFalse()
}
}
}
// ruleColumnEQConst propagates the "column = const" condition.
// "a = 3, b = a, c = a, d = b" => "a = 3, b = 3, c = 3, d = 3"
func ruleColumnEQConst(ctx sessionctx.Context, i, j int, exprs *exprSet) {
col, cons := validEqualCond(exprs.data[i])
if col != nil {
expr := ColumnSubstitute(exprs.data[j], NewSchema(col), []Expression{cons})
stmtctx := ctx.GetSessionVars().StmtCtx
if !bytes.Equal(expr.HashCode(stmtctx), exprs.data[j].HashCode(stmtctx)) {
exprs.Append(stmtctx, expr)
exprs.tombstone[j] = true
}
}
}