maropu commented on a change in pull request #24107: [SPARK-27174][SQL] Add 
support for casting integer types to binary
URL: https://github.com/apache/spark/pull/24107#discussion_r266866296
 
 

 ##########
 File path: 
sql/catalyst/src/test/scala/org/apache/spark/sql/catalyst/expressions/CastSuite.scala
 ##########
 @@ -836,6 +837,61 @@ class CastSuite extends SparkFunSuite with 
ExpressionEvalHelper {
     checkEvaluation(cast("", BooleanType), null)
   }
 
+  test("cast integer types to binary") {
+    val bos = new ByteArrayOutputStream()
+    val dos = new DataOutputStream(bos)
+
+    def withDos(f: DataOutputStream => Unit): UTF8String = {
+      f(dos)
+      dos.flush()
+      val res = UTF8String.fromBytes(bos.toByteArray)
+      bos.reset()
+      res
+    }
+
+    def checkBinaryCast[T](in: T): Unit = in match {
+      case b: Byte =>
+        checkEvaluation(cast(cast(in, BinaryType), StringType), 
withDos(_.writeByte(b)))
+      case s: Short =>
+        checkEvaluation(cast(cast(in, BinaryType), StringType), 
withDos(_.writeShort(s)))
+      case i: Int =>
+        checkEvaluation(cast(cast(in, BinaryType), StringType), 
withDos(_.writeInt(i)))
+      case l: Long =>
+        checkEvaluation(cast(cast(in, BinaryType), StringType), 
withDos(_.writeLong(l)))
+    }
+
+    // Byte
+    checkBinaryCast(0.toByte)
+    checkBinaryCast(1.toByte)
+    checkBinaryCast(-1.toByte)
+    checkBinaryCast(Byte.MaxValue)
+    checkBinaryCast(Byte.MinValue)
+
+    // Short
+    checkBinaryCast(0.toShort)
+    checkBinaryCast(1.toShort)
+    checkBinaryCast(-1.toShort)
+    checkBinaryCast(Short.MaxValue)
+    checkBinaryCast(Short.MinValue)
+
+    // Int
+    checkBinaryCast(0)
+    checkBinaryCast(1)
+    checkBinaryCast(-1)
+    checkBinaryCast(Int.MaxValue)
+    checkBinaryCast(Int.MinValue)
+
+    // Int
+    checkBinaryCast(0L)
+    checkBinaryCast(1L)
+    checkBinaryCast(-1L)
+    checkBinaryCast(Long.MaxValue)
+    checkBinaryCast(Long.MinValue)
+
+    dos.close()
+    bos.close()
+  }
+
 
 Review comment:
   We still need these tests in CastSuite?  Are the tests in `cast.sql` not 
enough?

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