Skip to content

Commit

Permalink
CNDB-12139 minor refactor row count estimation code (#1463)
Browse files Browse the repository at this point in the history
Part of riptano/cndb#12139

Moves constant shard count outside looping shards to reduce confusion.
  • Loading branch information
k-rus authored Dec 17, 2024
1 parent 546d58a commit 52cd2e5
Showing 1 changed file with 4 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,9 @@ public KeyRangeIterator search(QueryContext queryContext, Expression expression,
// but the rest of the iterators are create lazily in the loop below.
assert rangeIndexes[startShard] != null;
KeyRangeIterator firstIterator = rangeIndexes[startShard].search(expression, keyRange);
var keyCount = firstIterator.getMaxKeys();
// Assume all shards are the same size, but we must not pass 0 because of some checks in KeyRangeIterator
// that assume 0 means empty iterator and could fail.
var keyCount = Math.max(1, firstIterator.getMaxKeys());
builder.add(firstIterator);

// Prepare the search on the remaining shards, but wrap them in KeyRangeLazyIterator, so they don't run
Expand All @@ -218,10 +220,7 @@ public KeyRangeIterator search(QueryContext queryContext, Expression expression,
var shardRange = boundaries.getBounds(shard);
var minKey = index.indexContext.keyFactory().createTokenOnly(shardRange.left.getToken());
var maxKey = index.indexContext.keyFactory().createTokenOnly(shardRange.right.getToken());
// Assume all shards are the same size, but we must not pass 0 because of some checks in KeyRangeIterator
// that assume 0 means empty iterator and could fail.
var count = Math.max(1, keyCount);
builder.add(new KeyRangeLazyIterator(() -> index.search(expression, keyRange), minKey, maxKey, count));
builder.add(new KeyRangeLazyIterator(() -> index.search(expression, keyRange), minKey, maxKey, keyCount));
}

return builder.build();
Expand Down

0 comments on commit 52cd2e5

Please sign in to comment.