Skip to content

Commit e04132c

Browse files
committed
opt: add support for region column inference to catalog and test catalog
This commit adds support for the new storage param `infer_rbr_region_col_using_constraint` to the optimizer and test catalogs. Informs #148243 Release note: None
1 parent 9f752ed commit e04132c

File tree

6 files changed

+66
-4
lines changed

6 files changed

+66
-4
lines changed

pkg/sql/opt/cat/table.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,13 @@ type Table interface {
171171
// different name in a `REGIONAL BY ROW AS` DDL clause.
172172
HomeRegionColName() (colName string, ok bool)
173173

174+
// RegionalByRowUsingConstraint returns the foreign-key constraint that is
175+
// used to look up the region for each row in a REGIONAL BY ROW table.
176+
// This is only set if the infer_rbr_region_col_using_constraint storage param
177+
// is set. If the storage param is not set, or if the table is not
178+
// REGIONAL BY ROW, RegionalByRowUsingConstraint returns nil.
179+
RegionalByRowUsingConstraint() ForeignKeyConstraint
180+
174181
// GetDatabaseID returns the owning database id of the table, or zero, if the
175182
// owning database could not be determined.
176183
GetDatabaseID() descpb.ID

pkg/sql/opt/exec/explain/plan_gist_factory.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -646,6 +646,11 @@ func (u *unknownTable) HomeRegionColName() (colName string, ok bool) {
646646
return "", false
647647
}
648648

649+
// RegionalByRowUsingConstraint is part of the cat.Table interface.
650+
func (ot *unknownTable) RegionalByRowUsingConstraint() cat.ForeignKeyConstraint {
651+
return nil
652+
}
653+
649654
// GetDatabaseID is part of the cat.Table interface.
650655
func (u *unknownTable) GetDatabaseID() descpb.ID {
651656
return 0

pkg/sql/opt/testutils/testcat/create_table.go

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -421,13 +421,23 @@ OuterLoop:
421421
cat.FamilyColumn{Column: col, Ordinal: colOrd})
422422
}
423423

424+
// Allow specifying the name of a FK constraint to use for lookup of region
425+
// column values for a REGIONAL BY ROW table.
426+
var rbrUsingConstraintName string
427+
if param := stmt.StorageParams.GetVal(catpb.RBRUsingConstraintTableSettingName); param != nil {
428+
rbrUsingConstraintName = param.(*tree.StrVal).RawString()
429+
}
430+
424431
// Search for foreign key constraints. We want to process them after first
425432
// processing all the indexes (otherwise the foreign keys could add
426433
// unnecessary indexes).
427434
for _, def := range stmt.Defs {
428435
switch def := def.(type) {
429436
case *tree.ForeignKeyConstraintTableDef:
430-
tc.resolveFK(tab, def)
437+
fk := tc.resolveFK(tab, def)
438+
if rbrUsingConstraintName != "" && string(def.Name) == rbrUsingConstraintName {
439+
tab.regionalByRowUsingConstraint = fk
440+
}
431441
}
432442
}
433443

@@ -539,7 +549,9 @@ func (tc *Catalog) CreateTableAs(name tree.TableName, columns []cat.Column) *Tab
539549
}
540550

541551
// resolveFK processes a foreign key constraint.
542-
func (tc *Catalog) resolveFK(tab *Table, d *tree.ForeignKeyConstraintTableDef) {
552+
func (tc *Catalog) resolveFK(
553+
tab *Table, d *tree.ForeignKeyConstraintTableDef,
554+
) cat.ForeignKeyConstraint {
543555
fromCols := make([]int, len(d.FromCols))
544556
for i, c := range d.FromCols {
545557
fromCols[i] = tab.FindOrdinal(string(c))
@@ -709,6 +721,7 @@ func (tc *Catalog) resolveFK(tab *Table, d *tree.ForeignKeyConstraintTableDef) {
709721
}
710722
tab.outboundFKs = append(tab.outboundFKs, fk)
711723
targetTable.inboundFKs = append(targetTable.inboundFKs, fk)
724+
return &fk
712725
}
713726

714727
func (tt *Table) addCheckConstraint(check *tree.CheckConstraintTableDef) {

pkg/sql/opt/testutils/testcat/test_catalog.go

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -898,7 +898,8 @@ type Table struct {
898898

899899
multiRegion bool
900900

901-
implicitRBRIndexElem *tree.IndexElem
901+
implicitRBRIndexElem *tree.IndexElem
902+
regionalByRowUsingConstraint cat.ForeignKeyConstraint
902903

903904
homeRegion string
904905

@@ -1104,6 +1105,14 @@ func (tt *Table) HomeRegionColName() (colName string, ok bool) {
11041105
return string(tree.RegionalByRowRegionDefaultColName), true
11051106
}
11061107

1108+
// RegionalByRowUsingConstraint is part of the cat.Table interface.
1109+
func (tt *Table) RegionalByRowUsingConstraint() cat.ForeignKeyConstraint {
1110+
if !tt.IsRegionalByRow() {
1111+
return nil
1112+
}
1113+
return tt.regionalByRowUsingConstraint
1114+
}
1115+
11071116
// GetDatabaseID is part of the cat.Table interface.
11081117
func (tt *Table) GetDatabaseID() descpb.ID {
11091118
return tt.DatabaseID

pkg/sql/opt/testutils/testcat/types.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,12 @@ func (tc *Catalog) ResolveType(
9797

9898
// ResolveTypeByOID is part of the cat.Catalog interface.
9999
func (tc *Catalog) ResolveTypeByOID(ctx context.Context, typID oid.Oid) (*types.T, error) {
100-
// First look for a matching user-defined enum type.
100+
// Look for a builtin type first.
101+
if typ, ok := types.OidToType[typID]; ok {
102+
return typ, nil
103+
}
104+
105+
// Look for a matching user-defined enum type.
101106
for _, typ := range tc.enumTypes {
102107
if typ.Oid() == typID {
103108
return typ, nil

pkg/sql/opt_catalog.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1179,6 +1179,7 @@ func newOptTable(
11791179
originColumns: fk.ForeignKeyDesc().OriginColumnIDs,
11801180
referencedTable: cat.StableID(fk.GetReferencedTableID()),
11811181
referencedColumns: fk.ForeignKeyDesc().ReferencedColumnIDs,
1182+
constraintID: fk.GetConstraintID(),
11821183
validity: fk.GetConstraintValidity(),
11831184
match: tree.CompositeKeyMatchMethodType[fk.Match()],
11841185
deleteAction: tree.ForeignKeyReferenceActionType[fk.OnDelete()],
@@ -1192,6 +1193,7 @@ func newOptTable(
11921193
originColumns: fk.ForeignKeyDesc().OriginColumnIDs,
11931194
referencedTable: ot.ID(),
11941195
referencedColumns: fk.ForeignKeyDesc().ReferencedColumnIDs,
1196+
constraintID: fk.GetConstraintID(),
11951197
validity: fk.GetConstraintValidity(),
11961198
match: tree.CompositeKeyMatchMethodType[fk.Match()],
11971199
deleteAction: tree.ForeignKeyReferenceActionType[fk.OnDelete()],
@@ -1570,6 +1572,21 @@ func (ot *optTable) HomeRegionColName() (colName string, ok bool) {
15701572
return *regionalByRowConfig.As, true
15711573
}
15721574

1575+
// RegionalByRowUsingConstraint is part of the cat.Table interface.
1576+
func (ot *optTable) RegionalByRowUsingConstraint() cat.ForeignKeyConstraint {
1577+
if !ot.desc.IsLocalityRegionalByRow() {
1578+
return nil
1579+
}
1580+
if id := ot.desc.GetRegionalByRowUsingConstraint(); id != catid.ConstraintID(0) {
1581+
for i := range ot.outboundFKs {
1582+
if ot.outboundFKs[i].constraintID == id {
1583+
return &ot.outboundFKs[i]
1584+
}
1585+
}
1586+
}
1587+
return nil
1588+
}
1589+
15731590
// GetDatabaseID is part of the cat.Table interface.
15741591
func (ot *optTable) GetDatabaseID() descpb.ID {
15751592
return ot.desc.GetParentID()
@@ -2283,6 +2300,7 @@ type optForeignKeyConstraint struct {
22832300
referencedTable cat.StableID
22842301
referencedColumns []descpb.ColumnID
22852302

2303+
constraintID catid.ConstraintID
22862304
validity descpb.ConstraintValidity
22872305
match tree.CompositeKeyMatchMethod
22882306
deleteAction tree.ReferenceAction
@@ -2700,6 +2718,11 @@ func (ot *optVirtualTable) HomeRegionColName() (colName string, ok bool) {
27002718
return "", false
27012719
}
27022720

2721+
// RegionalByRowUsingConstraint is part of the cat.Table interface.
2722+
func (ot *optVirtualTable) RegionalByRowUsingConstraint() cat.ForeignKeyConstraint {
2723+
return nil
2724+
}
2725+
27032726
// GetDatabaseID is part of the cat.Table interface.
27042727
func (ot *optVirtualTable) GetDatabaseID() descpb.ID {
27052728
return 0

0 commit comments

Comments
 (0)