Skip to content

Commit

Permalink
Add support for WITH CTE remapping
Browse files Browse the repository at this point in the history
  • Loading branch information
arjunlol committed Dec 20, 2024
1 parent 5b1ce01 commit d8ffd79
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 0 deletions.
5 changes: 5 additions & 0 deletions src/query_handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -512,6 +512,11 @@ func TestHandleQuery(t *testing.T) {
"description": {"gss_authenticated", "encrypted"},
"values": {},
},
// WITH
"WITH RECURSIVE simple_cte AS (SELECT oid, rolname FROM pg_roles WHERE rolname = 'postgres' UNION ALL SELECT oid, rolname FROM pg_roles) SELECT * FROM simple_cte": {
"description": {"oid", "rolname"},
"values": {"10", "bemidb"},
},
}

for query, responses := range responsesByQuery {
Expand Down
10 changes: 10 additions & 0 deletions src/select_remapper.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,16 @@ func (selectRemapper *SelectRemapper) remapSelectStatement(selectStatement *pgQu
selectStatement = selectRemapper.remapperWhere.RemapWhereExpressions(selectStatement, selectStatement.WhereClause, indentLevel)
}

// WITH
if selectStatement.WithClause != nil {
selectRemapper.traceTreeTraversal("WITH CTE's", indentLevel)
for _, cte := range selectStatement.WithClause.Ctes {
if cteSelect := cte.GetCommonTableExpr().Ctequery.GetSelectStmt(); cteSelect != nil {
selectRemapper.remapSelectStatement(cteSelect, indentLevel+1) // recursive
}
}
}

// FROM
if len(selectStatement.FromClause) > 0 {
for i, fromNode := range selectStatement.FromClause {
Expand Down

0 comments on commit d8ffd79

Please sign in to comment.