Skip to content

Commit

Permalink
colexec: fix projections with constant NULL values
Browse files Browse the repository at this point in the history
This commit fixes a bug in the vectorized engine that caused incorrect
results when projections operated on constant NULL values. The
`proj_const_right_ops` operators do not correctly handle NULL inputs.
So, we avoid these code paths when operator does not operate on NULL
values and the RHS of an operator is NULL by simply projecting NULLs.

Fixes cockroachdb#127814

Release note (bug fix): A bug has been fixed that could cause incorrect
evaluation of scalar expressions involving `NULL` values in rare cases.
  • Loading branch information
mgartner committed Oct 18, 2024
1 parent 95d1979 commit 07835e1
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 2 deletions.
8 changes: 7 additions & 1 deletion pkg/sql/colexec/colbuilder/execplan.go
Original file line number Diff line number Diff line change
Expand Up @@ -2716,6 +2716,7 @@ func planProjectionExpr(
resultIdx = len(typs)
// The projection result will be outputted to a new column which is
// appended to the input batch.
// TODO(#127814): We may need to handle the case when the left is DNull.
op, err = colexecprojconst.GetProjectionLConstOperator(
allocator, typs, left.ResolvedType(), outputType, projOp, input,
rightIdx, lConstArg, resultIdx, evalCtx, binOp, cmpExpr, calledOnNullInput,
Expand Down Expand Up @@ -2758,7 +2759,12 @@ func planProjectionExpr(
// The projection result will be outputted to a new column which is
// appended to the input batch.
resultIdx = len(typs)
if isCmpProjOp {
if !calledOnNullInput && right == tree.DNull {
// If the right is NULL and the operator is not called on NULL,
// simply project NULL.
op = colexecbase.NewConstNullOp(allocator, input, resultIdx)
} else if isCmpProjOp {
// Use optimized operators for special cases.
switch cmpProjOp.Symbol {
case treecmp.Like, treecmp.NotLike, treecmp.ILike, treecmp.NotILike:
negate, caseInsensitive := examineLikeOp(cmpProjOp)
Expand Down
16 changes: 15 additions & 1 deletion pkg/sql/logictest/testdata/logic_test/subquery
Original file line number Diff line number Diff line change
Expand Up @@ -1039,4 +1039,18 @@ LEFT JOIN LATERAL (
row-1 row-1 {row-1} 1
row-2 row-2 {row-2} 1

subtest end
# Regression test for #127814. The vectorized engine should correctly project
# expressions operating on NULL.
statement ok
CREATE TABLE t127814_empty (i INT)

statement ok
CREATE TABLE t127814 (o OID)

statement ok
INSERT INTO t127814 VALUES (0)

query B
SELECT o NOT IN ((SELECT NULL FROM t127814_empty),) FROM t127814
----
NULL

0 comments on commit 07835e1

Please sign in to comment.