Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(spanner/spannertest): make SELECT list aliases visible to ORDER BY #3054

Merged
merged 1 commit into from Oct 20, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
15 changes: 8 additions & 7 deletions spanner/spannertest/db_query.go
Expand Up @@ -461,18 +461,19 @@ func (d *database) evalSelect(sel spansql.Select, qc *queryContext) (si *selIter
}
}

// Load aliases visible to any future iterators,
// including GROUP BY and ORDER BY. These are not visible to the WHERE clause.
ec.aliases = make(map[spansql.ID]spansql.Expr)
for i, alias := range sel.ListAliases {
ec.aliases[alias] = sel.List[i]
}
// TODO: Add aliases for "1", "2", etc.

// Apply GROUP BY.
// This only reorders rows to group rows together;
// aggregation happens next.
var rowGroups [][2]int // Sequence of half-open intervals of row numbers.
if len(sel.GroupBy) > 0 {
// Load aliases visible to this GROUP BY.
ec.aliases = make(map[spansql.ID]spansql.Expr)
for i, alias := range sel.ListAliases {
ec.aliases[alias] = sel.List[i]
}
// TODO: Add aliases for "1", "2", etc.

raw, err := toRawIter(ri)
if err != nil {
return nil, err
Expand Down
11 changes: 11 additions & 0 deletions spanner/spannertest/integration_test.go
Expand Up @@ -871,12 +871,23 @@ func TestIntegration_ReadsAndQueries(t *testing.T) {
},
// SELECT with aliases.
{
// Aliased table.
`SELECT s.Name FROM Staff AS s WHERE s.ID = 3 ORDER BY s.Tenure`,
nil,
[][]interface{}{
{"Sam"},
},
},
{
// Aliased expression.
`SELECT Name AS nom FROM Staff WHERE ID < 4 ORDER BY nom`,
nil,
[][]interface{}{
{"Daniel"},
{"Jack"},
{"Sam"},
},
},
// Joins.
{
`SELECT * FROM JoinA INNER JOIN JoinB ON JoinA.w = JoinB.y ORDER BY w, x, y, z`,
Expand Down