Github user HeartSaVioR commented on a diff in the pull request:

    https://github.com/apache/spark/pull/21477#discussion_r193291809
  
    --- Diff: 
sql/core/src/main/scala/org/apache/spark/sql/execution/python/PythonForeachWriter.scala
 ---
    @@ -0,0 +1,161 @@
    +/*
    + * 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.spark.sql.execution.python
    +
    +import java.io.File
    +import java.util.concurrent.TimeUnit
    +import java.util.concurrent.locks.ReentrantLock
    +
    +import org.apache.spark.{SparkEnv, TaskContext}
    +import org.apache.spark.api.python._
    +import org.apache.spark.internal.Logging
    +import org.apache.spark.memory.TaskMemoryManager
    +import org.apache.spark.sql.ForeachWriter
    +import org.apache.spark.sql.catalyst.expressions.UnsafeRow
    +import org.apache.spark.sql.types.StructType
    +import org.apache.spark.util.{NextIterator, Utils}
    +
    +class PythonForeachWriter(func: PythonFunction, schema: StructType)
    +  extends ForeachWriter[UnsafeRow] {
    +
    +  private lazy val context = TaskContext.get()
    +  private lazy val buffer = new PythonForeachWriter.UnsafeRowBuffer(
    +    context.taskMemoryManager, new 
File(Utils.getLocalDir(SparkEnv.get.conf)), schema.fields.length)
    +  private lazy val inputRowIterator = buffer.iterator
    +
    +  private lazy val inputByteIterator = {
    +    EvaluatePython.registerPicklers()
    +    val objIterator = inputRowIterator.map { row => 
EvaluatePython.toJava(row, schema) }
    +    new SerDeUtil.AutoBatchedPickler(objIterator)
    +  }
    +
    +  private lazy val pythonRunner = {
    +    val conf = SparkEnv.get.conf
    +    val bufferSize = conf.getInt("spark.buffer.size", 65536)
    +    val reuseWorker = conf.getBoolean("spark.python.worker.reuse", true)
    +    PythonRunner(func, bufferSize, reuseWorker)
    +  }
    +
    +  private lazy val outputIterator =
    +    pythonRunner.compute(inputByteIterator, context.partitionId(), context)
    +
    +  override def open(partitionId: Long, version: Long): Boolean = {
    +    outputIterator  // initialize everything
    +    TaskContext.get.addTaskCompletionListener { _ => buffer.close() }
    +    true
    +  }
    +
    +  override def process(value: UnsafeRow): Unit = {
    +    buffer.add(value)
    +  }
    +
    +  override def close(errorOrNull: Throwable): Unit = {
    +    buffer.allRowsAdded()
    +    if (outputIterator.hasNext) outputIterator.next() // to throw python 
exception if there was one
    +  }
    +}
    +
    +object PythonForeachWriter {
    +
    +  /**
    +   * A buffer that is designed for the sole purpose of buffering 
UnsafeRows in PythonForeahWriter.
    --- End diff --
    
    nit: PythonForeachWriter


---

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

Reply via email to