Github user cloud-fan commented on a diff in the pull request:
https://github.com/apache/spark/pull/10747#discussion_r49761722
--- Diff:
sql/core/src/main/scala/org/apache/spark/sql/execution/objects.scala ---
@@ -0,0 +1,182 @@
+/*
+ * 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
+
+import org.apache.spark.rdd.RDD
+import org.apache.spark.sql.catalyst.InternalRow
+import org.apache.spark.sql.catalyst.expressions._
+import
org.apache.spark.sql.catalyst.expressions.codegen.{GenerateSafeProjection,
GenerateUnsafeProjection, GenerateUnsafeRowJoiner}
+import org.apache.spark.sql.catalyst.plans.physical._
+import org.apache.spark.sql.types.ObjectType
+
+/**
+ * Helper functions for physical operators that work with user defined
objects.
+ */
+trait ObjectOperator extends SparkPlan {
+ def generateToObject(objExpr: Expression, inputSchema: Seq[Attribute]):
InternalRow => Any = {
+ val objectProjection = GenerateSafeProjection.generate(objExpr :: Nil,
inputSchema)
+ (i: InternalRow) => objectProjection(i).get(0, objExpr.dataType)
+ }
+
+ def generateToRow(serializer: Seq[Expression]): Any => InternalRow = {
+ val outputProjection = if
(serializer.head.dataType.isInstanceOf[ObjectType]) {
+ GenerateSafeProjection.generate(serializer)
+ } else {
+ GenerateUnsafeProjection.generate(serializer)
+ }
+ val inputType = serializer.head.collect { case b: BoundReference =>
b.dataType }.head
+ val outputRow = new SpecificMutableRow(inputType :: Nil)
+ (o: Any) => {
+ outputRow(0) = o
+ outputProjection(outputRow)
+ }
+ }
+}
+
+/**
+ * Applies the given function to each input row and encodes the result.
+ */
+case class MapPartitions(
+ func: Iterator[Any] => Iterator[Any],
+ input: Expression,
+ serializer: Seq[NamedExpression],
+ child: SparkPlan) extends UnaryNode with ObjectOperator {
+ override def output: Seq[Attribute] = serializer.map(_.toAttribute)
+
+ override protected def doExecute(): RDD[InternalRow] = {
+ child.execute().mapPartitionsInternal { iter =>
+ val getObject = generateToObject(input, child.output)
+ val outputObject = generateToRow(serializer)
+ func(iter.map(getObject)).map(outputObject)
+ }
+ }
+}
+
+/**
+ * Applies the given function to each input row, appending the encoded
result at the end of the row.
+ */
+case class AppendColumns(
+ func: Any => Any,
+ input: Expression,
+ serializer: Seq[NamedExpression],
+ child: SparkPlan) extends UnaryNode with ObjectOperator {
+
+ override def output: Seq[Attribute] = child.output ++
serializer.map(_.toAttribute)
+
+ private def newColumnSchema = serializer.map(_.toAttribute).toStructType
+
+ override protected def doExecute(): RDD[InternalRow] = {
+ child.execute().mapPartitionsInternal { iter =>
+ val getObject = generateToObject(input, child.output)
+ val combiner = GenerateUnsafeRowJoiner.create(child.schema,
newColumnSchema)
+ val outputObject = generateToRow(serializer)
+
+ iter.map { row =>
+ val newColumns = outputObject(func(getObject(row)))
--- End diff --
how do we ensure `newColumns` is `UnsafeRow`?
---
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]