diff --git a/pkg/sql/logictest/testdata/logic_test/composite_types b/pkg/sql/logictest/testdata/logic_test/composite_types index d97e3d38ae63..9e4d3d84f642 100644 --- a/pkg/sql/logictest/testdata/logic_test/composite_types +++ b/pkg/sql/logictest/testdata/logic_test/composite_types @@ -64,7 +64,7 @@ query TT SHOW CREATE TABLE atyp ---- atyp CREATE TABLE public.atyp ( - a T[] NULL, + a public.t[] NULL, rowid INT8 NOT VISIBLE NOT NULL DEFAULT unique_rowid(), CONSTRAINT atyp_pkey PRIMARY KEY (rowid ASC) ) diff --git a/pkg/sql/logictest/testdata/logic_test/drop_function b/pkg/sql/logictest/testdata/logic_test/drop_function index 1000a941387b..2b3686edd192 100644 --- a/pkg/sql/logictest/testdata/logic_test/drop_function +++ b/pkg/sql/logictest/testdata/logic_test/drop_function @@ -205,7 +205,7 @@ CREATE FUNCTION f114677(v t114677_2) RETURNS INT LANGUAGE SQL AS $$ SELECT 1; $$ query T nosort SELECT create_statement FROM [SHOW CREATE FUNCTION f114677] ORDER BY create_statement; ---- -CREATE FUNCTION public.f114677(v T114677) +CREATE FUNCTION public.f114677(v public.t114677) RETURNS INT8 VOLATILE NOT LEAKPROOF @@ -214,7 +214,7 @@ CREATE FUNCTION public.f114677(v T114677) AS $$ SELECT 0; $$ -CREATE FUNCTION public.f114677(v T114677_2) +CREATE FUNCTION public.f114677(v public.t114677_2) RETURNS INT8 VOLATILE NOT LEAKPROOF @@ -233,7 +233,7 @@ DROP FUNCTION f114677(t114677); query T SELECT create_statement FROM [SHOW CREATE FUNCTION f114677]; ---- -CREATE FUNCTION public.f114677(v T114677_2) +CREATE FUNCTION public.f114677(v public.t114677_2) RETURNS INT8 VOLATILE NOT LEAKPROOF diff --git a/pkg/sql/logictest/testdata/logic_test/show_create b/pkg/sql/logictest/testdata/logic_test/show_create index c0f994c42745..05b2937836f2 100644 --- a/pkg/sql/logictest/testdata/logic_test/show_create +++ b/pkg/sql/logictest/testdata/logic_test/show_create @@ -198,3 +198,40 @@ SHOW CREATE INDEXES FROM nonexistent statement error relation "nonexistent" does not exist SHOW CREATE SECONDARY INDEXES FROM nonexistent + +subtest comp_type + +statement ok +CREATE SCHEMA SC1; + +statement ok +CREATE SCHEMA SC2; + +statement ok +CREATE TYPE SC1.COMP1 AS (A INT, B TEXT); + +statement ok +CREATE TYPE SC2.COMP1 AS (C SMALLINT, D BOOL); + +statement ok +CREATE TABLE T_WITH_COMPS (C1 INT PRIMARY KEY, SC1 SC1.COMP1, SC2 SC2.COMP1, FAMILY F1(C1, SC1, SC2)); + +query T +SELECT create_statement FROM [SHOW CREATE TABLE T_WITH_COMPS]; +---- +CREATE TABLE public.t_with_comps ( + c1 INT8 NOT NULL, + sc1 sc1.comp1 NULL, + sc2 sc2.comp1 NULL, + CONSTRAINT t_with_comps_pkey PRIMARY KEY (c1 ASC), + FAMILY f1 (c1, sc1, sc2) +) + +statement ok +DROP TABLE T_WITH_COMPS; +DROP TYPE SC1.COMP1; +DROP TYPE SC2.COMP1; +DROP SCHEMA SC1; +DROP SCHEMA SC2; + +subtest end diff --git a/pkg/sql/logictest/testdata/logic_test/tuple b/pkg/sql/logictest/testdata/logic_test/tuple index 8a9d6c42422c..ae4ba4cc77eb 100644 --- a/pkg/sql/logictest/testdata/logic_test/tuple +++ b/pkg/sql/logictest/testdata/logic_test/tuple @@ -1002,11 +1002,11 @@ statement error incompatible COALESCE expressions: expected \(SELECT \(1,\)\) to SELECT COALESCE((SELECT ()), (SELECT ROW (1))) FROM t40297 # Adding a cast here should still work too. -statement error incompatible COALESCE expressions: expected \(SELECT \(1,\)\)::T to be of type tuple, found type tuple{int AS x} +statement error incompatible COALESCE expressions: expected \(SELECT \(1,\)\)::t to be of type tuple, found type tuple{int AS x} SELECT COALESCE((SELECT ()), (SELECT ROW (1))::t) FROM t40297 # If a NULL is casted, then it no longer can be coerced to the right type. -statement error incompatible COALESCE expressions: expected NULL::T to be of type tuple, found type tuple{int AS x} +statement error incompatible COALESCE expressions: expected NULL::t to be of type tuple, found type tuple{int AS x} SELECT COALESCE((SELECT ()), NULL::t) FROM t40297 # Type-checking should still work for non-empty tuples. @@ -1015,7 +1015,7 @@ SELECT COALESCE((SELECT '(1)'::t), NULL::t) FROM t40297 ---- # (SELECT (1)) is interpreted as an INT. -statement error incompatible COALESCE expressions: expected NULL::T to be of type int, found type tuple{int AS x} +statement error incompatible COALESCE expressions: expected NULL::t to be of type int, found type tuple{int AS x} SELECT COALESCE((SELECT (1)), NULL::t) FROM t40297 # Adding ROW allows it to be interpreted as a tuple. @@ -1024,11 +1024,11 @@ SELECT COALESCE((SELECT ROW (1)), NULL::t) FROM t40297 ---- # An anonymous tuple type only works if it is used with a tuple of the correct length. -statement error incompatible COALESCE expressions: expected NULL::T to be of type tuple{int, int}, found type tuple{int AS x} +statement error incompatible COALESCE expressions: expected NULL::t to be of type tuple{int, int}, found type tuple{int AS x} SELECT COALESCE((SELECT ROW (1, 2)), NULL::t) FROM t40297 # It should still type-check correctly without using a subquery. -statement error incompatible COALESCE expressions: expected NULL::T to be of type tuple{int, int}, found type tuple{int AS x} +statement error incompatible COALESCE expressions: expected NULL::t to be of type tuple{int, int}, found type tuple{int AS x} SELECT COALESCE(ROW (1, 2), NULL::t) FROM t40297 # Regression test for #70767. Make sure we avoid encoding arrays where the diff --git a/pkg/sql/logictest/testdata/logic_test/udf_star b/pkg/sql/logictest/testdata/logic_test/udf_star index 87d7e7684647..4a221891eb47 100644 --- a/pkg/sql/logictest/testdata/logic_test/udf_star +++ b/pkg/sql/logictest/testdata/logic_test/udf_star @@ -119,7 +119,7 @@ query TT SHOW CREATE FUNCTION f_allcolsel_alias ---- f_allcolsel_alias CREATE FUNCTION public.f_allcolsel_alias() - RETURNS T_TWOCOL + RETURNS public.t_twocol VOLATILE NOT LEAKPROOF CALLED ON NULL INPUT diff --git a/pkg/sql/types/types.go b/pkg/sql/types/types.go index 2721cfa4ec97..3264bd18ad54 100644 --- a/pkg/sql/types/types.go +++ b/pkg/sql/types/types.go @@ -2019,8 +2019,6 @@ func (t *T) SQLString() string { } return t.ArrayContents().SQLString() + "[]" case EnumFamily: - // TODO(125934): Include composite type names in this branch as well, so - // they can be properly identified in SHOW CREATE. if t.Oid() == oid.T_anyenum { return "anyenum" } @@ -2037,6 +2035,16 @@ func (t *T) SQLString() string { // databases when this function is called to produce DDL like in SHOW // CREATE. return t.TypeMeta.Name.FQName(false /* explicitCatalog */) + case TupleFamily: + if t.UserDefined() { + // Do not include the catalog name. We do not allow a table to reference + // a type in another database, so it will always be for the current database. + // Removing the catalog name makes the output more portable for other + // databases when this function is called to produce DDL like in SHOW + // CREATE. + return t.TypeMeta.Name.FQName(false /* explicitCatalog */) + } + return strings.ToUpper(t.Name()) case PGVectorFamily: if t.Width() == 0 { return "VECTOR" @@ -2049,8 +2057,8 @@ func (t *T) SQLString() string { // SQLStringFullyQualified is a wrapper for SQLString() for when we need the // type name to be a fully-qualified 3-part name. func (t *T) SQLStringFullyQualified() string { - // TODO(125934): include composite type names here - if t.TypeMeta.Name != nil && t.Family() == EnumFamily { + if t.TypeMeta.Name != nil && + (t.Family() == EnumFamily || (t.Family() == TupleFamily && t.UserDefined())) { // Include the catalog in the type name. This is necessary to properly // resolve the type, as some code paths require the database name to // correctly distinguish cross-database references. diff --git a/pkg/sql/types/types_test.go b/pkg/sql/types/types_test.go index 7b098ad5008f..5869db266efc 100644 --- a/pkg/sql/types/types_test.go +++ b/pkg/sql/types/types_test.go @@ -1236,7 +1236,7 @@ func TestSQLStringForError(t *testing.T) { }, { // Case 10: redacted because user-defined typ: userDefinedTuple, - expected: "USER DEFINED RECORD: ‹FOO›", + expected: "USER DEFINED RECORD: ‹foo›", }, { // Case 11: un-redacted typ: MakeArray(Int),