-
Notifications
You must be signed in to change notification settings - Fork 3.9k
/
Copy pathextract.go
232 lines (207 loc) · 6.41 KB
/
extract.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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
// Copyright 2018 The Cockroach Authors.
//
// 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,
// 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.
package memo
import (
"fmt"
"github.com/cockroachdb/cockroach/pkg/sql/opt"
"github.com/cockroachdb/cockroach/pkg/sql/sem/tree"
"github.com/cockroachdb/cockroach/pkg/sql/sem/types"
)
// This file contains various helper functions that extract useful information
// from expressions.
// CanExtractConstTuple returns true if the expression is a TupleOp with
// constant values (a nested tuple of constant values is considered constant).
func CanExtractConstTuple(e opt.Expr) bool {
return e.Op() == opt.TupleOp && CanExtractConstDatum(e)
}
// CanExtractConstDatum returns true if a constant datum can be created from the
// given expression (tuples and arrays of constant values are considered
// constant values). If CanExtractConstDatum returns true, then
// ExtractConstDatum is guaranteed to work as well.
func CanExtractConstDatum(e opt.Expr) bool {
if opt.IsConstValueOp(e) {
return true
}
if tup, ok := e.(*TupleExpr); ok {
for _, elem := range tup.Elems {
if !CanExtractConstDatum(elem) {
return false
}
}
return true
}
if arr, ok := e.(*ArrayExpr); ok {
for _, elem := range arr.Elems {
if !CanExtractConstDatum(elem) {
return false
}
}
return true
}
return false
}
// ExtractConstDatum returns the Datum that represents the value of an
// expression with a constant value. An expression with a constant value is:
// - one that has a ConstValue tag, or
// - a tuple or array where all children are constant values.
func ExtractConstDatum(e opt.Expr) tree.Datum {
switch t := e.(type) {
case *NullExpr:
return tree.DNull
case *TrueExpr:
return tree.DBoolTrue
case *FalseExpr:
return tree.DBoolFalse
case *ConstExpr:
return t.Value
case *TupleExpr:
datums := make(tree.Datums, len(t.Elems))
for i := range datums {
datums[i] = ExtractConstDatum(t.Elems[i])
}
typ := t.Typ.(types.TTuple)
return tree.NewDTuple(typ, datums...)
case *ArrayExpr:
elementType := t.Typ.(types.TArray).Typ
a := tree.NewDArray(elementType)
a.Array = make(tree.Datums, len(t.Elems))
for i := range a.Array {
a.Array[i] = ExtractConstDatum(t.Elems[i])
if a.Array[i] == tree.DNull {
a.HasNulls = true
}
}
return a
}
panic(fmt.Sprintf("non-const expression: %+v", e))
}
// ExtractAggSingleInputColumn returns the input ColumnID of an aggregate
// operator that has a single input.
func ExtractAggSingleInputColumn(e opt.ScalarExpr) opt.ColumnID {
if !opt.IsAggregateOp(e) {
panic("not an Aggregate")
}
return ExtractVarFromAggInput(e.Child(0).(opt.ScalarExpr)).Col
}
// ExtractAggInputColumns returns the input columns of an aggregate (which can
// be empty).
func ExtractAggInputColumns(e opt.ScalarExpr) opt.ColSet {
if !opt.IsAggregateOp(e) {
panic("not an Aggregate")
}
var res opt.ColSet
for i, n := 0, e.ChildCount(); i < n; i++ {
res.Add(int(ExtractVarFromAggInput(e.Child(i).(opt.ScalarExpr)).Col))
}
return res
}
// ExtractVarFromAggInput is given an argument to an Aggregate and returns the
// inner Variable expression, stripping out modifiers like AggDistinct.
func ExtractVarFromAggInput(arg opt.ScalarExpr) *VariableExpr {
if distinct, ok := arg.(*AggDistinctExpr); ok {
arg = distinct.Input
}
if variable, ok := arg.(*VariableExpr); ok {
return variable
}
panic("aggregate input not a Variable")
}
// ExtractJoinEqualityColumns returns pairs of columns (one from the left side,
// one from the right side) which are constrained to be equal in a join (and
// have equivalent types).
func ExtractJoinEqualityColumns(
leftCols, rightCols opt.ColSet, on FiltersExpr,
) (leftEq opt.ColList, rightEq opt.ColList) {
for i := range on {
condition := on[i].Condition
ok, left, right := isJoinEquality(leftCols, rightCols, condition)
if !ok {
continue
}
// Don't allow any column to show up twice.
// TODO(radu): need to figure out the right thing to do in cases
// like: left.a = right.a AND left.a = right.b
duplicate := false
for i := range leftEq {
if leftEq[i] == left || rightEq[i] == right {
duplicate = true
break
}
}
if !duplicate {
leftEq = append(leftEq, left)
rightEq = append(rightEq, right)
}
}
return leftEq, rightEq
}
func isVarEquality(condition opt.ScalarExpr) (leftVar, rightVar *VariableExpr, ok bool) {
if eq, ok := condition.(*EqExpr); ok {
if leftVar, ok := eq.Left.(*VariableExpr); ok {
if rightVar, ok := eq.Right.(*VariableExpr); ok {
return leftVar, rightVar, true
}
}
}
return nil, nil, false
}
func isJoinEquality(
leftCols, rightCols opt.ColSet, condition opt.ScalarExpr,
) (ok bool, left, right opt.ColumnID) {
lvar, rvar, ok := isVarEquality(condition)
if !ok {
return false, 0, 0
}
// Don't allow mixed types (see #22519).
if !lvar.DataType().Equivalent(rvar.DataType()) {
return false, 0, 0
}
if leftCols.Contains(int(lvar.Col)) && rightCols.Contains(int(rvar.Col)) {
return true, lvar.Col, rvar.Col
}
if leftCols.Contains(int(rvar.Col)) && rightCols.Contains(int(lvar.Col)) {
return true, rvar.Col, lvar.Col
}
return false, 0, 0
}
// ExtractRemainingJoinFilters calculates the remaining ON condition after
// removing equalities that are handled separately. The given function
// determines if an equality is redundant. The result is empty if there are no
// remaining conditions.
func ExtractRemainingJoinFilters(on FiltersExpr, leftEq, rightEq opt.ColList) FiltersExpr {
var newFilters FiltersExpr
for i := range on {
leftVar, rightVar, ok := isVarEquality(on[i].Condition)
if ok {
a, b := leftVar.Col, rightVar.Col
found := false
for j := range leftEq {
if (a == leftEq[j] && b == rightEq[j]) ||
(a == rightEq[j] && b == leftEq[j]) {
found = true
break
}
}
if found {
// Skip this condition.
continue
}
}
if newFilters == nil {
newFilters = make(FiltersExpr, 0, len(on)-i)
}
newFilters = append(newFilters, on[i])
}
return newFilters
}