cloud-fan commented on code in PR #49210:
URL: https://github.com/apache/spark/pull/49210#discussion_r1895277529
##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/randomExpressions.scala:
##########
@@ -366,52 +371,63 @@ case class RandStr(
}
override def checkInputDataTypes(): TypeCheckResult = {
- var result: TypeCheckResult = TypeCheckResult.TypeCheckSuccess
- def requiredType = "INT or SMALLINT"
- Seq((length, "length", 0),
- (seedExpression, "seed", 1)).foreach {
- case (expr: Expression, name: String, index: Int) =>
- if (result == TypeCheckResult.TypeCheckSuccess) {
- if (!expr.foldable) {
- result = DataTypeMismatch(
- errorSubClass = "NON_FOLDABLE_INPUT",
- messageParameters = Map(
- "inputName" -> toSQLId(name),
- "inputType" -> requiredType,
- "inputExpr" -> toSQLExpr(expr)))
- } else expr.dataType match {
- case _: ShortType | _: IntegerType =>
- case _: LongType if index == 1 =>
- case _ =>
- result = DataTypeMismatch(
- errorSubClass = "UNEXPECTED_INPUT_TYPE",
- messageParameters = Map(
- "paramIndex" -> ordinalNumber(index),
- "requiredType" -> requiredType,
- "inputSql" -> toSQLExpr(expr),
- "inputType" -> toSQLType(expr.dataType)))
- }
+ var result: TypeCheckResult = super.checkInputDataTypes()
+ Seq((length, "length"),
+ (seedExpression, "seed")).foreach {
+ case (expr: Expression, name: String) =>
+ if (result == TypeCheckResult.TypeCheckSuccess && !expr.foldable) {
+ result = DataTypeMismatch(
+ errorSubClass = "NON_FOLDABLE_INPUT",
+ messageParameters = Map(
+ "inputName" -> toSQLId(name),
+ "inputType" -> "integer",
+ "inputExpr" -> toSQLExpr(expr)))
}
}
result
}
override def evalInternal(input: InternalRow): Any = {
- val numChars = length.eval(input).asInstanceOf[Number].intValue()
+ val numChars = lengthInteger()
ExpressionImplUtils.randStr(rng, numChars)
}
+ private def lengthInteger(): Int = {
+ def throwNullArgumentError(): Nothing = {
+ throw QueryCompilationErrors.nullArgumentError(prettyName, "length")
+ }
+ val result = length.dataType match {
+ case _: DecimalType =>
+ val eval = length.eval().asInstanceOf[Decimal]
+ if (eval == null) {
+ throwNullArgumentError()
+ }
+ eval.toInt
+ case _ =>
+ val eval = length.eval().asInstanceOf[Number]
+ if (eval == null) {
+ throwNullArgumentError()
+ }
+ eval.intValue()
+ }
+ if (result < 0) {
+ throw
QueryExecutionErrors.unexpectedValueForLengthInFunctionError(prettyName, result)
+ }
+ result
+ }
+
override def doGenCode(ctx: CodegenContext, ev: ExprCode): ExprCode = {
val className = classOf[XORShiftRandom].getName
val rngTerm = ctx.addMutableState(className, "rng")
ctx.addPartitionInitializationStatement(
s"$rngTerm = new $className(${seed}L + partitionIndex);")
val eval = length.genCode(ctx)
Review Comment:
do we still need to generate code for it if it's always foldable?
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]