-
Notifications
You must be signed in to change notification settings - Fork 28.9k
[SPARK-4861][SQL] Refactory command in spark sql #3712
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
Closed
Closed
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -26,34 +26,20 @@ import org.apache.spark.sql.catalyst.plans.logical | |
| import org.apache.spark.sql.catalyst.plans.logical.LogicalPlan | ||
| import org.apache.spark.sql.{SQLConf, SQLContext} | ||
|
|
||
| // TODO: DELETE ME... | ||
| trait Command { | ||
| this: SparkPlan => | ||
|
|
||
| /** | ||
| * A concrete command should override this lazy field to wrap up any side effects caused by the | ||
| * command or any other computation that should be evaluated exactly once. The value of this field | ||
| * can be used as the contents of the corresponding RDD generated from the physical plan of this | ||
| * command. | ||
| * | ||
| * The `execute()` method of all the physical command classes should reference `sideEffectResult` | ||
| * so that the command can be executed eagerly right after the command query is created. | ||
| */ | ||
| protected lazy val sideEffectResult: Seq[Row] = Seq.empty[Row] | ||
|
|
||
| override def executeCollect(): Array[Row] = sideEffectResult.toArray | ||
|
|
||
| override def execute(): RDD[Row] = sqlContext.sparkContext.parallelize(sideEffectResult, 1) | ||
| } | ||
|
|
||
| // TODO: Replace command with runnable command. | ||
| /** | ||
| * A logical command that is executed for its side-effects. `RunnableCommand`s are | ||
| * wrapped in `ExecutedCommand` during execution. | ||
| */ | ||
| trait RunnableCommand extends logical.Command { | ||
|
Contributor
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. Lets add some Scala doc: /**
* A logical command that is executed for its side-effects. `RunnableCommand`s are
* wrapped in `ExecutedCommand` during execution.
*/
Contributor
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. And below on /**
* A physical operator that executes the run method of a `RunnableCommand` and
* saves the result to prevent multiple executions.
*/ |
||
| self: Product => | ||
|
|
||
| def output: Seq[Attribute] | ||
| def run(sqlContext: SQLContext): Seq[Row] | ||
| } | ||
|
|
||
| /** | ||
| * A physical operator that executes the run method of a `RunnableCommand` and | ||
| * saves the result to prevent multiple executions. | ||
| */ | ||
| case class ExecutedCommand(cmd: RunnableCommand) extends SparkPlan { | ||
| /** | ||
| * A concrete command should override this lazy field to wrap up any side effects caused by the | ||
|
|
@@ -79,43 +65,41 @@ case class ExecutedCommand(cmd: RunnableCommand) extends SparkPlan { | |
| * :: DeveloperApi :: | ||
| */ | ||
| @DeveloperApi | ||
| case class SetCommand(kv: Option[(String, Option[String])], output: Seq[Attribute])( | ||
| @transient context: SQLContext) | ||
| extends LeafNode with Command with Logging { | ||
| case class SetCommand( | ||
| kv: Option[(String, Option[String])], | ||
| override val output: Seq[Attribute]) extends RunnableCommand with Logging { | ||
|
|
||
| override protected lazy val sideEffectResult: Seq[Row] = kv match { | ||
| override def run(sqlContext: SQLContext) = kv match { | ||
| // Configures the deprecated "mapred.reduce.tasks" property. | ||
| case Some((SQLConf.Deprecated.MAPRED_REDUCE_TASKS, Some(value))) => | ||
| logWarning( | ||
| s"Property ${SQLConf.Deprecated.MAPRED_REDUCE_TASKS} is deprecated, " + | ||
| s"automatically converted to ${SQLConf.SHUFFLE_PARTITIONS} instead.") | ||
| context.setConf(SQLConf.SHUFFLE_PARTITIONS, value) | ||
| sqlContext.setConf(SQLConf.SHUFFLE_PARTITIONS, value) | ||
| Seq(Row(s"${SQLConf.SHUFFLE_PARTITIONS}=$value")) | ||
|
|
||
| // Configures a single property. | ||
| case Some((key, Some(value))) => | ||
| context.setConf(key, value) | ||
| sqlContext.setConf(key, value) | ||
| Seq(Row(s"$key=$value")) | ||
|
|
||
| // Queries all key-value pairs that are set in the SQLConf of the context. Notice that different | ||
| // from Hive, here "SET -v" is an alias of "SET". (In Hive, "SET" returns all changed properties | ||
| // while "SET -v" returns all properties.) | ||
| // Queries all key-value pairs that are set in the SQLConf of the sqlContext. | ||
| // Notice that different from Hive, here "SET -v" is an alias of "SET". | ||
| // (In Hive, "SET" returns all changed properties while "SET -v" returns all properties.) | ||
| case Some(("-v", None)) | None => | ||
| context.getAllConfs.map { case (k, v) => Row(s"$k=$v") }.toSeq | ||
| sqlContext.getAllConfs.map { case (k, v) => Row(s"$k=$v") }.toSeq | ||
|
|
||
| // Queries the deprecated "mapred.reduce.tasks" property. | ||
| case Some((SQLConf.Deprecated.MAPRED_REDUCE_TASKS, None)) => | ||
| logWarning( | ||
| s"Property ${SQLConf.Deprecated.MAPRED_REDUCE_TASKS} is deprecated, " + | ||
| s"showing ${SQLConf.SHUFFLE_PARTITIONS} instead.") | ||
| Seq(Row(s"${SQLConf.SHUFFLE_PARTITIONS}=${context.numShufflePartitions}")) | ||
| Seq(Row(s"${SQLConf.SHUFFLE_PARTITIONS}=${sqlContext.numShufflePartitions}")) | ||
|
|
||
| // Queries a single property. | ||
| case Some((key, None)) => | ||
| Seq(Row(s"$key=${context.getConf(key, "<undefined>")}")) | ||
| Seq(Row(s"$key=${sqlContext.getConf(key, "<undefined>")}")) | ||
| } | ||
|
|
||
| override def otherCopyArgs = context :: Nil | ||
| } | ||
|
|
||
| /** | ||
|
|
@@ -128,22 +112,19 @@ case class SetCommand(kv: Option[(String, Option[String])], output: Seq[Attribut | |
| */ | ||
| @DeveloperApi | ||
| case class ExplainCommand( | ||
| logicalPlan: LogicalPlan, output: Seq[Attribute], extended: Boolean)( | ||
| @transient context: SQLContext) | ||
| extends LeafNode with Command { | ||
| logicalPlan: LogicalPlan, | ||
| override val output: Seq[Attribute], extended: Boolean) extends RunnableCommand { | ||
|
|
||
| // Run through the optimizer to generate the physical plan. | ||
| override protected lazy val sideEffectResult: Seq[Row] = try { | ||
| override def run(sqlContext: SQLContext) = try { | ||
| // TODO in Hive, the "extended" ExplainCommand prints the AST as well, and detailed properties. | ||
| val queryExecution = context.executePlan(logicalPlan) | ||
| val queryExecution = sqlContext.executePlan(logicalPlan) | ||
| val outputString = if (extended) queryExecution.toString else queryExecution.simpleString | ||
|
|
||
| outputString.split("\n").map(Row(_)) | ||
| } catch { case cause: TreeNodeException[_] => | ||
| ("Error occurred during query planning: \n" + cause.getMessage).split("\n").map(Row(_)) | ||
| } | ||
|
|
||
| override def otherCopyArgs = context :: Nil | ||
| } | ||
|
|
||
| /** | ||
|
|
@@ -153,10 +134,9 @@ case class ExplainCommand( | |
| case class CacheTableCommand( | ||
| tableName: String, | ||
| plan: Option[LogicalPlan], | ||
| isLazy: Boolean) | ||
| extends LeafNode with Command { | ||
| isLazy: Boolean) extends RunnableCommand { | ||
|
|
||
| override protected lazy val sideEffectResult = { | ||
| override def run(sqlContext: SQLContext) = { | ||
| import sqlContext._ | ||
|
|
||
| plan.foreach(_.registerTempTable(tableName)) | ||
|
|
@@ -178,8 +158,9 @@ case class CacheTableCommand( | |
| * :: DeveloperApi :: | ||
| */ | ||
| @DeveloperApi | ||
| case class UncacheTableCommand(tableName: String) extends LeafNode with Command { | ||
| override protected lazy val sideEffectResult: Seq[Row] = { | ||
| case class UncacheTableCommand(tableName: String) extends RunnableCommand { | ||
|
|
||
| override def run(sqlContext: SQLContext) = { | ||
| sqlContext.table(tableName).unpersist() | ||
| Seq.empty[Row] | ||
| } | ||
|
|
@@ -191,11 +172,11 @@ case class UncacheTableCommand(tableName: String) extends LeafNode with Command | |
| * :: DeveloperApi :: | ||
| */ | ||
| @DeveloperApi | ||
| case class DescribeCommand(child: SparkPlan, output: Seq[Attribute])( | ||
| @transient context: SQLContext) | ||
| extends LeafNode with Command { | ||
| case class DescribeCommand( | ||
| child: SparkPlan, | ||
| override val output: Seq[Attribute]) extends RunnableCommand { | ||
|
|
||
| override protected lazy val sideEffectResult: Seq[Row] = { | ||
| override def run(sqlContext: SQLContext) = { | ||
| Row("# Registered as a temporary table", null, null) +: | ||
| child.output.map(field => Row(field.name, field.dataType.toString, null)) | ||
| } | ||
|
|
||
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
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
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.
Can't we get rid of strategy entirely and just have this single line added to
BasicOperators. That was kind of my goal with this refactoring.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.
I will try, maybe we can add a rule to translate logical.XXXCommand to RunnableCommand in analyzer.
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.
I don think it needs to be in the
Analyzer. I think just this single line in theBasicOperatorsStrategyshould be sufficient.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.
But for set command, catalyst first parse it as logical.SetCommand, when to make it as execution.SetCommand (RunnableCommand)?
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 can not avoid this step to translate logical.XXXCommand to RunnableCommand, so keep CommandStrategy here.
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.
Okay, I see the problem now. These commands still live inside of catalyst and thus cannot be
RunnableCommands. I would argue that that should be changed. My whole goal here was to eliminate boilerplate from the planner and this still seems to have a bunch of it. That said, this could be done in a follow up PR.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.
yes, i think we can parse command in sql/core instead of parsing in catalyst in the follow up PR