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: fix pg_catalog.pg_constraint's confkey column #31610

Merged
merged 1 commit into from
Oct 22, 2018
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
58 changes: 58 additions & 0 deletions pkg/sql/logictest/testdata/logic_test/pg_catalog
Original file line number Diff line number Diff line change
Expand Up @@ -1624,3 +1624,61 @@ query OT
SELECT typ.oid, typ.typname FROM pg_attribute att JOIN pg_type typ ON atttypid=typ.oid WHERE attrelid='coltab'::regclass AND attname='a'
----
25 text

subtest 31545

# Test an index of 2 referencing an index of 2.
statement ok
CREATE TABLE a (
id_a_1 INT UNIQUE,
id_a_2 INT,
PRIMARY KEY (id_a_1, id_a_2)
)

statement ok
CREATE TABLE b (
id_b_1 INT,
id_b_2 INT,
PRIMARY KEY (id_b_1, id_b_2),
CONSTRAINT my_fkey FOREIGN KEY (id_b_1, id_b_2) REFERENCES a (id_a_1, id_a_2)
)

query TT colnames
SELECT conkey, confkey FROM pg_catalog.pg_constraint WHERE conname = 'my_fkey'
----
conkey confkey
{1,2} {1,2}

# Test an index of 3 referencing an index of 2.
statement ok
DROP TABLE b;
CREATE TABLE b (
id_b_1 INT,
id_b_2 INT,
id_b_3 INT,
PRIMARY KEY (id_b_1, id_b_2, id_b_3),
CONSTRAINT my_fkey FOREIGN KEY (id_b_1, id_b_2) REFERENCES a (id_a_1, id_a_2)
)

query TT colnames
SELECT conkey, confkey FROM pg_catalog.pg_constraint WHERE conname = 'my_fkey'
----
conkey confkey
{1,2} {1,2}

# Test an index of 3 referencing an index of 1.
statement ok
DROP TABLE b;
CREATE TABLE b (
id_b_1 INT,
id_b_2 INT,
id_b_3 INT,
PRIMARY KEY (id_b_1, id_b_2, id_b_3),
CONSTRAINT my_fkey FOREIGN KEY (id_b_1) REFERENCES a (id_a_1)
)

query TT colnames
SELECT conkey, confkey FROM pg_catalog.pg_constraint WHERE conname = 'my_fkey'
----
conkey confkey
{1} {1}
16 changes: 15 additions & 1 deletion pkg/sql/pg_catalog.go
Original file line number Diff line number Diff line change
Expand Up @@ -680,7 +680,21 @@ CREATE TABLE pg_catalog.pg_constraint (
confupdtype = fkActionNone
confdeltype = fkActionNone
confmatchtype = fkMatchTypeSimple
if conkey, err = colIDArrayToDatum(con.Index.ColumnIDs); err != nil {
columnIDs := con.Index.ColumnIDs
if int(con.FK.SharedPrefixLen) > len(columnIDs) {
return errors.Errorf(
"For foreign key %q's shared prefix len (%d) is greater than the number of columns "+
"in the index (%d). This might be an indication of inconsistency.",
con.FK.Name,
con.FK.SharedPrefixLen,
int32(len(columnIDs)),
)
}
sharedPrefixLen := len(columnIDs)
if int(con.FK.SharedPrefixLen) > 0 {
sharedPrefixLen = int(con.FK.SharedPrefixLen)
}
if conkey, err = colIDArrayToDatum(columnIDs[:sharedPrefixLen]); err != nil {
return err
}
if confkey, err = colIDArrayToDatum(con.ReferencedIndex.ColumnIDs); err != nil {
Expand Down
2 changes: 2 additions & 0 deletions pkg/sql/sqlbase/structured.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions pkg/sql/sqlbase/structured.proto
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,8 @@ message InterleaveDescriptor {
// grandparent. Thus, the sum of SharedPrefixLens in the components of an
// InterleaveDescriptor is never more than the number of fields in the index
// being interleaved.
// In cockroach 1.0, this value did not exist and thus a check for > 0
// must be performed prior to its use.
optional uint32 shared_prefix_len = 3 [(gogoproto.nullable) = false,
(gogoproto.customname) = "SharedPrefixLen"];
}
Expand Down