-
Notifications
You must be signed in to change notification settings - Fork 28.7k
[SPARK-7133][SQL] Implement struct, array, and map field accessor #5744
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
Closed
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
c9d85f5
generalize UnresolvedGetField to support all map, struct, and array
cloud-fan b5961a9
fix style
cloud-fan ab35ab5
fix python test
cloud-fan 3f880c3
add java doc
cloud-fan 6bf72bc
address comments
cloud-fan 2a70526
use _bin_op in dataframe.py
cloud-fan 239730c
fix test compile
cloud-fan 8df6199
fix python test
cloud-fan f515d69
add apply method and test cases
cloud-fan 4f0833a
add python test
cloud-fan 7ea5b31
fix python test
cloud-fan 715c589
address comments
cloud-fan File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
206 changes: 206 additions & 0 deletions
206
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/ExtractValue.scala
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,206 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one or more | ||
* contributor license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright ownership. | ||
* The ASF licenses this file to You under the Apache License, Version 2.0 | ||
* (the "License"); you may not use this file except in compliance with | ||
* the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.apache.spark.sql.catalyst.expressions | ||
|
||
import scala.collection.Map | ||
|
||
import org.apache.spark.sql.AnalysisException | ||
import org.apache.spark.sql.catalyst.analysis._ | ||
import org.apache.spark.sql.types._ | ||
|
||
object ExtractValue { | ||
/** | ||
* Returns the resolved `ExtractValue`. It will return one kind of concrete `ExtractValue`, | ||
* depend on the type of `child` and `extraction`. | ||
* | ||
* `child` | `extraction` | concrete `ExtractValue` | ||
* ---------------------------------------------------------------- | ||
* Struct | Literal String | GetStructField | ||
* Array[Struct] | Literal String | GetArrayStructFields | ||
* Array | Integral type | GetArrayItem | ||
* Map | Any type | GetMapValue | ||
*/ | ||
def apply( | ||
child: Expression, | ||
extraction: Expression, | ||
resolver: Resolver): ExtractValue = { | ||
|
||
(child.dataType, extraction) match { | ||
case (StructType(fields), Literal(fieldName, StringType)) => | ||
val ordinal = findField(fields, fieldName.toString, resolver) | ||
GetStructField(child, fields(ordinal), ordinal) | ||
case (ArrayType(StructType(fields), containsNull), Literal(fieldName, StringType)) => | ||
val ordinal = findField(fields, fieldName.toString, resolver) | ||
GetArrayStructFields(child, fields(ordinal), ordinal, containsNull) | ||
case (_: ArrayType, _) if extraction.dataType.isInstanceOf[IntegralType] => | ||
GetArrayItem(child, extraction) | ||
case (_: MapType, _) => | ||
GetMapValue(child, extraction) | ||
case (otherType, _) => | ||
val errorMsg = otherType match { | ||
case StructType(_) | ArrayType(StructType(_), _) => | ||
s"Field name should be String Literal, but it's $extraction" | ||
case _: ArrayType => | ||
s"Array index should be integral type, but it's ${extraction.dataType}" | ||
case other => | ||
s"Can't extract value from $child" | ||
} | ||
throw new AnalysisException(errorMsg) | ||
} | ||
} | ||
|
||
def unapply(g: ExtractValue): Option[(Expression, Expression)] = { | ||
g match { | ||
case o: ExtractValueWithOrdinal => Some((o.child, o.ordinal)) | ||
case _ => Some((g.child, null)) | ||
} | ||
} | ||
|
||
/** | ||
* Find the ordinal of StructField, report error if no desired field or over one | ||
* desired fields are found. | ||
*/ | ||
private def findField(fields: Array[StructField], fieldName: String, resolver: Resolver): Int = { | ||
val checkField = (f: StructField) => resolver(f.name, fieldName) | ||
val ordinal = fields.indexWhere(checkField) | ||
if (ordinal == -1) { | ||
throw new AnalysisException( | ||
s"No such struct field $fieldName in ${fields.map(_.name).mkString(", ")}") | ||
} else if (fields.indexWhere(checkField, ordinal + 1) != -1) { | ||
throw new AnalysisException( | ||
s"Ambiguous reference to fields ${fields.filter(checkField).mkString(", ")}") | ||
} else { | ||
ordinal | ||
} | ||
} | ||
} | ||
|
||
trait ExtractValue extends UnaryExpression { | ||
self: Product => | ||
|
||
type EvaluatedType = Any | ||
} | ||
|
||
/** | ||
* Returns the value of fields in the Struct `child`. | ||
*/ | ||
case class GetStructField(child: Expression, field: StructField, ordinal: Int) | ||
extends ExtractValue { | ||
|
||
override def dataType: DataType = field.dataType | ||
override def nullable: Boolean = child.nullable || field.nullable | ||
override def foldable: Boolean = child.foldable | ||
override def toString: String = s"$child.${field.name}" | ||
|
||
override def eval(input: Row): Any = { | ||
val baseValue = child.eval(input).asInstanceOf[Row] | ||
if (baseValue == null) null else baseValue(ordinal) | ||
} | ||
} | ||
|
||
/** | ||
* Returns the array of value of fields in the Array of Struct `child`. | ||
*/ | ||
case class GetArrayStructFields( | ||
child: Expression, | ||
field: StructField, | ||
ordinal: Int, | ||
containsNull: Boolean) extends ExtractValue { | ||
|
||
override def dataType: DataType = ArrayType(field.dataType, containsNull) | ||
override def nullable: Boolean = child.nullable | ||
override def foldable: Boolean = child.foldable | ||
override def toString: String = s"$child.${field.name}" | ||
|
||
override def eval(input: Row): Any = { | ||
val baseValue = child.eval(input).asInstanceOf[Seq[Row]] | ||
if (baseValue == null) null else { | ||
baseValue.map { row => | ||
if (row == null) null else row(ordinal) | ||
} | ||
} | ||
} | ||
} | ||
|
||
abstract class ExtractValueWithOrdinal extends ExtractValue { | ||
self: Product => | ||
|
||
def ordinal: Expression | ||
|
||
/** `Null` is returned for invalid ordinals. */ | ||
override def nullable: Boolean = true | ||
override def foldable: Boolean = child.foldable && ordinal.foldable | ||
override def toString: String = s"$child[$ordinal]" | ||
override def children: Seq[Expression] = child :: ordinal :: Nil | ||
|
||
override def eval(input: Row): Any = { | ||
val value = child.eval(input) | ||
if (value == null) { | ||
null | ||
} else { | ||
val o = ordinal.eval(input) | ||
if (o == null) { | ||
null | ||
} else { | ||
evalNotNull(value, o) | ||
} | ||
} | ||
} | ||
|
||
protected def evalNotNull(value: Any, ordinal: Any): Any | ||
} | ||
|
||
/** | ||
* Returns the field at `ordinal` in the Array `child` | ||
*/ | ||
case class GetArrayItem(child: Expression, ordinal: Expression) | ||
extends ExtractValueWithOrdinal { | ||
|
||
override def dataType: DataType = child.dataType.asInstanceOf[ArrayType].elementType | ||
|
||
override lazy val resolved = childrenResolved && | ||
child.dataType.isInstanceOf[ArrayType] && ordinal.dataType.isInstanceOf[IntegralType] | ||
|
||
protected def evalNotNull(value: Any, ordinal: Any) = { | ||
// TODO: consider using Array[_] for ArrayType child to avoid | ||
// boxing of primitives | ||
val baseValue = value.asInstanceOf[Seq[_]] | ||
val index = ordinal.asInstanceOf[Int] | ||
if (index >= baseValue.size || index < 0) { | ||
null | ||
} else { | ||
baseValue(index) | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* Returns the value of key `ordinal` in Map `child` | ||
*/ | ||
case class GetMapValue(child: Expression, ordinal: Expression) | ||
extends ExtractValueWithOrdinal { | ||
|
||
override def dataType: DataType = child.dataType.asInstanceOf[MapType].valueType | ||
|
||
override lazy val resolved = childrenResolved && child.dataType.isInstanceOf[MapType] | ||
|
||
protected def evalNotNull(value: Any, ordinal: Any) = { | ||
val baseValue = value.asInstanceOf[Map[Any, _]] | ||
baseValue.get(ordinal).orNull | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can we add a unit test?
you can add it in tests.py