karenfeng commented on a change in pull request #31024:
URL: https://github.com/apache/spark/pull/31024#discussion_r655724937



##########
File path: 
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/optimizer/PredicateReorder.scala
##########
@@ -0,0 +1,145 @@
+/*
+ * 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.plans.logical._
+import 
org.apache.spark.sql.catalyst.plans.logical.statsEstimation.FilterEstimation
+import org.apache.spark.sql.catalyst.rules.Rule
+
+/**
+ * A rule that reorder conditions in filters base on estimated selectivity and 
compute cost.
+ *
+ * {{{
+ *   SELECT * FROM lineitem WHERE l_comment LIKE '%a%' AND l_orderkey = 1024 
==>
+ *   SELECT * FROM lineitem WHERE l_orderkey = 1024 AND l_comment LIKE '%a%'
+ * }}}
+ */
+object PredicateReorder extends Rule[LogicalPlan] with PredicateHelper {
+
+  private val DEFAULT_SELECTIVITY = 0.1D
+
+  private val IS_OP_COST = 1.0D
+  private val CAST_COST = 4.0D
+  private val LIKE_COST = 5.0D
+  private val BINARY_OP_COST = 1.0D
+  private val UNARY_OP_COST = 1.0D
+  private val TERNARY_OP_COST = 3.0D
+  private val QUATERNARY_OP_COST = 4.0D
+  private val SEPTENARY_OP_COST = 7.0D
+  private val COMPLEX_TYPE_MERGING_OP_COST = 10.0D
+  private val USER_DEFINED_OP_COST = 20.0D
+  private val UNKNOWN_COST = 30.0D
+
+  private def filterSelectivity(
+      exp: Expression, filterEstimation: Option[FilterEstimation]): Double = {
+    filterEstimation
+      .flatMap(_.calculateFilterSelectivity(exp, update = false))
+      .getOrElse(DEFAULT_SELECTIVITY)
+  }
+
+  // Formula: (selectivity - 1.0D) / expression cost
+  private def rankingAnd(exp: Expression, filterEstimation: 
Option[FilterEstimation]): Double = {
+    (filterSelectivity(exp, filterEstimation) - 1.0D) / expressionCost(exp)
+  }
+
+  // Formula: (-selectivity) / expression cost
+  private def rankingOr(exp: Expression, filterEstimation: 
Option[FilterEstimation]): Double = {
+    -filterSelectivity(exp, filterEstimation) / expressionCost(exp)
+  }
+
+  // The cost of a call expression exp is computed as:
+  //   cost(exp) = typeSize + functionCost + cost(children).
+  private def expressionCost(exp: Expression): Double = exp match {
+      case e: Expression if e.children.isEmpty =>

Review comment:
       Why this over `LeafExpression`?

##########
File path: 
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/optimizer/PredicateReorder.scala
##########
@@ -0,0 +1,145 @@
+/*
+ * 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.plans.logical._
+import 
org.apache.spark.sql.catalyst.plans.logical.statsEstimation.FilterEstimation
+import org.apache.spark.sql.catalyst.rules.Rule
+
+/**
+ * A rule that reorder conditions in filters base on estimated selectivity and 
compute cost.
+ *
+ * {{{
+ *   SELECT * FROM lineitem WHERE l_comment LIKE '%a%' AND l_orderkey = 1024 
==>
+ *   SELECT * FROM lineitem WHERE l_orderkey = 1024 AND l_comment LIKE '%a%'
+ * }}}
+ */
+object PredicateReorder extends Rule[LogicalPlan] with PredicateHelper {
+
+  private val DEFAULT_SELECTIVITY = 0.1D
+
+  private val IS_OP_COST = 1.0D
+  private val CAST_COST = 4.0D
+  private val LIKE_COST = 5.0D
+  private val BINARY_OP_COST = 1.0D
+  private val UNARY_OP_COST = 1.0D
+  private val TERNARY_OP_COST = 3.0D
+  private val QUATERNARY_OP_COST = 4.0D
+  private val SEPTENARY_OP_COST = 7.0D
+  private val COMPLEX_TYPE_MERGING_OP_COST = 10.0D
+  private val USER_DEFINED_OP_COST = 20.0D
+  private val UNKNOWN_COST = 30.0D
+
+  private def filterSelectivity(
+      exp: Expression, filterEstimation: Option[FilterEstimation]): Double = {
+    filterEstimation
+      .flatMap(_.calculateFilterSelectivity(exp, update = false))
+      .getOrElse(DEFAULT_SELECTIVITY)
+  }
+
+  // Formula: (selectivity - 1.0D) / expression cost
+  private def rankingAnd(exp: Expression, filterEstimation: 
Option[FilterEstimation]): Double = {
+    (filterSelectivity(exp, filterEstimation) - 1.0D) / expressionCost(exp)
+  }
+
+  // Formula: (-selectivity) / expression cost
+  private def rankingOr(exp: Expression, filterEstimation: 
Option[FilterEstimation]): Double = {
+    -filterSelectivity(exp, filterEstimation) / expressionCost(exp)
+  }
+
+  // The cost of a call expression exp is computed as:
+  //   cost(exp) = typeSize + functionCost + cost(children).
+  private def expressionCost(exp: Expression): Double = exp match {
+      case e: Expression if e.children.isEmpty =>
+        e.dataType.defaultSize
+      case e: IsNull =>
+        e.dataType.defaultSize + IS_OP_COST + 
e.children.map(expressionCost).sum

Review comment:
       If I'm reading this right, `e.dataType.defaultSize` will (usually) have 
a heavier weight than the operator cost. This 
[matches](https://github.com/apache/hive/blob/7b3ecf617a6d46f48a3b6f77e0339fd4ad95a420/ql/src/java/org/apache/hadoop/hive/ql/optimizer/calcite/rules/HiveFilterSortPredicates.java#L237)
 Hive, but 
[not](https://github.com/apache/impala/blob/b28da054f3595bb92873433211438306fc22fbc7/fe/src/main/java/org/apache/impala/analysis/IsNullPredicate.java#L165)
 Impala. Have you seen any issues with this?

##########
File path: 
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/optimizer/PredicateReorder.scala
##########
@@ -0,0 +1,145 @@
+/*
+ * 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.plans.logical._
+import 
org.apache.spark.sql.catalyst.plans.logical.statsEstimation.FilterEstimation
+import org.apache.spark.sql.catalyst.rules.Rule
+
+/**
+ * A rule that reorder conditions in filters base on estimated selectivity and 
compute cost.
+ *
+ * {{{
+ *   SELECT * FROM lineitem WHERE l_comment LIKE '%a%' AND l_orderkey = 1024 
==>
+ *   SELECT * FROM lineitem WHERE l_orderkey = 1024 AND l_comment LIKE '%a%'
+ * }}}
+ */
+object PredicateReorder extends Rule[LogicalPlan] with PredicateHelper {
+
+  private val DEFAULT_SELECTIVITY = 0.1D
+
+  private val IS_OP_COST = 1.0D
+  private val CAST_COST = 4.0D
+  private val LIKE_COST = 5.0D
+  private val BINARY_OP_COST = 1.0D
+  private val UNARY_OP_COST = 1.0D
+  private val TERNARY_OP_COST = 3.0D
+  private val QUATERNARY_OP_COST = 4.0D
+  private val SEPTENARY_OP_COST = 7.0D
+  private val COMPLEX_TYPE_MERGING_OP_COST = 10.0D
+  private val USER_DEFINED_OP_COST = 20.0D
+  private val UNKNOWN_COST = 30.0D
+
+  private def filterSelectivity(
+      exp: Expression, filterEstimation: Option[FilterEstimation]): Double = {
+    filterEstimation
+      .flatMap(_.calculateFilterSelectivity(exp, update = false))
+      .getOrElse(DEFAULT_SELECTIVITY)
+  }
+
+  // Formula: (selectivity - 1.0D) / expression cost
+  private def rankingAnd(exp: Expression, filterEstimation: 
Option[FilterEstimation]): Double = {
+    (filterSelectivity(exp, filterEstimation) - 1.0D) / expressionCost(exp)
+  }
+
+  // Formula: (-selectivity) / expression cost
+  private def rankingOr(exp: Expression, filterEstimation: 
Option[FilterEstimation]): Double = {
+    -filterSelectivity(exp, filterEstimation) / expressionCost(exp)
+  }
+
+  // The cost of a call expression exp is computed as:
+  //   cost(exp) = typeSize + functionCost + cost(children).
+  private def expressionCost(exp: Expression): Double = exp match {
+      case e: Expression if e.children.isEmpty =>
+        e.dataType.defaultSize

Review comment:
       Given that `e.dataType.defaultSize` is used everywhere, can we move it 
out of the cases?
   
   I.e.
   ```
   private def expressionCost(exp: Expression): Double = 
exp.dataType.defaultSize + exp match { ...
   ```




-- 
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:
[email protected]



---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to