forked from pingcap/tidb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathin.go
180 lines (148 loc) · 3.98 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
// Copyright 2013 The ql Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSES/QL-LICENSE file.
// Copyright 2015 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 (
"fmt"
"strings"
"github.com/juju/errors"
"github.com/pingcap/tidb/context"
"github.com/pingcap/tidb/util/types"
)
var (
_ Expression = (*PatternIn)(nil)
)
// PatternIn is the expression for in operator, like "expr in (1, 2, 3)" or "expr in (select c from t)".
type PatternIn struct {
// Expr is the value expression to be compared.
Expr Expression
// List is the list expression in compare list.
List []Expression
// Not is true, the expression is "not in".
Not bool
// Sel is the sub query.
Sel SubQuery
}
// Clone implements the Expression Clone interface.
func (n *PatternIn) Clone() Expression {
expr := n.Expr.Clone()
list := cloneExpressionList(n.List)
return &PatternIn{
Expr: expr,
List: list,
Not: n.Not,
Sel: n.Sel,
}
}
// IsStatic implements the Expression IsStatic interface.
func (n *PatternIn) IsStatic() bool {
if !n.Expr.IsStatic() || n.Sel != nil {
return false
}
for _, v := range n.List {
if !v.IsStatic() {
return false
}
}
return true
}
// String implements the Expression String interface.
func (n *PatternIn) String() string {
if n.Sel == nil {
a := []string{}
for _, v := range n.List {
a = append(a, v.String())
}
if n.Not {
return fmt.Sprintf("%s NOT IN (%s)", n.Expr, strings.Join(a, ","))
}
return fmt.Sprintf("%s IN (%s)", n.Expr, strings.Join(a, ","))
}
if n.Not {
return fmt.Sprintf("%s NOT IN %s", n.Expr, n.Sel)
}
return fmt.Sprintf("%s IN %s", n.Expr, n.Sel)
}
func (n *PatternIn) checkInList(in interface{}, list []interface{}) (interface{}, error) {
hasNull := false
for _, v := range list {
if v == nil {
hasNull = true
continue
}
r, err := types.Compare(in, v)
if err != nil {
return nil, err
}
if r == 0 {
return !n.Not, nil
}
}
if hasNull {
// if no matched but we got null in In, return null
// e.g 1 in (null, 2, 3) returns null
return nil, nil
}
return n.Not, nil
}
func evalExprList(ctx context.Context, args map[interface{}]interface{}, list []Expression) ([]interface{}, error) {
var err error
values := make([]interface{}, len(list))
for i := range values {
values[i], err = list[i].Eval(ctx, args)
if err != nil {
return nil, errors.Trace(err)
}
}
return values, nil
}
// Eval implements the Expression Eval interface.
func (n *PatternIn) Eval(ctx context.Context, args map[interface{}]interface{}) (v interface{}, err error) {
lhs, err := n.Expr.Eval(ctx, args)
if err != nil {
return nil, err
}
if lhs == nil {
return nil, nil
}
if n.Sel == nil {
if err := hasSameColumnCount(ctx, n.Expr, n.List...); err != nil {
return nil, errors.Trace(err)
}
var values []interface{}
values, err = evalExprList(ctx, args, n.List)
if err != nil {
return nil, errors.Trace(err)
}
return n.checkInList(lhs, values)
}
if !n.Sel.UseOuterQuery() && n.Sel.Value() != nil {
return n.checkInList(lhs, n.Sel.Value().([]interface{}))
}
var res []interface{}
if err = hasSameColumnCount(ctx, n.Expr, n.Sel); err != nil {
return nil, errors.Trace(err)
}
res, err = n.Sel.EvalRows(ctx, args, -1)
if err != nil {
return nil, errors.Trace(err)
}
n.Sel.SetValue(res)
return n.checkInList(lhs, res)
}
// Accept implements Expression Accept interface.
func (n *PatternIn) Accept(v Visitor) (Expression, error) {
return v.VisitPatternIn(n)
}