Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 52 additions & 0 deletions aggregation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,58 @@ func TestSQLiteNativeAggregation(t *testing.T) {
}
}

func TestSQLiteNativeAggregationRowsHaveDistinctSyntheticKeys(t *testing.T) {
raw, err := sql.Open("sqlite", ":memory:")
if err != nil {
t.Fatal(err)
}
defer func() {
if err := raw.Close(); err != nil {
t.Errorf("close sqlite: %v", err)
}
}()
if _, err := raw.Exec(`CREATE TABLE orders (country TEXT, quantity INTEGER);
INSERT INTO orders VALUES ('IE', 1), ('US', 2);`); err != nil {
t.Fatal(err)
}
count := dal.Count()
count.Alias = "orders"
q := dal.From(dal.NewRootCollectionRef("orders", "")).NewQuery().
GroupBy(dal.Field("country")).
OrderBy(dal.Ascending(dal.Field("country"))).
SelectColumns(dal.Column{Expression: dal.Field("country")}, count)
db := NewDatabase(raw, newSchema(), DbOptions{StructuredQueryDialect: "sqlite"})
reader, err := db.ExecuteQueryToRecordsReader(context.Background(), q)
if err != nil {
t.Fatal(err)
}
defer func() {
if err := reader.Close(); err != nil {
t.Errorf("close aggregation reader: %v", err)
}
}()
for ordinal, country := range []string{"IE", "US"} {
record, err := reader.Next()
if err != nil {
t.Fatal(err)
}
wantKey := []string{"0", "1"}[ordinal]
if got := record.Key().ID; got != wantKey {
t.Fatalf("row %d key ID = %#v, want %q", ordinal, got, wantKey)
}
row := record.Data().(map[string]any)
if got := row["country"]; got != country {
t.Fatalf("row %d country = %#v, want %q", ordinal, got, country)
}
if _, ok := row[recordIDHelperColumn]; ok {
t.Fatalf("row %d unexpectedly exposes %q: %#v", ordinal, recordIDHelperColumn, row)
}
}
if _, err := reader.Next(); err != dal.ErrNoMoreRecords {
t.Fatalf("next err = %v", err)
}
}

func TestSQLiteUngroupedEmptyAggregation(t *testing.T) {
raw, err := sql.Open("sqlite", ":memory:")
if err != nil {
Expand Down
17 changes: 15 additions & 2 deletions reader_records.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"fmt"
"math"
"reflect"
"strconv"

"github.com/dal-go/dalgo/dal"
dalrecord "github.com/dal-go/record"
Expand All @@ -32,8 +33,20 @@ func getRecordsReaderWithOptions(ctx context.Context, query dal.Query, execute e
rr.newRecord = func() dalrecord.Record { return q.IntoRecord() }
} else if from := q.From(); from != nil && from.Base() != nil {
collection := from.Base().Name()
rr.newRecord = func() dalrecord.Record {
return dalrecord.NewRecordWithData(dalrecord.NewKeyWithID(collection, recordIDHelperColumn), make(map[string]any))
if rr.validateFinite {
// Aggregate result rows do not have a source record identity. Give
// each result row a deterministic synthetic key without exposing a
// helper column in its data.
ordinal := 0
rr.newRecord = func() dalrecord.Record {
record := dalrecord.NewRecordWithData(dalrecord.NewKeyWithID(collection, strconv.Itoa(ordinal)), make(map[string]any))
ordinal++
return record
}
} else {
rr.newRecord = func() dalrecord.Record {
return dalrecord.NewRecordWithData(dalrecord.NewKeyWithID(collection, recordIDHelperColumn), make(map[string]any))
}
}
}
if primaryKey := primaryKeyForQuery(options, query); primaryKey != "" && !dal.HasAggregation(q) {
Expand Down
Loading