From 459a4f3d9e4f996482867b403f02e8b474820ae7 Mon Sep 17 00:00:00 2001 From: Rafi Shamim Date: Wed, 27 Jul 2022 10:41:33 -0400 Subject: [PATCH] sql: allow NULL in create view definition Release note (sql change): CREATE VIEW statements can now have a constant NULL column definition. The resulting column is of type TEXT. --- pkg/sql/create_view.go | 6 +++++- pkg/sql/logictest/testdata/logic_test/views | 21 +++++++++++++++++++++ 2 files changed, 26 insertions(+), 1 deletion(-) diff --git a/pkg/sql/create_view.go b/pkg/sql/create_view.go index bebf5931993c..395513f92527 100644 --- a/pkg/sql/create_view.go +++ b/pkg/sql/create_view.go @@ -636,7 +636,11 @@ func addResultColumns( resultColumns colinfo.ResultColumns, ) error { for _, colRes := range resultColumns { - columnTableDef := tree.ColumnTableDef{Name: tree.Name(colRes.Name), Type: colRes.Typ} + colTyp := colRes.Typ + if colTyp.Family() == types.UnknownFamily { + colTyp = types.String + } + columnTableDef := tree.ColumnTableDef{Name: tree.Name(colRes.Name), Type: colTyp} // Nullability constraints do not need to exist on the view, since they are // already enforced on the source data. columnTableDef.Nullable.Nullability = tree.SilentNull diff --git a/pkg/sql/logictest/testdata/logic_test/views b/pkg/sql/logictest/testdata/logic_test/views index d4fd45f4aa76..dc6f6a78e532 100644 --- a/pkg/sql/logictest/testdata/logic_test/views +++ b/pkg/sql/logictest/testdata/logic_test/views @@ -1313,3 +1313,24 @@ DROP MATERIALIZED VIEW mv1 statement ok DROP MATERIALIZED VIEW mv2 + +statement ok +CREATE VIEW view_with_null AS SELECT 1 AS a, null AS c + +query TTBTTTB +SHOW COLUMNS FROM view_with_null +---- +a INT8 true NULL · {} false +c STRING true NULL · {} false + + +statement ok +CREATE MATERIALIZED VIEW materialized_view_with_null AS SELECT a, NULL AS b, b AS c FROM test_view + +query TTBTTTB +SHOW COLUMNS FROM materialized_view_with_null +---- +a INT8 true NULL · {materialized_view_with_null_pkey} false +b STRING true NULL · {materialized_view_with_null_pkey} false +c INT8 true NULL · {materialized_view_with_null_pkey} false +rowid INT8 false unique_rowid() · {materialized_view_with_null_pkey} true