adds filter support to MinRow and MaxRow

This commit is contained in:
Yuce Tekol 2019-06-03 16:29:04 +03:00
parent 3e738e9760
commit b64a3e0c68
No known key found for this signature in database
GPG key ID: CB59E46D2FB90573
2 changed files with 62 additions and 10 deletions

View file

@ -762,6 +762,15 @@ func (e *executor) executeMaxShard(ctx context.Context, index string, c *pql.Cal
// executeMinRowShard returns the minimum row ID for a shard.
func (e *executor) executeMinRowShard(ctx context.Context, index string, c *pql.Call, shard uint64) (Pair, error) {
var filter *Row
if len(c.Children) == 1 {
row, err := e.executeBitmapCallShard(ctx, index, c.Children[0], shard)
if err != nil {
return Pair{}, err
}
filter = row
}
fieldName, _ := c.Args["field"].(string)
field := e.Holder.Field(index, fieldName)
if field == nil {
@ -773,18 +782,24 @@ func (e *executor) executeMinRowShard(ctx context.Context, index string, c *pql.
return Pair{}, nil
}
count := uint64(1)
if !fragment.hasRowID {
count = 0
}
minRowID, count := fragment.minRow(filter)
return Pair{
ID: fragment.minRowID,
ID: minRowID,
Count: count,
}, nil
}
// executeMaxRowShard returns the minimum row ID for a shard.
func (e *executor) executeMaxRowShard(ctx context.Context, index string, c *pql.Call, shard uint64) (Pair, error) {
var filter *Row
if len(c.Children) == 1 {
row, err := e.executeBitmapCallShard(ctx, index, c.Children[0], shard)
if err != nil {
return Pair{}, err
}
filter = row
}
fieldName, _ := c.Args["field"].(string)
field := e.Holder.Field(index, fieldName)
if field == nil {
@ -796,12 +811,9 @@ func (e *executor) executeMaxRowShard(ctx context.Context, index string, c *pql.
return Pair{}, nil
}
count := uint64(1)
if !fragment.hasRowID {
count = 0
}
maxRowID, count := fragment.maxRow(filter)
return Pair{
ID: fragment.maxRowID,
ID: maxRowID,
Count: count,
}, nil
}

View file

@ -1036,6 +1036,46 @@ func (f *fragment) maxUnsigned(filter *Row, bitDepth uint) (max int64, count uin
return max, count
}
// minRow returns minRowID of the rows in the filter and its count.
// if filter is nil, it returns fragment.minRowID, 1
// if fragment has no rows, it returns 0, 0
func (f *fragment) minRow(filter *Row) (uint64, uint64) {
if f.hasRowID {
if filter == nil {
return f.minRowID, 1
}
// iterate from min row ID and return the first that intersects with filter.
for i := f.minRowID; i <= f.maxRowID; i++ {
row := f.row(i).Intersect(filter)
count := row.Count()
if count > 0 {
return i, count
}
}
}
return 0, 0
}
// maxRow returns maxRowID of the rows in the filter and its count.
// if filter is nil, it returns fragment.maxRowID, 1
// if fragment has no rows, it returns 0, 0
func (f *fragment) maxRow(filter *Row) (uint64, uint64) {
if f.hasRowID {
if filter == nil {
return f.maxRowID, 1
}
// iterate back from max row ID and return the first that intersects with filter.
for i := f.maxRowID; i >= f.minRowID; i-- {
row := f.row(i).Intersect(filter)
count := row.Count()
if count > 0 {
return i, count
}
}
}
return 0, 0
}
// rangeOp returns bitmaps with a bsiGroup value encoding matching the predicate.
func (f *fragment) rangeOp(op pql.Token, bitDepth uint, predicate int64) (*Row, error) {
switch op {