KevinyhZou commented on code in PR #8558:
URL: https://github.com/apache/incubator-gluten/pull/8558#discussion_r2002911567


##########
backends-clickhouse/src/main/scala/org/apache/gluten/expression/CHCollapseNestedExpressionsTransformer.scala:
##########
@@ -0,0 +1,143 @@
+/*
+ * 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.expression
+
+import org.apache.gluten.config.GlutenConfig
+import org.apache.gluten.substrait.expression.{ExpressionBuilder, 
ExpressionNode}
+
+import org.apache.spark.internal.Logging
+import org.apache.spark.sql.catalyst.expressions._
+import org.apache.spark.sql.types.DataType
+
+import java.util
+
+/**
+ * Collapse nested expressions for optimization, to reduce expression calls. 
Now support `and`,
+ * `or`. e.g. select ... and(and(a=1, b=2), c=3) => select ... and(a=1, b=2, 
c=3).
+ */
+case class CHCollapseNestedExpressionsTransformer(
+    substraitExprName: String,
+    children: Seq[ExpressionTransformer],
+    original: Expression)
+  extends ExpressionTransformer
+  with Logging {
+
+  override def doTransform(args: Object): ExpressionNode = {
+    if (canBeOptimized(original)) {
+      val functionMap = args.asInstanceOf[util.HashMap[String, java.lang.Long]]
+      val newExprNode = doTransform(original, children, functionMap)
+      logDebug("The new expression node: " + newExprNode.toProtobuf)
+      newExprNode
+    } else {
+      super.doTransform(args)
+    }
+  }
+
+  def getExpressionName(expr: Expression): Option[String] = expr match {
+    case _: And => ExpressionMappings.expressionsMap.get(classOf[And])
+    case _: Or => ExpressionMappings.expressionsMap.get(classOf[Or])

Review Comment:
   it removed now



##########
backends-clickhouse/src/main/scala/org/apache/gluten/expression/CHCollapseNestedExpressionsTransformer.scala:
##########
@@ -0,0 +1,143 @@
+/*
+ * 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.expression
+
+import org.apache.gluten.config.GlutenConfig
+import org.apache.gluten.substrait.expression.{ExpressionBuilder, 
ExpressionNode}
+
+import org.apache.spark.internal.Logging
+import org.apache.spark.sql.catalyst.expressions._
+import org.apache.spark.sql.types.DataType
+
+import java.util
+
+/**
+ * Collapse nested expressions for optimization, to reduce expression calls. 
Now support `and`,
+ * `or`. e.g. select ... and(and(a=1, b=2), c=3) => select ... and(a=1, b=2, 
c=3).
+ */
+case class CHCollapseNestedExpressionsTransformer(
+    substraitExprName: String,
+    children: Seq[ExpressionTransformer],
+    original: Expression)
+  extends ExpressionTransformer
+  with Logging {
+
+  override def doTransform(args: Object): ExpressionNode = {
+    if (canBeOptimized(original)) {
+      val functionMap = args.asInstanceOf[util.HashMap[String, java.lang.Long]]
+      val newExprNode = doTransform(original, children, functionMap)
+      logDebug("The new expression node: " + newExprNode.toProtobuf)
+      newExprNode
+    } else {
+      super.doTransform(args)
+    }
+  }
+
+  def getExpressionName(expr: Expression): Option[String] = expr match {
+    case _: And => ExpressionMappings.expressionsMap.get(classOf[And])
+    case _: Or => ExpressionMappings.expressionsMap.get(classOf[Or])
+    case _ => Option.empty[String]
+  }
+
+  private def canBeOptimized(expr: Expression): Boolean = {
+    var exprCall = expr
+    expr match {
+      case a: Alias => exprCall = a.child
+      case _ =>
+    }
+    val exprName = getExpressionName(exprCall)
+    exprName match {
+      case None =>
+        exprCall match {
+          case _: LeafExpression => false
+          case _ => exprCall.children.exists(c => canBeOptimized(c))
+        }
+      case Some(f) =>
+        GlutenConfig.get.getSupportedCollapsedExpressions.split(",").exists(c 
=> c.equals(f))

Review Comment:
   Yes, it will be optimized.  And it have been tested in the ut.



##########
backends-clickhouse/src/main/scala/org/apache/gluten/expression/CHCollapseNestedExpressionsTransformer.scala:
##########
@@ -0,0 +1,143 @@
+/*
+ * 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.expression
+
+import org.apache.gluten.config.GlutenConfig
+import org.apache.gluten.substrait.expression.{ExpressionBuilder, 
ExpressionNode}
+
+import org.apache.spark.internal.Logging
+import org.apache.spark.sql.catalyst.expressions._
+import org.apache.spark.sql.types.DataType
+
+import java.util
+
+/**
+ * Collapse nested expressions for optimization, to reduce expression calls. 
Now support `and`,
+ * `or`. e.g. select ... and(and(a=1, b=2), c=3) => select ... and(a=1, b=2, 
c=3).
+ */
+case class CHCollapseNestedExpressionsTransformer(
+    substraitExprName: String,
+    children: Seq[ExpressionTransformer],
+    original: Expression)
+  extends ExpressionTransformer
+  with Logging {
+
+  override def doTransform(args: Object): ExpressionNode = {
+    if (canBeOptimized(original)) {
+      val functionMap = args.asInstanceOf[util.HashMap[String, java.lang.Long]]
+      val newExprNode = doTransform(original, children, functionMap)
+      logDebug("The new expression node: " + newExprNode.toProtobuf)
+      newExprNode
+    } else {
+      super.doTransform(args)
+    }
+  }
+
+  def getExpressionName(expr: Expression): Option[String] = expr match {
+    case _: And => ExpressionMappings.expressionsMap.get(classOf[And])
+    case _: Or => ExpressionMappings.expressionsMap.get(classOf[Or])
+    case _ => Option.empty[String]
+  }
+
+  private def canBeOptimized(expr: Expression): Boolean = {

Review Comment:
   No, this will return the orignal plan in the `doTransform` method.



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