Skip to content

Commit

Permalink
feat(spanner/spannertest): make SELECT list aliases visible to ORDER …
Browse files Browse the repository at this point in the history
…BY (#3054)

As noted in #3043.
  • Loading branch information
dsymonds committed Oct 20, 2020
1 parent f7a5a10 commit 7d2d83e
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 7 deletions.
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

0 comments on commit 7d2d83e

Please sign in to comment.