Skip to content
This repository was archived by the owner on Feb 21, 2024. It is now read-only.

Commit f12587f

Browse files
committed
handle null results gracefully in SetContains{Any,All}
SetContains returns null if either of the values it's given is null. SetContainsAny and SetContainsAll should also do this.
1 parent 2b4d49e commit f12587f

2 files changed

Lines changed: 53 additions & 0 deletions

File tree

sql3/planner/inbuiltfunctionsset.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,11 @@ func (n *callPlanExpression) EvaluateSetContainsAny(currentRow []interface{}) (i
6969
return nil, err
7070
}
7171

72+
//if either term is null, then null
73+
if testSetEval == nil || targetSetEval == nil {
74+
return nil, nil
75+
}
76+
7277
if targetSetEval != nil {
7378
switch typ := n.args[0].Type().(type) {
7479
case *parser.DataTypeStringSet:
@@ -116,6 +121,11 @@ func (n *callPlanExpression) EvaluateSetContainsAll(currentRow []interface{}) (i
116121
return nil, err
117122
}
118123

124+
//if either term is null, then null
125+
if testSetEval == nil || targetSetEval == nil {
126+
return nil, nil
127+
}
128+
119129
if targetSetEval != nil {
120130
switch typ := n.args[0].Type().(type) {
121131
case *parser.DataTypeStringSet:

sql3/test/defs/defs_set_functions.go

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,8 @@ var setFunctionTests = TableTest{
146146
name: "set-contains-int",
147147
SQLs: sqls(
148148
"select * from selectwithset where setcontains(ievent, 101)",
149+
"select * from selectwithset where setcontainsany(ievent, [101])",
150+
"select * from selectwithset where setcontainsall(ievent, [101])",
149151
),
150152
ExpHdrs: hdrs(
151153
hdr("_id", fldTypeID),
@@ -159,6 +161,23 @@ var setFunctionTests = TableTest{
159161
),
160162
Compare: CompareExactUnordered,
161163
},
164+
{
165+
// SetContainsInt
166+
name: "set-contains-int-using-value",
167+
SQLs: sqls(
168+
"select _id, setcontainsany(ievent, [101]) from selectwithset",
169+
),
170+
ExpHdrs: hdrs(
171+
hdr("_id", fldTypeID),
172+
hdr("", fldTypeBool),
173+
),
174+
ExpRows: rows(
175+
row(int64(1), true),
176+
row(int64(2), nil),
177+
row(int64(3), nil),
178+
),
179+
Compare: CompareExactUnordered,
180+
},
162181
{
163182
// SetContainsOrSetContains
164183
// SetContainsAny
@@ -227,6 +246,30 @@ var setFunctionTests = TableTest{
227246
),
228247
ExpErr: "types 'stringset' and 'stringset' are not equatable",
229248
},
249+
{
250+
// SetContainsWrongTypeSet
251+
name: "set-contains-null-in-values",
252+
SQLs: sqls(
253+
"select * from selectwithset where setcontains(event, [null])",
254+
),
255+
ExpErr: "set literal must contain ints or strings",
256+
},
257+
{
258+
// SetContainsWrongTypeSet
259+
name: "set-contains-null-value",
260+
SQLs: sqls(
261+
"select * from selectwithset where setcontains(event, null)",
262+
),
263+
ExpErr: "types 'stringset' and 'void' are not equatable",
264+
},
265+
{
266+
// SetContainsWrongTypeSet
267+
name: "set-contains-null-set",
268+
SQLs: sqls(
269+
"select * from selectwithset where setcontains(null, [1])",
270+
),
271+
ExpErr: "set expression expected",
272+
},
230273
},
231274
}
232275

0 commit comments

Comments
 (0)