Skip to content

Commit 5f27e7c

Browse files
committed
fix(engine/sqlite): fixed to be able to find relation from WITH clause
fix #2136
1 parent d12db53 commit 5f27e7c

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed

internal/engine/sqlite/convert.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -331,6 +331,25 @@ func (c *cc) convertMultiSelect_stmtContext(n *parser.Select_stmtContext) ast.No
331331
var where ast.Node
332332
var groups = []ast.Node{}
333333
var having ast.Node
334+
var ctes []ast.Node
335+
336+
if ct := n.Common_table_stmt(); ct != nil {
337+
recursive := ct.RECURSIVE_() != nil
338+
for _, cte := range ct.AllCommon_table_expression() {
339+
tableName := identifier(cte.Table_name().GetText())
340+
var cteCols ast.List
341+
for _, col := range cte.AllColumn_name() {
342+
cteCols.Items = append(cteCols.Items, NewIdentifer(col.GetText()))
343+
}
344+
ctes = append(ctes, &ast.CommonTableExpr{
345+
Ctename: &tableName,
346+
Ctequery: c.convert(cte.Select_stmt()),
347+
Location: cte.GetStart().GetStart(),
348+
Cterecursive: recursive,
349+
Ctecolnames: &cteCols,
350+
})
351+
}
352+
}
334353

335354
for _, icore := range n.AllSelect_core() {
336355
core, ok := icore.(*parser.Select_coreContext)
@@ -377,6 +396,9 @@ func (c *cc) convertMultiSelect_stmtContext(n *parser.Select_stmtContext) ast.No
377396
LimitCount: limitCount,
378397
LimitOffset: limitOffset,
379398
ValuesLists: &ast.List{},
399+
WithClause: &ast.WithClause{
400+
Ctes: &ast.List{Items: ctes},
401+
},
380402
}
381403
}
382404

0 commit comments

Comments
 (0)