Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
remove unnecessary abstraction for ExtractValue
  • Loading branch information
cloud-fan committed Jul 10, 2015
commit 6a37c129964337fc6fad3f5a97752669f982933d
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,8 @@ class Analyzer(
child match {
case _: UnresolvedAttribute => u
case ne: NamedExpression => ne
case ev: ExtractValueWithStruct => Alias(ev, ev.field.name)()
case g: GetStructField => Alias(g, g.field.name)()
case g: GetArrayStructFields => Alias(g, g.field.name)()
case g: Generator if g.resolved && g.elementTypes.size > 1 => MultiAlias(g, Nil)
case e if !e.resolved => u
case other => Alias(other, s"_c$i")()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,10 +78,12 @@ object ExtractValue {
}
}

def unapply(g: ExtractValue): Option[(Expression, Expression)] = g match {
def unapply(g: Expression): Option[(Expression, Expression)] = g match {
case o: GetArrayItem => Some((o.child, o.ordinal))
case o: GetMapValue => Some((o.child, o.key))
case s: ExtractValueWithStruct => Some((s.child, null))
case o: GetStructField => Some((o.child, null))
Copy link
Contributor

Choose a reason for hiding this comment

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

I find it odd to return null in the 2nd field. Why is the semantics of that?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This unapply is only used in optimizer to save code. I have removed it.

case o: GetArrayStructFields => Some((o.child, null))
case _ => None
}

/**
Expand All @@ -103,32 +105,17 @@ object ExtractValue {
}
}

/**
* A common interface of all kinds of extract value expressions.
* Note: concrete extract value expressions are created only by `ExtractValue.apply`,
* we don't need to do type check for them.
*/
trait ExtractValue {
self: Expression =>
}

abstract class ExtractValueWithStruct extends UnaryExpression with ExtractValue {
self: Product =>

def field: StructField
override def toString: String = s"$child.${field.name}"
}

/**
* Returns the value of fields in the Struct `child`.
*
* No need to do type checking since it is handled by [[ExtractValue]].
*/
case class GetStructField(child: Expression, field: StructField, ordinal: Int)
extends ExtractValueWithStruct {
extends UnaryExpression {

override def dataType: DataType = field.dataType
override def nullable: Boolean = child.nullable || field.nullable
override def toString: String = s"$child.${field.name}"

protected override def nullSafeEval(input: Any): Any =
input.asInstanceOf[InternalRow](ordinal)
Expand All @@ -155,10 +142,11 @@ case class GetArrayStructFields(
child: Expression,
field: StructField,
ordinal: Int,
containsNull: Boolean) extends ExtractValueWithStruct {
containsNull: Boolean) extends UnaryExpression {

override def dataType: DataType = ArrayType(field.dataType, containsNull)
override def nullable: Boolean = child.nullable || containsNull || field.nullable
override def toString: String = s"$child.${field.name}"

protected override def nullSafeEval(input: Any): Any = {
input.asInstanceOf[Seq[InternalRow]].map { row =>
Expand Down Expand Up @@ -191,8 +179,7 @@ case class GetArrayStructFields(
*
* No need to do type checking since it is handled by [[ExtractValue]].
*/
case class GetArrayItem(child: Expression, ordinal: Expression)
extends BinaryExpression with ExtractValue {
case class GetArrayItem(child: Expression, ordinal: Expression) extends BinaryExpression {

override def toString: String = s"$child[$ordinal]"

Expand Down Expand Up @@ -231,12 +218,11 @@ case class GetArrayItem(child: Expression, ordinal: Expression)
}

/**
* Returns the value of key `ordinal` in Map `child`.
* Returns the value of key `key` in Map `child`.
*
* No need to do type checking since it is handled by [[ExtractValue]].
*/
case class GetMapValue(child: Expression, key: Expression)
extends BinaryExpression with ExtractValue {
case class GetMapValue(child: Expression, key: Expression) extends BinaryExpression {

override def toString: String = s"$child[$key]"

Expand Down