Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
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 @@ -40,9 +40,14 @@ private[execution] case class ProjectionOverSchema(schema: StructType) {
case a: GetArrayStructFields =>
getProjection(a.child).map(p => (p, p.dataType)).map {
case (projection, ArrayType(projSchema @ StructType(_), _)) =>
// For case-sensitivity aware field resolution, we should take `ordinal` which
// points to correct struct field.
val selectedField = a.child.dataType.asInstanceOf[ArrayType]
.elementType.asInstanceOf[StructType](a.ordinal)
val prunedField = projSchema(selectedField.name)
GetArrayStructFields(projection,
projSchema(a.field.name),
projSchema.fieldIndex(a.field.name),
prunedField.copy(name = a.field.name),
projSchema.fieldIndex(selectedField.name),
projSchema.size,
a.containsNull)
case (_, projSchema) =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,19 +81,25 @@ private[execution] object SelectedField {
case GetArrayItem(child, _) =>
selectField(child, fieldOpt)
// Handles case "expr0.field.subfield", where "expr0" and "expr0.field" are of array type.
case GetArrayStructFields(child: GetArrayStructFields,
field @ StructField(name, dataType, nullable, metadata), _, _, _) =>
val childField = fieldOpt.map(field => StructField(name,
wrapStructType(dataType, field),
nullable, metadata)).orElse(Some(field))
case GetArrayStructFields(child: GetArrayStructFields, _, ordinal, _, _) =>
// For case-sensitivity aware field resolution, we should take `ordinal` which
// points to correct struct field.
val selectedField = child.dataType.asInstanceOf[ArrayType]
.elementType.asInstanceOf[StructType](ordinal)
val childField = fieldOpt.map(field => StructField(selectedField.name,
wrapStructType(selectedField.dataType, field),
selectedField.nullable, selectedField.metadata)).orElse(Some(selectedField))
selectField(child, childField)
// Handles case "expr0.field", where "expr0" is of array type.
case GetArrayStructFields(child,
field @ StructField(name, dataType, nullable, metadata), _, _, _) =>
case GetArrayStructFields(child, _, ordinal, _, _) =>
// For case-sensitivity aware field resolution, we should take `ordinal` which
// points to correct struct field.
val selectedField = child.dataType.asInstanceOf[ArrayType]
.elementType.asInstanceOf[StructType](ordinal)
val childField =
fieldOpt.map(field => StructField(name,
wrapStructType(dataType, field),
nullable, metadata)).orElse(Some(field))
fieldOpt.map(field => StructField(selectedField.name,
wrapStructType(selectedField.dataType, field),
selectedField.nullable, selectedField.metadata)).orElse(Some(selectedField))
selectField(child, childField)
// Handles case "expr0.field[key]", where "expr0" is of struct type and "expr0.field" is of
// map type.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -416,4 +416,46 @@ class ParquetSchemaPruningSuite
assert(scanSchema === expectedScanSchema)
}
}

testSchemaPruning("SPARK-34963: extract case-insensitive struct field from array") {
withSQLConf(SQLConf.CASE_SENSITIVE.key -> "false") {
val query1 = spark.table("contacts")
.select("friends.First", "friends.MiDDle")
checkScan(query1, "struct<friends:array<struct<first:string,middle:string>>>")
checkAnswer(query1,
Row(Array.empty[String], Array.empty[String]) ::
Row(Array("Susan"), Array("Z.")) ::
Row(null, null) ::
Row(null, null) :: Nil)

val query2 = spark.table("contacts")
.where("friends.First is not null")
.select("friends.First", "friends.MiDDle")
checkScan(query2, "struct<friends:array<struct<first:string,middle:string>>>")
checkAnswer(query2,
Row(Array.empty[String], Array.empty[String]) ::
Row(Array("Susan"), Array("Z.")) :: Nil)
}
}

testSchemaPruning("SPARK-34963: extract case-insensitive struct field from struct") {
withSQLConf(SQLConf.CASE_SENSITIVE.key -> "false") {
val query1 = spark.table("contacts")
.select("Name.First", "NAME.MiDDle")
checkScan(query1, "struct<name:struct<first:string,middle:string>>")
checkAnswer(query1,
Row("Jane", "X.") ::
Row("Janet", null) ::
Row("Jim", null) ::
Row("John", "Y.") :: Nil)

val query2 = spark.table("contacts")
.where("Name.MIDDLE is not null")
.select("Name.First", "NAME.MiDDle")
checkScan(query2, "struct<name:struct<first:string,middle:string>>")
checkAnswer(query2,
Row("Jane", "X.") ::
Row("John", "Y.") :: Nil)
}
}
}