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 2023/01/04 03:10:23 UTC

[GitHub] [spark] ulysses-you opened a new pull request, #39377: [SPARK-41867][SQL] Selective predicate should respect InMemoryRelation

ulysses-you opened a new pull request, #39377:
URL: https://github.com/apache/spark/pull/39377

   <!--
   Thanks for sending a pull request!  Here are some tips for you:
     1. If this is your first time, please read our contributor guidelines: https://spark.apache.org/contributing.html
     2. Ensure you have added or run the appropriate tests for your PR: https://spark.apache.org/developer-tools.html
     3. If the PR is unfinished, add '[WIP]' in your PR title, e.g., '[WIP][SPARK-XXXX] Your PR title ...'.
     4. Be sure to keep the PR description updated to reflect all changes.
     5. Please write your PR title to summarize what this PR proposes.
     6. If possible, provide a concise example to reproduce the issue for a faster review.
     7. If you want to add a new configuration, please read the guideline first for naming configurations in
        'core/src/main/scala/org/apache/spark/internal/config/ConfigEntry.scala'.
     8. If you want to add or modify an error type or message, please read the guideline first in
        'core/src/main/resources/error/README.md'.
   -->
   
   ### What changes were proposed in this pull request?
   <!--
   Please clarify what changes you are proposing. The purpose of this section is to outline the changes and how this PR fixes the issue. 
   If possible, please consider writing useful notes for better and faster reviews in your PR. See the examples below.
     1. If you refactor some codes with changing classes, showing the class hierarchy will help reviewers.
     2. If you fix some SQL features, you can provide some references of other DBMSes.
     3. If there is design documentation, please add the link.
     4. If there is a discussion in the mailing list, please add the link.
   -->
   This pr add a new trait `SelectivePredicateHelper` at `sql/core` module that provide a method to find if the plan has selective predicate even if the predicate is in `InMemoryRelation`. The reason why it should be placed in `sql/core` module is the `InMemoryRelation` is at `sql/core`.
   
   Then, move the `InjectRuntimeFilter` from `sql/catalyst` module to `sql/core` module so it can use the new trait.
   Also make `PartitionPruning` use `SelectivePredicateHelper` instead of `PredicateHelper`.
   
   
   ### Why are the changes needed?
   <!--
   Please clarify why the changes are needed. For instance,
     1. If you propose a new API, clarify the use case for a new API.
     2. If you fix a bug, you can clarify why it is a bug.
   -->
   DPP and Runtime Filter require the build side has a selective predicate. It should also work if the filter is inside the cached relation.
   
   
   ### Does this PR introduce _any_ user-facing change?
   <!--
   Note that it means *any* user-facing change including all aspects such as the documentation fix.
   If yes, please clarify the previous behavior and the change this PR proposes - provide the console output, description and/or an example to show the behavior difference if possible.
   If possible, please also clarify if this is a user-facing change compared to the released Spark versions or within the unreleased branches such as master.
   If no, write 'No'.
   -->
   yes, more cases can be optimized
   
   ### How was this patch tested?
   <!--
   If tests were added, say they were added here. Please make sure to add some test cases that check the changes thoroughly including negative and positive cases if possible.
   If it was tested in a way different from regular unit tests, please clarify how you tested step by step, ideally copy and paste-able, so that other reviewers can test and check, and descendants can verify in the future.
   If tests were not added, please describe why they were not added and/or why it was difficult to add.
   If benchmark tests were added, please run the benchmarks in GitHub Actions for the consistent environment, and the instructions could accord to: https://spark.apache.org/developer-tools.html#github-workflow-benchmarks.
   -->
   add test


-- 
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: reviews-unsubscribe@spark.apache.org

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


[GitHub] [spark] ulysses-you commented on a diff in pull request #39377: [SPARK-41867][SQL] Selective predicate should respect InMemoryRelation

Posted by GitBox <gi...@apache.org>.
ulysses-you commented on code in PR #39377:
URL: https://github.com/apache/spark/pull/39377#discussion_r1062152134


##########
sql/core/src/main/scala/org/apache/spark/sql/execution/dynamicpruning/PartitionPruning.scala:
##########
@@ -167,44 +167,10 @@ object PartitionPruning extends Rule[LogicalPlan] with PredicateHelper with Join
     }
 
     val estimatePruningSideSize = filterRatio * partPlan.stats.sizeInBytes.toFloat
-    val overhead = calculatePlanOverhead(otherPlan)
+    val overhead = collectLeafPlanOverhead(otherPlan).sum.toFloat
     estimatePruningSideSize > overhead
   }
 
-  /**
-   * Calculates a heuristic overhead of a logical plan. Normally it returns the total
-   * size in bytes of all scan relations. We don't count in-memory relation which uses
-   * only memory.
-   */
-  private def calculatePlanOverhead(plan: LogicalPlan): Float = {
-    val (cached, notCached) = plan.collectLeaves().partition(p => p match {
-      case _: InMemoryRelation => true
-      case _ => false
-    })
-    val scanOverhead = notCached.map(_.stats.sizeInBytes).sum.toFloat
-    val cachedOverhead = cached.map {
-      case m: InMemoryRelation if m.cacheBuilder.storageLevel.useDisk &&

Review Comment:
   this method moved into the new trait.



-- 
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: reviews-unsubscribe@spark.apache.org

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


[GitHub] [spark] ulysses-you commented on a diff in pull request #39377: [SPARK-41867][SQL] Selective predicate should respect InMemoryRelation

Posted by GitBox <gi...@apache.org>.
ulysses-you commented on code in PR #39377:
URL: https://github.com/apache/spark/pull/39377#discussion_r1063232417


##########
sql/core/src/main/scala/org/apache/spark/sql/catalyst/optimizer/SelectivePredicateHelper.scala:
##########
@@ -0,0 +1,48 @@
+/*
+ * 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.PredicateHelper
+import org.apache.spark.sql.catalyst.plans.logical.{Filter, LogicalPlan}
+import org.apache.spark.sql.execution.FilterExec
+import org.apache.spark.sql.execution.columnar.InMemoryRelation
+
+/**
+ * [[InMemoryRelation]] is at sql/core module, so this helper trait also places in sql/core.
+ */
+trait SelectivePredicateHelper extends PredicateHelper {
+
+  /**
+   * Search a filtering predicate in a given logical plan
+   */
+  def hasSelectivePredicate(plan: LogicalPlan): Boolean = {
+    plan.exists {
+      // We do not actually need a selective predicate if the `InMemoryRelation` is materialized,
+      // because its statistics is correct enough. The call side would validate if the statistics
+      // is bigger than requirements.
+      case relation: InMemoryRelation if relation.isMaterialized => true

Review Comment:
   It acutally only give a chance to optimize that the `InMemoryRelation` has no selective predicate but is a small relation.
   
   According to the call side:
   1. `PartitionPruning`, it use the the sum of overhead of leaf plan
        So if one of the leaf plan is a materialized `InMemoryRelation` with no selective predicate, the sum of overhead will smaller than before and it likely can be optimized which can't before.
   
   2. `InjectRuntimeFilter`, it use the maximum overhead of leaf plan
        So if one of the cached plan is small enough, it won't affect the final result. But if there is only one small cached plan, it likely can be optimized as well.



-- 
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: reviews-unsubscribe@spark.apache.org

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


[GitHub] [spark] ulysses-you commented on pull request #39377: [SPARK-41867][SQL] Selective predicate should respect InMemoryRelation

Posted by GitBox <gi...@apache.org>.
ulysses-you commented on PR #39377:
URL: https://github.com/apache/spark/pull/39377#issuecomment-1371839438

   @cloud-fan good suggestion. Follow the current code path:
   1. check if build side has selective predicate. Now it always returns ture if the cached relation is materialized
   2. check if the statistics is match the requirements. Now both DPP and Runtime Filter use the same calculate overhead method which considers the cached relation


-- 
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: reviews-unsubscribe@spark.apache.org

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


[GitHub] [spark] cloud-fan commented on a diff in pull request #39377: [SPARK-41867][SQL] Selective predicate should respect InMemoryRelation

Posted by GitBox <gi...@apache.org>.
cloud-fan commented on code in PR #39377:
URL: https://github.com/apache/spark/pull/39377#discussion_r1063256483


##########
sql/core/src/main/scala/org/apache/spark/sql/catalyst/optimizer/InjectRuntimeFilter.scala:
##########
@@ -146,7 +147,7 @@ object InjectRuntimeFilter extends Rule[LogicalPlan] with PredicateHelper with J
           predicateReference ++ condition.references,
           hasHitFilter = true,
           hasHitSelectiveFilter = hasHitSelectiveFilter || isLikelySelective(condition))
-      case _: LeafNode => hasHitSelectiveFilter
+      case l: LeafNode => hasHitSelectiveFilter || hasSelectivePredicate(l)

Review Comment:
   how about we add a new trait `LeafNodeWithAccurateStats`? `InMemoryRelation` will be the only implementation. Then here we can just do
   ```
   case l: LeafNodeWithAccurateStats => l.stats.rowCount.exists(_ < a_threshold) 
   ```



-- 
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: reviews-unsubscribe@spark.apache.org

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


[GitHub] [spark] github-actions[bot] commented on pull request #39377: [SPARK-41867][SQL] Selective predicate should respect InMemoryRelation

Posted by "github-actions[bot] (via GitHub)" <gi...@apache.org>.
github-actions[bot] commented on PR #39377:
URL: https://github.com/apache/spark/pull/39377#issuecomment-1510534697

   We're closing this PR because it hasn't been updated in a while. This isn't a judgement on the merit of the PR in any way. It's just a way of keeping the PR queue manageable.
   If you'd like to revive this PR, please reopen it and ask a committer to remove the Stale tag!


-- 
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: reviews-unsubscribe@spark.apache.org

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


[GitHub] [spark] ulysses-you commented on a diff in pull request #39377: [SPARK-41867][SQL] Selective predicate should respect InMemoryRelation

Posted by GitBox <gi...@apache.org>.
ulysses-you commented on code in PR #39377:
URL: https://github.com/apache/spark/pull/39377#discussion_r1062151959


##########
sql/core/src/main/scala/org/apache/spark/sql/execution/dynamicpruning/PartitionPruning.scala:
##########
@@ -167,44 +167,10 @@ object PartitionPruning extends Rule[LogicalPlan] with PredicateHelper with Join
     }
 
     val estimatePruningSideSize = filterRatio * partPlan.stats.sizeInBytes.toFloat
-    val overhead = calculatePlanOverhead(otherPlan)
+    val overhead = collectLeafPlanOverhead(otherPlan).sum.toFloat
     estimatePruningSideSize > overhead
   }
 
-  /**
-   * Calculates a heuristic overhead of a logical plan. Normally it returns the total
-   * size in bytes of all scan relations. We don't count in-memory relation which uses
-   * only memory.
-   */
-  private def calculatePlanOverhead(plan: LogicalPlan): Float = {
-    val (cached, notCached) = plan.collectLeaves().partition(p => p match {
-      case _: InMemoryRelation => true
-      case _ => false
-    })
-    val scanOverhead = notCached.map(_.stats.sizeInBytes).sum.toFloat
-    val cachedOverhead = cached.map {
-      case m: InMemoryRelation if m.cacheBuilder.storageLevel.useDisk &&

Review Comment:
   the cached relation should be materialized right ? otherwise its statistics should be same with normal plan.



-- 
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: reviews-unsubscribe@spark.apache.org

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


[GitHub] [spark] ulysses-you commented on pull request #39377: [SPARK-41867][SQL] Selective predicate should respect InMemoryRelation

Posted by GitBox <gi...@apache.org>.
ulysses-you commented on PR #39377:
URL: https://github.com/apache/spark/pull/39377#issuecomment-1370527422

   cc @cloud-fan @viirya @sigmod 


-- 
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: reviews-unsubscribe@spark.apache.org

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


[GitHub] [spark] ulysses-you commented on a diff in pull request #39377: [SPARK-41867][SQL] Selective predicate should respect InMemoryRelation

Posted by GitBox <gi...@apache.org>.
ulysses-you commented on code in PR #39377:
URL: https://github.com/apache/spark/pull/39377#discussion_r1063226296


##########
sql/core/src/main/scala/org/apache/spark/sql/execution/dynamicpruning/PartitionPruning.scala:
##########
@@ -183,28 +184,20 @@ object PartitionPruning extends Rule[LogicalPlan] with PredicateHelper with Join
     })
     val scanOverhead = notCached.map(_.stats.sizeInBytes).sum.toFloat
     val cachedOverhead = cached.map {
-      case m: InMemoryRelation if m.cacheBuilder.storageLevel.useDisk &&
-          !m.cacheBuilder.storageLevel.useMemory =>
-        m.stats.sizeInBytes.toFloat
-      case m: InMemoryRelation if m.cacheBuilder.storageLevel.useDisk =>
-        m.stats.sizeInBytes.toFloat * 0.2
-      case m: InMemoryRelation if m.cacheBuilder.storageLevel.useMemory =>
-        0.0
+      case m: InMemoryRelation if m.isMaterialized =>
+        if (m.cacheBuilder.storageLevel.useDisk && !m.cacheBuilder.storageLevel.useMemory) {
+          m.stats.sizeInBytes.toFloat
+        } else if (m.cacheBuilder.storageLevel.useDisk) {
+          m.stats.sizeInBytes.toFloat * 0.2
+        } else {
+          // use memory only
+          0.0
+        }
+      case m => m.stats.sizeInBytes.toFloat

Review Comment:
   yeas, to be conservative I use the overhead of original plan, otherwise it may cause different behavior with same query but one is cached



-- 
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: reviews-unsubscribe@spark.apache.org

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


[GitHub] [spark] ulysses-you commented on a diff in pull request #39377: [SPARK-41867][SQL] Selective predicate should respect InMemoryRelation

Posted by GitBox <gi...@apache.org>.
ulysses-you commented on code in PR #39377:
URL: https://github.com/apache/spark/pull/39377#discussion_r1063232417


##########
sql/core/src/main/scala/org/apache/spark/sql/catalyst/optimizer/SelectivePredicateHelper.scala:
##########
@@ -0,0 +1,48 @@
+/*
+ * 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.PredicateHelper
+import org.apache.spark.sql.catalyst.plans.logical.{Filter, LogicalPlan}
+import org.apache.spark.sql.execution.FilterExec
+import org.apache.spark.sql.execution.columnar.InMemoryRelation
+
+/**
+ * [[InMemoryRelation]] is at sql/core module, so this helper trait also places in sql/core.
+ */
+trait SelectivePredicateHelper extends PredicateHelper {
+
+  /**
+   * Search a filtering predicate in a given logical plan
+   */
+  def hasSelectivePredicate(plan: LogicalPlan): Boolean = {
+    plan.exists {
+      // We do not actually need a selective predicate if the `InMemoryRelation` is materialized,
+      // because its statistics is correct enough. The call side would validate if the statistics
+      // is bigger than requirements.
+      case relation: InMemoryRelation if relation.isMaterialized => true

Review Comment:
   It acutally only give a chance to optimize that the `InMemoryRelation` has no selective predicate but is a small relation.
   
   According to the call side:
   1. `PartitionPruning`, it use the the sum of overhead of leaf plan
        So if one of the leaf plan is a materialized `InMemoryRelation` with no selective predicate, the overhead summary will smaller than before and it likely can be optimized which can't before.
   
   2. `InjectRuntimeFilter`, it use the maximum overhead of leaf plan
        So if one of the cached plan is small enough, it won't affect the final result. But if there is only one small cached plan, it likely can be optimized as well.



-- 
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: reviews-unsubscribe@spark.apache.org

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


[GitHub] [spark] github-actions[bot] closed pull request #39377: [SPARK-41867][SQL] Selective predicate should respect InMemoryRelation

Posted by "github-actions[bot] (via GitHub)" <gi...@apache.org>.
github-actions[bot] closed pull request #39377: [SPARK-41867][SQL] Selective predicate should respect InMemoryRelation
URL: https://github.com/apache/spark/pull/39377


-- 
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: reviews-unsubscribe@spark.apache.org

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


[GitHub] [spark] ulysses-you commented on a diff in pull request #39377: [SPARK-41867][SQL] Selective predicate should respect InMemoryRelation

Posted by GitBox <gi...@apache.org>.
ulysses-you commented on code in PR #39377:
URL: https://github.com/apache/spark/pull/39377#discussion_r1062152134


##########
sql/core/src/main/scala/org/apache/spark/sql/execution/dynamicpruning/PartitionPruning.scala:
##########
@@ -167,44 +167,10 @@ object PartitionPruning extends Rule[LogicalPlan] with PredicateHelper with Join
     }
 
     val estimatePruningSideSize = filterRatio * partPlan.stats.sizeInBytes.toFloat
-    val overhead = calculatePlanOverhead(otherPlan)
+    val overhead = collectLeafPlanOverhead(otherPlan).sum.toFloat
     estimatePruningSideSize > overhead
   }
 
-  /**
-   * Calculates a heuristic overhead of a logical plan. Normally it returns the total
-   * size in bytes of all scan relations. We don't count in-memory relation which uses
-   * only memory.
-   */
-  private def calculatePlanOverhead(plan: LogicalPlan): Float = {
-    val (cached, notCached) = plan.collectLeaves().partition(p => p match {
-      case _: InMemoryRelation => true
-      case _ => false
-    })
-    val scanOverhead = notCached.map(_.stats.sizeInBytes).sum.toFloat
-    val cachedOverhead = cached.map {
-      case m: InMemoryRelation if m.cacheBuilder.storageLevel.useDisk &&

Review Comment:
   this method moved into the new trait.



-- 
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: reviews-unsubscribe@spark.apache.org

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


[GitHub] [spark] viirya commented on a diff in pull request #39377: [SPARK-41867][SQL] Selective predicate should respect InMemoryRelation

Posted by GitBox <gi...@apache.org>.
viirya commented on code in PR #39377:
URL: https://github.com/apache/spark/pull/39377#discussion_r1063170911


##########
sql/core/src/main/scala/org/apache/spark/sql/execution/dynamicpruning/PartitionPruning.scala:
##########
@@ -183,28 +184,20 @@ object PartitionPruning extends Rule[LogicalPlan] with PredicateHelper with Join
     })
     val scanOverhead = notCached.map(_.stats.sizeInBytes).sum.toFloat
     val cachedOverhead = cached.map {
-      case m: InMemoryRelation if m.cacheBuilder.storageLevel.useDisk &&
-          !m.cacheBuilder.storageLevel.useMemory =>
-        m.stats.sizeInBytes.toFloat
-      case m: InMemoryRelation if m.cacheBuilder.storageLevel.useDisk =>
-        m.stats.sizeInBytes.toFloat * 0.2
-      case m: InMemoryRelation if m.cacheBuilder.storageLevel.useMemory =>
-        0.0
+      case m: InMemoryRelation if m.isMaterialized =>
+        if (m.cacheBuilder.storageLevel.useDisk && !m.cacheBuilder.storageLevel.useMemory) {
+          m.stats.sizeInBytes.toFloat
+        } else if (m.cacheBuilder.storageLevel.useDisk) {
+          m.stats.sizeInBytes.toFloat * 0.2
+        } else {
+          // use memory only
+          0.0
+        }
+      case m => m.stats.sizeInBytes.toFloat

Review Comment:
   Hmm, this looks okay, although I guess that the cost of materializing maybe be still larger for `useDisk` (writing to disk?) than `useMemory` too?



-- 
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: reviews-unsubscribe@spark.apache.org

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


[GitHub] [spark] ulysses-you commented on a diff in pull request #39377: [SPARK-41867][SQL] Selective predicate should respect InMemoryRelation

Posted by GitBox <gi...@apache.org>.
ulysses-you commented on code in PR #39377:
URL: https://github.com/apache/spark/pull/39377#discussion_r1063232417


##########
sql/core/src/main/scala/org/apache/spark/sql/catalyst/optimizer/SelectivePredicateHelper.scala:
##########
@@ -0,0 +1,48 @@
+/*
+ * 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.PredicateHelper
+import org.apache.spark.sql.catalyst.plans.logical.{Filter, LogicalPlan}
+import org.apache.spark.sql.execution.FilterExec
+import org.apache.spark.sql.execution.columnar.InMemoryRelation
+
+/**
+ * [[InMemoryRelation]] is at sql/core module, so this helper trait also places in sql/core.
+ */
+trait SelectivePredicateHelper extends PredicateHelper {
+
+  /**
+   * Search a filtering predicate in a given logical plan
+   */
+  def hasSelectivePredicate(plan: LogicalPlan): Boolean = {
+    plan.exists {
+      // We do not actually need a selective predicate if the `InMemoryRelation` is materialized,
+      // because its statistics is correct enough. The call side would validate if the statistics
+      // is bigger than requirements.
+      case relation: InMemoryRelation if relation.isMaterialized => true

Review Comment:
   It acutally only give a chance to optimize that the `InMemoryRelation` has no selective predicate but is a small relation.
   
   According to the call side:
   1. `PartitionPruning`, it use the overhead summary of leaf plan
        So if one of the leaf plan is a materialized `InMemoryRelation` with no selective predicate, the overhead summary will smaller than before and it likely can be optimized which can't before.
   
   2. `InjectRuntimeFilter`, it use the maximum overhead of leaf plan
        So if one of the cached plan is small enough, it won't affect the final result. But if there is only one small cached plan, it likely can be optimized as well.



-- 
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: reviews-unsubscribe@spark.apache.org

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


[GitHub] [spark] viirya commented on a diff in pull request #39377: [SPARK-41867][SQL] Selective predicate should respect InMemoryRelation

Posted by GitBox <gi...@apache.org>.
viirya commented on code in PR #39377:
URL: https://github.com/apache/spark/pull/39377#discussion_r1063176762


##########
sql/core/src/main/scala/org/apache/spark/sql/catalyst/optimizer/SelectivePredicateHelper.scala:
##########
@@ -0,0 +1,48 @@
+/*
+ * 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.PredicateHelper
+import org.apache.spark.sql.catalyst.plans.logical.{Filter, LogicalPlan}
+import org.apache.spark.sql.execution.FilterExec
+import org.apache.spark.sql.execution.columnar.InMemoryRelation
+
+/**
+ * [[InMemoryRelation]] is at sql/core module, so this helper trait also places in sql/core.
+ */
+trait SelectivePredicateHelper extends PredicateHelper {
+
+  /**
+   * Search a filtering predicate in a given logical plan
+   */
+  def hasSelectivePredicate(plan: LogicalPlan): Boolean = {
+    plan.exists {
+      // We do not actually need a selective predicate if the `InMemoryRelation` is materialized,
+      // because its statistics is correct enough. The call side would validate if the statistics
+      // is bigger than requirements.
+      case relation: InMemoryRelation if relation.isMaterialized => true

Review Comment:
   So if the other side (filtering plan) has any `InMemoryRelation`, it will trigger pruning? If the cached plan is just (small) part of the filtering plan?



-- 
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: reviews-unsubscribe@spark.apache.org

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


[GitHub] [spark] ulysses-you commented on a diff in pull request #39377: [SPARK-41867][SQL] Selective predicate should respect InMemoryRelation

Posted by GitBox <gi...@apache.org>.
ulysses-you commented on code in PR #39377:
URL: https://github.com/apache/spark/pull/39377#discussion_r1063284976


##########
sql/core/src/main/scala/org/apache/spark/sql/catalyst/optimizer/InjectRuntimeFilter.scala:
##########
@@ -146,7 +147,7 @@ object InjectRuntimeFilter extends Rule[LogicalPlan] with PredicateHelper with J
           predicateReference ++ condition.references,
           hasHitFilter = true,
           hasHitSelectiveFilter = hasHitSelectiveFilter || isLikelySelective(condition))
-      case _: LeafNode => hasHitSelectiveFilter
+      case l: LeafNode => hasHitSelectiveFilter || hasSelectivePredicate(l)

Review Comment:
   how about this ? then we can use accurate stats of `InMemoryRelation` after materialized.
   ```scala
   trait LeafNodeWithAccurateStats extends LeafNode {
     def statsAvailable: Boolean
   }
   ```



-- 
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: reviews-unsubscribe@spark.apache.org

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