Skip to content

Commit cb6bd5b

Browse files
authored
vector.Not: Set true/null to false/null (#6471)
Closes #6431
1 parent a43032d commit cb6bd5b

File tree

3 files changed

+32
-1
lines changed

3 files changed

+32
-1
lines changed

runtime/vam/expr/logic.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,11 @@ func (n *Not) Eval(val vector.Any) vector.Any {
2727
func (n *Not) eval(vecs ...vector.Any) vector.Any {
2828
switch vec := vecs[0].(type) {
2929
case *vector.Bool:
30-
return vector.NewBool(bitvec.Not(vec.Bits), vec.Nulls)
30+
return vector.Not(vec)
3131
case *vector.Const:
32+
if vec.Value().IsNull() {
33+
return vec
34+
}
3235
return vector.NewConst(super.NewBool(!vec.Value().Bool()), vec.Len(), vec.Nulls)
3336
case *vector.Error:
3437
return vec

runtime/ztests/expr/logical-not.yaml

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,21 @@ output: |
1515
null::bool
1616
error("missing")
1717
error("foo")
18+
19+
---
20+
21+
# Ensure true/null values are converted to false/null.
22+
spq: |
23+
values false or not this
24+
25+
vector: true
26+
27+
input: |
28+
true
29+
false
30+
null::bool
31+
32+
output: |
33+
false
34+
true
35+
null::bool

vector/bool.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,16 @@ func Or(a, b *Bool) *Bool {
6464
return NewBool(bits, nulls)
6565
}
6666

67+
func Not(vec *Bool) *Bool {
68+
bits := bitvec.Not(vec.Bits)
69+
if vec.Nulls.IsZero() {
70+
return NewBool(bits, bitvec.Zero)
71+
}
72+
// Flip true/null values to false/null.
73+
bits = bitvec.And(bits, bitvec.Not(vec.Nulls))
74+
return NewBool(bits, vec.Nulls)
75+
}
76+
6777
// BoolValue returns the value of slot in vec if the value is a Boolean. It
6878
// returns false otherwise.
6979
func BoolValue(vec Any, slot uint32) (bool, bool) {

0 commit comments

Comments
 (0)