Github user cloud-fan commented on a diff in the pull request:

    https://github.com/apache/spark/pull/18655#discussion_r129477058
  
    --- Diff: 
sql/core/src/main/scala/org/apache/spark/sql/execution/arrow/ArrowConverters.scala
 ---
    @@ -77,95 +59,55 @@ private[sql] object ArrowConverters {
       private[sql] def toPayloadIterator(
           rowIter: Iterator[InternalRow],
           schema: StructType,
    -      maxRecordsPerBatch: Int): Iterator[ArrowPayload] = {
    -    new Iterator[ArrowPayload] {
    -      private val _allocator = new RootAllocator(Long.MaxValue)
    -      private var _nextPayload = if (rowIter.nonEmpty) convert() else null
    +      maxRecordsPerBatch: Int,
    +      context: TaskContext): Iterator[ArrowPayload] = {
     
    -      override def hasNext: Boolean = _nextPayload != null
    -
    -      override def next(): ArrowPayload = {
    -        val obj = _nextPayload
    -        if (hasNext) {
    -          if (rowIter.hasNext) {
    -            _nextPayload = convert()
    -          } else {
    -            _allocator.close()
    -            _nextPayload = null
    -          }
    -        }
    -        obj
    -      }
    -
    -      private def convert(): ArrowPayload = {
    -        val batch = internalRowIterToArrowBatch(rowIter, schema, 
_allocator, maxRecordsPerBatch)
    -        ArrowPayload(batch, schema, _allocator)
    -      }
    -    }
    -  }
    +    val arrowSchema = ArrowUtils.toArrowSchema(schema)
    +    val allocator =
    +      ArrowUtils.rootAllocator.newChildAllocator("toPayloadIterator", 0, 
Long.MaxValue)
     
    -  /**
    -   * Iterate over InternalRows and write to an ArrowRecordBatch, stopping 
when rowIter is consumed
    -   * or the number of records in the batch equals maxRecordsInBatch.  If 
maxRecordsPerBatch is 0,
    -   * then rowIter will be fully consumed.
    -   */
    -  private def internalRowIterToArrowBatch(
    -      rowIter: Iterator[InternalRow],
    -      schema: StructType,
    -      allocator: BufferAllocator,
    -      maxRecordsPerBatch: Int = 0): ArrowRecordBatch = {
    +    val root = VectorSchemaRoot.create(arrowSchema, allocator)
    +    val arrowWriter = ArrowWriter.create(root)
     
    -    val columnWriters = schema.fields.zipWithIndex.map { case (field, 
ordinal) =>
    -      ColumnWriter(field.dataType, ordinal, allocator).init()
    -    }
    +    var closed = false
     
    -    val writerLength = columnWriters.length
    -    var recordsInBatch = 0
    -    while (rowIter.hasNext && (maxRecordsPerBatch <= 0 || recordsInBatch < 
maxRecordsPerBatch)) {
    -      val row = rowIter.next()
    -      var i = 0
    -      while (i < writerLength) {
    -        columnWriters(i).write(row)
    -        i += 1
    +    context.addTaskCompletionListener { _ =>
    +      if (!closed) {
    --- End diff --
    
    do we really need this? I think it's ok to close twice?


---
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 infrastruct...@apache.org or file a JIRA ticket
with INFRA.
---

---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscr...@spark.apache.org
For additional commands, e-mail: reviews-h...@spark.apache.org

Reply via email to