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

Commit 21c3ce6

Browse files
committed
sql/analyzer: only check aliases to qualify in the topmost project
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 <[email protected]>
1 parent 3fd2047 commit 21c3ce6

File tree

2 files changed

+25
-7
lines changed

2 files changed

+25
-7
lines changed

Diff for: engine_test.go

+20
Original file line numberDiff line numberDiff line change
@@ -929,6 +929,26 @@ var queries = []struct {
929929
`SELECT i AS i FROM mytable ORDER BY i`,
930930
[]sql.Row{{int64(1)}, {int64(2)}, {int64(3)}},
931931
},
932+
{
933+
`
934+
SELECT
935+
i,
936+
foo
937+
FROM (
938+
SELECT
939+
i,
940+
COUNT(DISTINCT s) AS foo
941+
FROM mytable
942+
GROUP BY i
943+
) AS q
944+
ORDER BY foo DESC
945+
`,
946+
[]sql.Row{
947+
{int64(1), int64(1)},
948+
{int64(2), int64(1)},
949+
{int64(3), int64(1)},
950+
},
951+
},
932952
}
933953

934954
func TestQueries(t *testing.T) {

Diff for: sql/analyzer/resolve_columns.go

+5-7
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,11 @@ import (
66
"strings"
77

88
"gopkg.in/src-d/go-errors.v1"
9+
"gopkg.in/src-d/go-mysql-server.v0/internal/similartext"
910
"gopkg.in/src-d/go-mysql-server.v0/sql"
1011
"gopkg.in/src-d/go-mysql-server.v0/sql/expression"
1112
"gopkg.in/src-d/go-mysql-server.v0/sql/plan"
1213
"gopkg.in/src-d/go-vitess.v1/vt/sqlparser"
13-
"gopkg.in/src-d/go-mysql-server.v0/internal/similartext"
1414
)
1515

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

315315
var found bool
316-
plan.InspectExpressions(x, func(e sql.Expression) bool {
317-
alias, ok := e.(*expression.Alias)
316+
for _, expr := range x.(sql.Expressioner).Expressions() {
317+
alias, ok := expr.(*expression.Alias)
318318
if ok && strings.ToLower(alias.Name()) == strings.ToLower(col.Name()) {
319319
found = true
320-
return false
320+
break
321321
}
322-
323-
return true
324-
})
322+
}
325323

326324
return found
327325
}

0 commit comments

Comments
 (0)