dtenedor commented on code in PR #36365:
URL: https://github.com/apache/spark/pull/36365#discussion_r859978460


##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/numberFormatExpressions.scala:
##########
@@ -168,3 +168,141 @@ case class TryToNumber(left: Expression, right: 
Expression)
       newRight: Expression): TryToNumber =
     copy(left = newLeft, right = newRight)
 }
+
+/**
+ * A function that converts decimal values to strings, returning NULL if the 
decimal value fails to
+ * match the format string.
+ */
+@ExpressionDescription(
+  usage = """
+     _FUNC_(numberExpr, formatExpr) - Convert `numberExpr` to a string based 
on the `formatExpr`.
+       Throws an exception if the conversion fails. The format follows the 
same semantics as the
+       to_number function.
+  """,
+  examples = """
+    Examples:
+      > SELECT _FUNC_(454, '999');
+       454
+      > SELECT _FUNC_(454.00, '000D00');
+       454.00
+      > SELECT _FUNC_(12454, '99G999');
+       12,454
+      > SELECT _FUNC_(78.12, '$99.99');
+       $78.12
+      > SELECT _FUNC_(-12454.8, '99G999D9S');
+       12,454.8-
+  """,
+  since = "3.3.0",
+  group = "string_funcs")
+case class ToCharacter(left: Expression, right: Expression)
+  extends BinaryExpression with ImplicitCastInputTypes with NullIntolerant {
+  private lazy val numberFormat = 
right.eval().toString.toUpperCase(Locale.ROOT)
+  private lazy val numberFormatter = new ToNumberParser(numberFormat, true)
+
+  override def dataType: DataType = StringType
+  override def inputTypes: Seq[AbstractDataType] = Seq(DecimalType, StringType)
+  override def checkInputDataTypes(): TypeCheckResult = {
+    val inputTypeCheck = super.checkInputDataTypes()
+    if (inputTypeCheck.isSuccess) {
+      if (right.foldable) {
+        numberFormatter.check()
+      } else {
+        TypeCheckResult.TypeCheckFailure(s"Format expression must be foldable, 
but got $right")
+      }
+    } else {
+      inputTypeCheck
+    }
+  }
+  override def prettyName: String = "to_char"
+  override def nullSafeEval(decimal: Any, format: Any): Any = {
+    val input = decimal.asInstanceOf[Decimal]
+    numberFormatter.format(input)
+  }
+  override def doGenCode(ctx: CodegenContext, ev: ExprCode): ExprCode = {
+    val builder =
+      ctx.addReferenceObj("builder", numberFormatter, 
classOf[ToNumberParser].getName)
+    val eval = left.genCode(ctx)
+    val result =
+      code"""
+        |${eval.code}

Review Comment:
   Done.



##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/numberFormatExpressions.scala:
##########
@@ -168,3 +168,141 @@ case class TryToNumber(left: Expression, right: 
Expression)
       newRight: Expression): TryToNumber =
     copy(left = newLeft, right = newRight)
 }
+
+/**
+ * A function that converts decimal values to strings, returning NULL if the 
decimal value fails to
+ * match the format string.
+ */
+@ExpressionDescription(
+  usage = """
+     _FUNC_(numberExpr, formatExpr) - Convert `numberExpr` to a string based 
on the `formatExpr`.
+       Throws an exception if the conversion fails. The format follows the 
same semantics as the
+       to_number function.
+  """,
+  examples = """
+    Examples:
+      > SELECT _FUNC_(454, '999');
+       454
+      > SELECT _FUNC_(454.00, '000D00');
+       454.00
+      > SELECT _FUNC_(12454, '99G999');
+       12,454
+      > SELECT _FUNC_(78.12, '$99.99');
+       $78.12
+      > SELECT _FUNC_(-12454.8, '99G999D9S');
+       12,454.8-
+  """,
+  since = "3.3.0",
+  group = "string_funcs")
+case class ToCharacter(left: Expression, right: Expression)
+  extends BinaryExpression with ImplicitCastInputTypes with NullIntolerant {
+  private lazy val numberFormat = 
right.eval().toString.toUpperCase(Locale.ROOT)
+  private lazy val numberFormatter = new ToNumberParser(numberFormat, true)
+
+  override def dataType: DataType = StringType
+  override def inputTypes: Seq[AbstractDataType] = Seq(DecimalType, StringType)
+  override def checkInputDataTypes(): TypeCheckResult = {
+    val inputTypeCheck = super.checkInputDataTypes()
+    if (inputTypeCheck.isSuccess) {
+      if (right.foldable) {
+        numberFormatter.check()
+      } else {
+        TypeCheckResult.TypeCheckFailure(s"Format expression must be foldable, 
but got $right")
+      }
+    } else {
+      inputTypeCheck
+    }
+  }
+  override def prettyName: String = "to_char"
+  override def nullSafeEval(decimal: Any, format: Any): Any = {
+    val input = decimal.asInstanceOf[Decimal]
+    numberFormatter.format(input)
+  }
+  override def doGenCode(ctx: CodegenContext, ev: ExprCode): ExprCode = {
+    val builder =
+      ctx.addReferenceObj("builder", numberFormatter, 
classOf[ToNumberParser].getName)
+    val eval = left.genCode(ctx)
+    val result =
+      code"""
+        |${eval.code}
+        |boolean ${ev.isNull} = ${eval.isNull};
+        |${CodeGenerator.javaType(dataType)} ${ev.value} = 
${CodeGenerator.defaultValue(dataType)};
+        |if (!${ev.isNull}) {
+        |  ${ev.value} = $builder.format(${eval.value});
+        |}
+      """
+    val stripped = result.stripMargin
+    ev.copy(code = stripped)
+  }
+  override protected def withNewChildrenInternal(
+      newLeft: Expression, newRight: Expression): ToCharacter =
+    copy(left = newLeft, right = newRight)
+}
+
+/**
+ * A function that converts decimal values to strings, returning NULL if the 
decimal value fails to
+ * match the format string.
+ */
+@ExpressionDescription(
+  usage = """
+     _FUNC_(numberExpr, formatExpr) - Convert `numberExpr` to a string based 
on the `formatExpr`.

Review Comment:
   Done.



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