maropu commented on a change in pull request #25172: [SPARK-28412][SQL] ANSI 
SQL: OVERLAY function support byte array
URL: https://github.com/apache/spark/pull/25172#discussion_r309512454
 
 

 ##########
 File path: 
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/stringExpressions.scala
 ##########
 @@ -496,19 +509,39 @@ case class Overlay(input: Expression, replace: 
Expression, pos: Expression, len:
     this(str, replace, pos, Literal.create(-1, IntegerType))
   }
 
-  override def dataType: DataType = StringType
+  override def dataType: DataType = input.dataType
 
-  override def inputTypes: Seq[AbstractDataType] =
-    Seq(StringType, StringType, IntegerType, IntegerType)
+  override def inputTypes: Seq[AbstractDataType] = 
Seq(TypeCollection(StringType, BinaryType),
+    TypeCollection(StringType, BinaryType), IntegerType, IntegerType)
 
   override def children: Seq[Expression] = input :: replace :: pos :: len :: 
Nil
 
+  override def checkInputDataTypes(): TypeCheckResult = {
+    val (inputType, replaceType, posType, lenType) =
+      (input.dataType, replace.dataType, pos.dataType, len.dataType)
+    (inputType, replaceType) match {
+      case (StringType, StringType) | (BinaryType, BinaryType) =>
+      case (inputType, replaceType) =>
+        return TypeCheckResult.TypeCheckFailure(s"Invalid data type 
${inputType.simpleString}" +
+          s" and ${replaceType.simpleString}. The result type of the input 
expression and the" +
+          "replace expression are either String or Byte Array.")
+    }
+    if (IntegerType.acceptsType(posType) && IntegerType.acceptsType(lenType)) {
+      TypeCheckResult.TypeCheckSuccess
+    } else {
+      TypeCheckResult.TypeCheckFailure("Pos expression and len expression must 
be integer type.")
+    }
+  }
+
   override def nullSafeEval(inputEval: Any, replaceEval: Any, posEval: Any, 
lenEval: Any): Any = {
-    val inputStr = inputEval.asInstanceOf[UTF8String]
-    val replaceStr = replaceEval.asInstanceOf[UTF8String]
     val position = posEval.asInstanceOf[Int]
     val length = lenEval.asInstanceOf[Int]
-    Overlay.calculate(inputStr, replaceStr, position, length)
+    input.dataType match {
+      case StringType => Overlay.calculate(inputEval.asInstanceOf[UTF8String],
+        replaceEval.asInstanceOf[UTF8String], position, length)
+      case BinaryType => Overlay.calculate(inputEval.asInstanceOf[Array[Byte]],
+        replaceEval.asInstanceOf[Array[Byte]], position, length)
+    }
 
 Review comment:
   ```
     private lazy val replaceFunc = input.dataType match {
       case StringType =>
         (inputEval: Any, replaceEval: Any, posEval: Int, lenEval: Int) => {
           Overlay.calculate(
             inputEval.asInstanceOf[UTF8String],
             replaceEval.asInstanceOf[UTF8String],
             posEval, lenEval)
         }
       case BinaryType =>
         (inputEval: Any, replaceEval: Any, posEval: Int, lenEval: Int) => {
           Overlay.calculate(
             inputEval.asInstanceOf[Array[Byte]],
             replaceEval.asInstanceOf[Array[Byte]],
             posEval, lenEval)
         }
     }
   
     override def nullSafeEval(inputEval: Any, replaceEval: Any, posEval: Any, 
lenEval: Any): Any = {
       replaceFunc(inputEval, replaceEval, posEval.asInstanceOf[Int], 
lenEval.asInstanceOf[Int])
     }
   ```

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