Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
35052: sql: fix conupdtype, confdeltype, conmatchtype in pg_constraint r=knz a=knz

Fixes cockroachdb#34862. For TypeORM compat. cc @awoods187 


Co-authored-by: Raphael 'kena' Poss <[email protected]>
  • Loading branch information
craig[bot] and knz committed Feb 20, 2019
2 parents 1b0a449 + 0f16a6f commit 35929c5
Show file tree
Hide file tree
Showing 9 changed files with 357 additions and 265 deletions.
60 changes: 60 additions & 0 deletions pkg/sql/logictest/testdata/logic_test/pg_catalog
Original file line number Diff line number Diff line change
Expand Up @@ -1717,3 +1717,63 @@ t

statement ok
DROP DATABASE d34856 CASCADE

subtest regression_34862

statement ok
CREATE DATABASE d34862; SET database=d34862

statement ok
CREATE TABLE t(x INT UNIQUE);
CREATE TABLE u(
a INT REFERENCES t(x) ON DELETE NO ACTION,
b INT REFERENCES t(x) ON DELETE RESTRICT,
c INT REFERENCES t(x) ON DELETE SET NULL,
d INT DEFAULT 123 REFERENCES t(x) ON DELETE SET DEFAULT,
e INT REFERENCES t(x) ON DELETE CASCADE,
f INT REFERENCES t(x) ON UPDATE NO ACTION,
g INT REFERENCES t(x) ON UPDATE RESTRICT,
h INT REFERENCES t(x) ON UPDATE SET NULL,
i INT DEFAULT 123 REFERENCES t(x) ON UPDATE SET DEFAULT,
j INT REFERENCES t(x) ON UPDATE CASCADE,
k INT REFERENCES t(x) ON DELETE RESTRICT ON UPDATE SET NULL
);

query TTT
SELECT conname, confupdtype, confdeltype FROM pg_constraint ORDER BY conname
----
fk_a_ref_t a a
fk_b_ref_t a r
fk_c_ref_t a n
fk_d_ref_t a d
fk_e_ref_t a c
fk_f_ref_t a a
fk_g_ref_t r a
fk_h_ref_t n a
fk_i_ref_t d a
fk_j_ref_t c a
fk_k_ref_t n r
t_x_key NULL NULL

statement ok
DROP TABLE u; DROP TABLE t

statement ok
CREATE TABLE v(x INT, y INT, UNIQUE (x,y))

statement ok
CREATE TABLE w(
a INT, b INT, c INT, d INT,
FOREIGN KEY (a,b) REFERENCES v(x,y) MATCH FULL,
FOREIGN KEY (c,d) REFERENCES v(x,y) MATCH SIMPLE
);

query TT
SELECT conname, confmatchtype FROM pg_constraint ORDER BY conname
----
fk_a_ref_v f
fk_c_ref_v s
v_x_y_key NULL

statement ok
DROP DATABASE d34862 CASCADE; SET database=test
32 changes: 21 additions & 11 deletions pkg/sql/pg_catalog.go
Original file line number Diff line number Diff line change
Expand Up @@ -627,19 +627,23 @@ var (
fkActionSetNull = tree.NewDString("n")
fkActionSetDefault = tree.NewDString("d")

// Avoid unused warning for constants.
_ = fkActionRestrict
_ = fkActionCascade
_ = fkActionSetNull
_ = fkActionSetDefault
fkActionMap = map[sqlbase.ForeignKeyReference_Action]tree.Datum{
sqlbase.ForeignKeyReference_NO_ACTION: fkActionNone,
sqlbase.ForeignKeyReference_RESTRICT: fkActionRestrict,
sqlbase.ForeignKeyReference_CASCADE: fkActionCascade,
sqlbase.ForeignKeyReference_SET_NULL: fkActionSetNull,
sqlbase.ForeignKeyReference_SET_DEFAULT: fkActionSetDefault,
}

fkMatchTypeFull = tree.NewDString("f")
fkMatchTypePartial = tree.NewDString("p")
fkMatchTypeSimple = tree.NewDString("s")

// Avoid unused warning for constants.
_ = fkMatchTypeFull
_ = fkMatchTypePartial
fkMatchMap = map[sqlbase.ForeignKeyReference_Match]tree.Datum{
sqlbase.ForeignKeyReference_SIMPLE: fkMatchTypeSimple,
sqlbase.ForeignKeyReference_FULL: fkMatchTypeFull,
sqlbase.ForeignKeyReference_PARTIAL: fkMatchTypePartial,
}
)

// See: https://www.postgresql.org/docs/9.6/static/catalog-pg-constraint.html.
Expand Down Expand Up @@ -727,9 +731,15 @@ CREATE TABLE pg_catalog.pg_constraint (
contype = conTypeFK
conindid = h.IndexOid(referencedDB, tree.PublicSchema, con.ReferencedTable, con.ReferencedIndex)
confrelid = defaultOid(con.ReferencedTable.ID)
confupdtype = fkActionNone
confdeltype = fkActionNone
confmatchtype = fkMatchTypeSimple
if r, ok := fkActionMap[con.FK.OnUpdate]; ok {
confupdtype = r
}
if r, ok := fkActionMap[con.FK.OnDelete]; ok {
confdeltype = r
}
if r, ok := fkMatchMap[con.FK.Match]; ok {
confmatchtype = r
}
columnIDs := con.Index.ColumnIDs
if int(con.FK.SharedPrefixLen) > len(columnIDs) {
return pgerror.NewAssertionErrorf(
Expand Down
4 changes: 4 additions & 0 deletions pkg/sql/row/cascader.go
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,10 @@ func spanForIndexValues(
if nulls {
return roachpb.Span{}, nil
}

case sqlbase.ForeignKeyReference_PARTIAL:
return roachpb.Span{}, pgerror.UnimplementedWithIssueError(20305, "MATCH PARTIAL not supported")

default:
return roachpb.Span{}, pgerror.NewAssertionErrorf("unknown composite key match type: %v", match)
}
Expand Down
4 changes: 4 additions & 0 deletions pkg/sql/row/fk_existence_base.go
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,10 @@ func computeFkCheckColumnIDs(
return nil, pgerror.NewErrorf(pgerror.CodeForeignKeyViolationError,
"missing values for columns %q in multi-part foreign key", missingColumns)
}

case sqlbase.ForeignKeyReference_PARTIAL:
return nil, pgerror.UnimplementedWithIssueError(20305, "MATCH PARTIAL not supported")

default:
return nil, pgerror.NewAssertionErrorf("unknown composite key match type: %v", match)
}
Expand Down
3 changes: 3 additions & 0 deletions pkg/sql/row/fk_existence_check.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,9 @@ outer:
return err
}

case sqlbase.ForeignKeyReference_PARTIAL:
return pgerror.UnimplementedWithIssueError(20305, "MATCH PARTIAL not supported")

default:
return pgerror.NewAssertionErrorf("unknown composite key match type: %v", fk.ref.Match)
}
Expand Down
6 changes: 4 additions & 2 deletions pkg/sql/sem/tree/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -652,11 +652,13 @@ type CompositeKeyMatchMethod int
const (
MatchSimple CompositeKeyMatchMethod = iota
MatchFull
MatchPartial // Note: PARTIAL not actually supported at this point.
)

var compositeKeyMatchMethodName = [...]string{
MatchSimple: "MATCH SIMPLE",
MatchFull: "MATCH FULL",
MatchSimple: "MATCH SIMPLE",
MatchFull: "MATCH FULL",
MatchPartial: "MATCH PARTIAL",
}

func (c CompositeKeyMatchMethod) String() string {
Expand Down
12 changes: 8 additions & 4 deletions pkg/sql/sqlbase/structured.go
Original file line number Diff line number Diff line change
Expand Up @@ -2604,16 +2604,18 @@ func (cc *TableDescriptor_CheckConstraint) UsesColumn(
// CompositeKeyMatchMethodValue allows the conversion from a
// tree.ReferenceCompositeKeyMatchMethod to a ForeignKeyReference_Match.
var CompositeKeyMatchMethodValue = [...]ForeignKeyReference_Match{
tree.MatchSimple: ForeignKeyReference_SIMPLE,
tree.MatchFull: ForeignKeyReference_FULL,
tree.MatchSimple: ForeignKeyReference_SIMPLE,
tree.MatchFull: ForeignKeyReference_FULL,
tree.MatchPartial: ForeignKeyReference_PARTIAL,
}

// ForeignKeyReferenceMatchValue allows the conversion from a
// ForeignKeyReference_Match to a tree.ReferenceCompositeKeyMatchMethod.
// This should match CompositeKeyMatchMethodValue.
var ForeignKeyReferenceMatchValue = [...]tree.CompositeKeyMatchMethod{
ForeignKeyReference_SIMPLE: tree.MatchSimple,
ForeignKeyReference_FULL: tree.MatchFull,
ForeignKeyReference_SIMPLE: tree.MatchSimple,
ForeignKeyReference_FULL: tree.MatchFull,
ForeignKeyReference_PARTIAL: tree.MatchPartial,
}

// String implements the fmt.Stringer interface.
Expand All @@ -2623,6 +2625,8 @@ func (x ForeignKeyReference_Match) String() string {
return "MATCH SIMPLE"
case ForeignKeyReference_FULL:
return "MATCH FULL"
case ForeignKeyReference_PARTIAL:
return "MATCH PARTIAL"
default:
return strconv.Itoa(int(x))
}
Expand Down
Loading

0 comments on commit 35929c5

Please sign in to comment.