Skip to content

Commit 2a91a87

Browse files
committed
add more test case and clean the code
1 parent 12d800d commit 2a91a87

File tree

4 files changed

+40
-16
lines changed

4 files changed

+40
-16
lines changed

sql/hive/src/main/scala/org/apache/spark/sql/hive/TableReader.scala

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -335,23 +335,17 @@ private[hive] object HadoopTableReader extends HiveInspectors {
335335
}
336336
}
337337

338+
/**
339+
* when the soi and deserializer.getObjectInspector is equal,
340+
* we will get `IdentityConverter`,which mean it won't convert the
341+
* value when schema match
342+
*/
338343
val partTblObjectInspectorConverter = ObjectInspectorConverters.getConverter(
339344
deserializer.getObjectInspector, soi)
340345

341346
// Map each tuple to a row object
342347
iterator.map { value =>
343-
val raw = convertdeserializer match {
344-
case Some(convert) =>
345-
if (deserializer.getObjectInspector.equals(convert.getObjectInspector)) {
346-
deserializer.deserialize(value)
347-
}
348-
// If partition schema does not match table schema, update the row to match
349-
else {
350-
partTblObjectInspectorConverter.convert(deserializer.deserialize(value))
351-
}
352-
case None =>
353-
deserializer.deserialize(value)
354-
}
348+
val raw = partTblObjectInspectorConverter.convert(deserializer.deserialize(value))
355349
var i = 0
356350
while (i < fieldRefs.length) {
357351
val fieldValue = soi.getStructFieldData(raw, fieldRefs(i))

sql/hive/src/test/scala/org/apache/spark/sql/hive/InsertIntoHiveTableSuite.scala

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ import org.apache.spark.sql.hive.test.TestHive._
2929

3030
case class TestData(key: Int, value: String)
3131

32+
case class ThreeCloumntable(key: Int, value: String, key1: String)
33+
3234
class InsertIntoHiveTableSuite extends QueryTest {
3335
val testData = TestHive.sparkContext.parallelize(
3436
(1 to 100).map(i => TestData(i, i.toString)))
@@ -172,19 +174,43 @@ class InsertIntoHiveTableSuite extends QueryTest {
172174

173175
sql("DROP TABLE hiveTableWithStructValue")
174176
}
175-
177+
176178
test("SPARK-5498:partition schema does not match table schema"){
177179
val testData = TestHive.sparkContext.parallelize(
178180
(1 to 10).map(i => TestData(i, i.toString)))
179181
testData.registerTempTable("testData")
182+
183+
val testDatawithNull = TestHive.sparkContext.parallelize(
184+
(1 to 10).map(i => ThreeCloumntable(i, i.toString,null)))
185+
180186
val tmpDir = Files.createTempDir()
181187
sql(s"CREATE TABLE table_with_partition(key int,value string) PARTITIONED by (ds string) location '${tmpDir.toURI.toString}' ")
182188
sql("INSERT OVERWRITE TABLE table_with_partition partition (ds='1') SELECT key,value FROM testData")
189+
190+
//test schema is the same
183191
sql("ALTER TABLE table_with_partition CHANGE COLUMN key key BIGINT")
184192
checkAnswer(sql("select key,value from table_with_partition where ds='1' "),
185193
testData.toSchemaRDD.collect.toSeq
186194
)
187-
sql("DROP TABLE table_with_partition")
188195

196+
// test difference type of field
197+
sql("ALTER TABLE table_with_partition CHANGE COLUMN key key BIGINT")
198+
checkAnswer(sql("select key,value from table_with_partition where ds='1' "),
199+
testData.toSchemaRDD.collect.toSeq
200+
)
201+
202+
// add column to table
203+
sql("ALTER TABLE table_with_partition ADD COLUMNS(key1 string)")
204+
checkAnswer(sql("select key,value,key1 from table_with_partition where ds='1' "),
205+
testDatawithNull.toSchemaRDD.collect.toSeq
206+
)
207+
208+
// change column name to table
209+
sql("ALTER TABLE table_with_partition CHANGE COLUMN key keynew BIGINT")
210+
checkAnswer(sql("select keynew,value from table_with_partition where ds='1' "),
211+
testData.toSchemaRDD.collect.toSeq
212+
)
213+
214+
sql("DROP TABLE table_with_partition")
189215
}
190216
}

sql/hive/v0.12.0/src/main/scala/org/apache/spark/sql/hive/Shim12.scala

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -243,8 +243,11 @@ private[hive] object HiveShim {
243243
}
244244

245245
// make getConvertedOI compatible between 0.12.0 and 0.13.1
246-
def getConvertedOI(inputOI: ObjectInspector, outputOI: ObjectInspector): ObjectInspector = {
247-
ObjectInspectorConverters.getConvertedOI(inputOI, outputOI, new java.lang.Boolean(true))
246+
def getConvertedOI(inputOI: ObjectInspector,
247+
outputOI: ObjectInspector,
248+
equalsCheck: java.lang.Boolean =
249+
new java.lang.Boolean(true)): ObjectInspector = {
250+
ObjectInspectorConverters.getConvertedOI(inputOI, outputOI, equalsCheck)
248251
}
249252

250253
def prepareWritable(w: Writable): Writable = {

sql/hive/v0.13.1/src/main/scala/org/apache/spark/sql/hive/Shim13.scala

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
package org.apache.spark.sql.hive
1919

20+
import java.util
2021
import java.util.{ArrayList => JArrayList}
2122
import java.util.Properties
2223
import java.rmi.server.UID

0 commit comments

Comments
 (0)