Skip to content

[SPARK-40470][SQL] Handle GetArrayStructFields and GetMapValue in "arrays_zip" function #37911

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

Closed
wants to merge 1 commit into from
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 @@ -270,7 +270,9 @@ case class ArraysZip(children: Seq[Expression], names: Seq[Expression])
case (u: UnresolvedAttribute, _) => Literal(u.nameParts.last)
case (e: NamedExpression, _) if e.resolved => Literal(e.name)
case (e: NamedExpression, _) => NamePlaceholder
case (e: GetStructField, _) => Literal(e.extractFieldName)
case (g: GetStructField, _) => Literal(g.extractFieldName)
case (g: GetArrayStructFields, _) => Literal(g.field.name)
case (g: GetMapValue, _) => Literal(g.key)
case (_, idx) => Literal(idx.toString)
})
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -782,6 +782,51 @@ class DataFrameFunctionsSuite extends QueryTest with SharedSparkSession {
assert(fieldNames.toSeq === Seq("arr_1", "arr_2", "arr_3"))
}

test("SPARK-40470: array_zip should return field names in GetArrayStructFields") {
val df = spark.read.json(Seq(
"""
{
"arr": [
{
"obj": {
"nested": {
"field1": [1],
"field2": [2]
}
}
}
]
}
""").toDS())

val res = df
.selectExpr("arrays_zip(arr.obj.nested.field1, arr.obj.nested.field2) as arr")
.select(col("arr.field1"), col("arr.field2"))

val fieldNames = res.schema.fieldNames
assert(fieldNames.toSeq === Seq("field1", "field2"))

checkAnswer(res, Row(Seq(Seq(1)), Seq(Seq(2))) :: Nil)
}

test("SPARK-40470: arrays_zip should return field names in GetMapValue") {
val df = spark.sql("""
select
map(
'arr_1', array(1, 2),
'arr_2', array(3, 4)
) as map_obj
""")

val res = df.selectExpr("arrays_zip(map_obj.arr_1, map_obj.arr_2) as arr")

val fieldNames = res.schema.head.dataType.asInstanceOf[ArrayType]
.elementType.asInstanceOf[StructType].fieldNames
assert(fieldNames.toSeq === Seq("arr_1", "arr_2"))

checkAnswer(res, Row(Seq(Row(1, 3), Row(2, 4))))
}

def testSizeOfMap(sizeOfNull: Any): Unit = {
val df = Seq(
(Map[Int, Int](1 -> 1, 2 -> 2), "x"),
Expand Down