This repository has been archived by the owner on Feb 16, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 108
/
select.go
282 lines (257 loc) · 8.48 KB
/
select.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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
// Copyright 2019 eBay Inc.
// Primary authors: Simon Fell, Diego Ongaro,
// Raymond Kroeker, and Sathish Kandasamy.
//
// 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
// https://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 exec
import (
"context"
"fmt"
"strings"
"github.com/ebay/akutan/query/planner/plandef"
"github.com/ebay/akutan/rpc"
"github.com/ebay/akutan/util/parallel"
)
// TODO: What are the comparison/selection semantics for langID/unitID ?
// newSelectLitOp returns a new operator for the supplied SelectLit plan
// operator and its inputs. selectLitOp expects a single input.
func newSelectLitOp(op *plandef.SelectLit, inputs []queryOperator) operator {
if len(inputs) != 1 {
panic(fmt.Sprintf("selectLit operator called with unexpected number of inputs: %d", len(inputs)))
}
objComparerOp := buildComparers(op)
testIdx, exists := inputs[0].columns().IndexOf(op.Test)
if !exists {
panic(fmt.Sprintf("selectLit operator has plan with variable %s but, that's not in the input columns %v",
op.Test, inputs[0].columns()))
}
return &selectOp{
def: op,
input: inputs[0],
comparer: func(row []Value) bool {
return objComparerOp(row[testIdx].KGObject)
},
}
}
// newSelectVarOp returns a new operator for the supplied SelectVar plan
// operator and its inputs. selectVarOp expects a single input.
func newSelectVarOp(op *plandef.SelectVar, inputs []queryOperator) operator {
if len(inputs) != 1 {
panic(fmt.Sprintf("selectVar operator called with unexpected number of inputs: %d", len(inputs)))
}
return &selectOp{
def: op,
input: inputs[0],
comparer: selectVarCompareOp(op, inputs[0].columns()),
}
}
// selectOp does the bulk of the operator work for SelectLit & SelectVar. Its an
// operator that executes the input, and filters out results where the comparer
// returns false.
type selectOp struct {
def plandef.Operator
input queryOperator
comparer func(row []Value) bool
}
func (s *selectOp) operator() plandef.Operator {
return s.def
}
func (s *selectOp) columns() Columns {
return s.input.columns()
}
func (s *selectOp) execute(ctx context.Context, binder valueBinder, res results) error {
inputResCh := make(chan ResultChunk, 4)
wait := parallel.Go(func() {
for chunk := range inputResCh {
for i := range chunk.offsets {
row := chunk.Row(i)
if s.comparer(row) {
// this factset meets the selection criteria pass it along
res.add(ctx, chunk.offsets[i], chunk.Facts[i], row)
}
}
}
})
err := s.input.run(ctx, binder, inputResCh)
wait()
return err
}
// comparer should return true if the supplied value meets the required condition
type comparer func(val rpc.KGObject) bool
func and(c1, c2 comparer) comparer {
return func(v rpc.KGObject) bool {
return c1(v) && c2(v)
}
}
func not(c comparer) comparer {
return func(v rpc.KGObject) bool {
return !c(v)
}
}
func buildComparers(op *plandef.SelectLit) comparer {
if len(op.Clauses) == 1 {
return buildComparer(op.Test, &op.Clauses[0])
}
c := make([]comparer, len(op.Clauses))
for i := range op.Clauses {
c[i] = buildComparer(op.Test, &op.Clauses[i])
}
return func(v rpc.KGObject) bool {
for i := range c {
if !c[i](v) {
return false
}
}
return true
}
}
func buildComparer(v *plandef.Variable, clause *plandef.SelectClause) comparer {
// TODO: for functional parity we include comparison of literal units and languages however we need to resolve whether
// or not no unit/language indicated in the query object indicates whether or not we ignore units in the comparison
//
toType := clause.Literal1.Value.ValueType()
langID := clause.Literal1.Value.LangID()
unitID := clause.Literal1.Value.UnitID()
var langUnitTypeCheck comparer
switch toType {
case rpc.KtKID:
if clause.Comparison != rpc.OpNotEqual && clause.Comparison != rpc.OpEqual {
panic("invalid comparison, if the literal value is a KID, only == & != are allowed comparisons")
}
fallthrough
case rpc.KtNil:
langUnitTypeCheck = func(v rpc.KGObject) bool {
return v.IsType(toType)
}
case rpc.KtString:
langUnitTypeCheck = func(v rpc.KGObject) bool {
return v.LangID() == langID && v.IsType(toType)
}
case rpc.KtInt64:
fallthrough
case rpc.KtFloat64:
fallthrough
case rpc.KtBool:
fallthrough
case rpc.KtTimestamp:
langUnitTypeCheck = func(v rpc.KGObject) bool {
return v.UnitID() == unitID && v.IsType(toType)
}
default:
panic(fmt.Sprintf("Unexpected KGObject type of %T/%v in select.go", clause.Literal1.Value, clause.Literal1.Value))
}
switch clause.Comparison {
case rpc.OpEqual:
// equal & notEqual don't need the langUnitTypeCheck because they are comparing the entire KGObject
// and that includes the lang & type
return clause.Literal1.Value.Equal
case rpc.OpNotEqual:
return not(clause.Literal1.Value.Equal)
case rpc.OpLessOrEqual:
return and(langUnitTypeCheck, cLessOrEqual(clause.Literal1.Value))
case rpc.OpLess:
return and(langUnitTypeCheck, cLess(clause.Literal1.Value))
case rpc.OpGreater:
return and(langUnitTypeCheck, cGreater(clause.Literal1.Value))
case rpc.OpGreaterOrEqual:
return and(langUnitTypeCheck, cGreaterOrEqual(clause.Literal1.Value))
case rpc.OpPrefix:
return and(langUnitTypeCheck, cPrefix(clause.Literal1.Value))
case rpc.OpRangeIncInc:
return and(langUnitTypeCheck, cRange(rInc, rInc, clause.Literal1.Value, clause.Literal2.Value))
case rpc.OpRangeIncExc:
return and(langUnitTypeCheck, cRange(rInc, rExc, clause.Literal1.Value, clause.Literal2.Value))
case rpc.OpRangeExcInc:
return and(langUnitTypeCheck, cRange(rExc, rInc, clause.Literal1.Value, clause.Literal2.Value))
case rpc.OpRangeExcExc:
return and(langUnitTypeCheck, cRange(rExc, rExc, clause.Literal1.Value, clause.Literal2.Value))
}
panic(fmt.Sprintf("Unexpected comparison type in buildComparer %d", clause.Comparison))
}
type rangeType int
const (
rInc rangeType = iota
rExc
)
func cLessOrEqual(to rpc.KGObject) comparer {
return func(v rpc.KGObject) bool {
return v.Less(to) || v.Equal(to)
}
}
func cLess(to rpc.KGObject) comparer {
return func(v rpc.KGObject) bool {
return v.Less(to)
}
}
func cGreater(to rpc.KGObject) comparer {
return func(v rpc.KGObject) bool {
return to.Less(v)
}
}
func cGreaterOrEqual(to rpc.KGObject) comparer {
return func(v rpc.KGObject) bool {
return to.Less(v) || to.Equal(v)
}
}
func cPrefix(to rpc.KGObject) comparer {
prefix := to.ValString()
return func(v rpc.KGObject) bool {
return strings.HasPrefix(v.ValString(), prefix)
}
}
func cRange(startType, endType rangeType, lowerBound rpc.KGObject, upperBound rpc.KGObject) comparer {
// upper & lower bounds better be the same type
if lowerBound.ValueType() != upperBound.ValueType() {
panic(fmt.Sprintf("qexec.cRange was passed lower/upper bounds of different types %d/%d", lowerBound.ValueType(), upperBound.ValueType()))
}
var lowerCmp comparer
var upperCmp comparer
switch startType {
case rInc:
lowerCmp = cGreaterOrEqual(lowerBound)
case rExc:
lowerCmp = cGreater(lowerBound)
}
switch endType {
case rInc:
upperCmp = cLessOrEqual(upperBound)
case rExc:
upperCmp = cLess(upperBound)
}
return and(lowerCmp, upperCmp)
}
// selectVarCompareOp returns a function that returns true if the supplied
// FactSet passes the selection criteria.
func selectVarCompareOp(op *plandef.SelectVar, cols Columns) func(row []Value) bool {
if op.Operator != rpc.OpEqual && op.Operator != rpc.OpNotEqual {
panic(fmt.Sprintf("selectVarCompareOp operator '%v' not supported", op.Operator))
}
leftIdx, exists := cols.IndexOf(op.Left)
if !exists {
panic(fmt.Sprintf("selectOp is trying to use variable %s for the Left side, but that's not available (have %s)", op.Left, cols))
}
rightIdx, exists := cols.IndexOf(op.Right)
if !exists {
panic(fmt.Sprintf("selectOp is trying to use variable %s for the Right side, but that's not available (has %s)", op.Left, cols))
}
return func(row []Value) bool {
left := row[leftIdx].KGObject
right := row[rightIdx].KGObject
switch op.Operator {
case rpc.OpEqual:
return left.Equal(right)
case rpc.OpNotEqual:
return !left.Equal(right)
}
panic("not reachable")
}
}