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
7 changes: 6 additions & 1 deletion core/src/main/resources/error/error-classes.json
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,11 @@
"Input to the function <functionName> cannot contain elements of the \"MAP\" type. In Spark, same maps may have different hashcode, thus hash expressions are prohibited on \"MAP\" elements. To restore previous behavior set \"spark.sql.legacy.allowHashOnMapType\" to \"true\"."
]
},
"INPUT_SIZE_NOT_ONE" : {
"message" : [
"Length of <exprName> should be 1"
]
},
"INVALID_ARG_VALUE" : {
"message" : [
"The <inputName> value must to be a <requireType> literal of <validValues>, but got <inputValue>."
Expand Down Expand Up @@ -5211,4 +5216,4 @@
"grouping() can only be used with GroupingSets/Cube/Rollup"
]
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -802,6 +802,9 @@ object FunctionRegistry {
castAlias("binary", BinaryType),
castAlias("string", StringType),

// mask functions
expression[Mask]("mask"),

// csv
expression[CsvToStructs]("from_csv"),
expression[SchemaOfCsv]("schema_of_csv"),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -979,6 +979,133 @@ abstract class QuaternaryExpression extends Expression with QuaternaryLike[Expre
}
}

/**
* An expression with five inputs and one output. The output is by default evaluated to null if
* any input is evaluated to null.
*/
abstract class QuinaryExpression 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 QuinaryExpression. If
* subclass of QuinaryExpression override nullable, probably should also override this.
*/
override def eval(input: InternalRow): Any = {
val exprs = children
val v1 = exprs(0).eval(input)
if (v1 != null) {
val v2 = exprs(1).eval(input)
if (v2 != null) {
val v3 = exprs(2).eval(input)
if (v3 != null) {
val v4 = exprs(3).eval(input)
if (v4 != null) {
val v5 = exprs(4).eval(input)
if (v5 != null) {
return nullSafeEval(v1, v2, v3, v4, v5)
}
}
}
}
}
null
}

/**
* Called by default [[eval]] implementation. If subclass of QuinaryExpression 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,
input5: Any): Any = {
throw QueryExecutionErrors.notOverrideExpectedMethodsError(
"QuinaryExpression",
"eval",
"nullSafeEval")
}

/**
* Short hand for generating quinary evaluation code. If either of the sub-expressions is null,
* the result of this computation is assumed to be null.
*
* @param f
* accepts seven variable names and returns Java code to compute the output.
*/
protected def defineCodeGen(
ctx: CodegenContext,
ev: ExprCode,
f: (String, String, String, String, String) => String): ExprCode = {
nullSafeCodeGen(
ctx,
ev,
(eval1, eval2, eval3, eval4, eval5) => {
s"${ev.value} = ${f(eval1, eval2, eval3, eval4, eval5)};"
})
}

/**
* Short hand for generating quinary 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 5 non-null evaluation result names of children and returns Java
* code to compute the output.
*/
protected def nullSafeCodeGen(
ctx: CodegenContext,
ev: ExprCode,
f: (String, String, String, String, String) => String): ExprCode = {
val firstGen = children(0).genCode(ctx)
val secondGen = children(1).genCode(ctx)
val thirdGen = children(2).genCode(ctx)
val fourthGen = children(3).genCode(ctx)
val fifthGen = children(4).genCode(ctx)
val resultCode =
f(firstGen.value, secondGen.value, thirdGen.value, fourthGen.value, fifthGen.value)

if (nullable) {
val nullSafeEval =
firstGen.code + ctx.nullSafeExec(children(0).nullable, firstGen.isNull) {
secondGen.code + ctx.nullSafeExec(children(1).nullable, secondGen.isNull) {
thirdGen.code + ctx.nullSafeExec(children(2).nullable, thirdGen.isNull) {
fourthGen.code + ctx.nullSafeExec(children(3).nullable, fourthGen.isNull) {
fifthGen.code + ctx.nullSafeExec(children(4).nullable, fifthGen.isNull) {
s"""
${ev.isNull} = false; // resultCode could change nullability.
$resultCode
"""
}
}
}
}
}

ev.copy(code = code"""
boolean ${ev.isNull} = true;
${CodeGenerator.javaType(dataType)} ${ev.value} = ${CodeGenerator.defaultValue(dataType)};
$nullSafeEval""")
} else {
ev.copy(
code = code"""
${firstGen.code}
${secondGen.code}
${thirdGen.code}
${fourthGen.code}
${fifthGen.code}
${CodeGenerator.javaType(dataType)} ${ev.value} = ${CodeGenerator.defaultValue(dataType)};
$resultCode""",
isNull = FalseLiteral)
}
}
}

/**
* An expression with six inputs + 7th optional input and one output.
* The output is by default evaluated to null if any input is evaluated to null.
Expand Down
Loading