Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[SPARK-40433][SS][PYTHON] Add toJVMRow in PythonSQLUtils to convert pickled PySpark Row to JVM Row #37891

Closed
wants to merge 2 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -22,27 +22,45 @@ import java.net.Socket
import java.nio.channels.Channels
import java.util.Locale

import net.razorvine.pickle.Pickler
import net.razorvine.pickle.{Pickler, Unpickler}

import org.apache.spark.api.python.DechunkedInputStream
import org.apache.spark.internal.Logging
import org.apache.spark.security.SocketAuthServer
import org.apache.spark.sql.{Column, DataFrame, Row, SparkSession}
import org.apache.spark.sql.catalyst.CatalystTypeConverters
import org.apache.spark.sql.catalyst.{CatalystTypeConverters, InternalRow}
import org.apache.spark.sql.catalyst.analysis.FunctionRegistry
import org.apache.spark.sql.catalyst.encoders.ExpressionEncoder
import org.apache.spark.sql.catalyst.expressions._
import org.apache.spark.sql.catalyst.expressions.aggregate._
import org.apache.spark.sql.catalyst.parser.CatalystSqlParser
import org.apache.spark.sql.execution.{ExplainMode, QueryExecution}
import org.apache.spark.sql.execution.arrow.ArrowConverters
import org.apache.spark.sql.execution.python.EvaluatePython
import org.apache.spark.sql.internal.SQLConf
import org.apache.spark.sql.types.DataType
import org.apache.spark.sql.types.{DataType, StructType}

private[sql] object PythonSQLUtils extends Logging {
private lazy val internalRowPickler = {
private def withInternalRowPickler(f: Pickler => Array[Byte]): Array[Byte] = {
EvaluatePython.registerPicklers()
new Pickler(true, false)
val pickler = new Pickler(true, false)
val ret = try {
f(pickler)
} finally {
pickler.close()
}
ret
}

private def withInternalRowUnpickler(f: Unpickler => Any): Any = {
EvaluatePython.registerPicklers()
val unpickler = new Unpickler
val ret = try {
f(unpickler)
} finally {
unpickler.close()
}
ret
}

def parseDataType(typeText: String): DataType = CatalystSqlParser.parseDataType(typeText)
Expand Down Expand Up @@ -94,8 +112,18 @@ private[sql] object PythonSQLUtils extends Logging {

def toPyRow(row: Row): Array[Byte] = {
assert(row.isInstanceOf[GenericRowWithSchema])
internalRowPickler.dumps(EvaluatePython.toJava(
CatalystTypeConverters.convertToCatalyst(row), row.schema))
withInternalRowPickler(_.dumps(EvaluatePython.toJava(
CatalystTypeConverters.convertToCatalyst(row), row.schema)))
}

def toJVMRow(
arr: Array[Byte],
returnType: StructType,
deserializer: ExpressionEncoder.Deserializer[Row]): Row = {
val fromJava = EvaluatePython.makeFromJava(returnType)
val internalRow =
fromJava(withInternalRowUnpickler(_.loads(arr))).asInstanceOf[InternalRow]
deserializer(internalRow)
}

def castTimestampNTZToLong(c: Column): Column = Column(CastTimestampNTZToLong(c.expr))
Expand Down