diff --git a/go/vt/vtgate/planbuilder/operators/delete.go b/go/vt/vtgate/planbuilder/operators/delete.go index e4f1fc0e7ae..4d30d9b9cc1 100644 --- a/go/vt/vtgate/planbuilder/operators/delete.go +++ b/go/vt/vtgate/planbuilder/operators/delete.go @@ -52,8 +52,8 @@ func (d *Delete) GetOrdering(*plancontext.PlanningContext) []OrderBy { return nil } -func (d *Delete) TablesUsed() []string { - return SingleQualifiedIdentifier(d.Target.VTable.Keyspace, d.Target.VTable.Name) +func (d *Delete) TablesUsed(in []string) []string { + return append(in, QualifiedString(d.Target.VTable.Keyspace, d.Target.VTable.Name.String())) } func (d *Delete) ShortDescription() string { diff --git a/go/vt/vtgate/planbuilder/operators/helpers.go b/go/vt/vtgate/planbuilder/operators/helpers.go index 31d9bcfd279..36b10c96ae1 100644 --- a/go/vt/vtgate/planbuilder/operators/helpers.go +++ b/go/vt/vtgate/planbuilder/operators/helpers.go @@ -18,6 +18,7 @@ package operators import ( "fmt" + "slices" "sort" "vitess.io/vitess/go/vt/sqlparser" @@ -82,20 +83,21 @@ func TableID(op Operator) (result semantics.TableSet) { // TableUser is used to signal that this operator directly interacts with one or more tables type TableUser interface { - TablesUsed() []string + TablesUsed([]string) []string } func TablesUsed(op Operator) []string { - addString, collect := collectSortedUniqueStrings() + var in []string _ = Visit(op, func(this Operator) error { if tbl, ok := this.(TableUser); ok { - for _, u := range tbl.TablesUsed() { - addString(u) - } + in = tbl.TablesUsed(in) } return nil }) - return collect() + + slices.Sort(in) + compacted := slices.Compact(in) + return compacted } func CostOf(op Operator) (cost int) { diff --git a/go/vt/vtgate/planbuilder/operators/insert.go b/go/vt/vtgate/planbuilder/operators/insert.go index 3176bac50a2..4ce37901a77 100644 --- a/go/vt/vtgate/planbuilder/operators/insert.go +++ b/go/vt/vtgate/planbuilder/operators/insert.go @@ -96,8 +96,12 @@ func (i *Insert) Clone([]Operator) Operator { } } -func (i *Insert) TablesUsed() []string { - return SingleQualifiedIdentifier(i.VTable.Keyspace, i.VTable.Name) +func (i *Insert) TablesUsed(in []string) []string { + return append(in, i.tableTarget()) +} + +func (i *Insert) tableTarget() string { + return QualifiedString(i.VTable.Keyspace, i.VTable.Name.String()) } func (i *Insert) Statement() sqlparser.Statement { @@ -423,7 +427,7 @@ func insertSelectPlan( // When the table you are streaming data from and table you are inserting from are same. // Then due to locking of the index range on the table we might not be able to insert into the table. // Therefore, instead of streaming, this flag will ensure the records are first read and then inserted. - insertTbl := insOp.TablesUsed()[0] + insertTbl := insOp.tableTarget() selTables := TablesUsed(selOp) for _, tbl := range selTables { if insertTbl == tbl { diff --git a/go/vt/vtgate/planbuilder/operators/route.go b/go/vt/vtgate/planbuilder/operators/route.go index a8cf8582851..551d7b6d95e 100644 --- a/go/vt/vtgate/planbuilder/operators/route.go +++ b/go/vt/vtgate/planbuilder/operators/route.go @@ -717,14 +717,11 @@ func (r *Route) GetOrdering(ctx *plancontext.PlanningContext) []OrderBy { // TablesUsed returns tables used by MergedWith routes, which are not included // in Inputs() and thus not a part of the operator tree -func (r *Route) TablesUsed() []string { - addString, collect := collectSortedUniqueStrings() +func (r *Route) TablesUsed(in []string) []string { for _, mw := range r.MergedWith { - for _, u := range TablesUsed(mw) { - addString(u) - } + in = append(in, mw.TablesUsed(in)...) } - return collect() + return in } func isSpecialOrderBy(o OrderBy) bool { diff --git a/go/vt/vtgate/planbuilder/operators/table.go b/go/vt/vtgate/planbuilder/operators/table.go index 6f221f2337a..4391380480c 100644 --- a/go/vt/vtgate/planbuilder/operators/table.go +++ b/go/vt/vtgate/planbuilder/operators/table.go @@ -107,11 +107,11 @@ func (to *Table) AddCol(col *sqlparser.ColName) { to.Columns = append(to.Columns, col) } -func (to *Table) TablesUsed() []string { +func (to *Table) TablesUsed(in []string) []string { if sqlparser.SystemSchema(to.QTable.Table.Qualifier.String()) { - return nil + return in } - return SingleQualifiedIdentifier(to.VTable.Keyspace, to.VTable.Name) + return append(in, QualifiedString(to.VTable.Keyspace, to.VTable.Name.String())) } func addColumn(ctx *plancontext.PlanningContext, op ColNameColumns, e sqlparser.Expr) int { diff --git a/go/vt/vtgate/planbuilder/operators/update.go b/go/vt/vtgate/planbuilder/operators/update.go index 9844b341670..dd0a86c2de2 100644 --- a/go/vt/vtgate/planbuilder/operators/update.go +++ b/go/vt/vtgate/planbuilder/operators/update.go @@ -86,8 +86,8 @@ func (u *Update) GetOrdering(*plancontext.PlanningContext) []OrderBy { return nil } -func (u *Update) TablesUsed() []string { - return SingleQualifiedIdentifier(u.Target.VTable.Keyspace, u.Target.VTable.Name) +func (u *Update) TablesUsed(in []string) []string { + return append(in, QualifiedString(u.Target.VTable.Keyspace, u.Target.VTable.Name.String())) } func (u *Update) ShortDescription() string { diff --git a/go/vt/vtgate/planbuilder/operators/vindex.go b/go/vt/vtgate/planbuilder/operators/vindex.go index fbfbb6c0ccd..30f13701df6 100644 --- a/go/vt/vtgate/planbuilder/operators/vindex.go +++ b/go/vt/vtgate/planbuilder/operators/vindex.go @@ -164,8 +164,8 @@ func (v *Vindex) AddPredicate(ctx *plancontext.PlanningContext, expr sqlparser.E // TablesUsed implements the Operator interface. // It is not keyspace-qualified. -func (v *Vindex) TablesUsed() []string { - return []string{v.Table.Table.Name.String()} +func (v *Vindex) TablesUsed(in []string) []string { + return append(in, v.Table.Table.Name.String()) } func (v *Vindex) ShortDescription() string {