-
Notifications
You must be signed in to change notification settings - Fork 305
fix: scalar subquery pushdown and reuse for CometNativeScanExec (SPARK-43402) #4053
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 4 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
7dc98f1
Fix CometNativeScanExec.equals to include dataFilters (which was sile…
mbutrovich 8f0200f
Merge branch 'main' into data_filter_reuse_4042
mbutrovich 9742bc9
Fix unnecessary include in Spark 4.0.1 diff.
mbutrovich 25a7eb9
Fix CometReuseSubquery double-wrapping: skip subqueries already wrapp…
mbutrovich 0654aa6
Merge branch 'main' into data_filter_reuse_4042
mbutrovich d04fc2b
Address PR feedback.
mbutrovich 3ab689b
Merge branch 'main' into data_filter_reuse_4042
mbutrovich File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
67 changes: 67 additions & 0 deletions
67
spark/src/main/scala/org/apache/comet/rules/CometReuseSubquery.scala
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,67 @@ | ||
| /* | ||
| * 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.comet.rules | ||
|
|
||
| import scala.collection.mutable | ||
|
|
||
| import org.apache.spark.sql.catalyst.rules.Rule | ||
| import org.apache.spark.sql.catalyst.trees.TreePattern.PLAN_EXPRESSION | ||
| import org.apache.spark.sql.execution.{BaseSubqueryExec, ExecSubqueryExpression, ReusedSubqueryExec, SparkPlan} | ||
|
|
||
| /** | ||
| * Re-applies subquery deduplication after Comet node conversions. | ||
| * | ||
| * Spark's ReuseAdaptiveSubquery runs as a queryStageOptimizerRule before postStageCreationRules, | ||
| * which is where CometScanRule/CometExecRule replace Spark operators with Comet equivalents. The | ||
| * Comet rules copy expressions from the original Spark nodes, which can disrupt subquery reuse | ||
| * that was already applied by Spark's rule. This rule runs after Comet conversions to restore | ||
| * proper deduplication. | ||
| * | ||
| * Uses the same algorithm as Spark's ReuseExchangeAndSubquery (subquery portion): top-down | ||
| * traversal via transformAllExpressionsWithPruning, caching by canonical form. | ||
| * | ||
| * For non-AQE, Spark's ReuseExchangeAndSubquery runs after ApplyColumnarRulesAndInsertTransitions | ||
| * in QueryExecution.preparations and handles reuse correctly without this rule. | ||
| * | ||
| * @see | ||
| * ReuseExchangeAndSubquery (Spark's non-AQE subquery reuse) | ||
| * @see | ||
| * ReuseAdaptiveSubquery (Spark's AQE subquery reuse) | ||
| */ | ||
| case object CometReuseSubquery extends Rule[SparkPlan] { | ||
|
|
||
| def apply(plan: SparkPlan): SparkPlan = { | ||
| if (!conf.subqueryReuseEnabled) { | ||
| return plan | ||
| } | ||
|
|
||
| val cache = mutable.Map.empty[SparkPlan, BaseSubqueryExec] | ||
|
|
||
| plan.transformAllExpressionsWithPruning(_.containsPattern(PLAN_EXPRESSION)) { | ||
| case sub: ExecSubqueryExpression if !sub.plan.isInstanceOf[ReusedSubqueryExec] => | ||
| val cached = cache.getOrElseUpdate(sub.plan.canonicalized, sub.plan) | ||
| if (cached.ne(sub.plan)) { | ||
| sub.withNewPlan(ReusedSubqueryExec(cached)) | ||
| } else { | ||
| sub | ||
| } | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
48 changes: 48 additions & 0 deletions
48
spark/src/main/spark-3.5/org/apache/comet/shims/ShimCometSparkSessionExtensions.scala
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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.comet.shims | ||
|
|
||
| import org.apache.spark.sql.SparkSessionExtensions | ||
| import org.apache.spark.sql.catalyst.rules.Rule | ||
| import org.apache.spark.sql.execution.{QueryExecution, SparkPlan} | ||
|
|
||
| trait ShimCometSparkSessionExtensions { | ||
|
|
||
| protected val EXTENDED_EXPLAIN_PROVIDERS_KEY = "spark.sql.extendedExplainProviders" | ||
|
|
||
| def supportsExtendedExplainInfo(qe: QueryExecution): Boolean = { | ||
| try { | ||
| qe.getClass.getDeclaredMethod( | ||
| "extendedExplainInfo", | ||
| classOf[String => Unit], | ||
| classOf[SparkPlan]) | ||
| } catch { | ||
| case _: NoSuchMethodException | _: SecurityException => return false | ||
| } | ||
| true | ||
| } | ||
|
|
||
| // Available since Spark 3.5 (SPARK-45785) | ||
| def injectQueryStageOptimizerRuleShim( | ||
| extensions: SparkSessionExtensions, | ||
| rule: Rule[SparkPlan]): Unit = { | ||
| extensions.injectQueryStageOptimizerRule(_ => rule) | ||
| } | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should start thinking about an audit process for areas where we are replicating Spark logic, so that when we upgrade to future versions of Spark we can check that we staying consistent. Maybe a starting point is adding annotations to sections of code where we replicate upstream logic? No action needed on this PR ... just thinking aloud