Skip to content

Commit 014c998

Browse files
ericlMichael Allman
authored andcommitted
Also support mixed case field resolution for converted ORC tables (#7)
* Fri Oct 14 14:04:01 PDT 2016 * stray println
1 parent 83a168c commit 014c998

File tree

2 files changed

+33
-1
lines changed

2 files changed

+33
-1
lines changed

sql/hive/src/main/scala/org/apache/spark/sql/hive/orc/OrcFileFormat.scala

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -313,7 +313,17 @@ private[orc] object OrcRelation extends HiveInspectors {
313313

314314
def setRequiredColumns(
315315
conf: Configuration, physicalSchema: StructType, requestedSchema: StructType): Unit = {
316-
val ids = requestedSchema.map(a => physicalSchema.fieldIndex(a.name): Integer)
316+
val caseInsensitiveFieldMap: Map[String, Int] = physicalSchema.fieldNames
317+
.zipWithIndex
318+
.map(f => (f._1.toLowerCase, f._2))
319+
.toMap
320+
val ids = requestedSchema.map { a =>
321+
val exactMatch: Option[Int] = physicalSchema.getFieldIndex(a.name)
322+
val res = exactMatch.getOrElse(
323+
caseInsensitiveFieldMap.getOrElse(a.name,
324+
throw new IllegalArgumentException(s"""Field "$a.name" does not exist.""")))
325+
res: Integer
326+
}
317327
val (sortedIDs, sortedNames) = ids.zip(requestedSchema.fieldNames).sorted.unzip
318328
HiveShim.appendReadColumns(conf, sortedIDs, sortedNames)
319329
}

sql/hive/src/test/scala/org/apache/spark/sql/hive/orc/OrcQuerySuite.scala

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -474,6 +474,28 @@ class OrcQuerySuite extends QueryTest with BeforeAndAfterAll with OrcTest {
474474
}
475475
}
476476

477+
test("converted ORC table supports resolving mixed case field") {
478+
withSQLConf(HiveUtils.CONVERT_METASTORE_ORC.key -> "true") {
479+
withTable("dummy_orc") {
480+
withTempPath { dir =>
481+
val df = spark.range(5).selectExpr("id", "id as valueField", "id as partitionValue")
482+
df.write
483+
.partitionBy("partitionValue")
484+
.mode("overwrite")
485+
.orc(dir.getAbsolutePath)
486+
487+
spark.sql(s"""
488+
|create external table dummy_orc (id long, valueField long)
489+
|partitioned by (partitionValue int)
490+
|stored as orc
491+
|location "${dir.getAbsolutePath}"""".stripMargin)
492+
spark.sql(s"msck repair table dummy_orc")
493+
checkAnswer(spark.sql("select * from dummy_orc"), df)
494+
}
495+
}
496+
}
497+
}
498+
477499
test("SPARK-14962 Produce correct results on array type with isnotnull") {
478500
withSQLConf(SQLConf.ORC_FILTER_PUSHDOWN_ENABLED.key -> "true") {
479501
val data = (0 until 10).map(i => Tuple1(Array(i)))

0 commit comments

Comments
 (0)