Skip to content

[SPARK-32002][SQL]Support ExtractValue from nested ArrayStruct #28860

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 5 commits 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 @@ -59,6 +59,23 @@ object ExtractValue {
GetArrayStructFields(child, fields(ordinal).copy(name = fieldName),
ordinal, fields.length, containsNull)

case (ExtractNestedArray(StructType(fields), containsNull, containsNullSeq),
NonNullLiteral(v, StringType)) =>
Copy link
Member

Choose a reason for hiding this comment

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

nit:

case (ExtractNestedArray(
    StructType(fields), containsNull, containsNullSeq), NonNullLiteral(v, StringType)) =>

Copy link
Member

Choose a reason for hiding this comment

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

Let's also update the documentation and table above.

child match {
case ExtractGetArrayStructField(_, num) if num == containsNullSeq.size =>
val fieldName = v.toString
val ordinal = findField(fields, fieldName, resolver)
val row = (0 until num).foldRight(child) { (_, e) =>
GetArrayItem(e, Literal(0))
}
val innerArray = GetArrayStructFields(row, fields(ordinal).copy(name = fieldName),
ordinal, fields.length, containsNull)
containsNullSeq.foldRight(innerArray: Expression) { (_, expr) =>
new CreateArray(Seq(expr))
}
case _ => GetArrayItem(child, extraction)
}

case (_: ArrayType, _) => GetArrayItem(child, extraction)

case (MapType(kt, _, _), _) => GetMapValue(child, extraction)
Expand Down Expand Up @@ -95,6 +112,50 @@ object ExtractValue {

trait ExtractValue extends Expression

object ExtractNestedArray {
Copy link
Member

Choose a reason for hiding this comment

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

Let's add documentation here.

Copy link
Member

@HyukjinKwon HyukjinKwon Jun 26, 2020

Choose a reason for hiding this comment

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

Let's also name it something like ExtractNestedArrayType


type ReturnType = Option[(DataType, Boolean, Seq[Boolean])]
Copy link
Member

Choose a reason for hiding this comment

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

Let's also add some comments for what this type means.


def unapply(dataType: DataType): ReturnType = {
extractArrayType(dataType)
}

def extractArrayType(dataType: DataType): ReturnType = {
Copy link
Member

Choose a reason for hiding this comment

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

Can we combine this and unapply?

dataType match {
case ArrayType(dt, containsNull) =>
extractArrayType(dt) match {
case Some((d, cn, seq)) => Some(d, cn, containsNull +: seq)
case None => Some(dt, containsNull, Seq.empty[Boolean])
}
case _ => None
}
}
}

/**
* Extract GetArrayStructField from Expression
*/
object ExtractGetArrayStructField {

type ReturnType = Option[(Expression, Int)]

def unapply(expr: Expression): ReturnType = {
extractArrayStruct(expr)
}

def extractArrayStruct(expr: Expression): ReturnType = {
expr match {
case gas @ GetArrayStructFields(child, _, _, _, _) =>
Copy link
Member

Choose a reason for hiding this comment

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

Let's avoid arguments matching. This is actually an anti pattern - https://github.com/databricks/scala-style-guide#pattern-matching

extractArrayStruct(child) match {
case Some((e, deep)) => Some(e, deep + 1)
Copy link
Member

Choose a reason for hiding this comment

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

deep -> depth?

case None => Some(child, 1)
}
case _ => None
}
}
}


/**
* Returns the value of fields in the Struct `child`.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -378,12 +378,6 @@ class SelectedFieldSuite extends AnalysisTest {
StructField("subfield1", IntegerType, nullable = false) :: Nil)) :: Nil)))
}

testSelect(arrayWithMultipleFields, "col7.field3.subfield1") {
StructField("col7", ArrayType(StructType(
StructField("field3", ArrayType(StructType(
StructField("subfield1", IntegerType, nullable = false) :: Nil))) :: Nil)))
}

// Array with a nested int array
// |-- col1: string (nullable = false)
// |-- col8: array (nullable = true)
Expand Down
21 changes: 21 additions & 0 deletions sql/core/src/test/scala/org/apache/spark/sql/SQLQuerySuite.scala
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ import java.net.{MalformedURLException, URL}
import java.sql.{Date, Timestamp}
import java.util.concurrent.atomic.AtomicBoolean

import scala.collection.mutable.ArrayBuffer

import org.apache.spark.{AccumulatorSuite, SparkException}
import org.apache.spark.scheduler.{SparkListener, SparkListenerJobStart}
import org.apache.spark.sql.catalyst.expressions.GenericRow
Expand Down Expand Up @@ -3521,6 +3523,25 @@ class SQLQuerySuite extends QueryTest with SharedSparkSession with AdaptiveSpark
|""".stripMargin), Row(1))
}
}

test("SPARK-32002: Support Extract value from nested ArrayStruct") {
withTempView("rows") {
val df = spark.read
.json(Seq(
"""{"a": [{"b": [{"c": [1,2]}]}]}""",
"""{"a": [{"b": [{"c": [1]}, {"c": [2]}]}]}""",
"""{"a":[{}]}""").toDS())
df.createOrReplaceTempView("nest")

checkAnswer(sql(
"""
|SELECT a.b.c FROM nest
Copy link
Member

@HyukjinKwon HyukjinKwon Jun 26, 2020

Choose a reason for hiding this comment

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

Can we add deeper cases? Also, you can simplify the test cases, for example, as below:

val df = spark.range(10).select(array(struct(array(struct("id")).alias("col1"))).alias("col0"))
df.selectExpr("col0.col1.id")

""".stripMargin),
Row(ArrayBuffer(ArrayBuffer(ArrayBuffer(1, 2)))) ::
Row(ArrayBuffer(ArrayBuffer(ArrayBuffer(1), ArrayBuffer(2)))) ::
Row(ArrayBuffer(null)) :: Nil)
}
}
}

case class Foo(bar: Option[String])