/
in.go
144 lines (117 loc) · 3.84 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
// 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/Ciyfly/FakerMysql/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))
}