Skip to content

Commit

Permalink
Merge pull request #137768 from spilchen/backport24.2-127158
Browse files Browse the repository at this point in the history
release-24.2: sql: SHOW CREATE now uses two-part names for composite types
  • Loading branch information
spilchen authored Dec 19, 2024
2 parents b6611ce + 873ee83 commit 3782b7a
Show file tree
Hide file tree
Showing 7 changed files with 60 additions and 15 deletions.
2 changes: 1 addition & 1 deletion pkg/sql/logictest/testdata/logic_test/composite_types
Original file line number Diff line number Diff line change
Expand Up @@ -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)
)
Expand Down
6 changes: 3 additions & 3 deletions pkg/sql/logictest/testdata/logic_test/drop_function
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down
37 changes: 37 additions & 0 deletions pkg/sql/logictest/testdata/logic_test/show_create
Original file line number Diff line number Diff line change
Expand Up @@ -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
10 changes: 5 additions & 5 deletions pkg/sql/logictest/testdata/logic_test/tuple
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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.
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion pkg/sql/logictest/testdata/logic_test/udf_star
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
16 changes: 12 additions & 4 deletions pkg/sql/types/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
Expand All @@ -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"
Expand All @@ -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.
Expand Down
2 changes: 1 addition & 1 deletion pkg/sql/types/types_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down

0 comments on commit 3782b7a

Please sign in to comment.