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


##########
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:
   Currently, I have extracted the internal logic of `randstr` to 
`ExpressionImplUtils#randStr(...)` and called it separately in `interpreted` 
and `codegen`.



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