Skip to content

Commit

Permalink
fix(embedded/sql): return invalid value when using aggregated col sel…
Browse files Browse the repository at this point in the history
…ector in temporal queries

Signed-off-by: Jeronimo Irazabal <[email protected]>
  • Loading branch information
jeroiraz committed May 23, 2022
1 parent 740ba5c commit 217b1a7
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
12 changes: 12 additions & 0 deletions embedded/sql/engine_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5085,6 +5085,18 @@ func TestTemporalQueriesEdgeCases(t *testing.T) {
params: map[string]interface{}{"tx": -1},
err: ErrIllegalArguments,
},
{
title: "querying data with col selector as tx id should return error",
query: "SELECT id, title FROM table1 SINCE TX id",
params: nil,
err: ErrInvalidValue,
},
{
title: "querying data with aggregations as tx id should return error",
query: "SELECT id, title FROM table1 SINCE TX COUNT(*)",
params: nil,
err: ErrInvalidValue,
},
{
title: "querying data with invalid tx id should return error",
query: "SELECT id, title FROM table1 AFTER TX @tx",
Expand Down
6 changes: 5 additions & 1 deletion embedded/sql/stmt.go
Original file line number Diff line number Diff line change
Expand Up @@ -2599,7 +2599,7 @@ func (sel *ColSelector) substitute(params map[string]interface{}) (ValueExp, err

func (sel *ColSelector) reduce(catalog *Catalog, row *Row, implicitDB, implicitTable string) (TypedValue, error) {
if row == nil {
return nil, ErrInvalidValue
return nil, fmt.Errorf("%w: no row to evaluate in current context", ErrInvalidValue)
}

aggFn, db, table, col := sel.resolve(implicitDB, implicitTable)
Expand Down Expand Up @@ -2706,6 +2706,10 @@ func (sel *AggColSelector) substitute(params map[string]interface{}) (ValueExp,
}

func (sel *AggColSelector) reduce(catalog *Catalog, row *Row, implicitDB, implicitTable string) (TypedValue, error) {
if row == nil {
return nil, fmt.Errorf("%w: no row to evaluate aggregation (%s) in current context", ErrInvalidValue, sel.aggFn)
}

v, ok := row.ValuesBySelector[EncodeSelector(sel.resolve(implicitDB, implicitTable))]
if !ok {
return nil, fmt.Errorf("%w (%s)", ErrColumnDoesNotExist, sel.col)
Expand Down

0 comments on commit 217b1a7

Please sign in to comment.