Skip to content

Commit

Permalink
sql: fix ColumnAccessExpr.Eval with NULL inner expression
Browse files Browse the repository at this point in the history
Previously, `ColumnAccessExpr.Eval` would panic if the
`ColumnAccessExpr`'s inner expression evaluated to `NULL`, because it
attempted to cast this `NULL` to a `DTuple`. Now, if the inner
expression is `NULL`, `ColumnAccessExpr.Eval` returns `NULL`.

Fixes #78159

Release note (bug fix): A bug has been fixed that caused an internal
error when the inner expression of a column access expression evaluated
to `NULL`. For example, evaluation of the expression
`(CASE WHEN b THEN ((ROW(1) AS a)) ELSE NULL END).a` would error when
`b` is `false`. This bug has been present since version 19.1 or earlier.
  • Loading branch information
mgartner committed Mar 24, 2022
1 parent 70dd5ca commit 84db765
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 0 deletions.
15 changes: 15 additions & 0 deletions pkg/sql/logictest/testdata/logic_test/tuple
Original file line number Diff line number Diff line change
Expand Up @@ -978,3 +978,18 @@ CREATE TABLE t74729 AS SELECT g % 2 = 1 AS _bool FROM generate_series(1, 5) AS g

statement ok
SELECT CASE WHEN _bool THEN (1, ('a', 2)) ELSE (3, NULL) END FROM t74729

# Regression test for #78159. Column access of NULL should not cause an internal
# error.
subtest 78159

statement ok
CREATE TABLE t78159 (b BOOL)

statement ok
INSERT INTO t78159 VALUES (false)

query B
SELECT (CASE WHEN b THEN ((ROW(1) AS a)) ELSE NULL END).a from t78159
----
NULL
3 changes: 3 additions & 0 deletions pkg/sql/sem/tree/eval.go
Original file line number Diff line number Diff line change
Expand Up @@ -3927,6 +3927,9 @@ func (expr *ColumnAccessExpr) Eval(ctx *EvalContext) (Datum, error) {
if err != nil {
return nil, err
}
if d == DNull {
return d, nil
}
return d.(*DTuple).D[expr.ColIndex], nil
}

Expand Down

0 comments on commit 84db765

Please sign in to comment.