Skip to content

Commit e1a8898

Browse files
committed
remove support for arbitrary nested arrays
1 parent ee8a724 commit e1a8898

File tree

3 files changed

+7
-102
lines changed

3 files changed

+7
-102
lines changed

sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/complexTypes.scala

+6-48
Original file line numberDiff line numberDiff line change
@@ -71,74 +71,32 @@ case class GetItem(child: Expression, ordinal: Expression) extends Expression {
7171
}
7272

7373
/**
74-
* Returns the value of fields in the `child`.
75-
* The type of `child` can be struct, or array of struct,
76-
* or array of array of struct, or array of array ... of struct.
74+
* Returns the value of fields in the Struct `child`.
7775
*/
7876
case class GetField(child: Expression, fieldName: String) extends UnaryExpression {
7977
type EvaluatedType = Any
8078

81-
lazy val dataType = {
82-
structType
83-
buildDataType(field.dataType)
84-
}
85-
79+
def dataType = field.dataType
8680
override def nullable = child.nullable || field.nullable
8781
override def foldable = child.foldable
8882

89-
private var _buildDataType = identity[DataType] _
90-
private lazy val buildDataType = {
91-
structType
92-
_buildDataType
93-
}
94-
95-
private var _nestedArrayCount = 0
96-
private lazy val nestedArrayCount = {
97-
structType
98-
_nestedArrayCount
99-
}
100-
101-
private def getStructType(t: DataType): StructType = t match {
102-
case ArrayType(elementType, containsNull) =>
103-
_buildDataType = {(t: DataType) => ArrayType(t, containsNull)} andThen _buildDataType
104-
_nestedArrayCount += 1
105-
getStructType(elementType)
83+
protected def structType = child.dataType match {
10684
case s: StructType => s
10785
case otherType => sys.error(s"GetField is not valid on fields of type $otherType")
10886
}
10987

110-
protected lazy val structType: StructType = {
111-
child match {
112-
case n: GetField =>
113-
this._buildDataType = n._buildDataType
114-
this._nestedArrayCount = n._nestedArrayCount
115-
getStructType(n.field.dataType)
116-
case _ => getStructType(child.dataType)
117-
}
118-
}
119-
12088
lazy val field =
12189
structType.fields
12290
.find(_.name == fieldName)
12391
.getOrElse(sys.error(s"No such field $fieldName in ${child.dataType}"))
12492

12593
lazy val ordinal = structType.fields.indexOf(field)
12694

127-
override lazy val resolved = childrenResolved
95+
override lazy val resolved = childrenResolved && child.dataType.isInstanceOf[StructType]
12896

12997
override def eval(input: Row): Any = {
130-
val baseValue = child.eval(input)
131-
evaluateValue(baseValue, nestedArrayCount)
132-
}
133-
134-
private def evaluateValue(v: Any, count: Int): Any = {
135-
if (v == null) {
136-
null
137-
} else if (count > 0) {
138-
v.asInstanceOf[Seq[_]].map(r => evaluateValue(r, count - 1))
139-
} else {
140-
v.asInstanceOf[Row](ordinal)
141-
}
98+
val baseValue = child.eval(input).asInstanceOf[Row]
99+
if (baseValue == null) null else baseValue(ordinal)
142100
}
143101

144102
override def toString = s"$child.$fieldName"

sql/core/src/test/scala/org/apache/spark/sql/json/JsonSuite.scala

-28
Original file line numberDiff line numberDiff line change
@@ -594,33 +594,5 @@ class JsonSuite extends QueryTest {
594594
sql("select complexArrayOfStruct[0].field1[1].inner2[0], complexArrayOfStruct[1].field2[0][1] from jsonTable"),
595595
("str2", 6) :: Nil
596596
)
597-
598-
checkAnswer(
599-
sql("select arrayOfStruct.field1, arrayOfStruct.field2 from jsonTable"),
600-
(Seq(true, false, null), Seq("str1", null, null)) :: Nil
601-
)
602-
603-
checkAnswer(
604-
sql("select complexNestedArray.field, complexNestedArray.field.innerField from jsonTable"),
605-
(
606-
Seq(
607-
Seq(
608-
Seq("str1", null),
609-
Seq("str2", null)
610-
),
611-
Seq(
612-
Seq("str3", null),
613-
Seq(null, "str4")
614-
),
615-
null
616-
),
617-
618-
Seq(
619-
Seq("str1", "str2"),
620-
Seq("str3", null),
621-
null
622-
)
623-
) :: Nil
624-
)
625597
}
626598
}

sql/core/src/test/scala/org/apache/spark/sql/json/TestJsonData.scala

+1-26
Original file line numberDiff line numberDiff line change
@@ -106,31 +106,6 @@ object TestJsonData {
106106
"inner1": "str4"
107107
}],
108108
"field2": [[5, 6], [7, 8]]
109-
}],
110-
"complexNestedArray": [
111-
{
112-
"field": [
113-
{
114-
"innerField": "str1"
115-
},
116-
{
117-
"innerField": "str2"
118-
}
119-
]
120-
},
121-
{
122-
"field": [
123-
{
124-
"innerField": "str3"
125-
},
126-
{
127-
"otherInner": "str4"
128-
}
129-
]
130-
},
131-
{
132-
"otherField": "str5"
133-
}
134-
]
109+
}]
135110
}""" :: Nil)
136111
}

0 commit comments

Comments
 (0)