Skip to content
Permalink
Browse files
feat(spanner/spannertest): make SELECT list aliases visible to ORDER …
…BY (#3054)

As noted in #3043.
  • Loading branch information
dsymonds committed Oct 20, 2020
1 parent f7a5a10 commit 7d2d83ee1cce58d4014d5570bc599bcef1ed9c22
Showing with 19 additions and 7 deletions.
  1. +8 −7 spanner/spannertest/db_query.go
  2. +11 −0 spanner/spannertest/integration_test.go
@@ -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
@@ -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`,

0 comments on commit 7d2d83e

Please sign in to comment.