You are viewing a plain text version of this content. The canonical link for it is here.
Posted to reviews@spark.apache.org by GitBox <gi...@apache.org> on 2019/06/20 04:51:54 UTC
[GitHub] [spark] HyukjinKwon 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
HyukjinKwon 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_r295643198
##########
File path: sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/optimizer/DeterministicLiteralUDFFolding.scala
##########
@@ -0,0 +1,39 @@
+/*
+ * 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 DeterministicLiteralUDFFolding extends Rule[LogicalPlan] {
+ def apply(plan: LogicalPlan): LogicalPlan =
+ if (!SQLConf.get.deterministicLiteralUdfFoldingEnabled) {
+ plan
+ } else plan transformAllExpressions {
+ case udf @ ScalaUDF(_, dataType, children, _, _, _, _, _)
+ if udf.deterministic && children.forall(_.isInstanceOf[Literal]) =>
+ val res = udf.eval(null)
+ Literal(res, dataType)
Review comment:
I think this can be part of https://github.com/apache/spark/pull/24553. Can `ScalaUDF` be foldable when all children are foldable and deterministic?
----------------------------------------------------------------
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:
users@infra.apache.org
With regards,
Apache Git Services
---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org