Skip to content

Commit fb9e375

Browse files
authored
fix(compiler): Fix to not scan children under ast.RangeSubselect when retrieving table listing (#2573)
* fix(compiler): Fix to not scan children under ast.RangeSubselect when retrieving table listing close #2569 * test: add endtoend
1 parent c619fce commit fb9e375

File tree

11 files changed

+164
-8
lines changed

11 files changed

+164
-8
lines changed

internal/compiler/output_columns.go

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -454,6 +454,23 @@ func isTableRequired(n ast.Node, col *Column, prior int) int {
454454
return tableNotFound
455455
}
456456

457+
type tableVisitor struct {
458+
list ast.List
459+
}
460+
461+
func (r *tableVisitor) Visit(n ast.Node) astutils.Visitor {
462+
switch n.(type) {
463+
case *ast.RangeVar, *ast.RangeFunction:
464+
r.list.Items = append(r.list.Items, n)
465+
return r
466+
case *ast.RangeSubselect:
467+
r.list.Items = append(r.list.Items, n)
468+
return nil
469+
default:
470+
return r
471+
}
472+
}
473+
457474
// Compute the output columns for a statement.
458475
//
459476
// Return an error if column references are ambiguous
@@ -470,14 +487,9 @@ func (c *Compiler) sourceTables(qc *QueryCatalog, node ast.Node) ([]*Table, erro
470487
Items: []ast.Node{n.Relation},
471488
}
472489
case *ast.SelectStmt:
473-
list = astutils.Search(n.FromClause, func(node ast.Node) bool {
474-
switch node.(type) {
475-
case *ast.RangeVar, *ast.RangeSubselect, *ast.RangeFunction:
476-
return true
477-
default:
478-
return false
479-
}
480-
})
490+
var tv tableVisitor
491+
astutils.Walk(&tv, n.FromClause)
492+
list = &tv.list
481493
case *ast.TruncateStmt:
482494
list = astutils.Search(n.Relations, func(node ast.Node) bool {
483495
_, ok := node.(*ast.RangeVar)

internal/endtoend/testdata/select_star/mysql/go/query.sql.go

Lines changed: 27 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,5 @@
11
/* name: GetAll :many */
22
SELECT * FROM users;
3+
4+
/* name: GetIDAll :many */
5+
SELECT * FROM (SELECT id FROM users) t;

internal/endtoend/testdata/select_star/postgresql/pgx/v4/go/query.sql.go

Lines changed: 24 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,5 @@
11
-- name: GetAll :many
22
SELECT * FROM users;
3+
4+
/* name: GetIDAll :many */
5+
SELECT * FROM (SELECT id FROM users) t;

internal/endtoend/testdata/select_star/postgresql/pgx/v5/go/query.sql.go

Lines changed: 24 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,5 @@
11
-- name: GetAll :many
22
SELECT * FROM users;
3+
4+
/* name: GetIDAll :many */
5+
SELECT * FROM (SELECT id FROM users) t;

internal/endtoend/testdata/select_star/postgresql/stdlib/go/query.sql.go

Lines changed: 27 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,5 @@
11
-- name: GetAll :many
22
SELECT * FROM users;
3+
4+
/* name: GetIDAll :many */
5+
SELECT * FROM (SELECT id FROM users) t;

internal/endtoend/testdata/select_star/sqlite/go/query.sql.go

Lines changed: 27 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,5 @@
11
-- name: GetAll :many
22
SELECT * FROM users;
3+
4+
/* name: GetIDAll :many */
5+
SELECT * FROM (SELECT id FROM users) t;

0 commit comments

Comments
 (0)