From c0408b25c6eb575ca30d0b5a4647e3e59f7cdab5 Mon Sep 17 00:00:00 2001 From: Jane Xing Date: Wed, 7 Sep 2022 23:53:20 -0500 Subject: [PATCH] sql: fix `is_generated` in `information_schema.column` table Previously, the `is_generated` column returns the string `YES` or `NO`, depending on whether the column is computed. While postgres returns `ALWAYS` and `NEVER`. This commit is to fix it. fixes #87481 Release note (bug fix): fix `is_generated` in `information_schema.column` table --- pkg/sql/information_schema.go | 19 +++++++++++----- .../testdata/logic_test/information_schema | 22 ++++++++++++++++--- .../schemachange/operation_generator.go | 4 ++-- 3 files changed, 35 insertions(+), 10 deletions(-) diff --git a/pkg/sql/information_schema.go b/pkg/sql/information_schema.go index 33adce65f1d1..0adb5e2f7c54 100644 --- a/pkg/sql/information_schema.go +++ b/pkg/sql/information_schema.go @@ -173,8 +173,10 @@ var ( // STRINGs. The BOOLEAN data type should NEVER be used in information_schema // tables. Instead, define columns as STRINGs and map bools to STRINGs using // yesOrNoDatum. - yesString = tree.NewDString("YES") - noString = tree.NewDString("NO") + yesString = tree.NewDString("YES") + noString = tree.NewDString("NO") + alwaysString = tree.NewDString("ALWAYS") + neverString = tree.NewDString("NEVER") ) func yesOrNoDatum(b bool) tree.Datum { @@ -184,6 +186,13 @@ func yesOrNoDatum(b bool) tree.Datum { return noString } +func alwaysOrNeverDatum(b bool) tree.Datum { + if b { + return alwaysString + } + return neverString +} + func dNameOrNull(s string) tree.Datum { if s == "" { return tree.DNull @@ -543,9 +552,9 @@ https://www.postgresql.org/docs/9.5/infoschema-columns.html`, identityMin, // identity_minimum // TODO(janexing): we don't support CYCLE syntax for sequences yet. // https://github.com/cockroachdb/cockroach/issues/20961 - tree.DNull, // identity_cycle - yesOrNoDatum(column.IsComputed()), // is_generated - colComputed, // generation_expression + tree.DNull, // identity_cycle + alwaysOrNeverDatum(column.IsComputed()), // is_generated + colComputed, // generation_expression yesOrNoDatum(table.IsTable() && !table.IsVirtualTable() && !column.IsComputed(), diff --git a/pkg/sql/logictest/testdata/logic_test/information_schema b/pkg/sql/logictest/testdata/logic_test/information_schema index a3d8b4438157..0e5b9ae96c14 100644 --- a/pkg/sql/logictest/testdata/logic_test/information_schema +++ b/pkg/sql/logictest/testdata/logic_test/information_schema @@ -2430,9 +2430,9 @@ FROM information_schema.columns WHERE table_schema = 'public' AND table_name = 'computed' ---- column_name is_generated generation_expression is_updatable -a NO · YES -b YES a + 1 NO -rowid NO · YES +a NEVER · YES +b ALWAYS a + 1 NO +rowid NEVER · YES statement ok CREATE TABLE generated_as_identity ( @@ -5122,3 +5122,19 @@ query TTTTT select identity_start, identity_increment, identity_maximum, identity_minimum, identity_cycle from information_schema.columns where table_name = 't' and column_name='id1'; ---- 10 1 9223372036854775807 1 NULL + +subtest generated_as_cols + +statement ok +DROP TABLE IF EXISTS t + +statement ok +CREATE TABLE t (i INTEGER PRIMARY KEY, x VARCHAR, y VARCHAR, z VARCHAR GENERATED ALWAYS AS (x || ' ' || y) STORED); + +query TT +SELECT column_name, is_generated FROM information_schema.columns WHERE table_name = 't'; +---- +i NEVER +x NEVER +y NEVER +z ALWAYS diff --git a/pkg/workload/schemachange/operation_generator.go b/pkg/workload/schemachange/operation_generator.go index 78301332b18d..f4ca24d89ad6 100644 --- a/pkg/workload/schemachange/operation_generator.go +++ b/pkg/workload/schemachange/operation_generator.go @@ -2791,9 +2791,9 @@ func (og *operationGenerator) randChildColumnForFkRelation( `, typ)) if isNotComputed { - query.WriteString(`AND is_generated = 'NO'`) + query.WriteString(`AND is_generated = 'NEVER'`) } else { - query.WriteString(`AND is_generated = 'YES'`) + query.WriteString(`AND is_generated = 'ALWAYS'`) } var tableSchema string