feat(spanner/spannertest): make SELECT list aliases visible to ORDER BY (#3054)
As noted in #3043.
diff --git a/spanner/spannertest/db_query.go b/spanner/spannertest/db_query.go
index dc207e8..b9bbc01 100644
--- a/spanner/spannertest/db_query.go
+++ b/spanner/spannertest/db_query.go
@@ -461,18 +461,19 @@
}
}
+ // 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
diff --git a/spanner/spannertest/integration_test.go b/spanner/spannertest/integration_test.go
index 349e7f6..6ee0d6f 100644
--- a/spanner/spannertest/integration_test.go
+++ b/spanner/spannertest/integration_test.go
@@ -871,12 +871,23 @@
},
// 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`,