Skip to content

Commit

Permalink
[MINOR] Avoid code duplication for nullable in Higher Order function
Browse files Browse the repository at this point in the history
## What changes were proposed in this pull request?

Most of  `HigherOrderFunction`s have the same `nullable` definition, ie. they are nullable when one of their arguments is nullable. The PR refactors it in order to avoid code duplication.

## How was this patch tested?

NA

Closes #22243 from mgaido91/MINOR_nullable_hof.

Authored-by: Marco Gaido <marcogaido91@gmail.com>
Signed-off-by: hyukjinkwon <gurwls223@apache.org>
  • Loading branch information
mgaido91 authored and HyukjinKwon committed Aug 29, 2018
1 parent bbbf814 commit 32c8a3d
Showing 1 changed file with 2 additions and 16 deletions.
Expand Up @@ -90,6 +90,8 @@ object LambdaFunction {
*/
trait HigherOrderFunction extends Expression with ExpectsInputTypes {

override def nullable: Boolean = arguments.exists(_.nullable)

override def children: Seq[Expression] = arguments ++ functions

/**
Expand Down Expand Up @@ -217,8 +219,6 @@ case class ArrayTransform(
function: Expression)
extends ArrayBasedSimpleHigherOrderFunction with CodegenFallback {

override def nullable: Boolean = argument.nullable

override def dataType: ArrayType = ArrayType(function.dataType, function.nullable)

override def bind(f: (Expression, Seq[(DataType, Boolean)]) => LambdaFunction): ArrayTransform = {
Expand Down Expand Up @@ -287,8 +287,6 @@ case class MapFilter(
copy(function = f(function, (keyType, false) :: (valueType, valueContainsNull) :: Nil))
}

override def nullable: Boolean = argument.nullable

override def nullSafeEval(inputRow: InternalRow, argumentValue: Any): Any = {
val m = argumentValue.asInstanceOf[MapData]
val f = functionForEval
Expand Down Expand Up @@ -328,8 +326,6 @@ case class ArrayFilter(
function: Expression)
extends ArrayBasedSimpleHigherOrderFunction with CodegenFallback {

override def nullable: Boolean = argument.nullable

override def dataType: DataType = argument.dataType

override def functionType: AbstractDataType = BooleanType
Expand Down Expand Up @@ -375,8 +371,6 @@ case class ArrayExists(
function: Expression)
extends ArrayBasedSimpleHigherOrderFunction with CodegenFallback {

override def nullable: Boolean = argument.nullable

override def dataType: DataType = BooleanType

override def functionType: AbstractDataType = BooleanType
Expand Down Expand Up @@ -516,8 +510,6 @@ case class TransformKeys(
function: Expression)
extends MapBasedSimpleHigherOrderFunction with CodegenFallback {

override def nullable: Boolean = argument.nullable

@transient lazy val MapType(keyType, valueType, valueContainsNull) = argument.dataType

override def dataType: DataType = MapType(function.dataType, valueType, valueContainsNull)
Expand Down Expand Up @@ -568,8 +560,6 @@ case class TransformValues(
function: Expression)
extends MapBasedSimpleHigherOrderFunction with CodegenFallback {

override def nullable: Boolean = argument.nullable

@transient lazy val MapType(keyType, valueType, valueContainsNull) = argument.dataType

override def dataType: DataType = MapType(keyType, function.dataType, function.nullable)
Expand Down Expand Up @@ -638,8 +628,6 @@ case class MapZipWith(left: Expression, right: Expression, function: Expression)

override def functionTypes: Seq[AbstractDataType] = AnyDataType :: Nil

override def nullable: Boolean = left.nullable || right.nullable

override def dataType: DataType = MapType(keyType, function.dataType, function.nullable)

override def bind(f: (Expression, Seq[(DataType, Boolean)]) => LambdaFunction): MapZipWith = {
Expand Down Expand Up @@ -810,8 +798,6 @@ case class ZipWith(left: Expression, right: Expression, function: Expression)

override def functionTypes: Seq[AbstractDataType] = AnyDataType :: Nil

override def nullable: Boolean = left.nullable || right.nullable

override def dataType: ArrayType = ArrayType(function.dataType, function.nullable)

override def bind(f: (Expression, Seq[(DataType, Boolean)]) => LambdaFunction): ZipWith = {
Expand Down

0 comments on commit 32c8a3d

Please sign in to comment.