Skip to content

Commit

Permalink
fix(compiler): Fix to not scan children under ast.RangeSubselect when…
Browse files Browse the repository at this point in the history
… retrieving table listing (#2573)

* fix(compiler): Fix to not scan children under ast.RangeSubselect when retrieving table listing

close #2569

* test: add endtoend
  • Loading branch information
orisano authored Aug 28, 2023
1 parent c619fce commit fb9e375
Show file tree
Hide file tree
Showing 11 changed files with 164 additions and 8 deletions.
28 changes: 20 additions & 8 deletions internal/compiler/output_columns.go
Original file line number Diff line number Diff line change
Expand Up @@ -454,6 +454,23 @@ func isTableRequired(n ast.Node, col *Column, prior int) int {
return tableNotFound
}

type tableVisitor struct {
list ast.List
}

func (r *tableVisitor) Visit(n ast.Node) astutils.Visitor {
switch n.(type) {
case *ast.RangeVar, *ast.RangeFunction:
r.list.Items = append(r.list.Items, n)
return r
case *ast.RangeSubselect:
r.list.Items = append(r.list.Items, n)
return nil
default:
return r
}
}

// Compute the output columns for a statement.
//
// Return an error if column references are ambiguous
Expand All @@ -470,14 +487,9 @@ func (c *Compiler) sourceTables(qc *QueryCatalog, node ast.Node) ([]*Table, erro
Items: []ast.Node{n.Relation},
}
case *ast.SelectStmt:
list = astutils.Search(n.FromClause, func(node ast.Node) bool {
switch node.(type) {
case *ast.RangeVar, *ast.RangeSubselect, *ast.RangeFunction:
return true
default:
return false
}
})
var tv tableVisitor
astutils.Walk(&tv, n.FromClause)
list = &tv.list
case *ast.TruncateStmt:
list = astutils.Search(n.Relations, func(node ast.Node) bool {
_, ok := node.(*ast.RangeVar)
Expand Down
27 changes: 27 additions & 0 deletions internal/endtoend/testdata/select_star/mysql/go/query.sql.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions internal/endtoend/testdata/select_star/mysql/query.sql
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
/* name: GetAll :many */
SELECT * FROM users;

/* name: GetIDAll :many */
SELECT * FROM (SELECT id FROM users) t;

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
-- name: GetAll :many
SELECT * FROM users;

/* name: GetIDAll :many */
SELECT * FROM (SELECT id FROM users) t;

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
-- name: GetAll :many
SELECT * FROM users;

/* name: GetIDAll :many */
SELECT * FROM (SELECT id FROM users) t;

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
-- name: GetAll :many
SELECT * FROM users;

/* name: GetIDAll :many */
SELECT * FROM (SELECT id FROM users) t;
27 changes: 27 additions & 0 deletions internal/endtoend/testdata/select_star/sqlite/go/query.sql.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions internal/endtoend/testdata/select_star/sqlite/query.sql
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
-- name: GetAll :many
SELECT * FROM users;

/* name: GetIDAll :many */
SELECT * FROM (SELECT id FROM users) t;

0 comments on commit fb9e375

Please sign in to comment.