panbingkun commented on code in PR #48493:
URL: https://github.com/apache/spark/pull/48493#discussion_r1805910466


##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/randomExpressions.scala:
##########
@@ -418,32 +418,33 @@ case class RandStr(
           bytes.update(i, ('A' + num - 36).toByte)
       }
     }
-    val result: UTF8String = UTF8String.fromBytes(bytes.toArray)
-    result
+    UTF8String.fromBytes(bytes)
   }
 
   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 bytesTerm = ctx.freshName("bytes")
+    val i = ctx.freshName("i")
+    val v = ctx.freshName("v")
     val eval = length.genCode(ctx)
     ev.copy(code =
       code"""
         |${eval.code}
-        |int length = (int)(${eval.value});
-        |char[] chars = new char[length];
-        |for (int i = 0; i < length; i++) {
-        |  int v = Math.abs($rngTerm.nextInt() % 62);
-        |  if (v < 10) {
-        |    chars[i] = (char)('0' + v);
-        |  } else if (v < 36) {
-        |    chars[i] = (char)('a' + (v - 10));
+        |byte[] $bytesTerm = new byte[(int)(${eval.value})];

Review Comment:
   It's almost very difficult to use `StaticInvoke/Invoke` + 
`RuntimeReplaceable` to unify it, because:
   - We cannot have `RandStr` inherit both `Nondeterministic` and 
`RuntimeReplaceable` at the same time, they all define `eval()...` as `final` 
internally, and compilation will result in errors
   
https://github.com/apache/spark/blob/5697df70b3b7ffdd5e38c2099515cb73a57a79bc/sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/Expression.scala#L510-L514
   
https://github.com/apache/spark/blob/5697df70b3b7ffdd5e38c2099515cb73a57a79bc/sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/Expression.scala#L423-L424



-- 
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]

Reply via email to