Merge pull request #57 from tgruben/fix-minmax-count

return total match counts for either min or max
This commit is contained in:
Travis Turner 2019-12-13 15:28:27 -06:00 committed by GitHub
commit c5aeed0715
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 95 additions and 2 deletions

View file

@ -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,
}
}

View file

@ -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))
}
}
})
}