Skip to content

Commit e233359

Browse files
craig[bot]yuzefovich
andcommitted
Merge #142748
142748: sql: fix up recent stmt bundle change r=yuzefovich a=yuzefovich Epic: None Release note: None Co-authored-by: Yahor Yuzefovich <[email protected]>
2 parents 721f46e + c6f9df2 commit e233359

File tree

3 files changed

+42
-41
lines changed

3 files changed

+42
-41
lines changed

pkg/sql/explain_bundle.go

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -628,19 +628,19 @@ func (b *stmtBundleBuilder) addEnv(ctx context.Context) {
628628
ctx,
629629
b.plan.catalog,
630630
mem.Metadata().AllTables(),
631-
func(table cat.Table, fk cat.ForeignKeyConstraint) (exploreFKs bool) {
631+
func(table cat.Table, fk cat.ForeignKeyConstraint) (recurse bool) {
632632
if includeAll {
633633
return true
634634
}
635635
if !hasMutation {
636636
// For read-only queries, we don't care about any tables not
637-
// referenced by metadata, so we don't want to explore any
638-
// FKs.
637+
// referenced by metadata, so we don't want to recursed into
638+
// any FKs.
639639
return false
640640
}
641641
if referencedByMetadata.Contains(int(table.ID())) || fk == nil {
642-
// For mutations, we always want to explore FKs of tables
643-
// referenced by metadata.
642+
// For mutations, we always want to recurse into FKs of
643+
// tables referenced by metadata.
644644
//
645645
// The second part of the conditional should never evaluate
646646
// to 'true' since nil FK parameter is provided only for
@@ -654,31 +654,33 @@ func (b *stmtBundleBuilder) addEnv(ctx context.Context) {
654654
// CREATE TABLE child (pk INT PRIMARY KEY, fk INT REFERENCES parent(pk));
655655
// CREATE TABLE grandchild (pk INT PRIMARY KEY, fk INT REFERENCES child(pk));
656656
if table.ID() == fk.ReferencedTableID() {
657-
// The table we're considering for exploration is a
658-
// referenced table of a FK constraint where the referencing
659-
// (origin) table has already been visited.
657+
// The table we're considering for recursion is a referenced
658+
// table of a FK constraint where the referencing (origin)
659+
// table has already been visited.
660660
//
661661
// In our example, we've already visited 'child' and are
662-
// considering exploring 'parent', but we never actually
663-
// want to explore it.
662+
// considering recursing into 'parent', but we never
663+
// actually want to do that.
664664
return false
665665
}
666-
// The table we're considering for exploration is an origin
666+
// The table we're considering for recursion is an origin
667667
// table of a FK constraint where the referenced table has
668668
// already been visited.
669669
//
670670
// In our example, we've already visited 'parent' and are
671-
// considering exploring 'child'.
671+
// considering recursing into 'child'.
672672
if hasDelete && fk.DeleteReferenceAction() == tree.Cascade {
673673
// We deleted from 'parent', and we have the ON DELETE
674-
// CASCADE action of the 'child' FK, so we need to explore
675-
// child's FKs in order to additionally visit 'grandchild'.
674+
// CASCADE action of the 'child' FK, so we need to recurse
675+
// into child's FKs in order to additionally visit
676+
// 'grandchild'.
676677
return true
677678
}
678679
if (hasUpdate || hasUpsert) && fk.UpdateReferenceAction() == tree.Cascade {
679680
// We updated the 'parent', and we have the ON UPDATE
680-
// CASCADE action of the 'child' FK, so we need to explore
681-
// child's FKs in order to additionally visit 'grandchild'.
681+
// CASCADE action of the 'child' FK, so we need to recurse
682+
// into child's FKs in order to additionally visit
683+
// 'grandchild'.
682684
//
683685
// We don't know whether UPSERT resulted in an UPDATE or an
684686
// INSERT, but we'll assume the former to be on the safer

pkg/sql/opt/exec/execbuilder/relational.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4164,7 +4164,7 @@ func (b *Builder) getEnvData() (exec.ExplainEnvData, error) {
41644164
b.ctx,
41654165
b.catalog,
41664166
b.mem.Metadata().AllTables(),
4167-
func(cat.Table, cat.ForeignKeyConstraint) (exploreFKs bool) {
4167+
func(cat.Table, cat.ForeignKeyConstraint) (recurse bool) {
41684168
return true
41694169
},
41704170
func(table cat.Table, _ cat.ForeignKeyConstraint) {

pkg/sql/opt/util.go

Lines changed: 23 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,8 @@ import (
1818
// are visited in sorted order so that later tables reference earlier tables.
1919
//
2020
// The visiting is controlled by two callbacks:
21-
// - visitPreFn should tell the visitor whether the FKs of the given tables
22-
// should be explored (i.e. whether the visitor should "recurse" into FK
23-
// reference tables of the given one).
21+
// - recurseFn should tell the visitor whether it should recurse into FK
22+
// reference tables of the given one.
2423
// - visitFn allows the caller to do any work on the table being visited.
2524
//
2625
// In both functions:
@@ -35,39 +34,39 @@ func VisitFKReferenceTables(
3534
ctx context.Context,
3635
catalog cat.Catalog,
3736
tables []TableMeta,
38-
visitPreFn func(_ cat.Table, fk cat.ForeignKeyConstraint) (exploreFKs bool),
37+
recurseFn func(_ cat.Table, fk cat.ForeignKeyConstraint) bool,
3938
visitFn func(_ cat.Table, fk cat.ForeignKeyConstraint),
4039
) {
41-
// tableExplored tracks which tables we've already explored FKs of. Once a
42-
// table is explored, it is considered "fully processed" and we effectively
43-
// ignore it from now on. If a table has already been visited but is not
44-
// explored, we still might want to explore it later (because we might get
45-
// to it via a different FK that requires exploration).
46-
var tableExplored intsets.Fast
40+
// tableRecursed tracks which tables we've already recursed into. Once a
41+
// table is recursed into, it is considered "fully processed" and we
42+
// effectively ignore it from now on. If a table has already been visited
43+
// but is not recursed into, we still might want to recurse into it later
44+
// (because we might get to it via a different FK that requires recursion).
45+
var tableRecursed intsets.Fast
4746
var visitForeignKeyReferencedTables func(tab cat.Table)
4847
var visitForeignKeyReferencingTables func(tab cat.Table)
49-
visitTable := func(table cat.Table, fk cat.ForeignKeyConstraint, exploreFKs bool) {
48+
visitTable := func(table cat.Table, fk cat.ForeignKeyConstraint, recurse bool) {
5049
tabID := table.ID()
51-
if exploreFKs {
52-
tableExplored.Add(int(tabID))
50+
if recurse {
51+
tableRecursed.Add(int(tabID))
5352
}
5453
// The order of visiting here is important: namely, we want to visit
5554
// all tables that we reference first, then ourselves, and only then
5655
// tables that reference us.
57-
if exploreFKs {
56+
if recurse {
5857
visitForeignKeyReferencedTables(table)
5958
}
6059
visitFn(table, fk)
61-
if exploreFKs {
60+
if recurse {
6261
visitForeignKeyReferencingTables(table)
6362
}
6463
}
6564
// handleRelatedTables is a helper function that processes the given table
66-
// if it hasn't been explored yet by visiting all referenced and referencing
67-
// table of the given one, including via transient (recursive) FK
68-
// relationships.
65+
// if it hasn't been recursed into yet by visiting all referenced and
66+
// referencing table of the given one, including via transient (recursive)
67+
// FK relationships.
6968
handleRelatedTables := func(tabID cat.StableID, fk cat.ForeignKeyConstraint) {
70-
if !tableExplored.Contains(int(tabID)) {
69+
if !tableRecursed.Contains(int(tabID)) {
7170
ds, _, err := catalog.ResolveDataSourceByID(ctx, cat.Flags{}, tabID)
7271
if err != nil {
7372
// This is a best-effort attempt to get all the tables, so don't
@@ -80,8 +79,8 @@ func VisitFKReferenceTables(
8079
// error.
8180
return
8281
}
83-
exploreFKs := visitPreFn(refTab, fk)
84-
visitTable(refTab, fk, exploreFKs)
82+
recurse := recurseFn(refTab, fk)
83+
visitTable(refTab, fk, recurse)
8584
}
8685
}
8786
visitForeignKeyReferencedTables = func(tab cat.Table) {
@@ -98,9 +97,9 @@ func VisitFKReferenceTables(
9897
}
9998
for _, tabMeta := range tables {
10099
tabID := tabMeta.Table.ID()
101-
if !tableExplored.Contains(int(tabID)) {
102-
exploreFKs := visitPreFn(tabMeta.Table, nil /* fk */)
103-
visitTable(tabMeta.Table, nil /* fk */, exploreFKs)
100+
if !tableRecursed.Contains(int(tabID)) {
101+
recurse := recurseFn(tabMeta.Table, nil /* fk */)
102+
visitTable(tabMeta.Table, nil /* fk */, recurse)
104103
}
105104
}
106105
}

0 commit comments

Comments
 (0)