forked from apache/spark
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[SPARK-7133] [SQL] Implement struct, array, and map field accessor
It's the first step: generalize UnresolvedGetField to support all map, struct, and array TODO: add `apply` in Scala and `__getitem__` in Python, and unify the `getItem` and `getField` methods to one single API(or should we keep them for compatibility?). Author: Wenchen Fan <[email protected]> Closes apache#5744 from cloud-fan/generalize and squashes the following commits: 715c589 [Wenchen Fan] address comments 7ea5b31 [Wenchen Fan] fix python test 4f0833a [Wenchen Fan] add python test f515d69 [Wenchen Fan] add apply method and test cases 8df6199 [Wenchen Fan] fix python test 239730c [Wenchen Fan] fix test compile 2a70526 [Wenchen Fan] use _bin_op in dataframe.py 6bf72bc [Wenchen Fan] address comments 3f880c3 [Wenchen Fan] add java doc ab35ab5 [Wenchen Fan] fix python test b5961a9 [Wenchen Fan] fix style c9d85f5 [Wenchen Fan] generalize UnresolvedGetField to support all map, struct, and array
- Loading branch information
1 parent
462ea6f
commit f39969f
Showing
16 changed files
with
327 additions
and
191 deletions.
There are no files selected for viewing
This file contains 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 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 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 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 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 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 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.