diff --git a/executor.go b/executor.go index 5d8c94c75..9a39d7697 100644 --- a/executor.go +++ b/executor.go @@ -3769,9 +3769,13 @@ func (vc *ValCount) smaller(other ValCount) ValCount { if vc.Count == 0 || (other.Val < vc.Val && other.Count > 0) { return other } + extra := int64(0) + if vc.Val == other.Val { + extra += other.Count + } return ValCount{ Val: vc.Val, - Count: vc.Count, + Count: vc.Count + extra, } } @@ -3780,9 +3784,13 @@ func (vc *ValCount) larger(other ValCount) ValCount { if vc.Count == 0 || (other.Val > vc.Val && other.Count > 0) { return other } + extra := int64(0) + if vc.Val == other.Val { + extra += other.Count + } return ValCount{ Val: vc.Val, - Count: vc.Count, + Count: vc.Count + extra, } } diff --git a/executor_test.go b/executor_test.go index 212b1eebf..cea26c8af 100644 --- a/executor_test.go +++ b/executor_test.go @@ -4254,3 +4254,88 @@ func TestExecutor_Execute_IncludesColumn(t *testing.T) { }) }) } + +func TestExecutor_Execute_MinMaxCountEqual(t *testing.T) { + c := test.MustRunCluster(t, 1) + defer c.Close() + hldr := test.Holder{Holder: c[0].Server.Holder()} + + idx, err := hldr.CreateIndex("i", pilosa.IndexOptions{}) + if err != nil { + t.Fatal(err) + } + + if _, err := idx.CreateField("x", pilosa.OptFieldTypeDefault()); err != nil { + t.Fatal(err) + } + + if _, err := idx.CreateField("f", pilosa.OptFieldTypeInt(-1100, 1000)); err != nil { + t.Fatal(err) + } + + if _, err := c[0].API.Query(context.Background(), &pilosa.QueryRequest{Index: "i", Query: ` + Set(0, f=3) + Set(1, f=3) + Set(2, f=4) + Set(3, f=5) + Set(4, f=5) + Set(` + strconv.Itoa(ShardWidth+1) + `, f=3) + Set(` + strconv.Itoa(ShardWidth+2) + `, f=5) + Set(` + strconv.Itoa(ShardWidth+3) + `, f=5) + Set(` + strconv.Itoa(ShardWidth+4) + `, f=5) + Set(` + strconv.Itoa(ShardWidth+5) + `, f=4) + Set(` + strconv.Itoa(2*ShardWidth+1) + `, f=3) + Set(0, x=3) + Set(1, x=3) + + `}); err != nil { + t.Fatal(err) + } + + t.Run("Min", func(t *testing.T) { + tests := []struct { + filter string + exp int64 + cnt int64 + }{ + {filter: ``, exp: 3, cnt: 4}, + {filter: `Row(x=3)`, exp: 3, cnt: 2}, + } + for i, tt := range tests { + var pql string + if tt.filter == "" { + pql = `Min(field=f)` + } else { + pql = fmt.Sprintf(`Min(%s, field=f)`, tt.filter) + } + if result, err := c[0].API.Query(context.Background(), &pilosa.QueryRequest{Index: "i", Query: pql}); err != nil { + t.Fatal(err) + } else if !reflect.DeepEqual(result.Results[0], pilosa.ValCount{Val: tt.exp, Count: tt.cnt}) { + t.Fatalf("unexpected result, test %d: %s", i, spew.Sdump(result)) + } + } + }) + + t.Run("Max", func(t *testing.T) { + tests := []struct { + filter string + exp int64 + cnt int64 + }{ + {filter: ``, exp: 5, cnt: 5}, + } + for i, tt := range tests { + var pql string + if tt.filter == "" { + pql = `Max(field=f)` + } else { + pql = fmt.Sprintf(`Max(%s, field=f)`, tt.filter) + } + if result, err := c[0].API.Query(context.Background(), &pilosa.QueryRequest{Index: "i", Query: pql}); err != nil { + t.Fatal(err) + } else if !reflect.DeepEqual(result.Results[0], pilosa.ValCount{Val: tt.exp, Count: tt.cnt}) { + t.Fatalf("unexpected result, test %d: %s", i, spew.Sdump(result)) + } + } + }) +}