Skip to content

Commit

Permalink
exec: explicitly check for nulls in selBoolOp
Browse files Browse the repository at this point in the history
Previously, we would copy the bool vector from boolVecToSelOp directly.
However, we also need to look at the null values so that nulls are
treated as false. Now this is fixed.

Release note: None
  • Loading branch information
yuzefovich committed Sep 9, 2019
1 parent 746d213 commit 7a382a2
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 1 deletion.
31 changes: 30 additions & 1 deletion pkg/sql/exec/bool_vec_to_sel.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,35 @@ func (d selBoolOp) Init() {

func (d selBoolOp) Next(ctx context.Context) coldata.Batch {
batch := d.input.Next(ctx)
d.boolVecToSelOp.outputCol = batch.ColVec(d.colIdx).Bool()
inputCol := batch.ColVec(d.colIdx)
d.boolVecToSelOp.outputCol = inputCol.Bool()
if inputCol.MaybeHasNulls() {
// If the input column has null values, we need to explicitly set the
// values of the output column that correspond to those null values to
// false. For example, doing the comparison 'NULL < 0' will put true into
// the boolean Vec (because NULLs are smaller than any integer) but will
// also set the null. In the code above, we only copied the values' vector,
// so we need to adjust it.
// TODO(yuzefovich): think through this case more, possibly clean this up.
outputCol := d.boolVecToSelOp.outputCol
n := batch.Length()
sel := batch.Selection()
nulls := inputCol.Nulls()
if sel != nil {
sel = sel[:n]
for _, i := range sel {
if nulls.NullAt(i) {
outputCol[i] = false
}
}
} else {
outputCol = outputCol[0:n]
for i := range outputCol {
if nulls.NullAt(uint16(i)) {
outputCol[i] = false
}
}
}
}
return batch
}
11 changes: 11 additions & 0 deletions pkg/sql/logictest/testdata/logic_test/vectorize
Original file line number Diff line number Diff line change
Expand Up @@ -835,3 +835,14 @@ SELECT * FROM t40372_1 NATURAL JOIN t40372_2
1 1 1 1
2 2 2 2
3 3 3 3

# Test that comparison against a null value selects the value out.
statement ok
CREATE TABLE tnull(a INT, b INT)

statement ok
INSERT INTO tnull VALUES(NULL, 238)

query I rowsort
SELECT a FROM tnull WHERE (a<=b OR a>=b)
----

0 comments on commit 7a382a2

Please sign in to comment.