Skip to content

Commit

Permalink
apacheGH-3044: HashJoin iterator construction no longer eagerly build…
Browse files Browse the repository at this point in the history
…s initial hash probe table.
  • Loading branch information
Aklakan committed Mar 5, 2025
1 parent 6a86b5d commit 1d84676
Showing 1 changed file with 33 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@ public abstract class AbstractIterHashJoin extends QueryIter2 {
protected long s_trailerResults = 0 ; // Results from the trailer iterator.
// See also stats in the probe table.

protected final JoinKey joinKey ;
protected final MultiHashProbeTable hashTable ;
protected JoinKey joinKey ;
protected MultiHashProbeTable hashTable ;

private QueryIterator iterStream ;
private Binding rowStream = null ;
Expand All @@ -61,40 +61,46 @@ enum Phase { INIT, HASH , STREAM, TRAILER, DONE }
protected AbstractIterHashJoin(JoinKey initialJoinKey, QueryIterator probeIter, QueryIterator streamIter, ExecutionContext execCxt) {
super(probeIter, streamIter, execCxt) ;

if ( initialJoinKey == null ) {
// This block computes an initial join key from the common variables of each iterator's first binding.
QueryIterPeek pProbe = QueryIterPeek.create(probeIter, execCxt) ;
QueryIterPeek pStream = QueryIterPeek.create(streamIter, execCxt) ;
this.joinKey = initialJoinKey ;
this.iterStream = streamIter ;
this.iterCurrent = null ;
}

Binding bLeft = pProbe.peek() ;
Binding bRight = pStream.peek() ;
private void doInit() {
QueryIterator probeIter = getLeft();
try {
if ( joinKey == null ) {
// This block computes an initial join key from the common variables of each iterator's first binding.
ExecutionContext execCxt = getExecContext();
QueryIterPeek pProbe = QueryIterPeek.create(probeIter, execCxt) ;
probeIter = pProbe ;

List<Var> varsLeft = Iter.toList(bLeft.vars()) ;
List<Var> varsRight = Iter.toList(bRight.vars()) ;
QueryIterPeek pStream = QueryIterPeek.create(iterStream, execCxt) ;
this.iterStream = pStream ;

initialJoinKey = JoinKey.create(varsLeft, varsRight) ;
Binding bLeft = pProbe.peek() ;
Binding bRight = pStream.peek() ;

probeIter = pProbe ;
streamIter = pStream ;
}
List<Var> varsLeft = Iter.toList(bLeft.vars()) ;
List<Var> varsRight = Iter.toList(bRight.vars()) ;

JoinKey maxJoinKey = null;
joinKey = JoinKey.create(varsLeft, varsRight) ;
}

this.joinKey = initialJoinKey ;
this.iterStream = streamIter ;
this.hashTable = new MultiHashProbeTable(maxJoinKey, initialJoinKey) ;
this.iterCurrent = null ;
buildHashTable(probeIter) ;
JoinKey maxJoinKey = null;
this.hashTable = new MultiHashProbeTable(maxJoinKey, joinKey) ;
buildHashTable(probeIter) ;
} finally {
probeIter.close();
}
}

private void buildHashTable(QueryIterator iter1) {
state = Phase.HASH ;
for (; iter1.hasNext();) {
Binding row1 = iter1.next() ;
iter1.forEachRemaining(row1 -> {
s_countProbe ++ ;
hashTable.put(row1) ;
}
iter1.close() ;
});
state = Phase.STREAM ;
}

Expand Down Expand Up @@ -127,8 +133,10 @@ protected Binding moveToNextBindingOrNull() {
switch ( state ) {
case DONE : return null ;
case HASH :
case INIT :
throw new IllegalStateException() ;
case INIT :
doInit();
break;
case TRAILER :
return doOneTail() ;
case STREAM :
Expand Down

0 comments on commit 1d84676

Please sign in to comment.