Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ package org.apache.spark.sql.execution.command
import java.time.{Duration, Period}

import org.apache.spark.sql.{AnalysisException, QueryTest, Row}
import org.apache.spark.sql.catalyst.analysis.PartitionsAlreadyExistException
import org.apache.spark.sql.internal.SQLConf

/**
Expand Down Expand Up @@ -167,23 +166,6 @@ trait AlterTableAddPartitionSuiteBase extends QueryTest with DDLCommandTestUtils
}
}

test("partition already exists") {
withNamespaceAndTable("ns", "tbl") { t =>
sql(s"CREATE TABLE $t (id bigint, data string) $defaultUsing PARTITIONED BY (id)")
sql(s"ALTER TABLE $t ADD PARTITION (id=2) LOCATION 'loc1'")

val errMsg = intercept[PartitionsAlreadyExistException] {
sql(s"ALTER TABLE $t ADD PARTITION (id=1) LOCATION 'loc'" +
" PARTITION (id=2) LOCATION 'loc1'")
}.getMessage
assert(errMsg.contains("The following partitions already exists"))

sql(s"ALTER TABLE $t ADD IF NOT EXISTS PARTITION (id=1) LOCATION 'loc'" +
" PARTITION (id=2) LOCATION 'loc1'")
checkPartitions(t, Map("id" -> "1"), Map("id" -> "2"))
}
}

test("SPARK-33474: Support typed literals as partition spec values") {
withNamespaceAndTable("ns", "tbl") { t =>
sql(s"CREATE TABLE $t(name STRING, part DATE) USING PARQUET PARTITIONED BY (part)")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
package org.apache.spark.sql.execution.command.v1

import org.apache.spark.sql.{AnalysisException, Row}
import org.apache.spark.sql.catalyst.analysis.PartitionsAlreadyExistException
import org.apache.spark.sql.execution.command
import org.apache.spark.sql.internal.SQLConf

Expand Down Expand Up @@ -135,6 +136,26 @@ trait AlterTableAddPartitionSuiteBase extends command.AlterTableAddPartitionSuit
}
}
}

// TODO: Move this test to the common trait as soon as it is migrated on checkError()
test("partition already exists") {
withNamespaceAndTable("ns", "tbl") { t =>
sql(s"CREATE TABLE $t (id bigint, data string) $defaultUsing PARTITIONED BY (id)")
sql(s"ALTER TABLE $t ADD PARTITION (id=2) LOCATION 'loc1'")

val errMsg = intercept[PartitionsAlreadyExistException] {
sql(s"ALTER TABLE $t ADD PARTITION (id=1) LOCATION 'loc'" +
" PARTITION (id=2) LOCATION 'loc1'")
}.getMessage
assert(errMsg ===
"""The following partitions already exists in table 'tbl' database 'ns':
|Map(id -> 2)""".stripMargin)

sql(s"ALTER TABLE $t ADD IF NOT EXISTS PARTITION (id=1) LOCATION 'loc'" +
" PARTITION (id=2) LOCATION 'loc1'")
checkPartitions(t, Map("id" -> "1"), Map("id" -> "2"))
}
}
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
package org.apache.spark.sql.execution.command.v2

import org.apache.spark.sql.{AnalysisException, Row}
import org.apache.spark.sql.catalyst.analysis.PartitionsAlreadyExistException
import org.apache.spark.sql.execution.command

/**
Expand Down Expand Up @@ -99,4 +100,22 @@ class AlterTableAddPartitionSuite
}
}
}

// TODO: Move this test to the common trait as soon as it is migrated on checkError()
test("partition already exists") {
withNamespaceAndTable("ns", "tbl") { t =>
sql(s"CREATE TABLE $t (id bigint, data string) $defaultUsing PARTITIONED BY (id)")
sql(s"ALTER TABLE $t ADD PARTITION (id=2) LOCATION 'loc1'")

val errMsg = intercept[PartitionsAlreadyExistException] {
sql(s"ALTER TABLE $t ADD PARTITION (id=1) LOCATION 'loc'" +
" PARTITION (id=2) LOCATION 'loc1'")
}.getMessage
assert(errMsg === s"The following partitions already exists in table $t:2 -> id")
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here is another mistake. Must be id -> 2, I think. I will fix this separately as soon as I have this test in.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here is the fix: #38152


sql(s"ALTER TABLE $t ADD IF NOT EXISTS PARTITION (id=1) LOCATION 'loc'" +
" PARTITION (id=2) LOCATION 'loc1'")
checkPartitions(t, Map("id" -> "1"), Map("id" -> "2"))
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -635,7 +635,11 @@ private[hive] class HiveClientImpl(
ignoreIfExists: Boolean): Unit = withHiveState {
def replaceExistException(e: Throwable): Unit = e match {
case _: HiveException if e.getCause.isInstanceOf[AlreadyExistsException] =>
throw new PartitionsAlreadyExistException(db, table, parts.map(_.spec))
val t = shim.getTable(client, db, table)
val exists = parts.filter { part =>
shim.getPartition(client, t, part.spec.asJava, forceCreate = false) != null
}
throw new PartitionsAlreadyExistException(db, table, exists.map(_.spec))
case _ => throw e
}
try {
Expand Down