Skip to content

Commit

Permalink
fix imports
Browse files Browse the repository at this point in the history
  • Loading branch information
j-baker committed Nov 15, 2018
1 parent b6a191a commit 417582d
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 9 deletions.
Expand Up @@ -478,27 +478,28 @@ object PreWriteCheck extends (LogicalPlan => Unit) {
}

object DDLPreprocessingUtils {
private def noReorderedStructs(
private def hasMismatchingStructs(
expected: DataType,
actual: DataType): Boolean = {
(expected, actual) match {
case (StructType(expectedTypes), StructType(actualTypes)) =>
expectedTypes.zip(actualTypes).forall {
case (left, right) =>
left.name.equals(right.name) && noReorderedStructs(left.dataType, right.dataType)
!left.name.equals(right.name) || hasMismatchingStructs(left.dataType, right.dataType)
}
case (ArrayType(expectedType, _), ArrayType(actualType, _)) =>
noReorderedStructs(expectedType, actualType)
hasMismatchingStructs(expectedType, actualType)
case (MapType(expectedKey, expectedValue, _), MapType(actualKey, actualValue, _)) =>
noReorderedStructs(expectedKey, actualKey) && noReorderedStructs(expectedValue, actualValue)
case (x, y) => true
hasMismatchingStructs(expectedKey, actualKey) ||
hasMismatchingStructs(expectedValue, actualValue)
case (x, y) => false
}
}

private def failIfHasReorderedStructs(expected: DataType, actual: DataType): Unit = {
if (!noReorderedStructs(expected, actual)) {
private def failIfMismatchingStructs(expected: DataType, actual: DataType): Unit = {
if (hasMismatchingStructs(expected, actual)) {
throw new AnalysisException(s"It is not safe to write out datatype $actual " +
s"into a table of type $expected, because struct columns would be in the wrong order.")
s"into a column of type $expected, because struct columns are not identical.")
}
}

Expand All @@ -516,7 +517,7 @@ object DDLPreprocessingUtils {
expected.metadata == actual.metadata) {
actual
} else {
failIfHasReorderedStructs(expected.dataType, actual.dataType)
failIfMismatchingStructs(expected.dataType, actual.dataType)
// Renaming is needed for handling the following cases like
// 1) Column names/types do not match, e.g., INSERT INTO TABLE tab1 SELECT 1, 2
// 2) Target tables have column metadata
Expand Down
Expand Up @@ -668,6 +668,19 @@ abstract class DDLSuite extends QueryTest with SQLTestUtils {
}
}

test("SPARK-25943: Writing data into a catalog table with a different struct schema") {
withTable("t") {
sql(s"CREATE TABLE t USING ${dataSource} AS SELECT STRUCT('d1' AS `first`) AS `a`")
val e = intercept[AnalysisException] {
sql(s"INSERT INTO t SELECT STRUCT('d3' AS `second`) AS `a`")
}
assert(e.message.contains("It is not safe to write out datatype " +
"StructType(StructField(second,StringType,false)) " +
"into a column of type StructType(StructField(first,StringType,true)), " +
"because struct columns are not identical."))
}
}

test("Refresh table after changing the data source table partitioning") {
import testImplicits._

Expand Down

0 comments on commit 417582d

Please sign in to comment.