-
Notifications
You must be signed in to change notification settings - Fork 29k
[SPARK-17409] [SQL] Do Not Optimize Query in CTAS More Than Once #15048
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
Changes from 3 commits
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 |
|---|---|---|
|
|
@@ -68,7 +68,7 @@ class ResolveDataSource(sparkSession: SparkSession) extends Rule[LogicalPlan] { | |
| /** | ||
| * Preprocess some DDL plans, e.g. [[CreateTable]], to do some normalization and checking. | ||
|
||
| */ | ||
| case class PreprocessDDL(conf: SQLConf) extends Rule[LogicalPlan] { | ||
| case class PreprocessDDL(sparkSession: SparkSession) extends Rule[LogicalPlan] { | ||
|
|
||
| def apply(plan: LogicalPlan): LogicalPlan = plan transform { | ||
| // When we CREATE TABLE without specifying the table schema, we should fail the query if | ||
|
|
@@ -95,9 +95,19 @@ case class PreprocessDDL(conf: SQLConf) extends Rule[LogicalPlan] { | |
| // * can't use all table columns as partition columns. | ||
| // * partition columns' type must be AtomicType. | ||
| // * sort columns' type must be orderable. | ||
| case c @ CreateTable(tableDesc, mode, query) if c.childrenResolved => | ||
| val schema = if (query.isDefined) query.get.schema else tableDesc.schema | ||
| val columnNames = if (conf.caseSensitiveAnalysis) { | ||
| case c @ CreateTable(tableDesc, mode, query) => | ||
| val analyzedQuery = query.map { q => | ||
| // Analyze the query in CTAS and then we can do the normalization and checking. | ||
| val qe = sparkSession.sessionState.executePlan(q) | ||
| qe.assertAnalyzed() | ||
| qe.analyzed | ||
| } | ||
| val schema = if (analyzedQuery.isDefined) { | ||
| analyzedQuery.get.schema | ||
| } else { | ||
| tableDesc.schema | ||
| } | ||
| val columnNames = if (sparkSession.sessionState.conf.caseSensitiveAnalysis) { | ||
| schema.map(_.name) | ||
| } else { | ||
| schema.map(_.name.toLowerCase) | ||
|
|
@@ -106,7 +116,7 @@ case class PreprocessDDL(conf: SQLConf) extends Rule[LogicalPlan] { | |
|
|
||
| val partitionColsChecked = checkPartitionColumns(schema, tableDesc) | ||
| val bucketColsChecked = checkBucketColumns(schema, partitionColsChecked) | ||
| c.copy(tableDesc = bucketColsChecked) | ||
| c.copy(tableDesc = bucketColsChecked, query = analyzedQuery) | ||
| } | ||
|
|
||
| private def checkPartitionColumns(schema: StructType, tableDesc: CatalogTable): CatalogTable = { | ||
|
|
@@ -176,6 +186,7 @@ case class PreprocessDDL(conf: SQLConf) extends Rule[LogicalPlan] { | |
| colName: String, | ||
| colType: String): String = { | ||
| val tableCols = schema.map(_.name) | ||
| val conf = sparkSession.sessionState.conf | ||
| tableCols.find(conf.resolver(_, colName)).getOrElse { | ||
| failAnalysis(s"$colType column $colName is not defined in table $tableIdent, " + | ||
| s"defined table columns are: ${tableCols.mkString(", ")}") | ||
|
|
||
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.
extend LeafNode?
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.
Yeah. : )