MaxGekk commented on a change in pull request #25963: [SPARK-28137][SQL] Add 
Postgresql function to_number.
URL: https://github.com/apache/spark/pull/25963#discussion_r333042863
 
 

 ##########
 File path: 
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/stringExpressions.scala
 ##########
 @@ -2267,6 +2267,215 @@ case class FormatNumber(x: Expression, d: Expression)
   override def prettyName: String = "format_number"
 }
 
+/**
+ * A function that converts string to numeric.
+ */
+// scalastyle:off line.size.limit
+@ExpressionDescription(
+  usage = """
+    _FUNC_(strExpr, patternExpr) - Convert a string to a number based on the 
pattern.
+    The pattern can consist of the following characters:
+      '9': digit position (can be dropped if insignificant)
+      '0': digit position (will not be dropped, even if insignificant)
+      '.': decimal point (only allowed once)
+      ',': group (thousands) separator
+      'S': sign anchored to number (uses locale)
+      'L': currency symbol (uses locale)
+      'D': decimal point (uses locale)
+      'G': group separator (uses locale)
+  """,
+  examples = """
+    Examples:
+      > SELECT _FUNC_('4540', '999');
+       454
+      > SELECT _FUNC_('454.00', '000D00');
+       454
+      > SELECT _FUNC_('12,454.8-', '99G999D9S');
+       -12454.8
+      > SELECT _FUNC_('CNY234234.4350', 'L999999.0000');
+       234234.435
+  """)
+// scalastyle:on line.size.limit
+case class ToNumber(strExpr: Expression, patternExpr: Expression)
+  extends BinaryExpression with ImplicitCastInputTypes {
+
+  // scalastyle:off caselocale
+  private lazy val patternStr = 
patternExpr.eval().asInstanceOf[UTF8String].toUpperCase.toString
+  // scalastyle:on caselocale
+
+  override def left: Expression = strExpr
+  override def right: Expression = patternExpr
+  override def dataType: DataType = StringType
+  override def inputTypes: Seq[DataType] = Seq(StringType, StringType)
+
+  override def checkInputDataTypes(): TypeCheckResult = {
+    def checkDecimalPointNum(c: Char): Boolean = {
+      c == '.' || c.toUpper == 'D'
+    }
+
+    val inputTypeCheck = super.checkInputDataTypes()
+    if(inputTypeCheck.isSuccess) {
+      if (patternStr.count(checkDecimalPointNum(_)) > 1) {
 
 Review comment:
   ```suggestion
         if (patternStr.count(checkDecimalPointNum) > 1) {
   ```

----------------------------------------------------------------
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.
 
For queries about this service, please contact Infrastructure at:
[email protected]


With regards,
Apache Git Services

---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to