Skip to content

Commit 5065ef5

Browse files
committed
[binder] duplicate CTEs get distinct table and column ids
1 parent 999a371 commit 5065ef5

File tree

7 files changed

+23087
-24233
lines changed

7 files changed

+23087
-24233
lines changed

enginetest/queries/imdb_plans.go

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

enginetest/queries/integration_plans.go

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

enginetest/queries/queries.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -778,6 +778,12 @@ var SpatialQueryTests = []QueryTest{
778778
}
779779

780780
var QueryTests = []QueryTest{
781+
{
782+
Query: "WITH cte AS (SELECT * FROM xy) SELECT *, (SELECT SUM(x) FROM cte) AS xy FROM cte",
783+
Expected: []sql.Row{
784+
{0, 2, float64(6)}, {1, 0, float64(6)}, {2, 1, float64(6)}, {3, 3, float64(6)},
785+
},
786+
},
781787
{
782788
Query: "select 0 as col1, 1 as col2, 2 as col2 group by col2 having col2 = 1",
783789
Expected: []sql.Row{

enginetest/queries/query_plans.go

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

sql/planbuilder/cte.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ func (b *Builder) buildCte(inScope *scope, e ast.TableExpr, name string, columns
8585

8686
func (b *Builder) buildRecursiveCte(inScope *scope, union *ast.SetOp, name string, columns []string) *scope {
8787
l, r := splitRecursiveCteUnion(name, union)
88+
scopeMapping := make(map[sql.ColumnId]sql.Expression)
8889
if r == nil {
8990
// not recursive
9091
sqScope := inScope.pushSubquery()
@@ -104,9 +105,10 @@ func (b *Builder) buildRecursiveCte(inScope *scope, union *ast.SetOp, name strin
104105
c.tableId = tabId
105106
cteScope.cols[i] = c
106107
colset.Add(sql.ColumnId(c.id))
108+
scopeMapping[sql.ColumnId(c.id)] = c.scalarGf()
107109
}
108110

109-
cteScope.node = sq.WithId(tabId).WithColumns(colset)
111+
cteScope.node = sq.WithScopeMapping(scopeMapping).WithId(tabId).WithColumns(colset)
110112
}
111113
return cteScope
112114
}
@@ -128,7 +130,6 @@ func (b *Builder) buildRecursiveCte(inScope *scope, union *ast.SetOp, name strin
128130
cteScope := leftScope.replace()
129131
tableId := cteScope.addTable(name)
130132
var cols sql.ColSet
131-
scopeMapping := make(map[sql.ColumnId]sql.Expression)
132133
{
133134
rInit = leftScope.node
134135
recSch = make(sql.Schema, len(rInit.Schema()))
@@ -149,7 +150,6 @@ func (b *Builder) buildRecursiveCte(inScope *scope, union *ast.SetOp, name strin
149150
c.scalar = nil
150151
c.table = name
151152
toId := cteScope.newColumn(c)
152-
scopeMapping[sql.ColumnId(toId)] = c.scalarGf()
153153
cols.Add(sql.ColumnId(toId))
154154
}
155155
b.renameSource(cteScope, name, columns)

sql/planbuilder/parse_test.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,9 @@ func TestPlanBuilder(t *testing.T) {
5252
//rewrite = true
5353

5454
var tests = []planTest{
55+
{
56+
Query: "WITH cte AS (SELECT * FROM xy) SELECT *, (SELECT SUM(x) FROM cte) AS xy FROM cte",
57+
},
5558
{
5659
Query: "select 0 as col1, 1 as col2, 2 as col2 group by col2 having col2 = 1",
5760
ExpectedPlan: `

sql/planbuilder/scope.go

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -360,13 +360,19 @@ func (s *scope) aliasCte(alias string) *scope {
360360
return nil
361361
}
362362
outScope := s.copy()
363-
if _, ok := s.tables[alias]; ok || alias == "" {
364-
return outScope
365-
}
366363

367364
sq, _ := outScope.node.(*plan.SubqueryAlias)
368365

369-
tabId := outScope.addTable(alias)
366+
name := strings.ToLower(outScope.node.(sql.NameableNode).Name())
367+
368+
var tabId sql.TableId
369+
if alias != "" {
370+
tabId = outScope.addTable(alias)
371+
} else {
372+
alias = name
373+
tabId = s.tables[strings.ToLower(name)]
374+
}
375+
370376
outScope.cols = nil
371377
var colSet sql.ColSet
372378
scopeMapping := make(map[sql.ColumnId]sql.Expression)
@@ -462,6 +468,7 @@ func (s *scope) getCte(name string) *scope {
462468
if checkScope.ctes != nil {
463469
cte, ok := checkScope.ctes[strings.ToLower(name)]
464470
if ok {
471+
cte.tables[name] += 1
465472
return cte
466473
}
467474
}

0 commit comments

Comments
 (0)