-
Notifications
You must be signed in to change notification settings - Fork 29k
[SPARK-18346][SQL] TRUNCATE TABLE should fail if no partition is matched for the given non-partial partition spec #15805
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 1 commit
Commits
Show all changes
2 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -30,13 +30,13 @@ import org.apache.hadoop.fs.Path | |
|
|
||
| import org.apache.spark.sql.{AnalysisException, Row, SparkSession} | ||
| import org.apache.spark.sql.catalyst.TableIdentifier | ||
| import org.apache.spark.sql.catalyst.analysis.NoSuchPartitionException | ||
| import org.apache.spark.sql.catalyst.catalog._ | ||
| import org.apache.spark.sql.catalyst.catalog.CatalogTableType._ | ||
| import org.apache.spark.sql.catalyst.catalog.CatalogTypes.TablePartitionSpec | ||
| import org.apache.spark.sql.catalyst.expressions.{Attribute, AttributeReference} | ||
| import org.apache.spark.sql.catalyst.util.quoteIdentifier | ||
| import org.apache.spark.sql.execution.datasources.PartitioningUtils | ||
| import org.apache.spark.sql.internal.StaticSQLConf.CATALOG_IMPLEMENTATION | ||
| import org.apache.spark.sql.types._ | ||
| import org.apache.spark.util.Utils | ||
|
|
||
|
|
@@ -324,38 +324,45 @@ case class TruncateTableCommand( | |
| override def run(spark: SparkSession): Seq[Row] = { | ||
| val catalog = spark.sessionState.catalog | ||
| val table = catalog.getTableMetadata(tableName) | ||
| val tableIdentwithDB = table.identifier.quotedString | ||
| val tableIdentWithDB = table.identifier.quotedString | ||
|
|
||
| if (table.tableType == CatalogTableType.EXTERNAL) { | ||
| throw new AnalysisException( | ||
| s"Operation not allowed: TRUNCATE TABLE on external tables: $tableIdentwithDB") | ||
| s"Operation not allowed: TRUNCATE TABLE on external tables: $tableIdentWithDB") | ||
| } | ||
| if (table.tableType == CatalogTableType.VIEW) { | ||
| throw new AnalysisException( | ||
| s"Operation not allowed: TRUNCATE TABLE on views: $tableIdentwithDB") | ||
| s"Operation not allowed: TRUNCATE TABLE on views: $tableIdentWithDB") | ||
| } | ||
| if (table.partitionColumnNames.isEmpty && partitionSpec.isDefined) { | ||
| throw new AnalysisException( | ||
| s"Operation not allowed: TRUNCATE TABLE ... PARTITION is not supported " + | ||
| s"for tables that are not partitioned: $tableIdentwithDB") | ||
| s"for tables that are not partitioned: $tableIdentWithDB") | ||
| } | ||
| if (partitionSpec.isDefined) { | ||
| DDLUtils.verifyPartitionProviderIsHive(spark, table, "TRUNCATE TABLE ... PARTITION") | ||
| } | ||
|
|
||
| val partCols = table.partitionColumnNames | ||
| val locations = | ||
| if (table.partitionColumnNames.isEmpty) { | ||
| if (partCols.isEmpty) { | ||
| Seq(table.storage.locationUri) | ||
| } else { | ||
| // Here we diverge from Hive when the given partition spec contains all partition columns | ||
| // but no partition is matched: Hive will throw an exception and we just do nothing. | ||
| val normalizedSpec = partitionSpec.map { spec => | ||
| PartitioningUtils.normalizePartitionSpec( | ||
| spec, | ||
| table.partitionColumnNames, | ||
| partCols, | ||
| table.identifier.quotedString, | ||
| spark.sessionState.conf.resolver) | ||
| } | ||
| catalog.listPartitions(table.identifier, normalizedSpec).map(_.storage.locationUri) | ||
| val parts = | ||
| catalog.listPartitions(table.identifier, normalizedSpec).map(_.storage.locationUri) | ||
|
|
||
| for (spec <- partitionSpec if parts.isEmpty && spec.size == partCols.length) { | ||
|
||
| throw new NoSuchPartitionException(table.database, table.identifier.table, spec) | ||
| } | ||
|
|
||
| parts | ||
| } | ||
| val hadoopConf = spark.sessionState.newHadoopConf() | ||
| locations.foreach { location => | ||
|
|
@@ -368,7 +375,7 @@ case class TruncateTableCommand( | |
| } catch { | ||
| case NonFatal(e) => | ||
| throw new AnalysisException( | ||
| s"Failed to truncate table $tableIdentwithDB when removing data of the path: $path " + | ||
| s"Failed to truncate table $tableIdentWithDB when removing data of the path: $path " + | ||
| s"because of ${e.toString}") | ||
| } | ||
| } | ||
|
|
@@ -381,7 +388,7 @@ case class TruncateTableCommand( | |
| spark.sharedState.cacheManager.uncacheQuery(spark.table(table.identifier)) | ||
| } catch { | ||
| case NonFatal(e) => | ||
| log.warn(s"Exception when attempting to uncache table $tableIdentwithDB", e) | ||
| log.warn(s"Exception when attempting to uncache table $tableIdentWithDB", e) | ||
| } | ||
| Seq.empty[Row] | ||
| } | ||
|
|
||
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
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.
Nit: How about
partLocations?