Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

sql: disallow cross-database type references in CTAS #105579

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions pkg/sql/create_table.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ import (
"github.com/cockroachdb/cockroach/pkg/sql/catalog/resolver"
"github.com/cockroachdb/cockroach/pkg/sql/catalog/schemaexpr"
"github.com/cockroachdb/cockroach/pkg/sql/catalog/tabledesc"
"github.com/cockroachdb/cockroach/pkg/sql/catalog/typedesc"
"github.com/cockroachdb/cockroach/pkg/sql/paramparse"
"github.com/cockroachdb/cockroach/pkg/sql/parser"
"github.com/cockroachdb/cockroach/pkg/sql/pgwire/pgcode"
Expand Down Expand Up @@ -1223,6 +1224,27 @@ func newTableDescIfAs(
}
}

// Check if there is any reference to a user defined type that belongs to
// another database which is not allowed.
for _, def := range p.Defs {
if d, ok := def.(*tree.ColumnTableDef); ok {
// In CTAS, ColumnTableDef are generated from resultColumns which are
// resolved already. So we may cast it to *types.T directly without
// resolving it again.
typ := d.Type.(*types.T)
if typ.UserDefined() {
tn, typDesc, err := params.p.GetTypeDescriptor(params.ctx, typedesc.UserDefinedTypeOIDToID(typ.Oid()))
if err != nil {
return nil, err
}
if typDesc.GetParentID() != db.GetID() {
return nil, pgerror.Newf(
pgcode.FeatureNotSupported, "cross database type references are not supported: %s", tn.String())
}
}
}
}

desc, err = newTableDesc(
params,
p,
Expand Down
26 changes: 26 additions & 0 deletions pkg/sql/logictest/testdata/logic_test/create_as
Original file line number Diff line number Diff line change
Expand Up @@ -382,3 +382,29 @@ query I
SELECT * FROM tab_from_seq
----
2

# Regression test for #105393
subtest regression_105393

statement ok
CREATE DATABASE db105393_1;
CREATE DATABASE db105393_2;
USE db105393_1;
CREATE TYPE e105393 AS ENUM ('a');
CREATE TABLE t105393 (a INT PRIMARY KEY, b e105393);
USE db105393_2;

statement error pq: cross database type references are not supported: db105393_1.public.e105393
CREATE TABLE e105393 AS TABLE db105393_1.public.t105393;

statement error pq: cross database type references are not supported: db105393_1.public.e105393
CREATE TABLE e105393 AS SELECT * FROM db105393_1.public.t105393;

statement error pq: cross database type references are not supported: db105393_1.public.e105393
CREATE TABLE e105393 AS SELECT b FROM db105393_1.public.t105393;

statement error pq: cross database type references are not supported: db105393_1.public.e105393
CREATE TABLE e105393 (a PRIMARY KEY, b) AS TABLE db105393_1.public.t105393;

statement error pq: cross database type references are not supported: db105393_1.public.e105393
CREATE TABLE e105393 (b) AS SELECT b FROM db105393_1.public.t105393;