forked from pingcap/tidb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcmp_subquery.go
175 lines (147 loc) · 4.26 KB
/
cmp_subquery.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
// 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"
"github.com/juju/errors"
"github.com/pingcap/tidb/context"
"github.com/pingcap/tidb/parser/opcode"
"github.com/pingcap/tidb/util/types"
)
// CompareSubQuery is the expression for "expr cmp (select ...)".
// See: https://dev.mysql.com/doc/refman/5.7/en/comparisons-using-subqueries.html
// See: https://dev.mysql.com/doc/refman/5.7/en/any-in-some-subqueries.html
// See: https://dev.mysql.com/doc/refman/5.7/en/all-subqueries.html
type CompareSubQuery struct {
// L is the left expression
L Expression
// Op is the comparison opcode.
Op opcode.Op
// R is the sub query for right expression.
R SubQuery
// All is true, we should compare all records in subquery.
All bool
}
// Clone implements the Expression Clone interface.
func (cs *CompareSubQuery) Clone() Expression {
l := cs.L.Clone()
r := cs.R.Clone()
return &CompareSubQuery{L: l, Op: cs.Op, R: r.(SubQuery), All: cs.All}
}
// IsStatic implements the Expression IsStatic interface.
func (cs *CompareSubQuery) IsStatic() bool {
return cs.L.IsStatic() && cs.R.IsStatic()
}
// String implements the Expression String interface.
func (cs *CompareSubQuery) String() string {
anyOrAll := "ANY"
if cs.All {
anyOrAll = "ALL"
}
return fmt.Sprintf("%s %s %s %s", cs.L, cs.Op, anyOrAll, cs.R)
}
// Eval implements the Expression Eval interface.
func (cs *CompareSubQuery) Eval(ctx context.Context, args map[interface{}]interface{}) (interface{}, error) {
if err := hasSameColumnCount(ctx, cs.L, cs.R); err != nil {
return nil, errors.Trace(err)
}
lv, err := cs.L.Eval(ctx, args)
if err != nil {
return nil, errors.Trace(err)
}
if lv == nil {
return nil, nil
}
if !cs.R.UseOuterQuery() && cs.R.Value() != nil {
return cs.checkResult(lv, cs.R.Value().([]interface{}))
}
res, err := cs.R.EvalRows(ctx, args, -1)
if err != nil {
return nil, errors.Trace(err)
}
cs.R.SetValue(res)
return cs.checkResult(lv, cs.R.Value().([]interface{}))
}
// Accept implements Expression Accept interface.
func (cs *CompareSubQuery) Accept(v Visitor) (Expression, error) {
return v.VisitCompareSubQuery(cs)
}
func (cs *CompareSubQuery) checkAllResult(lv interface{}, result []interface{}) (interface{}, error) {
hasNull := false
for _, v := range result {
if v == nil {
hasNull = true
continue
}
comRes, err := types.Compare(lv, v)
if err != nil {
return nil, errors.Trace(err)
}
res, err := getCompResult(cs.Op, comRes)
if err != nil {
return nil, errors.Trace(err)
}
if !res {
return false, nil
}
}
if hasNull {
// If no matched but we get null, return null.
// Like `insert t (c) values (1),(2),(null)`, then
// `select 3 > all (select c from t)`, returns null.
return nil, nil
}
return true, nil
}
func (cs *CompareSubQuery) checkAnyResult(lv interface{}, result []interface{}) (interface{}, error) {
hasNull := false
for _, v := range result {
if v == nil {
hasNull = true
continue
}
comRes, err := types.Compare(lv, v)
if err != nil {
return nil, errors.Trace(err)
}
res, err := getCompResult(cs.Op, comRes)
if err != nil {
return nil, errors.Trace(err)
}
if res {
return true, nil
}
}
if hasNull {
// If no matched but we get null, return null.
// Like `insert t (c) values (1),(2),(null)`, then
// `select 0 > any (select c from t)`, returns null.
return nil, nil
}
return false, nil
}
func (cs *CompareSubQuery) checkResult(lv interface{}, result []interface{}) (interface{}, error) {
if cs.All {
return cs.checkAllResult(lv, result)
}
return cs.checkAnyResult(lv, result)
}
// NewCompareSubQuery creates a CompareSubQuery object.
func NewCompareSubQuery(op opcode.Op, lhs Expression, rhs SubQuery, all bool) *CompareSubQuery {
return &CompareSubQuery{
Op: op,
L: lhs,
R: rhs,
All: all,
}
}