allisonwang-db commented on code in PR #43360: URL: https://github.com/apache/spark/pull/43360#discussion_r1364576961
########## sql/core/src/main/scala/org/apache/spark/sql/execution/python/UserDefinedPythonDataSource.scala: ########## @@ -0,0 +1,86 @@ +/* + * 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.{DataInputStream, DataOutputStream} + +import scala.collection.mutable.ArrayBuffer + +import net.razorvine.pickle.Pickler + +import org.apache.spark.api.python.{PythonFunction, PythonWorkerUtils, SpecialLengths} +import org.apache.spark.sql.{DataFrame, Dataset, SparkSession} +import org.apache.spark.sql.catalyst.plans.logical.PythonDataSource +import org.apache.spark.sql.catalyst.types.DataTypeUtils.toAttributes +import org.apache.spark.sql.errors.QueryCompilationErrors +import org.apache.spark.sql.types.StructType + +/** + * A user-defined Python data source. This is used by the Python API. + */ +case class UserDefinedPythonDataSource( + dataSource: PythonFunction, + schema: StructType) { + def apply(session: SparkSession): DataFrame = { + val source = PythonDataSource(dataSource, schema, output = toAttributes(schema)) + Dataset.ofRows(session, source) + } +} + +case class PythonDataSourceReadInfo( + func: Array[Byte], + partitions: Seq[Array[Byte]]) + +class UserDefinedPythonDataSourceReadRunner( + func: PythonFunction, + schema: StructType) extends PythonPlannerRunner[PythonDataSourceReadInfo](func) { + + override val workerModule = "pyspark.sql.worker.plan_data_source_read" + + override protected def writeToPython(dataOut: DataOutputStream, pickler: Pickler): Unit = { + // Send Python data source + PythonWorkerUtils.writePythonFunction(func, dataOut) + + // Send schema + PythonWorkerUtils.writeUTF(schema.json, dataOut) + } + + override protected def receiveFromPython(dataIn: DataInputStream): PythonDataSourceReadInfo = { + // Receive the picked reader or an exception raised in Python worker. + val length = dataIn.readInt() + if (length == SpecialLengths.PYTHON_EXCEPTION_THROWN) { + val msg = PythonWorkerUtils.readUTF(dataIn) + throw QueryCompilationErrors.tableValuedFunctionFailedToAnalyseInPythonError(msg) Review Comment: Will add some tests (and also change the name of the error) -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
