Skip to content

Commit

Permalink
Merge #128123
Browse files Browse the repository at this point in the history
128123: colexec: fix projections with constant NULL values r=mgartner a=mgartner

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 #127814

Release note (bug fix): A bug has been fixed that could cause incorrect
evaluation of scalar expressions involving `NULL` values in rare cases.


Co-authored-by: Marcus Gartner <[email protected]>
  • Loading branch information
craig[bot] and mgartner committed Sep 20, 2024
2 parents a4c6966 + 07e2f9a commit 04783da
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 @@ -2727,6 +2727,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 @@ -2769,7 +2770,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 04783da

Please sign in to comment.