Github user wzhfy commented on a diff in the pull request: https://github.com/apache/spark/pull/18323#discussion_r123402665 --- Diff: sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/Expression.scala --- @@ -631,3 +631,109 @@ abstract class TernaryExpression extends Expression { } } } + +/** + * An expression with four inputs and one output. The output is by default evaluated to null + * if any input is evaluated to null. + */ +abstract class QuaternaryExpression 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 TernaryExpression. + * If subclass of TernaryExpression 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) { + val value4 = exprs(3).eval(input) + if (value4 != null) { + return nullSafeEval(value1, value2, value3, value4) + } + } + } + } + null + } + + /** + * Called by default [[eval]] implementation. If subclass of TernaryExpression 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, input4: Any): Any = + sys.error(s"TernaryExpressions must override either eval or nullSafeEval") + + /** + * Short hand for generating ternary evaluation code. + * If either of the sub-expressions is null, the result of this computation + * is assumed to be null. + * + * @param f accepts three variable names and returns Java code to compute the output. + */ + protected def defineCodeGen( + ctx: CodegenContext, + ev: ExprCode, + f: (String, String, String, String) => String): ExprCode = { + nullSafeCodeGen(ctx, ev, (eval1, eval2, eval3, eval4) => { + s"${ev.value} = ${f(eval1, eval2, eval3, eval3)};" + }) + } + + /** + * Short hand for generating ternary 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 3 non-null evaluation result names of children --- End diff -- 3 -> 4
--- If your project is set up for it, you can reply to this email and have your reply appear on GitHub as well. If your project does not have this feature enabled and wishes so, or if the feature is enabled but not working, please contact infrastructure at infrastruct...@apache.org or file a JIRA ticket with INFRA. --- --------------------------------------------------------------------- To unsubscribe, e-mail: reviews-unsubscr...@spark.apache.org For additional commands, e-mail: reviews-h...@spark.apache.org