Skip to content

Commit e4d9d0a

Browse files
authored
Merge pull request #3110 from dolthub/angela/multidb
Use child column id's for union when assigning exec indexes
2 parents 9ae1a2f + 244370c commit e4d9d0a

File tree

5 files changed

+48
-9
lines changed

5 files changed

+48
-9
lines changed

enginetest/queries/query_plans.go

Lines changed: 6 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

enginetest/queries/script_queries.go

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11112,6 +11112,40 @@ where
1111211112
},
1111311113
},
1111411114
},
11115+
11116+
{
11117+
// TODO: This test currently fails in Doltgres because Doltgres does not allow `create table...as select...`
11118+
// even though it's a valid Postgres query. Remove Dialect tag once fixed in Doltgres
11119+
// https://github.com/dolthub/doltgresql/issues/1669
11120+
Dialect: "mysql",
11121+
Name: "union field indexes",
11122+
SetUpScript: []string{
11123+
"create table t(id int primary key auto_increment, words varchar(100))",
11124+
"insert into t(words) values ('foo'),('bar'),('baz'),('zap')",
11125+
"create table t2 as select * from t",
11126+
"update t2 set words = 'boo' where id = 1",
11127+
},
11128+
Assertions: []ScriptTestAssertion{
11129+
{
11130+
Query: "select * from " +
11131+
"(select id, words from t union " +
11132+
"select id,words from t2) as combined where combined.id=1",
11133+
Expected: []sql.Row{
11134+
{1, "foo"},
11135+
{1, "boo"},
11136+
},
11137+
},
11138+
{
11139+
Query: "select * from " +
11140+
"(select 'parent' as tbl, id, words from t union " +
11141+
"select 'child' as tbl, id,words from t2) as combined where combined.id=1",
11142+
Expected: []sql.Row{
11143+
{"parent", 1, "foo"},
11144+
{"child", 1, "boo"},
11145+
},
11146+
},
11147+
},
11148+
},
1111511149
}
1111611150

1111711151
var SpatialScriptTests = []ScriptTest{

sql/analyzer/fix_exec_indexes.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -692,6 +692,8 @@ func columnIdsForNode(n sql.Node) []sql.ColumnId {
692692
default:
693693
ret = append(ret, columnIdsForNode(n.Child)...)
694694
}
695+
case *plan.SetOp:
696+
ret = append(ret, columnIdsForNode(n.Left())...)
695697
case plan.TableIdNode:
696698
if rt, ok := n.(*plan.ResolvedTable); ok && plan.IsDualTable(rt.Table) {
697699
ret = append(ret, 0)
@@ -710,6 +712,8 @@ func columnIdsForNode(n sql.Node) []sql.ColumnId {
710712
break
711713
}
712714
}
715+
// TODO: columns are appended in increasing order by ColumnId instead of how they are actually ordered. likely
716+
// needs to be fixed
713717
cols.ForEach(func(col sql.ColumnId) {
714718
ret = append(ret, col)
715719
})

sql/plan/set_op.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,8 @@ var _ sql.Node = (*SetOp)(nil)
4444
var _ sql.Expressioner = (*SetOp)(nil)
4545
var _ sql.CollationCoercible = (*SetOp)(nil)
4646

47-
// var _ sql.NameableNode = (*SetOp)(nil)
47+
// TODO: This might not be necessary now that SetOp exec indexes are assigned based on its left child node, instead of
48+
// the cols in ColSet
4849
var _ TableIdNode = (*SetOp)(nil)
4950

5051
// NewSetOp creates a new SetOp node with the given children.

sql/planbuilder/cte.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ func (b *Builder) buildRecursiveCte(inScope *scope, union *ast.SetOp, name strin
9090
// not recursive
9191
sqScope := inScope.pushSubquery()
9292
cteScope := b.buildSelectStmt(sqScope, union)
93-
b.renameSource(cteScope, name, columns)
93+
9494
switch n := cteScope.node.(type) {
9595
case *plan.SetOp:
9696
sq := plan.NewSubqueryAlias(name, "", n)
@@ -107,9 +107,9 @@ func (b *Builder) buildRecursiveCte(inScope *scope, union *ast.SetOp, name strin
107107
colset.Add(sql.ColumnId(c.id))
108108
scopeMapping[sql.ColumnId(c.id)] = c.scalarGf()
109109
}
110-
111110
cteScope.node = sq.WithScopeMapping(scopeMapping).WithId(tabId).WithColumns(colset)
112111
}
112+
b.renameSource(cteScope, name, columns)
113113
return cteScope
114114
}
115115

0 commit comments

Comments
 (0)