Skip to content
This repository was archived by the owner on Jul 19, 2023. It is now read-only.

Commit 82e4f48

Browse files
committed
Optimize when the Seek destination is only a single row away
1 parent 6e6a326 commit 82e4f48

File tree

1 file changed

+11
-2
lines changed

1 file changed

+11
-2
lines changed

pkg/phlaredb/query/iters.go

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -417,6 +417,15 @@ func NewBinaryJoinIterator(definitionLevel int, left, right Iterator) *BinaryJoi
417417
}
418418
}
419419

420+
// nextOrSeek will use next if the iterator is exactly one row aways
421+
func (bj *BinaryJoinIterator) nextOrSeek(to RowNumberWithDefinitionLevel, it Iterator) bool {
422+
// Seek when definition level is higher then 0, there is not previous iteration or when the difference between current position and to is not 1
423+
if to.DefinitionLevel != 0 || it.At() == nil || to.RowNumber.Preceding() != it.At().RowNumber {
424+
return it.Seek(to)
425+
}
426+
return it.Next()
427+
}
428+
420429
func (bj *BinaryJoinIterator) Next() bool {
421430
for {
422431
if !bj.left.Next() {
@@ -426,7 +435,7 @@ func (bj *BinaryJoinIterator) Next() bool {
426435
resLeft := bj.left.At()
427436

428437
// now seek the right iterator to the left position
429-
if !bj.right.Seek(RowNumberWithDefinitionLevel{resLeft.RowNumber, bj.definitionLevel}) {
438+
if !bj.nextOrSeek(RowNumberWithDefinitionLevel{resLeft.RowNumber, bj.definitionLevel}, bj.right) {
430439
bj.err = bj.right.Err()
431440
return false
432441
}
@@ -446,7 +455,7 @@ func (bj *BinaryJoinIterator) Next() bool {
446455
makeResult()
447456
return true
448457
} else if cmp < 0 {
449-
if !bj.left.Seek(RowNumberWithDefinitionLevel{resRight.RowNumber, bj.definitionLevel}) {
458+
if !bj.nextOrSeek(RowNumberWithDefinitionLevel{resRight.RowNumber, bj.definitionLevel}, bj.left) {
450459
bj.err = bj.left.Err()
451460
return false
452461
}

0 commit comments

Comments
 (0)