-
Notifications
You must be signed in to change notification settings - Fork 0
/
in.go
executable file
·252 lines (203 loc) · 6.21 KB
/
in.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
// Copyright 2020-2021 Dolthub, 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,
// 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 expression
import (
"fmt"
"github.com/cespare/xxhash"
"github.com/Rock-liyi/p2pdb-store/sql"
)
// InTuple is an expression that checks an expression is inside a list of expressions.
type InTuple struct {
BinaryExpression
}
// We implement Comparer because we have a Left() and a Right(), but we can't be Compare()d
var _ Comparer = (*InTuple)(nil)
func (in *InTuple) Compare(ctx *sql.Context, row sql.Row) (int, error) {
panic("Compare not implemented for InTuple")
}
func (in *InTuple) Type() sql.Type {
return sql.Boolean
}
func (in *InTuple) Left() sql.Expression {
return in.BinaryExpression.Left
}
func (in *InTuple) Right() sql.Expression {
return in.BinaryExpression.Right
}
// NewInTuple creates an InTuple expression.
func NewInTuple(left sql.Expression, right sql.Expression) *InTuple {
return &InTuple{BinaryExpression{left, right}}
}
// Eval implements the Expression interface.
func (in *InTuple) Eval(ctx *sql.Context, row sql.Row) (interface{}, error) {
typ := in.Left().Type().Promote()
leftElems := sql.NumColumns(typ)
left, err := in.Left().Eval(ctx, row)
if err != nil {
return nil, err
}
if left == nil {
return nil, nil
}
// The NULL handling for IN expressions is tricky. According to
// https://dev.mysql.com/doc/refman/8.0/en/comparison-operators.html#operator_in:
// To comply with the SQL standard, IN() returns NULL not only if the expression on the left hand side is NULL, but
// also if no match is found in the list and one of the expressions in the list is NULL.
rightNull := false
left, err = typ.Convert(left)
if err != nil {
return nil, err
}
switch right := in.Right().(type) {
case Tuple:
for _, el := range right {
if sql.NumColumns(el.Type()) != leftElems {
return nil, sql.ErrInvalidOperandColumns.New(leftElems, sql.NumColumns(el.Type()))
}
}
for _, el := range right {
right, err := el.Eval(ctx, row)
if err != nil {
return nil, err
}
if !rightNull && right == nil {
rightNull = true
continue
}
right, err = typ.Convert(right)
if err != nil {
return nil, err
}
cmp, err := typ.Compare(left, right)
if err != nil {
return nil, err
}
if cmp == 0 {
return true, nil
}
}
if rightNull {
return nil, nil
}
return false, nil
default:
return nil, ErrUnsupportedInOperand.New(right)
}
}
// WithChildren implements the Expression interface.
func (in *InTuple) WithChildren(children ...sql.Expression) (sql.Expression, error) {
if len(children) != 2 {
return nil, sql.ErrInvalidChildrenNumber.New(in, len(children), 2)
}
return NewInTuple(children[0], children[1]), nil
}
func (in *InTuple) String() string {
return fmt.Sprintf("(%s IN %s)", in.Left(), in.Right())
}
func (in *InTuple) DebugString() string {
return fmt.Sprintf("(%s IN %s)", sql.DebugString(in.Left()), sql.DebugString(in.Right()))
}
// Children implements the Expression interface.
func (in *InTuple) Children() []sql.Expression {
return []sql.Expression{in.Left(), in.Right()}
}
// NewNotInTuple creates a new NotInTuple expression.
func NewNotInTuple(left sql.Expression, right sql.Expression) sql.Expression {
return NewNot(NewInTuple(left, right))
}
// HashInTuple is an expression that checks an expression is inside a list of expressions using a hashmap.
type HashInTuple struct {
InTuple
cmp map[uint64]sql.Expression
hasNull bool
}
var _ Comparer = (*InTuple)(nil)
// NewHashInTuple creates an InTuple expression.
func NewHashInTuple(left, right sql.Expression) (*HashInTuple, error) {
rightTup, ok := right.(Tuple)
if !ok {
return nil, ErrUnsupportedInOperand.New(right)
}
cmp, hasNull, err := newInMap(rightTup, left.Type())
if err != nil {
return nil, err
}
return &HashInTuple{InTuple: *NewInTuple(left, right), cmp: cmp, hasNull: hasNull}, nil
}
// newInMap hashes static expressions in the right child Tuple of a InTuple node
func newInMap(right Tuple, lType sql.Type) (map[uint64]sql.Expression, bool, error) {
if lType == sql.Null {
return nil, true, nil
}
elements := make(map[uint64]sql.Expression)
hasNull := false
for _, el := range right {
if el.Type() == sql.Null {
hasNull = true
}
i, err := el.Eval(sql.NewEmptyContext(), sql.Row{})
if err != nil {
return nil, hasNull, err
}
key, err := hashOfSimple(i, lType)
if err != nil {
return nil, hasNull, sql.ErrInvalidOperandColumns.New(el, sql.NumColumns(lType))
}
elements[key] = el
}
return elements, hasNull, nil
}
func hashOfSimple(i interface{}, t sql.Type) (uint64, error) {
hash := xxhash.New()
x, err := t.Promote().Convert(i)
if err != nil {
return 0, sql.ErrInvalidType.New(i)
}
if _, err := hash.Write([]byte(fmt.Sprintf("%#v,", x))); err != nil {
return 0, err
}
return hash.Sum64(), nil
}
// Eval implements the Expression interface.
func (hit *HashInTuple) Eval(ctx *sql.Context, row sql.Row) (interface{}, error) {
if hit.hasNull {
return nil, nil
}
leftElems := sql.NumColumns(hit.Left().Type().Promote())
leftVal, err := hit.Left().Eval(ctx, row)
if err != nil {
return nil, err
}
if leftVal == nil {
return nil, nil
}
key, err := hashOfSimple(leftVal, hit.Left().Type())
if err != nil {
return nil, err
}
right, ok := hit.cmp[key]
if !ok {
return false, nil
}
if sql.NumColumns(right.Type().Promote()) != leftElems {
return nil, sql.ErrInvalidOperandColumns.New(leftElems, sql.NumColumns(right.Type().Promote()))
}
return true, nil
}
func (hit *HashInTuple) String() string {
return fmt.Sprintf("(%s HASH IN %s)", hit.Left(), hit.Right())
}
func (hit *HashInTuple) DebugString() string {
return fmt.Sprintf("(%s HASH IN %s)", sql.DebugString(hit.Left()), sql.DebugString(hit.Right()))
}