From a22f0b83d15c7981e4b88f226830bad0f1aa148f Mon Sep 17 00:00:00 2001 From: Marcus Gartner Date: Mon, 18 May 2020 20:19:29 -0700 Subject: [PATCH 1/3] sql: add tests for IS (NOT) NULL with empty tuples Logic for `IS NULL` and `IS NOT NULL` with tuple operands was recently fixed in #48299. This commit adds tests for empty tuples as operands that were forgotten in that commit. This makes the correct evaluation logic of such an expression explicit and will help prevent regressions. Release note: None --- pkg/sql/opt/norm/fold_constants_funcs.go | 14 ++++++++------ pkg/sql/opt/norm/testdata/rules/comp | 20 ++++++++++++++++++++ pkg/sql/sem/tree/testdata/eval/is | 12 ++++++++++++ 3 files changed, 40 insertions(+), 6 deletions(-) diff --git a/pkg/sql/opt/norm/fold_constants_funcs.go b/pkg/sql/opt/norm/fold_constants_funcs.go index f7c055eda55f..1926bb479c1b 100644 --- a/pkg/sql/opt/norm/fold_constants_funcs.go +++ b/pkg/sql/opt/norm/fold_constants_funcs.go @@ -88,9 +88,10 @@ func (c *CustomFuncs) HasNullElement(input opt.ScalarExpr) bool { } // HasAllNullElements returns true if the input tuple has only constant, null -// elements. Note that it only returns true if all elements are known to be -// null. For example, given the tuple (NULL, x), it will return false because x -// is not guaranteed to be null. +// elements, or if the tuple is empty (has 0 elements). Note that it only +// returns true if all elements are known to be null. For example, given the +// tuple (NULL, x), it will return false because x is not guaranteed to be +// null. func (c *CustomFuncs) HasAllNullElements(input opt.ScalarExpr) bool { tup := input.(*memo.TupleExpr) for _, e := range tup.Elems { @@ -122,9 +123,10 @@ func (c *CustomFuncs) HasNonNullElement(input opt.ScalarExpr) bool { } // HasAllNonNullElements returns true if the input tuple has all constant, -// non-null elements. Note that it only returns true if all elements are known -// to be non-null. For example, given the tuple (1, x), it will return false -// because x is not guaranteed to be non-null. +// non-null elements, or if the tuple is empty (has 0 elements). Note that it +// only returns true if all elements are known to be non-null. For example, +// given the tuple (1, x), it will return false because x is not guaranteed to +// be non-null. func (c *CustomFuncs) HasAllNonNullElements(input opt.ScalarExpr) bool { tup := input.(*memo.TupleExpr) for _, e := range tup.Elems { diff --git a/pkg/sql/opt/norm/testdata/rules/comp b/pkg/sql/opt/norm/testdata/rules/comp index de9932ac148a..ca24a10975f2 100644 --- a/pkg/sql/opt/norm/testdata/rules/comp +++ b/pkg/sql/opt/norm/testdata/rules/comp @@ -363,6 +363,16 @@ values ├── fd: ()-->(1) └── (true,) +norm expect=FoldNullTupleIsTupleNull +SELECT () IS NULL AS r +---- +values + ├── columns: r:1!null + ├── cardinality: [1 - 1] + ├── key: () + ├── fd: ()-->(1) + └── (true,) + norm expect-not=FoldNullTupleIsTupleNull SELECT (k, NULL) IS NULL FROM a ---- @@ -535,6 +545,16 @@ values ├── fd: ()-->(1) └── (true,) +norm expect=FoldNonNullTupleIsTupleNotNull +SELECT () IS NOT NULL AS r +---- +values + ├── columns: r:1!null + ├── cardinality: [1 - 1] + ├── key: () + ├── fd: ()-->(1) + └── (true,) + norm expect-not=FoldNonNullTupleIsTupleNotNull SELECT (1, k) IS NOT NULL FROM a ---- diff --git a/pkg/sql/sem/tree/testdata/eval/is b/pkg/sql/sem/tree/testdata/eval/is index 93343c072250..878de7202e27 100644 --- a/pkg/sql/sem/tree/testdata/eval/is +++ b/pkg/sql/sem/tree/testdata/eval/is @@ -524,3 +524,15 @@ eval NOT ((NULL, NULL) IS DISTINCT FROM NULL) ---- false + +# Empty tuples. + +eval +() IS NULL +---- +true + +eval +() IS NOT NULL +---- +true From 7da3b8b31234eb2b5e69fc952870fb089bd10d41 Mon Sep 17 00:00:00 2001 From: John Sheaffer Date: Tue, 19 May 2020 11:20:02 -0700 Subject: [PATCH 2/3] authors: add John Sheaffer to authors file Release note: None --- AUTHORS | 1 + 1 file changed, 1 insertion(+) diff --git a/AUTHORS b/AUTHORS index dce4ac047411..ba761f2adc3e 100644 --- a/AUTHORS +++ b/AUTHORS @@ -144,6 +144,7 @@ Joel Kenny Joey Pereira joezxy Johan Brandhorst +John Sheaffer Jon Derryberry Jonas joowon From 0194583a682c506e8964e0dc96b25e543fc01b43 Mon Sep 17 00:00:00 2001 From: Alex Lunev Date: Tue, 19 May 2020 11:43:43 -0700 Subject: [PATCH 3/3] authors: Add alexl@ to AUTHORS file. Release note: None --- AUTHORS | 1 + 1 file changed, 1 insertion(+) diff --git a/AUTHORS b/AUTHORS index dce4ac047411..424205246fee 100644 --- a/AUTHORS +++ b/AUTHORS @@ -31,6 +31,7 @@ Aditya Maru Aid Idrizović Ajaya Agrawal Alex Gaynor +Alex Lunev Alex Robinson <@cockroachlabs.com> Alfonso Subiotto Marqués Alfonso Subiotto Marques Alyshan Jahani