Skip to content
Open
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
23 changes: 8 additions & 15 deletions frac/processor/batch_eval_tree.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,14 @@ var errBatchingUnsupported = errors.New("batching unsupported")
// maxBatchedTIDsPerLeaf limits number of TIDs for a single OrMulti node
const maxBatchedTIDsPerLeaf = 5

type leafTIDsCache map[parser.Token][]uint32

// tryBuildBatchEvalTree tries to build a batched eval tree if possible.
//
// Returns errBatchingUnsupported when the non-batched path should be used.
func tryBuildBatchEvalTree(
root *parser.ASTNode,
fracVer config.BinaryDataVersion,
ti tokenIndex,
cache tidsCache,
queryOpts QueryOptimizationConfig,
minLID, maxLID uint32,
stats *searchStats,
Expand All @@ -43,7 +42,6 @@ func tryBuildBatchEvalTree(
return nil, errBatchingUnsupported
}

cache := make(leafTIDsCache)
cost, err := calculateQueryIterationCost(root, ti, cache)
if err != nil {
return nil, err
Expand Down Expand Up @@ -86,7 +84,7 @@ func astSupportsBatching(root *parser.ASTNode) bool {
}
}

func calculateQueryIterationCost(root *parser.ASTNode, ti tokenIndex, cache leafTIDsCache) (uint64, error) {
func calculateQueryIterationCost(root *parser.ASTNode, ti tokenIndex, cache tidsCache) (uint64, error) {
if root == nil {
return 0, fmt.Errorf("empty AST")
}
Expand Down Expand Up @@ -126,8 +124,8 @@ func calculateQueryIterationCost(root *parser.ASTNode, ti tokenIndex, cache leaf
}
}

func leafIterationCost(ti tokenIndex, field string, token parser.Token, cache leafTIDsCache) (uint64, error) {
tids, err := ti.GetTIDsByTokenExpr(token)
func leafIterationCost(ti tokenIndex, field string, token parser.Token, cache tidsCache) (uint64, error) {
tids, err := cache.get(ti, token)
if err != nil {
return 0, err
}
Expand All @@ -136,7 +134,6 @@ func leafIterationCost(ti tokenIndex, field string, token parser.Token, cache le
if len(tids) > maxBatchedTIDsPerLeaf {
return 0, errBatchingUnsupported
}
cache[token] = tids
if len(tids) == 0 {
return 0, nil
}
Expand Down Expand Up @@ -195,21 +192,17 @@ func buildBatchEvalTree(
func evalBatchLeaf(
ti tokenIndex,
token parser.Token,
cache leafTIDsCache,
cache tidsCache,
sw *stopwatch.Stopwatch,
stats *searchStats,
minLID, maxLID uint32,
order seq.DocsOrder,
) (node.BatchedNode, error) {
stats.LeavesTotal++

tids, ok := cache[token]
if !ok {
var err error
tids, err = ti.GetTIDsByTokenExpr(token)
if err != nil {
return nil, err
}
tids, err := cache.get(ti, token)
if err != nil {
return nil, err
}

if len(tids) == 0 {
Expand Down
22 changes: 11 additions & 11 deletions frac/processor/batch_eval_tree_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,39 +107,39 @@ func TestQueryIterationCost(t *testing.T) {
t.Run("leaf", func(t *testing.T) {
q, err := parser.ParseSeqQL(`a:1`, nil)
require.NoError(t, err)
cost, err := calculateQueryIterationCost(q.Root, index, make(leafTIDsCache))
cost, err := calculateQueryIterationCost(q.Root, index, make(tidsCache))
require.NoError(t, err)
assert.Equal(t, uint64(80_000), cost)
})

t.Run("and", func(t *testing.T) {
q, err := parser.ParseSeqQL(`a:1 AND b:2`, nil)
require.NoError(t, err)
cost, err := calculateQueryIterationCost(q.Root, index, make(leafTIDsCache))
cost, err := calculateQueryIterationCost(q.Root, index, make(tidsCache))
require.NoError(t, err)
assert.Equal(t, uint64(80_000), cost)
})

t.Run("or", func(t *testing.T) {
q, err := parser.ParseSeqQL(`a:1 OR b:2`, nil)
require.NoError(t, err)
cost, err := calculateQueryIterationCost(q.Root, index, make(leafTIDsCache))
cost, err := calculateQueryIterationCost(q.Root, index, make(tidsCache))
require.NoError(t, err)
assert.Equal(t, uint64(200_000), cost)
})

t.Run("and not", func(t *testing.T) {
q, err := parser.ParseSeqQL(`a:1 AND NOT b:2`, nil)
require.NoError(t, err)
cost, err := calculateQueryIterationCost(q.Root, index, make(leafTIDsCache))
cost, err := calculateQueryIterationCost(q.Root, index, make(tidsCache))
require.NoError(t, err)
assert.Equal(t, uint64(200_000), cost)
})

t.Run("nested and-or", func(t *testing.T) {
q, err := parser.ParseSeqQL(`a:1 AND (b:2 OR c:3)`, nil)
require.NoError(t, err)
cost, err := calculateQueryIterationCost(q.Root, index, make(leafTIDsCache))
cost, err := calculateQueryIterationCost(q.Root, index, make(tidsCache))
require.NoError(t, err)
assert.Equal(t, uint64(80_000), cost)
})
Expand Down Expand Up @@ -175,7 +175,7 @@ func TestTryBuildBatchEvalTree(t *testing.T) {
q, err := parser.ParseSeqQL(`a:1 AND b:2`, nil)
require.NoError(t, err)
stats := &searchStats{}
tree, err := tryBuildBatchEvalTree(q.Root, config.BinaryDataV6, denseIndex, queryOpt, 1, 100, stats, seq.DocsOrderDesc, sw)
tree, err := tryBuildBatchEvalTree(q.Root, config.BinaryDataV6, denseIndex, make(tidsCache), queryOpt, 1, 100, stats, seq.DocsOrderDesc, sw)
require.NoError(t, err)
assert.NotNil(t, tree)
assert.Equal(t, 2, stats.LeavesTotal)
Expand All @@ -187,7 +187,7 @@ func TestTryBuildBatchEvalTree(t *testing.T) {
q, err := parser.ParseSeqQL(`a:1 AND b:2`, nil)
require.NoError(t, err)
stats := &searchStats{}
tree, err := tryBuildBatchEvalTree(q.Root, config.BinaryDataV6, denseIndex, disabled, 1, 100, stats, seq.DocsOrderDesc, sw)
tree, err := tryBuildBatchEvalTree(q.Root, config.BinaryDataV6, denseIndex, make(tidsCache), disabled, 1, 100, stats, seq.DocsOrderDesc, sw)
require.ErrorIs(t, err, errBatchingUnsupported)
assert.Nil(t, tree)
assert.Equal(t, 0, stats.LeavesTotal)
Expand All @@ -198,7 +198,7 @@ func TestTryBuildBatchEvalTree(t *testing.T) {
q, err := parser.ParseSeqQL(`a:1 AND b:2`, nil)
require.NoError(t, err)
stats := &searchStats{}
tree, err := tryBuildBatchEvalTree(q.Root, config.BinaryDataV6, sparseIndex, queryOpt, 1, 100, stats, seq.DocsOrderDesc, sw)
tree, err := tryBuildBatchEvalTree(q.Root, config.BinaryDataV6, sparseIndex, make(tidsCache), queryOpt, 1, 100, stats, seq.DocsOrderDesc, sw)
require.ErrorIs(t, err, errBatchingUnsupported)
assert.Nil(t, tree)
assert.Equal(t, 0, stats.LeavesTotal)
Expand All @@ -213,7 +213,7 @@ func TestTryBuildBatchEvalTree(t *testing.T) {
q, err := parser.ParseSeqQL(`a:1`, nil)
require.NoError(t, err)
stats := &searchStats{}
tree, err := tryBuildBatchEvalTree(q.Root, config.BinaryDataV6, index, queryOpt, 1, 100, stats, seq.DocsOrderDesc, sw)
tree, err := tryBuildBatchEvalTree(q.Root, config.BinaryDataV6, index, make(tidsCache), queryOpt, 1, 100, stats, seq.DocsOrderDesc, sw)
require.ErrorIs(t, err, errBatchingUnsupported)
assert.Nil(t, tree)
assert.Equal(t, 0, stats.LeavesTotal)
Expand All @@ -239,7 +239,7 @@ func TestTryBuildBatchEvalTree(t *testing.T) {
q, err := parser.ParseSeqQL(`a:1 AND b:2`, nil)
require.NoError(t, err)
stats := &searchStats{}
tree, err := tryBuildBatchEvalTree(q.Root, config.BinaryDataV6, index, queryOpt, 1, 100, stats, seq.DocsOrderDesc, sw)
tree, err := tryBuildBatchEvalTree(q.Root, config.BinaryDataV6, index, make(tidsCache), queryOpt, 1, 100, stats, seq.DocsOrderDesc, sw)
require.ErrorIs(t, err, errBatchingUnsupported)
assert.Nil(t, tree)
assert.Equal(t, 0, stats.LeavesTotal)
Expand All @@ -250,7 +250,7 @@ func TestTryBuildBatchEvalTree(t *testing.T) {
q, err := parser.ParseSeqQL(`NOT a:1`, nil)
require.NoError(t, err)
stats := &searchStats{}
tree, err := tryBuildBatchEvalTree(q.Root, config.BinaryDataV6, denseIndex, queryOpt, 1, 100, stats, seq.DocsOrderDesc, sw)
tree, err := tryBuildBatchEvalTree(q.Root, config.BinaryDataV6, denseIndex, make(tidsCache), queryOpt, 1, 100, stats, seq.DocsOrderDesc, sw)
require.ErrorIs(t, err, errBatchingUnsupported)
assert.Nil(t, tree)
assert.Equal(t, 0, stats.LeavesTotal)
Expand Down
7 changes: 3 additions & 4 deletions frac/processor/eval_tree.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,19 +64,18 @@ func buildEvalTree(root *parser.ASTNode, minVal, maxVal uint32, stats *searchSta
func evalLeaf(
ti tokenIndex,
token parser.Token,
cache tidsCache,
sw *stopwatch.Stopwatch,
stats *searchStats,
minLID, maxLID uint32,
order seq.DocsOrder,
) (node.Node, error) {
m := sw.Start("get_tids_by_token_expr")
tids, err := ti.GetTIDsByTokenExpr(token)
m.Stop()
tids, err := cache.get(ti, token)
if err != nil {
return nil, err
}

m = sw.Start("get_lids_from_tids")
m := sw.Start("get_lids_from_tids")
lidsTids := ti.GetLIDsFromTIDs(tids, stats, minLID, maxLID, order)
m.Stop()

Expand Down
Loading
Loading