jinchengchenghh commented on code in PR #10475:
URL: 
https://github.com/apache/incubator-gluten/pull/10475#discussion_r2293596463


##########
backends-velox/src/main/scala/org/apache/gluten/execution/ColumnarPartialGenerateExec.scala:
##########
@@ -0,0 +1,383 @@
+/*
+ * 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.gluten.execution
+
+import org.apache.gluten.backendsapi.BackendsApiManager
+import org.apache.gluten.columnarbatch.{ColumnarBatches, VeloxColumnarBatches}
+import org.apache.gluten.expression.InterpretedArrowGenerate
+import org.apache.gluten.extension.columnar.transition.Convention
+import org.apache.gluten.iterator.Iterators
+import org.apache.gluten.memory.arrow.alloc.ArrowBufferAllocators
+import org.apache.gluten.sql.shims.SparkShimLoader
+import org.apache.gluten.vectorized.{ArrowColumnarRow, 
ArrowWritableColumnVector}
+
+import org.apache.spark.rdd.RDD
+import org.apache.spark.sql.catalyst.InternalRow
+import org.apache.spark.sql.catalyst.expressions.{Attribute, 
AttributeReference, Expression, GenericInternalRow, Nondeterministic, 
SpecializedGetters}
+import org.apache.spark.sql.catalyst.expressions.BindReferences.bindReferences
+import org.apache.spark.sql.execution.{ExplainUtils, GenerateExec, SparkPlan, 
UnaryExecNode}
+import org.apache.spark.sql.execution.metric.{SQLMetric, SQLMetrics}
+import org.apache.spark.sql.types.{ArrayType, BinaryType, DataType, MapType, 
StringType, StructType}
+import org.apache.spark.sql.vectorized.{ColumnarBatch, ColumnVector}
+
+import scala.collection.mutable.{ArrayBuffer, ListBuffer}
+
+/**
+ * By rule <PartialGenerateRule>, if the generator is a instance of 
<HiveGenericUDTF>, then the
+ * generateExec will be changed to ColumnarPartialGenerateExec
+ *
+ * @param generateExec
+ *   the GenerateExec from vanilla
+ * @param child
+ *   child plan
+ */
+case class ColumnarPartialGenerateExec(generateExec: GenerateExec, child: 
SparkPlan)
+  extends UnaryExecNode
+  with ValidatablePlan {
+
+  private val generatorNullRow = new 
GenericInternalRow(generateExec.generatorOutput.length)
+
+  private val pruneChildAttributes: ListBuffer[Attribute] = ListBuffer()
+  private val pruneChildColumnIndices: ListBuffer[Int] = ListBuffer()
+  private val generateAttributes: ListBuffer[Attribute] = ListBuffer()
+  private val rightInputColumnIndices: ListBuffer[Int] = ListBuffer()
+
+  private val rightSchema =
+    
SparkShimLoader.getSparkShims.structFromAttributes(generateExec.generatorOutput)
+
+  private lazy val getLeftIndices = getColumnIndexInChildOutput(
+    pruneChildAttributes,
+    pruneChildColumnIndices,
+    generateExec.requiredChildOutput)
+  private lazy val getRightIndices = getColumnIndexInChildOutput(
+    generateAttributes,
+    rightInputColumnIndices,
+    Seq(generateExec.generator))
+
+  private lazy val generator = InterpretedArrowGenerate.create(
+    bindReferences(Seq(generateExec.generator), generateAttributes.toSeq).head)
+
+  @transient override lazy val metrics = Map(
+    "time" -> SQLMetrics.createTimingMetric(sparkContext, "total time of 
partial project"),

Review Comment:
   partial project -> partial generate



##########
backends-velox/src/main/scala/org/apache/gluten/execution/ColumnarPartialGenerateExec.scala:
##########
@@ -0,0 +1,383 @@
+/*
+ * 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.gluten.execution
+
+import org.apache.gluten.backendsapi.BackendsApiManager
+import org.apache.gluten.columnarbatch.{ColumnarBatches, VeloxColumnarBatches}
+import org.apache.gluten.expression.InterpretedArrowGenerate
+import org.apache.gluten.extension.columnar.transition.Convention
+import org.apache.gluten.iterator.Iterators
+import org.apache.gluten.memory.arrow.alloc.ArrowBufferAllocators
+import org.apache.gluten.sql.shims.SparkShimLoader
+import org.apache.gluten.vectorized.{ArrowColumnarRow, 
ArrowWritableColumnVector}
+
+import org.apache.spark.rdd.RDD
+import org.apache.spark.sql.catalyst.InternalRow
+import org.apache.spark.sql.catalyst.expressions.{Attribute, 
AttributeReference, Expression, GenericInternalRow, Nondeterministic, 
SpecializedGetters}
+import org.apache.spark.sql.catalyst.expressions.BindReferences.bindReferences
+import org.apache.spark.sql.execution.{ExplainUtils, GenerateExec, SparkPlan, 
UnaryExecNode}
+import org.apache.spark.sql.execution.metric.{SQLMetric, SQLMetrics}
+import org.apache.spark.sql.types.{ArrayType, BinaryType, DataType, MapType, 
StringType, StructType}
+import org.apache.spark.sql.vectorized.{ColumnarBatch, ColumnVector}
+
+import scala.collection.mutable.{ArrayBuffer, ListBuffer}
+
+/**
+ * By rule <PartialGenerateRule>, if the generator is a instance of 
<HiveGenericUDTF>, then the
+ * generateExec will be changed to ColumnarPartialGenerateExec
+ *
+ * @param generateExec
+ *   the GenerateExec from vanilla
+ * @param child
+ *   child plan
+ */
+case class ColumnarPartialGenerateExec(generateExec: GenerateExec, child: 
SparkPlan)
+  extends UnaryExecNode
+  with ValidatablePlan {
+
+  private val generatorNullRow = new 
GenericInternalRow(generateExec.generatorOutput.length)
+
+  private val pruneChildAttributes: ListBuffer[Attribute] = ListBuffer()
+  private val pruneChildColumnIndices: ListBuffer[Int] = ListBuffer()
+  private val generateAttributes: ListBuffer[Attribute] = ListBuffer()
+  private val rightInputColumnIndices: ListBuffer[Int] = ListBuffer()
+
+  private val rightSchema =
+    
SparkShimLoader.getSparkShims.structFromAttributes(generateExec.generatorOutput)
+
+  private lazy val getLeftIndices = getColumnIndexInChildOutput(
+    pruneChildAttributes,
+    pruneChildColumnIndices,
+    generateExec.requiredChildOutput)
+  private lazy val getRightIndices = getColumnIndexInChildOutput(
+    generateAttributes,
+    rightInputColumnIndices,
+    Seq(generateExec.generator))
+
+  private lazy val generator = InterpretedArrowGenerate.create(
+    bindReferences(Seq(generateExec.generator), generateAttributes.toSeq).head)
+
+  @transient override lazy val metrics = Map(
+    "time" -> SQLMetrics.createTimingMetric(sparkContext, "total time of 
partial project"),
+    "velox_to_arrow_time" -> SQLMetrics.createTimingMetric(
+      sparkContext,
+      "time of velox to Arrow ColumnarBatch"),
+    "arrow_to_velox_time" -> SQLMetrics.createTimingMetric(
+      sparkContext,
+      "time of Arrow ColumnarBatch to velox")
+  )
+
+  private def getColumnIndexInChildOutput(
+      attributes: ListBuffer[Attribute],
+      indices: ListBuffer[Int],
+      exprs: Seq[Expression]): Unit = {
+    exprs.forall {
+      case a: AttributeReference =>
+        val index = child.output.indexWhere(s => s.exprId.equals(a.exprId))
+
+        if (index < 0) {
+          throw new IllegalStateException(
+            s"Couldn't find $a in ${child.output.attrs.mkString("[", ",", 
"]")}")
+        } else if (!indices.contains(index)) {
+          attributes.append(a)
+          indices.append(index)
+          true
+        } else true
+      case p =>
+        getColumnIndexInChildOutput(attributes, indices, p.children)
+        true
+    }
+  }
+
+  override protected def doValidateInternal(): ValidationResult = {
+    ValidationResult.succeeded

Review Comment:
   Please respect attrNotExists and hasUnsupportedDataType as PartialProject, 
some Spark data type is not supported in Velox



##########
backends-velox/src/test/scala/org/apache/spark/sql/execution/GlutenHiveUDFSuite.scala:
##########
@@ -120,6 +121,30 @@ class GlutenHiveUDFSuite extends GlutenQueryTest with 
SQLTestUtils {
     }
   }
 
+  test("customer udtf") {
+    withTempFunction("testUDTF") {
+      sql(s"CREATE TEMPORARY FUNCTION testUDTF AS 
'${classOf[CustomerUDTF].getName}'")
+      val df = sql(
+        "select l_partkey, col0, col1 from lineitem lateral view" +
+          " testUDTF(l_partkey, l_comment) as col0, col1")
+      df.collect()
+      df.show()

Review Comment:
   remove



##########
gluten-substrait/src/main/scala/org/apache/gluten/config/GlutenConfig.scala:
##########
@@ -1449,6 +1451,12 @@ object GlutenConfig {
       .booleanConf
       .createWithDefault(true)
 
+  val ENABLE_COLUMNAR_PARTIAL_GENERATE =
+    buildConf("spark.gluten.sql.columnar.partial.generate")
+      .doc("evaluates the non-offload-able HiveUDTF using vanilla Spark 
generator")

Review Comment:
   evaluates -> Evaluates



##########
backends-velox/src/test/scala/org/apache/spark/sql/execution/GlutenHiveUDFSuite.scala:
##########
@@ -120,6 +121,30 @@ class GlutenHiveUDFSuite extends GlutenQueryTest with 
SQLTestUtils {
     }
   }
 
+  test("customer udtf") {
+    withTempFunction("testUDTF") {
+      sql(s"CREATE TEMPORARY FUNCTION testUDTF AS 
'${classOf[CustomerUDTF].getName}'")
+      val df = sql(
+        "select l_partkey, col0, col1 from lineitem lateral view" +
+          " testUDTF(l_partkey, l_comment) as col0, col1")
+      df.collect()
+      df.show()
+      checkOperatorMatch[ColumnarPartialGenerateExec](df)
+    }
+  }
+
+  test("simple udtf") {
+    withTempFunction("simpleUDTF") {
+      sql(s"CREATE TEMPORARY FUNCTION testUDTF AS 
'${classOf[SimpleUDTF].getName}'")
+      val df = sql(
+        "select l_partkey, col0 from lineitem lateral view" +
+          " testUDTF(l_orderkey) as col0")
+      df.collect()
+      df.show()

Review Comment:
   remove



-- 
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]

Reply via email to