Skip to content
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 @@ -432,3 +432,88 @@ abstract class BinaryOperator extends BinaryExpression with ExpectsInputTypes {
private[sql] object BinaryOperator {
def unapply(e: BinaryOperator): Option[(Expression, Expression)] = Some((e.left, e.right))
}

/**
* An expression with three inputs and one output. The output is by default evaluated to null
* if any input is evaluated to null.
*/
abstract class TernaryExpression extends Expression {

override def foldable: Boolean = children.forall(_.foldable)

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

/**
* Default behavior of evaluation according to the default nullability of BinaryExpression.
* If subclass of BinaryExpression override nullable, probably should also override this.
*/
override def eval(input: InternalRow): Any = {
val exprs = children
val value1 = exprs(0).eval(input)
if (value1 != null) {
val value2 = exprs(1).eval(input)
if (value2 != null) {
val value3 = exprs(2).eval(input)
if (value3 != null) {
return nullSafeEval(value1, value2, value3)
}
}
}
null
}

/**
* Called by default [[eval]] implementation. If subclass of BinaryExpression keep the default
* nullability, they can override this method to save null-check code. If we need full control
* of evaluation process, we should override [[eval]].
*/
protected def nullSafeEval(input1: Any, input2: Any, input3: Any): Any =
sys.error(s"BinaryExpressions must override either eval or nullSafeEval")

/**
* 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.
*/
protected def defineCodeGen(
ctx: CodeGenContext,
ev: GeneratedExpressionCode,
f: (String, String, String) => String): String = {
nullSafeCodeGen(ctx, ev, (eval1, eval2, eval3) => {
s"${ev.primitive} = ${f(eval1, eval2, eval3)};"
})
}

/**
* 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 function that accepts the 2 non-null evaluation result names of children
* and returns Java code to compute the output.
*/
protected def nullSafeCodeGen(
ctx: CodeGenContext,
ev: GeneratedExpressionCode,
f: (String, String, String) => String): String = {
val evals = children.map(_.gen(ctx))
val resultCode = f(evals(0).primitive, evals(1).primitive, evals(2).primitive)
s"""
${evals(0).code}
boolean ${ev.isNull} = true;
${ctx.javaType(dataType)} ${ev.primitive} = ${ctx.defaultValue(dataType)};
if (!${evals(0).isNull}) {
${evals(1).code}
if (!${evals(1).isNull}) {
${evals(2).code}
if (!${evals(2).isNull}) {
${ev.isNull} = false; // resultCode could change nullability
$resultCode
}
}
}
"""
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -305,7 +305,7 @@ abstract class CodeGenerator[InType <: AnyRef, OutType <: AnyRef] extends Loggin
evaluator.cook(code)
} catch {
case e: Exception =>
val msg = "failed to compile:\n " + CodeFormatter.format(code)
val msg = s"failed to compile: $e\n" + CodeFormatter.format(code)
logError(msg, e)
throw new Exception(msg, e)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -165,69 +165,29 @@ case class Cosh(child: Expression) extends UnaryMathExpression(math.cosh, "COSH"
* @param toBaseExpr to which base
*/
case class Conv(numExpr: Expression, fromBaseExpr: Expression, toBaseExpr: Expression)
extends Expression with ImplicitCastInputTypes {

override def foldable: Boolean = numExpr.foldable && fromBaseExpr.foldable && toBaseExpr.foldable

override def nullable: Boolean = numExpr.nullable || fromBaseExpr.nullable || toBaseExpr.nullable
extends TernaryExpression with ImplicitCastInputTypes {

override def children: Seq[Expression] = Seq(numExpr, fromBaseExpr, toBaseExpr)

override def inputTypes: Seq[AbstractDataType] = Seq(StringType, IntegerType, IntegerType)

override def dataType: DataType = StringType

/** Returns the result of evaluating this expression on a given input Row */
override def eval(input: InternalRow): Any = {
val num = numExpr.eval(input)
if (num != null) {
val fromBase = fromBaseExpr.eval(input)
if (fromBase != null) {
val toBase = toBaseExpr.eval(input)
if (toBase != null) {
NumberConverter.convert(
num.asInstanceOf[UTF8String].getBytes,
fromBase.asInstanceOf[Int],
toBase.asInstanceOf[Int])
} else {
null
}
} else {
null
}
} else {
null
}
override def nullSafeEval(num: Any, fromBase: Any, toBase: Any): Any = {
NumberConverter.convert(
num.asInstanceOf[UTF8String].getBytes,
fromBase.asInstanceOf[Int],
toBase.asInstanceOf[Int])
}

override def genCode(ctx: CodeGenContext, ev: GeneratedExpressionCode): String = {
val numGen = numExpr.gen(ctx)
val from = fromBaseExpr.gen(ctx)
val to = toBaseExpr.gen(ctx)

val numconv = NumberConverter.getClass.getName.stripSuffix("$")
s"""
${ctx.javaType(dataType)} ${ev.primitive} = ${ctx.defaultValue(dataType)};
${numGen.code}
boolean ${ev.isNull} = ${numGen.isNull};
if (!${ev.isNull}) {
${from.code}
if (!${from.isNull}) {
${to.code}
if (!${to.isNull}) {
${ev.primitive} = $numconv.convert(${numGen.primitive}.getBytes(),
${from.primitive}, ${to.primitive});
if (${ev.primitive} == null) {
${ev.isNull} = true;
}
} else {
${ev.isNull} = true;
}
} else {
${ev.isNull} = true;
}
nullSafeCodeGen(ctx, ev, (num, from, to) =>
s"""
${ev.primitive} = $numconv.convert($num.getBytes(), $from, $to);
if (${ev.primitive} == null) {
${ev.isNull} = true;
}
"""
"""
)
}
}

Expand Down
Loading