Skip to content

Commit c799663

Browse files
authored
[cte] fix issue with invalid cte scope mapping (#2857)
1 parent 9869b54 commit c799663

File tree

3 files changed

+103
-0
lines changed

3 files changed

+103
-0
lines changed

enginetest/queries/queries.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1072,6 +1072,16 @@ Select * from (
10721072
) dt;`,
10731073
Expected: []sql.Row{{1}},
10741074
},
1075+
{
1076+
Query: `
1077+
WITH RECURSIVE cte(d) AS (
1078+
SELECT 0
1079+
UNION ALL
1080+
SELECT cte.d + 1 FROM cte limit 3
1081+
)
1082+
SELECT * FROM cte WHERE d = 2;`,
1083+
Expected: []sql.Row{{2}},
1084+
},
10751085
{
10761086
// https://github.com/dolthub/dolt/issues/5642
10771087
Query: "SELECT count(*) FROM mytable WHERE i = 3720481604718463778705849469618542795;",

enginetest/queries/query_plans.go

Lines changed: 90 additions & 0 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 & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,9 @@ func (b *Builder) buildRecursiveCte(inScope *scope, union *ast.SetOp, name strin
154154
}
155155
b.renameSource(cteScope, name, columns)
156156

157+
for _, c := range cteScope.cols {
158+
scopeMapping[sql.ColumnId(c.id)] = c.scalarGf()
159+
}
157160
rTable = plan.NewRecursiveTable(name, recSch)
158161
cteScope.node = rTable.WithId(tableId).WithColumns(cols)
159162
}

0 commit comments

Comments
 (0)