From 9743fefe09feebfba7a141543e9a215846800974 Mon Sep 17 00:00:00 2001 From: Rebecca Taft Date: Thu, 9 Jul 2020 11:33:45 -0500 Subject: [PATCH] opt: fix error in case of casted NULL arguments to AddGeometryColumn This commit fixes an error that occurred when AddGeometryColumn was called with NULL arguments that were cast to the type specified by the function signature. #50992 already fixed the case when AddGeometryColumn was called with bare NULL arguments, since those were detected by TypeCheck. TypeCheck does not detect NULL arguments if they are cast to the correct type. This commit fixes the error by adding an explicit check in the optbuilder that each argument is not null before calling the SQLFn of the AddGeometryColumn overload. Informs #50296 Release note (bug fix): Fixed an internal error that occurred when AddGeometryColumn was called with NULL arguments. This now results in a no-op and returns NULL. --- pkg/sql/opt/exec/execbuilder/testdata/sql_fn | 5 +++++ pkg/sql/opt/optbuilder/sql_fn.go | 3 +++ 2 files changed, 8 insertions(+) diff --git a/pkg/sql/opt/exec/execbuilder/testdata/sql_fn b/pkg/sql/opt/exec/execbuilder/testdata/sql_fn index a303661672aa..5bed65bee121 100644 --- a/pkg/sql/opt/exec/execbuilder/testdata/sql_fn +++ b/pkg/sql/opt/exec/execbuilder/testdata/sql_fn @@ -248,3 +248,8 @@ query T SELECT addgeometrycolumn('a', 'b', 3, NULL, 2); ---- NULL + +query T +SELECT addgeometrycolumn('a', 'b', NULL::string, 'c', 9223372036854775807:::INT8, 'd', NULL::int) +---- +NULL diff --git a/pkg/sql/opt/optbuilder/sql_fn.go b/pkg/sql/opt/optbuilder/sql_fn.go index 1027e56c610b..d6981534e0ce 100644 --- a/pkg/sql/opt/optbuilder/sql_fn.go +++ b/pkg/sql/opt/optbuilder/sql_fn.go @@ -62,6 +62,9 @@ func (b *Builder) buildSQLFn( )) } exprs[i] = memo.ExtractConstDatum(info.args[i]) + if exprs[i] == tree.DNull && !info.def.Properties.NullableArgs { + return b.factory.ConstructNull(info.ResolvedType()) + } } // Get the SQL statement and parse it.