Skip to content

Commit

Permalink
fix compileMetadata bug for aggregate views (apache#1528)
Browse files Browse the repository at this point in the history
  • Loading branch information
beltran authored Jan 26, 2021
1 parent 994808f commit f242734
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 10 deletions.
25 changes: 19 additions & 6 deletions cassandra_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2310,10 +2310,9 @@ func TestAggregateMetadata(t *testing.T) {
if aggregates == nil {
t.Fatal("failed to query aggregate metadata, nil returned")
}
if len(aggregates) != 1 {
t.Fatal("expected only a single aggregate")
if len(aggregates) != 2 {
t.Fatal("expected two aggregates")
}
aggregate := aggregates[0]

expectedAggregrate := AggregateMetadata{
Keyspace: "gocql_test",
Expand All @@ -2338,8 +2337,12 @@ func TestAggregateMetadata(t *testing.T) {
expectedAggregrate.InitCond = string([]byte{0, 0, 0, 4, 0, 0, 0, 0, 0, 0, 0, 8, 0, 0, 0, 0, 0, 0, 0, 0})
}

if !reflect.DeepEqual(aggregate, expectedAggregrate) {
t.Fatalf("aggregate is %+v, but expected %+v", aggregate, expectedAggregrate)
if !reflect.DeepEqual(aggregates[0], expectedAggregrate) {
t.Fatalf("aggregate 'average' is %+v, but expected %+v", aggregates[0], expectedAggregrate)
}
expectedAggregrate.Name = "average2"
if !reflect.DeepEqual(aggregates[1], expectedAggregrate) {
t.Fatalf("aggregate 'average2' is %+v, but expected %+v", aggregates[1], expectedAggregrate)
}
}

Expand Down Expand Up @@ -2486,7 +2489,17 @@ func TestKeyspaceMetadata(t *testing.T) {

aggregate, found := keyspaceMetadata.Aggregates["average"]
if !found {
t.Fatal("failed to find the aggreate in metadata")
t.Fatal("failed to find the aggregate 'average' in metadata")
}
if aggregate.FinalFunc.Name != "avgfinal" {
t.Fatalf("expected final function %s, but got %s", "avgFinal", aggregate.FinalFunc.Name)
}
if aggregate.StateFunc.Name != "avgstate" {
t.Fatalf("expected state function %s, but got %s", "avgstate", aggregate.StateFunc.Name)
}
aggregate, found = keyspaceMetadata.Aggregates["average2"]
if !found {
t.Fatal("failed to find the aggregate 'average2' in metadata")
}
if aggregate.FinalFunc.Name != "avgfinal" {
t.Fatalf("expected final function %s, but got %s", "avgFinal", aggregate.FinalFunc.Name)
Expand Down
9 changes: 9 additions & 0 deletions common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,15 @@ func createAggregate(t *testing.T, session *Session) {
`).Exec(); err != nil {
t.Fatalf("failed to create aggregate with err: %v", err)
}
if err := session.Query(`
CREATE OR REPLACE AGGREGATE gocql_test.average2(int)
SFUNC avgState
STYPE tuple<int,bigint>
FINALFUNC avgFinal
INITCOND (0,0);
`).Exec(); err != nil {
t.Fatalf("failed to create aggregate with err: %v", err)
}
}

func staticAddressTranslator(newAddr net.IP, newPort int) AddressTranslator {
Expand Down
8 changes: 4 additions & 4 deletions metadata.go
Original file line number Diff line number Diff line change
Expand Up @@ -324,10 +324,10 @@ func compileMetadata(
keyspace.Functions[functions[i].Name] = &functions[i]
}
keyspace.Aggregates = make(map[string]*AggregateMetadata, len(aggregates))
for _, aggregate := range aggregates {
aggregate.FinalFunc = *keyspace.Functions[aggregate.finalFunc]
aggregate.StateFunc = *keyspace.Functions[aggregate.stateFunc]
keyspace.Aggregates[aggregate.Name] = &aggregate
for i, _ := range aggregates {
aggregates[i].FinalFunc = *keyspace.Functions[aggregates[i].finalFunc]
aggregates[i].StateFunc = *keyspace.Functions[aggregates[i].stateFunc]
keyspace.Aggregates[aggregates[i].Name] = &aggregates[i]
}
keyspace.Views = make(map[string]*ViewMetadata, len(views))
for i := range views {
Expand Down

0 comments on commit f242734

Please sign in to comment.