spark-reviews mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From GitBox <...@apache.org>
Subject [GitHub] [spark] dongjoon-hyun commented on a change in pull request #29565: [SPARK-24994][SQL] Simplify casts for literal types
Date Sat, 29 Aug 2020 23:30:51 GMT

dongjoon-hyun commented on a change in pull request #29565:
URL: https://github.com/apache/spark/pull/29565#discussion_r479700376



##########
File path: sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/optimizer/UnwrapCast.scala
##########
@@ -0,0 +1,214 @@
+/*
+ * 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._
+import org.apache.spark.sql.catalyst.expressions.Literal.FalseLiteral
+import org.apache.spark.sql.catalyst.plans.logical.LogicalPlan
+import org.apache.spark.sql.catalyst.rules.Rule
+import org.apache.spark.sql.types._
+
+/**
+ * Unwrap casts in binary comparison operations with patterns like following:
+ *
+ * `BinaryComparison(Cast(fromExp, toType), Literal(value, toType))`
+ *   or
+ * `BinaryComparison(Literal(value, toType), Cast(fromExp, toType))`
+ *
+ * This rule optimizes expressions with the above pattern by either replacing the cast with
simpler
+ * constructs, or moving the cast from the expression side to the literal side, which enables
them
+ * to be optimized away later and pushed down to data sources.
+ *
+ * Currently this only handles cases where `fromType` (of `fromExp`) and `toType` are of
integral
+ * types (i.e., byte, short, int and long). The rule checks to see if the literal `value`
is
+ * within range `(min, max)`, where `min` and `max` are the minimum and maximum value of
+ * `fromType`, respectively. If this is true then it means we can safely cast `value` to
`fromType`
+ * and thus able to move the cast to the literal side.
+ *
+ * If the `value` is not within range `(min, max)`, the rule breaks the scenario into different
+ * cases and try to replace each with simpler constructs.
+ *
+ * if `value > max`, the cases are of following:
+ *  - `cast(exp, ty) > value` ==> if(isnull(exp), null, false)
+ *  - `cast(exp, ty) >= value` ==> if(isnull(exp), null, false)
+ *  - `cast(exp, ty) === value` ==> if(isnull(exp), null, false)
+ *  - `cast(exp, ty) <=> value` ==> false
+ *  - `cast(exp, ty) <= value` ==> if(isnull(exp), null, true)
+ *  - `cast(exp, ty) < value` ==> if(isnull(exp), null, true)
+ *
+ * if `value == max`, the cases are of following:
+ *  - `cast(exp, ty) > value` ==> if(isnull(exp), null, false)
+ *  - `cast(exp, ty) >= value` ==> exp == max
+ *  - `cast(exp, ty) === value` ==> exp == max
+ *  - `cast(exp, ty) <=> value` ==> exp == max
+ *  - `cast(exp, ty) <= value` ==> if(isnull(exp), null, true)
+ *  - `cast(exp, ty) < value` ==> exp =!= max
+ *
+ * Similarly for the cases when `value == min` and `value < min`.
+ *
+ * Further, the above `if(isnull(exp), null, false)` is represented using conjunction
+ * `and(isnull(exp), null)`, to enable further optimization and filter pushdown to data sources.
+ * Similarly, `if(isnull(exp), null, true)` is represented with `or(isnotnull(exp), null)`.
+ */
+object UnwrapCast extends Rule[LogicalPlan] {

Review comment:
       You can keep the private function name `private def unwrapCast`.




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



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


Mime
View raw message