skambha commented on a change in pull request #24593: [SPARK-27692][SQL] Add 
new optimizer rule to evaluate the deterministic scala udf only once if all 
inputs are literals
URL: https://github.com/apache/spark/pull/24593#discussion_r289206289
 
 

 ##########
 File path: 
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/optimizer/DeterministicLiteralUDF.scala
 ##########
 @@ -0,0 +1,40 @@
+/*
+ * 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.catalyst.optimizer
+
+import org.apache.spark.sql.catalyst.expressions.{Literal, ScalaUDF}
+import org.apache.spark.sql.catalyst.plans.logical._
+import org.apache.spark.sql.catalyst.rules._
+import org.apache.spark.sql.internal.SQLConf
+
+/**
+ * If the UDF is deterministic and if the children are all literal, we can 
replace the udf
+ * with the output of the udf serialized
+ */
+object DeterministicLiteralUDF extends Rule[LogicalPlan] {
+  def apply(plan: LogicalPlan): LogicalPlan =
+    if (!SQLConf.get.deterministicLiteralUdfFoldEnabled) {
+      plan
+    } else plan transformAllExpressions {
+      case udf @ ScalaUDF(_, dataType, children, _, _, _, _, udfDeterministic)
+        if udf.deterministic && children.forall(_.isInstanceOf[Literal]) => {
 
 Review comment:
   There are places that I see 2 spaces in the code and also 4 spaces for the 
case guards.  
   fwiw,  I did go ahead and added 4 now.  

----------------------------------------------------------------
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.
 
For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


With regards,
Apache Git Services

---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscr...@spark.apache.org
For additional commands, e-mail: reviews-h...@spark.apache.org

Reply via email to