Skip to content
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

[SPARK-8770][SQL] Create BinaryOperator abstract class. #7170

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
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
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,7 @@ object HiveTypeCoercion {
* Converts string "NaN"s that are in binary operators with a NaN-able types (Float / Double) to
* the appropriate numeric equivalent.
*/
// TODO: remove this rule and make Cast handle Nan.
object ConvertNaNs extends Rule[LogicalPlan] {
private val StringNaN = Literal("NaN")

Expand All @@ -159,19 +160,19 @@ object HiveTypeCoercion {
case e if !e.childrenResolved => e

/* Double Conversions */
case b @ BinaryExpression(StringNaN, right @ DoubleType()) =>
case b @ BinaryOperator(StringNaN, right @ DoubleType()) =>
b.makeCopy(Array(Literal(Double.NaN), right))
case b @ BinaryExpression(left @ DoubleType(), StringNaN) =>
case b @ BinaryOperator(left @ DoubleType(), StringNaN) =>
b.makeCopy(Array(left, Literal(Double.NaN)))

/* Float Conversions */
case b @ BinaryExpression(StringNaN, right @ FloatType()) =>
case b @ BinaryOperator(StringNaN, right @ FloatType()) =>
b.makeCopy(Array(Literal(Float.NaN), right))
case b @ BinaryExpression(left @ FloatType(), StringNaN) =>
case b @ BinaryOperator(left @ FloatType(), StringNaN) =>
b.makeCopy(Array(left, Literal(Float.NaN)))

/* Use float NaN by default to avoid unnecessary type widening */
case b @ BinaryExpression(left @ StringNaN, StringNaN) =>
case b @ BinaryOperator(left @ StringNaN, StringNaN) =>
b.makeCopy(Array(left, Literal(Float.NaN)))
}
}
Expand Down Expand Up @@ -245,12 +246,12 @@ object HiveTypeCoercion {

Union(newLeft, newRight)

// Also widen types for BinaryExpressions.
// Also widen types for BinaryOperator.
case q: LogicalPlan => q transformExpressions {
// Skip nodes who's children have not been resolved yet.
case e if !e.childrenResolved => e

case b @ BinaryExpression(left, right) if left.dataType != right.dataType =>
case b @ BinaryOperator(left, right) if left.dataType != right.dataType =>
findTightestCommonTypeOfTwo(left.dataType, right.dataType).map { widestType =>
val newLeft = if (left.dataType == widestType) left else Cast(left, widestType)
val newRight = if (right.dataType == widestType) right else Cast(right, widestType)
Expand Down Expand Up @@ -478,7 +479,7 @@ object HiveTypeCoercion {

// Promote integers inside a binary expression with fixed-precision decimals to decimals,
// and fixed-precision decimals in an expression with floats / doubles to doubles
case b @ BinaryExpression(left, right) if left.dataType != right.dataType =>
case b @ BinaryOperator(left, right) if left.dataType != right.dataType =>
(left.dataType, right.dataType) match {
case (t, DecimalType.Fixed(p, s)) if intTypeToFixed.contains(t) =>
b.makeCopy(Array(Cast(left, intTypeToFixed(t)), right))
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/*
* 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 org.apache.spark.sql.catalyst.analysis.TypeCheckResult
import org.apache.spark.sql.types.DataType


/**
* An trait that gets mixin to define the expected input types of an expression.
*/
trait ExpectsInputTypes { self: Expression =>

/**
* Expected input types from child expressions. The i-th position in the returned seq indicates
* the type requirement for the i-th child.
*
* The possible values at each position are:
* 1. a specific data type, e.g. LongType, StringType.
* 2. a non-leaf data type, e.g. NumericType, IntegralType, FractionalType.
* 3. a list of specific data types, e.g. Seq(StringType, BinaryType).
*/
def inputTypes: Seq[Any]

override def checkInputDataTypes(): TypeCheckResult = {
// We will do the type checking in `HiveTypeCoercion`, so always returning success here.
TypeCheckResult.TypeCheckSuccess
}
}

/**
* Expressions that require a specific `DataType` as input should implement this trait
* so that the proper type conversions can be performed in the analyzer.
*/
trait AutoCastInputTypes { self: Expression =>

def inputTypes: Seq[DataType]

override def checkInputDataTypes(): TypeCheckResult = {
// We will always do type casting for `AutoCastInputTypes` in `HiveTypeCoercion`,
// so type mismatch error won't be reported here, but for underling `Cast`s.
TypeCheckResult.TypeCheckSuccess
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -119,17 +119,6 @@ abstract class Expression extends TreeNode[Expression] {
*/
def childrenResolved: Boolean = children.forall(_.resolved)

/**
* Returns a string representation of this expression that does not have developer centric
* debugging information like the expression id.
*/
def prettyString: String = {
transform {
case a: AttributeReference => PrettyAttribute(a.name)
case u: UnresolvedAttribute => PrettyAttribute(u.name)
}.toString
}

/**
* Returns true when two expressions will always compute the same result, even if they differ
* cosmetically (i.e. capitalization of names in attributes may be different).
Expand All @@ -154,71 +143,40 @@ abstract class Expression extends TreeNode[Expression] {
* Note: it's not valid to call this method until `childrenResolved == true`.
*/
def checkInputDataTypes(): TypeCheckResult = TypeCheckResult.TypeCheckSuccess
}

abstract class BinaryExpression extends Expression with trees.BinaryNode[Expression] {
self: Product =>

def symbol: String = sys.error(s"BinaryExpressions must override either toString or symbol")

override def foldable: Boolean = left.foldable && right.foldable

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

override def toString: String = s"($left $symbol $right)"

/**
* Short hand for generating binary evaluation code.
* If either of the sub-expressions is null, the result of this computation
* is assumed to be null.
*
* @param f accepts two variable names and returns Java code to compute the output.
* Returns a user-facing string representation of this expression's name.
* This should usually match the name of the function in SQL.
*/
protected def defineCodeGen(
ctx: CodeGenContext,
ev: GeneratedExpressionCode,
f: (String, String) => String): String = {
nullSafeCodeGen(ctx, ev, (result, eval1, eval2) => {
s"$result = ${f(eval1, eval2)};"
})
}
def prettyName: String = getClass.getSimpleName.toLowerCase

/**
* Short hand for generating binary evaluation code.
* If either of the sub-expressions is null, the result of this computation
* is assumed to be null.
* Returns a user-facing string representation of this expression, i.e. does not have developer
* centric debugging information like the expression id.
*/
protected def nullSafeCodeGen(
ctx: CodeGenContext,
ev: GeneratedExpressionCode,
f: (String, String, String) => String): String = {
val eval1 = left.gen(ctx)
val eval2 = right.gen(ctx)
val resultCode = f(ev.primitive, eval1.primitive, eval2.primitive)
s"""
${eval1.code}
boolean ${ev.isNull} = ${eval1.isNull};
${ctx.javaType(dataType)} ${ev.primitive} = ${ctx.defaultValue(dataType)};
if (!${ev.isNull}) {
${eval2.code}
if (!${eval2.isNull}) {
$resultCode
} else {
${ev.isNull} = true;
}
}
"""
def prettyString: String = {
transform {
case a: AttributeReference => PrettyAttribute(a.name)
case u: UnresolvedAttribute => PrettyAttribute(u.name)
}.toString
}
}

private[sql] object BinaryExpression {
def unapply(e: BinaryExpression): Option[(Expression, Expression)] = Some((e.left, e.right))
override def toString: String = prettyName + children.mkString("(", ",", ")")
}


/**
* A leaf expression, i.e. one without any child expressions.
*/
abstract class LeafExpression extends Expression with trees.LeafNode[Expression] {
self: Product =>
}


/**
* An expression with one input and one output. The output is by default evaluated to null
* if the input is evaluated to null.
*/
abstract class UnaryExpression extends Expression with trees.UnaryNode[Expression] {
self: Product =>

Expand Down Expand Up @@ -265,39 +223,76 @@ abstract class UnaryExpression extends Expression with trees.UnaryNode[Expressio
}
}


/**
* An trait that gets mixin to define the expected input types of an expression.
* An expression with two inputs and one output. The output is by default evaluated to null
* if any input is evaluated to null.
*/
trait ExpectsInputTypes { self: Expression =>
abstract class BinaryExpression extends Expression with trees.BinaryNode[Expression] {
self: Product =>

override def foldable: Boolean = left.foldable && right.foldable

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

/**
* Expected input types from child expressions. The i-th position in the returned seq indicates
* the type requirement for the i-th child.
* Short hand for generating binary evaluation code.
* If either of the sub-expressions is null, the result of this computation
* is assumed to be null.
*
* The possible values at each position are:
* 1. a specific data type, e.g. LongType, StringType.
* 2. a non-leaf data type, e.g. NumericType, IntegralType, FractionalType.
* 3. a list of specific data types, e.g. Seq(StringType, BinaryType).
* @param f accepts two variable names and returns Java code to compute the output.
*/
def inputTypes: Seq[Any]
protected def defineCodeGen(
ctx: CodeGenContext,
ev: GeneratedExpressionCode,
f: (String, String) => String): String = {
nullSafeCodeGen(ctx, ev, (result, eval1, eval2) => {
s"$result = ${f(eval1, eval2)};"
})
}

override def checkInputDataTypes(): TypeCheckResult = {
// We will do the type checking in `HiveTypeCoercion`, so always returning success here.
TypeCheckResult.TypeCheckSuccess
/**
* Short hand for generating binary evaluation code.
* If either of the sub-expressions is null, the result of this computation
* is assumed to be null.
*/
protected def nullSafeCodeGen(
ctx: CodeGenContext,
ev: GeneratedExpressionCode,
f: (String, String, String) => String): String = {
val eval1 = left.gen(ctx)
val eval2 = right.gen(ctx)
val resultCode = f(ev.primitive, eval1.primitive, eval2.primitive)
s"""
${eval1.code}
boolean ${ev.isNull} = ${eval1.isNull};
${ctx.javaType(dataType)} ${ev.primitive} = ${ctx.defaultValue(dataType)};
if (!${ev.isNull}) {
${eval2.code}
if (!${eval2.isNull}) {
$resultCode
} else {
${ev.isNull} = true;
}
}
"""
}
}


/**
* Expressions that require a specific `DataType` as input should implement this trait
* so that the proper type conversions can be performed in the analyzer.
* An expression that has two inputs that are expected to the be same type. If the two inputs have
* different types, the analyzer will find the tightest common type and do the proper type casting.
*/
trait AutoCastInputTypes { self: Expression =>
abstract class BinaryOperator extends BinaryExpression {
self: Product =>

def inputTypes: Seq[DataType]
def symbol: String

override def checkInputDataTypes(): TypeCheckResult = {
// We will always do type casting for `AutoCastInputTypes` in `HiveTypeCoercion`,
// so type mismatch error won't be reported here, but for underling `Cast`s.
TypeCheckResult.TypeCheckSuccess
}
override def toString: String = s"($left $symbol $right)"
}


private[sql] object BinaryOperator {
def unapply(e: BinaryOperator): Option[(Expression, Expression)] = Some((e.left, e.right))
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ case class ScalaUDF(function: AnyRef, dataType: DataType, children: Seq[Expressi

override def nullable: Boolean = true

override def toString: String = s"scalaUDF(${children.mkString(",")})"
override def toString: String = s"UDF(${children.mkString(",")})"

// scalastyle:off

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,6 @@ case class Max(child: Expression) extends PartialAggregate with trees.UnaryNode[

override def nullable: Boolean = true
override def dataType: DataType = child.dataType
override def toString: String = s"MAX($child)"

override def asPartial: SplitEvaluation = {
val partialMax = Alias(Max(child), "PartialMax")()
Expand Down Expand Up @@ -162,7 +161,6 @@ case class Count(child: Expression) extends PartialAggregate with trees.UnaryNod

override def nullable: Boolean = false
override def dataType: LongType.type = LongType
override def toString: String = s"COUNT($child)"

override def asPartial: SplitEvaluation = {
val partialCount = Alias(Count(child), "PartialCount")()
Expand Down Expand Up @@ -401,8 +399,6 @@ case class Average(child: Expression) extends PartialAggregate with trees.UnaryN
DoubleType
}

override def toString: String = s"AVG($child)"

override def asPartial: SplitEvaluation = {
child.dataType match {
case DecimalType.Fixed(_, _) | DecimalType.Unlimited =>
Expand Down Expand Up @@ -494,8 +490,6 @@ case class Sum(child: Expression) extends PartialAggregate with trees.UnaryNode[
child.dataType
}

override def toString: String = s"SUM($child)"

override def asPartial: SplitEvaluation = {
child.dataType match {
case DecimalType.Fixed(_, _) =>
Expand Down
Loading