Skip to content

Commit

Permalink
[SPARK-30112][SQL] Allow insert overwrite same table if using dynamic…
Browse files Browse the repository at this point in the history
… partition overwrite

### What changes were proposed in this pull request?

This patch proposes to allow insert overwrite same table if using dynamic partition overwrite.

### Why are the changes needed?

Currently, Insert overwrite cannot overwrite to same table even it is dynamic partition overwrite. But for dynamic partition overwrite, we do not delete partition directories ahead. We write to staging directories and move data to final partition directories. We should be able to insert overwrite to same table under dynamic partition overwrite.

This enables users to read data from a table and insert overwrite to same table by using dynamic partition overwrite. Because this is not allowed for now, users need to write to other temporary location and move it back to the table.

### Does this PR introduce any user-facing change?

Yes. Users can insert overwrite same table if using dynamic partition overwrite.

### How was this patch tested?

Unit test.

Closes #26752 from viirya/dynamic-overwrite-same-table.

Lead-authored-by: Liang-Chi Hsieh <liangchi@uber.com>
Co-authored-by: Liang-Chi Hsieh <viirya@gmail.com>
Signed-off-by: Dongjoon Hyun <dhyun@apple.com>
  • Loading branch information
2 people authored and dongjoon-hyun committed Dec 6, 2019
1 parent c8ed71b commit c1a5f94
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -188,15 +188,13 @@ case class DataSourceAnalysis(conf: SQLConf) extends Rule[LogicalPlan] with Cast
}

val outputPath = t.location.rootPaths.head
if (overwrite) DDLUtils.verifyNotReadPath(actualQuery, outputPath)

val mode = if (overwrite) SaveMode.Overwrite else SaveMode.Append

val partitionSchema = actualQuery.resolve(
t.partitionSchema, t.sparkSession.sessionState.analyzer.resolver)
val staticPartitions = parts.filter(_._2.nonEmpty).map { case (k, v) => k -> v.get }

InsertIntoHadoopFsRelationCommand(
val insertCommand = InsertIntoHadoopFsRelationCommand(
outputPath,
staticPartitions,
i.ifPartitionNotExists,
Expand All @@ -209,6 +207,14 @@ case class DataSourceAnalysis(conf: SQLConf) extends Rule[LogicalPlan] with Cast
table,
Some(t.location),
actualQuery.output.map(_.name))

// For dynamic partition overwrite, we do not delete partition directories ahead.
// We write to staging directories and move to final partition directories after writing
// job is done. So it is ok to have outputPath try to overwrite inputpath.
if (overwrite && !insertCommand.dynamicPartitionOverwrite) {
DDLUtils.verifyNotReadPath(actualQuery, outputPath)
}
insertCommand
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import org.apache.spark.sql.catalyst.plans.logical.LogicalPlan
import org.apache.spark.sql.catalyst.util.CaseInsensitiveMap
import org.apache.spark.sql.execution.SparkPlan
import org.apache.spark.sql.execution.command._
import org.apache.spark.sql.internal.SQLConf
import org.apache.spark.sql.internal.SQLConf.PartitionOverwriteMode
import org.apache.spark.sql.util.SchemaUtils

Expand Down Expand Up @@ -60,6 +61,21 @@ case class InsertIntoHadoopFsRelationCommand(
extends DataWritingCommand {
import org.apache.spark.sql.catalyst.catalog.ExternalCatalogUtils.escapePathName

private lazy val parameters = CaseInsensitiveMap(options)

private[sql] lazy val dynamicPartitionOverwrite: Boolean = {
val partitionOverwriteMode = parameters.get("partitionOverwriteMode")
// scalastyle:off caselocale
.map(mode => PartitionOverwriteMode.withName(mode.toUpperCase))
// scalastyle:on caselocale
.getOrElse(SQLConf.get.partitionOverwriteMode)
val enableDynamicOverwrite = partitionOverwriteMode == PartitionOverwriteMode.DYNAMIC
// This config only makes sense when we are overwriting a partitioned dataset with dynamic
// partition columns.
enableDynamicOverwrite && mode == SaveMode.Overwrite &&
staticPartitions.size < partitionColumns.length
}

override def run(sparkSession: SparkSession, child: SparkPlan): Seq[Row] = {
// Most formats don't do well with duplicate columns, so lets not allow that
SchemaUtils.checkColumnNameDuplication(
Expand Down Expand Up @@ -90,19 +106,6 @@ case class InsertIntoHadoopFsRelationCommand(
fs, catalogTable.get, qualifiedOutputPath, matchingPartitions)
}

val parameters = CaseInsensitiveMap(options)

val partitionOverwriteMode = parameters.get("partitionOverwriteMode")
// scalastyle:off caselocale
.map(mode => PartitionOverwriteMode.withName(mode.toUpperCase))
// scalastyle:on caselocale
.getOrElse(sparkSession.sessionState.conf.partitionOverwriteMode)
val enableDynamicOverwrite = partitionOverwriteMode == PartitionOverwriteMode.DYNAMIC
// This config only makes sense when we are overwriting a partitioned dataset with dynamic
// partition columns.
val dynamicPartitionOverwrite = enableDynamicOverwrite && mode == SaveMode.Overwrite &&
staticPartitions.size < partitionColumns.length

val committer = FileCommitProtocol.instantiate(
sparkSession.sessionState.conf.fileCommitProtocolClass,
jobId = java.util.UUID.randomUUID().toString,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,55 @@ class InsertSuite extends DataSourceTest with SharedSparkSession {
"INSERT OVERWRITE to a table while querying it should not be allowed.")
}

test("SPARK-30112: it is allowed to write to a table while querying it for " +
"dynamic partition overwrite.") {
Seq(PartitionOverwriteMode.DYNAMIC.toString,
PartitionOverwriteMode.STATIC.toString).foreach { mode =>
withSQLConf(SQLConf.PARTITION_OVERWRITE_MODE.key -> mode) {
withTable("insertTable") {
sql(
"""
|CREATE TABLE insertTable(i int, part1 int, part2 int) USING PARQUET
|PARTITIONED BY (part1, part2)
""".stripMargin)

sql("INSERT INTO TABLE insertTable PARTITION(part1=1, part2=1) SELECT 1")
checkAnswer(spark.table("insertTable"), Row(1, 1, 1))
sql("INSERT OVERWRITE TABLE insertTable PARTITION(part1=1, part2=2) SELECT 2")
checkAnswer(spark.table("insertTable"), Row(1, 1, 1) :: Row(2, 1, 2) :: Nil)

if (mode == PartitionOverwriteMode.DYNAMIC.toString) {
sql(
"""
|INSERT OVERWRITE TABLE insertTable PARTITION(part1=1, part2)
|SELECT i + 1, part2 FROM insertTable
""".stripMargin)
checkAnswer(spark.table("insertTable"), Row(2, 1, 1) :: Row(3, 1, 2) :: Nil)

sql(
"""
|INSERT OVERWRITE TABLE insertTable PARTITION(part1=1, part2)
|SELECT i + 1, part2 + 1 FROM insertTable
""".stripMargin)
checkAnswer(spark.table("insertTable"),
Row(2, 1, 1) :: Row(3, 1, 2) :: Row(4, 1, 3) :: Nil)
} else {
val message = intercept[AnalysisException] {
sql(
"""
|INSERT OVERWRITE TABLE insertTable PARTITION(part1=1, part2)
|SELECT i + 1, part2 FROM insertTable
""".stripMargin)
}.getMessage
assert(
message.contains("Cannot overwrite a path that is also being read from."),
"INSERT OVERWRITE to a table while querying it should not be allowed.")
}
}
}
}
}

test("Caching") {
// write something to the jsonTable
sql(
Expand Down

0 comments on commit c1a5f94

Please sign in to comment.