Skip to content
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

[SPARK-16037][SQL] Follow-up: add DataFrameWriter.insertInto() test cases for by position resolution #13810

Closed
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -469,4 +469,52 @@ class InsertIntoHiveTableSuite extends QueryTest with TestHiveSingleton with Bef
)
}
}

testPartitionedTable("insertInto() should match columns by position and ignore column names") {
tableName =>
withSQLConf("hive.exec.dynamic.partition.mode" -> "nonstrict") {
// Columns `df.c` and `df.d` are resolved by position, and thus mapped to partition columns
// `b` and `c` of the target table.
val df = Seq((1, 2, 3, 4)).toDF("a", "b", "c", "d")
df.write.insertInto(tableName)

checkAnswer(
sql(s"SELECT a, b, c, d FROM $tableName"),
Row(1, 3, 4, 2)
)
}
}

testPartitionedTable("insertInto() should match unnamed columns by position") {
tableName =>
withSQLConf("hive.exec.dynamic.partition.mode" -> "nonstrict") {
// Columns `c + 1` and `d + 1` are resolved by position, and thus mapped to partition
// columns `b` and `c` of the target table.
val df = Seq((1, 2, 3, 4)).toDF("a", "b", "c", "d")
df.select('a + 1, 'b + 1, 'c + 1, 'd + 1).write.insertInto(tableName)

checkAnswer(
sql(s"SELECT a, b, c, d FROM $tableName"),
Row(2, 4, 5, 3)
)
}
}

testPartitionedTable("insertInto() should reject missing columns") {
tableName =>
sql("CREATE TABLE t (a INT, b INT)")

intercept[AnalysisException] {
spark.table("t").write.insertInto(tableName)
}
}

testPartitionedTable("insertInto() should reject extra columns") {
tableName =>
sql("CREATE TABLE t (a INT, b INT, c INT, d INT, e INT)")

intercept[AnalysisException] {
spark.table("t").write.insertInto(tableName)
}
}
}