Skip to content
This repository has been archived by the owner on Jan 28, 2021. It is now read-only.

Commit

Permalink
sql/analyzer: only check aliases to qualify in the topmost project
Browse files Browse the repository at this point in the history
Because aliases on the topmost project or groupby were checked using
plan.InspectExpressions, it did so recursively. That was not the
intended behaviour, since it could find other aliases in a subquery
or other nodes down the tree.
Instead, now only the expressions of the project or groupby are
checked.

Signed-off-by: Miguel Molina <miguel@erizocosmi.co>
  • Loading branch information
erizocosmico committed Apr 23, 2019
1 parent 3fd2047 commit 21c3ce6
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 7 deletions.
20 changes: 20 additions & 0 deletions engine_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -929,6 +929,26 @@ var queries = []struct {
`SELECT i AS i FROM mytable ORDER BY i`,
[]sql.Row{{int64(1)}, {int64(2)}, {int64(3)}},
},
{
`
SELECT
i,
foo
FROM (
SELECT
i,
COUNT(DISTINCT s) AS foo
FROM mytable
GROUP BY i
) AS q
ORDER BY foo DESC
`,
[]sql.Row{
{int64(1), int64(1)},
{int64(2), int64(1)},
{int64(3), int64(1)},
},
},
}

func TestQueries(t *testing.T) {
Expand Down
12 changes: 5 additions & 7 deletions sql/analyzer/resolve_columns.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@ import (
"strings"

"gopkg.in/src-d/go-errors.v1"
"gopkg.in/src-d/go-mysql-server.v0/internal/similartext"
"gopkg.in/src-d/go-mysql-server.v0/sql"
"gopkg.in/src-d/go-mysql-server.v0/sql/expression"
"gopkg.in/src-d/go-mysql-server.v0/sql/plan"
"gopkg.in/src-d/go-vitess.v1/vt/sqlparser"
"gopkg.in/src-d/go-mysql-server.v0/internal/similartext"
)

func checkAliases(ctx *sql.Context, a *Analyzer, n sql.Node) (sql.Node, error) {
Expand Down Expand Up @@ -313,15 +313,13 @@ func isDefinedInChildProject(n sql.Node, col *expression.UnresolvedColumn) bool
}

var found bool
plan.InspectExpressions(x, func(e sql.Expression) bool {
alias, ok := e.(*expression.Alias)
for _, expr := range x.(sql.Expressioner).Expressions() {
alias, ok := expr.(*expression.Alias)
if ok && strings.ToLower(alias.Name()) == strings.ToLower(col.Name()) {
found = true
return false
break
}

return true
})
}

return found
}
Expand Down

0 comments on commit 21c3ce6

Please sign in to comment.