-
Notifications
You must be signed in to change notification settings - Fork 28.5k
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
[SPARK-21726][SQL] Check for structural integrity of the plan in Optimzer in test mode. #18956
Changes from 1 commit
9170ceb
c99011d
0af1efb
e1e4aa1
959e315
ecdfb7d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -63,6 +63,13 @@ abstract class RuleExecutor[TreeType <: TreeNode[_]] extends Logging { | |
/** Defines a sequence of rule batches, to be overridden by the implementation. */ | ||
protected def batches: Seq[Batch] | ||
|
||
/** | ||
* Defines a check function which checks for structural integrity of the plan in test mode after | ||
* the execution of each rule. For example, we can check whether a plan is still resolved after | ||
* each rule in `Optimizer`, so we can catch rules that return invalid plans. The check function | ||
* returns `false` if the given plan doesn't pass the structural integrity check. | ||
*/ | ||
protected def planChecker: Option[TreeType => Boolean] = None | ||
|
||
/** | ||
* Executes the batches of rules defined by the subclass. The batches are executed serially | ||
|
@@ -94,6 +101,13 @@ abstract class RuleExecutor[TreeType <: TreeNode[_]] extends Logging { | |
""".stripMargin) | ||
} | ||
|
||
// In test mode, run the structural integrity checker against the plan after each rule. | ||
if (Utils.isTesting && !planChecker.map(_.apply(result)).getOrElse(true)) { | ||
val message = s"After applying rule ${rule.ruleName} in batch ${batch.name}, " + | ||
"the structural integrity of the plan is broken." | ||
throw new TreeNodeException(result, message, null) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. move the exception throwing logics into the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nvm. The message also has rule and batch names. |
||
} | ||
|
||
result | ||
} | ||
iteration += 1 | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
/* | ||
* 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.analysis.{EmptyFunctionRegistry, UnresolvedAttribute} | ||
import org.apache.spark.sql.catalyst.catalog.{InMemoryCatalog, SessionCatalog} | ||
import org.apache.spark.sql.catalyst.dsl.plans._ | ||
import org.apache.spark.sql.catalyst.errors.TreeNodeException | ||
import org.apache.spark.sql.catalyst.expressions.{Alias, Literal} | ||
import org.apache.spark.sql.catalyst.plans.PlanTest | ||
import org.apache.spark.sql.catalyst.plans.logical.{LogicalPlan, OneRowRelation, Project} | ||
import org.apache.spark.sql.catalyst.rules._ | ||
import org.apache.spark.sql.internal.SQLConf | ||
|
||
|
||
class OptimizerSICheckerkSuite extends PlanTest { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. -> OptimizerStructuralIntegrityCheckerkSuite There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ok. |
||
|
||
object OptimizeRuleBreakSI extends Rule[LogicalPlan] { | ||
def apply(plan: LogicalPlan): LogicalPlan = plan transform { | ||
case Project(projectList, child) => | ||
val newAttr = UnresolvedAttribute("unresolvedAttr") | ||
Project(projectList ++ Seq(newAttr), child) | ||
} | ||
} | ||
|
||
object Optimize extends Optimizer( | ||
new SessionCatalog( | ||
new InMemoryCatalog, | ||
EmptyFunctionRegistry, | ||
new SQLConf())) { | ||
val newBatch = Batch("OptimizeRuleBreakSI", Once, OptimizeRuleBreakSI) | ||
override def batches: Seq[Batch] = Seq(newBatch) ++ super.batches | ||
} | ||
|
||
test("check for invalid plan after execution of rule") { | ||
val analyzed = Project(Alias(Literal(10), "attr")() :: Nil, OneRowRelation()).analyze | ||
assert(analyzed.resolved) | ||
val message = intercept[TreeNodeException[LogicalPlan]] { | ||
Optimize.execute(analyzed) | ||
}.getMessage | ||
val ruleName = OptimizeRuleBreakSI.ruleName | ||
assert(message.contains(s"After applying rule $ruleName in batch OptimizeRuleBreakSI")) | ||
assert(message.contains("the structural integrity of the plan is broken")) | ||
} | ||
} |
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.
can we move the checking of whether this is a test in here, then this method simply returns boolean, and by default it returns true.
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.
Thanks. I will update it.