Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
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 @@ -111,15 +111,16 @@ object PartitioningUtils {
caseSensitive: Boolean,
validatePartitionColumns: Boolean,
timeZone: TimeZone): PartitionSpec = {
val userSpecifiedDataTypes = if (userSpecifiedSchema.isDefined) {
val (userSpecifiedDataTypes, userSpecifiedNames) = if (userSpecifiedSchema.isDefined) {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

just a nit: we can maybe separate the userSpecifiedNames map creation in order to improve code readability. Moreover we need it only if !caseSensitive. So what about:

val userSpecifiedNames = userSpecifiedSchema.filterNot(_ => caseSensitive).map { schema => CaseInsensitiveMap(...) }.getOrElse(Map.empty[String, String])

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Thanks! I have revised the code.

val nameToDataType = userSpecifiedSchema.get.fields.map(f => f.name -> f.dataType).toMap
val nameToName = userSpecifiedSchema.get.fields.map(f => f.name -> f.name).toMap
if (!caseSensitive) {
CaseInsensitiveMap(nameToDataType)
(CaseInsensitiveMap(nameToDataType), CaseInsensitiveMap(nameToName))
} else {
nameToDataType
(nameToDataType, nameToName)
}
} else {
Map.empty[String, DataType]
(Map.empty[String, DataType], Map.empty[String, String])
}

val dateFormatter = DateFormatter()
Expand Down Expand Up @@ -170,7 +171,9 @@ object PartitioningUtils {
columnNames.zip(literals).map { case (name, Literal(_, dataType)) =>
// We always assume partition columns are nullable since we've no idea whether null values
// will be appended in the future.
StructField(name, userSpecifiedDataTypes.getOrElse(name, dataType), nullable = true)
val resultName = userSpecifiedNames.getOrElse(name, name)
val resultDataType = userSpecifiedDataTypes.getOrElse(name, dataType)
StructField(resultName, resultDataType, nullable = true)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,21 @@ class FileIndexSuite extends SharedSQLContext {
}
}

test("SPARK-26990: use user specified field names if possible") {
withTempDir { dir =>
val partitionDirectory = new File(dir, "a=foo")
partitionDirectory.mkdir()
val file = new File(partitionDirectory, "text.txt")
stringToFile(file, "text")
val path = new Path(dir.getCanonicalPath)
val schema = StructType(Seq(StructField("A", StringType, false)))
withSQLConf(SQLConf.CASE_SENSITIVE.key -> "false") {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Also test the behavior when the conf is on.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

When the conf is on, the schema will be a instead of A. Both 2.4 and the current code has the same result.
Not sure if we should throw exceptions on this.

val fileIndex = new InMemoryFileIndex(spark, Seq(path), Map.empty, Some(schema))
assert(fileIndex.partitionSchema.length == 1 && fileIndex.partitionSchema.head.name == "A")
}
}
}

test("SPARK-26230: if case sensitive, validate partitions with original column names") {
withTempDir { dir =>
val partitionDirectory = new File(dir, "a=1")
Expand Down