Github user kiszk commented on a diff in the pull request:
https://github.com/apache/spark/pull/18659#discussion_r129522956
--- Diff:
sql/core/src/main/scala/org/apache/spark/sql/execution/arrow/ArrowConverters.scala
---
@@ -132,6 +135,61 @@ private[sql] object ArrowConverters {
}
}
+ private[sql] def fromPayloadIterator(iter: Iterator[ArrowPayload]):
Iterator[InternalRow] = {
+ new Iterator[InternalRow] {
+ private val _allocator = new RootAllocator(Long.MaxValue)
+ private var _reader: ArrowFileReader = _
+ private var _root: VectorSchemaRoot = _
+ private var _index = 0
+
+ loadNextBatch()
+
+ override def hasNext: Boolean = _root != null && _index <
_root.getRowCount
+
+ override def next(): InternalRow = {
+ val fields = _root.getFieldVectors.asScala
+
+ val genericRowData = fields.map { field =>
+ field.getAccessor.getObject(_index)
+ }.toArray[Any]
--- End diff --
@BryanCutler
As @cloud-fan suggested
[here](https://issues.apache.org/jira/browse/SPARK-21190?focusedCommentId=16101014),
it is good to create `ColumnarBatch` with `ArrowColumnVector` and get an
iterator like this:
cc: @ueshin
```
new Iterator[InternalRow] {
private val _allocator = new RootAllocator(Long.MaxValue)
private var _reader: ArrowFileReader = _
private var _root: VectorSchemaRoot = _
private var _index = 0
private var _iterator = null
loadNextBatch()
override def hasNext: Boolean = _root != null && _index <
_root.getRowCount && _iterator.hasNext
override def next(): InternalRow = {
_index += 1
if (_index >= _root.getRowCount) {
_index = 0
loadNextBatch()
if (!hasNext) {
close()
}
}
_iterator.next()
}
...
private def loadNextBatch(): Unit = {
closeReader()
if (iter.hasNext) {
val in = new
ByteArrayReadableSeekableByteChannel(iter.next().asPythonSerializable)
_reader = new ArrowFileReader(in, _allocator)
_root = _reader.getVectorSchemaRoot // throws IOException
_reader.loadNextBatch() // throws IOException
val arrowSchema = ArrowUtils.fromArrowSchema(_root.getSchema)
val fields = _root.getFieldVectors
val rows = _root.getRowCount
val columnarBatch = ColumnarBatch.allocateArrow(
_root.getFieldVectors.asInstanceOf[java.util.List[ValueVector]],
ArrowUtils.fromArrowSchema(_root.getSchema), _root.getRowCount)
_iterator = columnarBatch.rowIterator
}
}
public final class ColumnarBatch {
...
public static ColumnarBatch allocateArrow(List<ValueVector> vectors,
StructType schema, int maxRows) {
// need to implement the following constructor for arrowColumnVector
return new ColumnarBatch(vectors, schema, maxRows);
}
...
}
```
---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at [email protected] or file a JIRA ticket
with INFRA.
---
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]