liujiayi771 commented on code in PR #4901:
URL: https://github.com/apache/incubator-gluten/pull/4901#discussion_r1520944825


##########
backends-velox/src/main/scala/io/glutenproject/execution/GenerateExecTransformer.scala:
##########
@@ -0,0 +1,232 @@
+/*
+ * 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 io.glutenproject.execution
+
+import io.glutenproject.backendsapi.BackendsApiManager
+import io.glutenproject.expression.{ConverterUtils, ExpressionConverter, 
ExpressionNames}
+import io.glutenproject.expression.ConverterUtils.FunctionConfig
+import io.glutenproject.extension.ValidationResult
+import io.glutenproject.metrics.{GenerateMetricsUpdater, MetricsUpdater}
+import io.glutenproject.substrait.`type`.TypeBuilder
+import io.glutenproject.substrait.SubstraitContext
+import io.glutenproject.substrait.expression.{ExpressionBuilder, 
ExpressionNode}
+import io.glutenproject.substrait.extensions.{AdvancedExtensionNode, 
ExtensionBuilder}
+import io.glutenproject.substrait.rel.{RelBuilder, RelNode}
+
+import org.apache.spark.sql.catalyst.expressions._
+import org.apache.spark.sql.execution.SparkPlan
+import org.apache.spark.sql.execution.metric.SQLMetrics
+import org.apache.spark.sql.types.{ArrayType, LongType, MapType, StructType}
+
+import com.google.common.collect.Lists
+import com.google.protobuf.StringValue
+
+import scala.collection.JavaConverters._
+
+case class GenerateExecTransformer(
+    generator: Generator,
+    requiredChildOutput: Seq[Attribute],
+    outer: Boolean,
+    generatorOutput: Seq[Attribute],
+    child: SparkPlan)
+  extends GenerateExecTransformerBase(
+    generator,
+    requiredChildOutput,
+    outer,
+    generatorOutput,
+    child) {
+
+  @transient
+  override lazy val metrics =
+    Map("numOutputRows" -> SQLMetrics.createMetric(sparkContext, "number of 
output rows"))
+
+  override def metricsUpdater(): MetricsUpdater = new 
GenerateMetricsUpdater(metrics)
+
+  override protected def withNewChildInternal(newChild: SparkPlan): 
GenerateExecTransformer =
+    copy(generator, requiredChildOutput, outer, generatorOutput, newChild)
+
+  override protected def doGeneratorValidate(
+      generator: Generator,
+      outer: Boolean): ValidationResult = {
+    if (outer) {
+      return ValidationResult.notOk(s"Velox backend does not support outer")
+    }
+    generator match {
+      case _: JsonTuple =>
+        ValidationResult.notOk(s"Velox backend does not support this 
json_tuple")
+      case _: ExplodeBase =>
+        ValidationResult.ok
+      case Inline(child) =>
+        child match {
+          case AttributeReference(_, ArrayType(_: StructType, _), _, _) =>
+            ValidationResult.ok
+          case _ =>
+            // TODO: Support Literal/CreateArray.
+            ValidationResult.notOk(
+              s"Velox backend does not support inline with expression " +
+                s"${child.getClass.getSimpleName}.")
+        }
+      case _ =>
+        ValidationResult.ok
+    }
+  }
+
+  override protected def getRelNode(
+      context: SubstraitContext,
+      inputRel: RelNode,
+      generatorNode: ExpressionNode,
+      validation: Boolean): RelNode = {
+    val operatorId = context.nextOperatorId(this.nodeName)
+
+    val newInput = if (!validation) {
+      applyPreProject(inputRel, context, operatorId)
+    } else {
+      // No need to validate the pre-projection. The generator output has been 
validated in
+      // doGeneratorValidate.
+      inputRel
+    }
+
+    val generateRel = RelBuilder.makeGenerateRel(
+      newInput,
+      generatorNode,
+      requiredChildOutputNodes.asJava,
+      getExtensionNode(validation),
+      context,
+      operatorId)
+
+    if (!validation) {
+      applyPostProject(generateRel, context, operatorId)
+    } else {
+      // No need to validate the post-projection on the native side as
+      // it only flattens the generator's output.
+      generateRel
+    }
+  }
+
+  private def getExtensionNode(validation: Boolean): AdvancedExtensionNode = {
+    if (!validation) {
+      // Start with "GenerateParameters:"
+      val parametersStr = new StringBuffer("GenerateParameters:")
+      // isPosExplode: 1 for PosExplode, 0 for others.
+      val isPosExplode = if (generator.isInstanceOf[PosExplode]) {
+        "1"
+      } else {
+        "0"
+      }
+      parametersStr
+        .append("isPosExplode=")
+        .append(isPosExplode)
+        .append("\n")
+      val message = StringValue
+        .newBuilder()
+        .setValue(parametersStr.toString)
+        .build()
+      val optimization = 
BackendsApiManager.getTransformerApiInstance.packPBMessage(message)
+      ExtensionBuilder.makeAdvancedExtension(optimization, null)
+    } else {
+      getExtensionNodeForValidation
+    }
+  }
+
+  // Select child outputs and append generator input.
+  private def applyPreProject(

Review Comment:
   Could you refactor this part using `PullOutPreProject` rule? Or I can take 
this in another PR.



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