forked from apache/calcite
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
splitting ArrowEnumerator into sub-classes
- Loading branch information
1 parent
5bb4d6c
commit c50e955
Showing
6 changed files
with
266 additions
and
191 deletions.
There are no files selected for viewing
81 changes: 81 additions & 0 deletions
81
arrow/src/main/java/org/apache/calcite/adapter/arrow/AbstractArrowEnumerator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one or more | ||
* contributor license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright ownership. | ||
* The ASF licenses this file to you under the Apache License, Version 2.0 | ||
* (the "License"); you may not use this file except in compliance with | ||
* the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package org.apache.calcite.adapter.arrow; | ||
|
||
import org.apache.calcite.linq4j.Enumerator; | ||
import org.apache.calcite.util.ImmutableIntList; | ||
import org.apache.calcite.util.Util; | ||
|
||
import org.apache.arrow.vector.ValueVector; | ||
import org.apache.arrow.vector.VectorSchemaRoot; | ||
import org.apache.arrow.vector.VectorUnloader; | ||
import org.apache.arrow.vector.ipc.ArrowFileReader; | ||
import org.apache.arrow.vector.ipc.message.ArrowRecordBatch; | ||
|
||
import java.io.IOException; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
/** | ||
* Enumerator that reads from a collection of Arrow value-vectors. | ||
*/ | ||
abstract class AbstractArrowEnumerator implements Enumerator<Object> { | ||
protected final ArrowFileReader arrowFileReader; | ||
protected int currRowIndex = -1; | ||
protected int rowCount; | ||
protected final List<ValueVector> valueVectors; | ||
protected final List<Integer> fields; | ||
|
||
AbstractArrowEnumerator(ArrowFileReader arrowFileReader, ImmutableIntList fields) { | ||
this.arrowFileReader = arrowFileReader; | ||
this.fields = fields; | ||
this.valueVectors = new ArrayList<>(fields.size()); | ||
} | ||
|
||
abstract void evaluateOperator(ArrowRecordBatch arrowRecordBatch); | ||
|
||
protected void loadNextArrowBatch() { | ||
try { | ||
final VectorSchemaRoot vsr = arrowFileReader.getVectorSchemaRoot(); | ||
for (int i : fields) { | ||
this.valueVectors.add(vsr.getVector(i)); | ||
} | ||
this.rowCount = vsr.getRowCount(); | ||
VectorUnloader vectorUnloader = new VectorUnloader(vsr); | ||
ArrowRecordBatch arrowRecordBatch = vectorUnloader.getRecordBatch(); | ||
evaluateOperator(arrowRecordBatch); | ||
} catch (IOException e) { | ||
throw Util.toUnchecked(e); | ||
} | ||
} | ||
|
||
@Override public Object current() { | ||
if (fields.size() == 1) { | ||
return this.valueVectors.get(0).getObject(currRowIndex); | ||
} | ||
Object[] current = new Object[valueVectors.size()]; | ||
for (int i = 0; i < valueVectors.size(); i++) { | ||
ValueVector vector = this.valueVectors.get(i); | ||
current[i] = vector.getObject(currRowIndex); | ||
} | ||
return current; | ||
} | ||
|
||
@Override public void reset() { | ||
throw new UnsupportedOperationException(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
184 changes: 0 additions & 184 deletions
184
arrow/src/main/java/org/apache/calcite/adapter/arrow/ArrowEnumerator.java
This file was deleted.
Oops, something went wrong.
97 changes: 97 additions & 0 deletions
97
arrow/src/main/java/org/apache/calcite/adapter/arrow/ArrowFilterEnumerator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one or more | ||
* contributor license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright ownership. | ||
* The ASF licenses this file to you under the Apache License, Version 2.0 | ||
* (the "License"); you may not use this file except in compliance with | ||
* the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package org.apache.calcite.adapter.arrow; | ||
|
||
import org.apache.arrow.gandiva.evaluator.Filter; | ||
import org.apache.arrow.gandiva.evaluator.SelectionVector; | ||
import org.apache.arrow.gandiva.evaluator.SelectionVectorInt16; | ||
import org.apache.arrow.gandiva.exceptions.GandivaException; | ||
import org.apache.arrow.memory.ArrowBuf; | ||
import org.apache.arrow.memory.BufferAllocator; | ||
import org.apache.arrow.memory.RootAllocator; | ||
import org.apache.arrow.vector.ipc.ArrowFileReader; | ||
import org.apache.arrow.vector.ipc.message.ArrowRecordBatch; | ||
|
||
import org.apache.calcite.util.ImmutableIntList; | ||
import org.apache.calcite.util.Util; | ||
|
||
import java.io.IOException; | ||
|
||
/** | ||
* Enumerator that reads from a filtered collection of Arrow value-vectors. | ||
*/ | ||
class ArrowFilterEnumerator extends AbstractArrowEnumerator { | ||
private final BufferAllocator allocator; | ||
private final Filter filter; | ||
private ArrowBuf buf; | ||
private SelectionVector selectionVector; | ||
private int selectionVectorIndex; | ||
|
||
ArrowFilterEnumerator(ArrowFileReader arrowFileReader, ImmutableIntList fields, Filter filter) { | ||
super(arrowFileReader, fields); | ||
this.allocator = new RootAllocator(Long.MAX_VALUE); | ||
this.filter = filter; | ||
} | ||
|
||
void evaluateOperator(ArrowRecordBatch arrowRecordBatch) { | ||
try { | ||
this.buf = this.allocator.buffer((long) rowCount * 2); | ||
this.selectionVector = new SelectionVectorInt16(buf); | ||
filter.evaluate(arrowRecordBatch, selectionVector); | ||
} catch (GandivaException e) { | ||
throw Util.toUnchecked(e); | ||
} | ||
} | ||
|
||
@Override public boolean moveNext() { | ||
if (selectionVector == null | ||
|| selectionVectorIndex >= selectionVector.getRecordCount()) { | ||
boolean hasNextBatch; | ||
while (true) { | ||
try { | ||
hasNextBatch = arrowFileReader.loadNextBatch(); | ||
} catch (IOException e) { | ||
throw Util.toUnchecked(e); | ||
} | ||
if (hasNextBatch) { | ||
selectionVectorIndex = 0; | ||
this.valueVectors.clear(); | ||
loadNextArrowBatch(); | ||
assert selectionVector != null; | ||
if (selectionVectorIndex >= selectionVector.getRecordCount()) { | ||
// the "filtered" batch is empty, but there may be more batches to fetch | ||
continue; | ||
} | ||
currRowIndex = selectionVector.getIndex(selectionVectorIndex++); | ||
} | ||
return hasNextBatch; | ||
} | ||
} else { | ||
currRowIndex = selectionVector.getIndex(selectionVectorIndex++); | ||
return true; | ||
} | ||
} | ||
|
||
@Override public void close() { | ||
try { | ||
buf.close(); | ||
filter.close(); | ||
} catch (GandivaException e) { | ||
throw Util.toUnchecked(e); | ||
} | ||
} | ||
} |
Oops, something went wrong.