Skip to content

Commit

Permalink
spanner/spannertest: support SELECT * queries
Browse files Browse the repository at this point in the history
These need special handling in the query evaluator since the expression
list doesn't have a 1:1 mapping to the output.

Change-Id: I38f6226d9b990c0d8ae5b10d4971635aeb9d461f
Reviewed-on: https://code-review.googlesource.com/c/gocloud/+/49190
Reviewed-by: kokoro <noreply+kokoro@google.com>
Reviewed-by: Knut Olav Løite <koloite@gmail.com>
  • Loading branch information
dsymonds committed Dec 6, 2019
1 parent 8988dea commit be07a62
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 11 deletions.
1 change: 0 additions & 1 deletion spanner/spannertest/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ by ascending esotericism:
- SELECT aliases
- subselects
- set operations (UNION, INTERSECT, EXCEPT)
- SELECT star expressions
- partition support
- conditional expressions
- table sampling (implementation)
Expand Down
39 changes: 29 additions & 10 deletions spanner/spannertest/db_eval.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,13 +97,21 @@ func (d *database) evalSelect(sel spansql.Select, params queryParams, aux []span
params: params,
}

for _, e := range sel.List {
ci, err := ec.colInfo(e)
if err != nil {
return nil, err
// Is this a `SELECT *` query?
selectStar := len(sel.List) == 1 && sel.List[0] == spansql.Star

if selectStar {
// Every column will appear in the output.
ri.Cols = append([]colInfo(nil), ec.table.cols...)
} else {
for _, e := range sel.List {
ci, err := ec.colInfo(e)
if err != nil {
return nil, err
}
// TODO: deal with ci.Name == ""?
ri.Cols = append(ri.Cols, ci)
}
// TODO: deal with ci.Name == ""?
ri.Cols = append(ri.Cols, ci)
}
for _, r := range t.rows {
ec.row = r
Expand All @@ -120,16 +128,27 @@ func (d *database) evalSelect(sel spansql.Select, params queryParams, aux []span
}

// Evaluate SELECT expression list on the row.
out, err := ec.evalExprList(sel.List)
if err != nil {
return nil, err
var res resultRow
if selectStar {
var out []interface{}
for i := range r {
out = append(out, r.copyDataElem(i))
}
res.data = out
} else {
out, err := ec.evalExprList(sel.List)
if err != nil {
return nil, err
}
res.data = out
}
a, err := ec.evalExprList(aux)
if err != nil {
return nil, err
}
res.aux = a

ri.rows = append(ri.rows, resultRow{data: out, aux: a})
ri.rows = append(ri.rows, res)
}

return ri, nil
Expand Down
9 changes: 9 additions & 0 deletions spanner/spannertest/db_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,15 @@ func TestTableData(t *testing.T) {
{int64(4)},
},
},
{
`SELECT * FROM Staff WHERE Name LIKE "S%"`,
nil,
[][]interface{}{
// These are returned in table column order.
// Note that the primary key columns get sorted first.
{"Sam", int64(3), int64(9), false, 1.75, nil},
},
},
{
`SELECT Name FROM Staff WHERE FirstSeen >= @min`,
queryParams{"min": "1996-01-01"},
Expand Down
8 changes: 8 additions & 0 deletions spanner/spansql/parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,14 @@ func TestParseQuery(t *testing.T) {
},
},
},
{`SELECT * FROM Packages`,
Query{
Select: Select{
List: []Expr{Star},
From: []SelectFrom{{Table: "Packages"}},
},
},
},
}
for _, test := range tests {
got, err := ParseQuery(test.in)
Expand Down

0 comments on commit be07a62

Please sign in to comment.