sunchao commented on a change in pull request #29792:
URL: https://github.com/apache/spark/pull/29792#discussion_r493753795



##########
File path: 
sql/catalyst/src/test/scala/org/apache/spark/sql/catalyst/optimizer/UnwrapCastInBinaryComparisonSuite.scala
##########
@@ -79,13 +106,65 @@ class UnwrapCastInBinaryComparisonSuite extends PlanTest 
with ExpressionEvalHelp
     assertEquivalent(castInt(f) < v, falseIfNotNull(f))
   }
 
-  test("unwrap casts when literal is within range (min, max)") {
-    assertEquivalent(castInt(f) > 300, f > 300.toShort)
-    assertEquivalent(castInt(f) >= 500, f >= 500.toShort)
-    assertEquivalent(castInt(f) === 32766, f === 32766.toShort)
-    assertEquivalent(castInt(f) <=> 32766, f <=> 32766.toShort)
-    assertEquivalent(castInt(f) <= -6000, f <= -6000.toShort)
-    assertEquivalent(castInt(f) < -32767, f < -32767.toShort)
+  test("unwrap casts when literal is within range (min, max) or fromType has 
no range") {
+    Seq(300, 500, 32766, -6000, -32767).foreach(v => {
+      assertEquivalent(castInt(f) > v, f > v.toShort)
+      assertEquivalent(castInt(f) >= v, f >= v.toShort)
+      assertEquivalent(castInt(f) === v, f === v.toShort)
+      assertEquivalent(castInt(f) <=> v, f <=> v.toShort)
+      assertEquivalent(castInt(f) <= v, f <= v.toShort)
+      assertEquivalent(castInt(f) < v, f < v.toShort)
+    })
+
+    Seq(3.14.toFloat.toDouble, -1000.0.toFloat.toDouble,
+      20.0.toFloat.toDouble, -2.414.toFloat.toDouble,
+      Float.MinValue.toDouble, Float.MaxValue.toDouble, 
Float.PositiveInfinity.toDouble
+    ).foreach(v => {
+      assertEquivalent(castDouble(f2) > v, f2 > v.toFloat)
+      assertEquivalent(castDouble(f2) >= v, f2 >= v.toFloat)
+      assertEquivalent(castDouble(f2) === v, f2 === v.toFloat)
+      assertEquivalent(castDouble(f2) <=> v, f2 <=> v.toFloat)
+      assertEquivalent(castDouble(f2) <= v, f2 <= v.toFloat)
+      assertEquivalent(castDouble(f2) < v, f2 < v.toFloat)
+    })
+
+    Seq(decimal2(100.20), decimal2(-200.50)).foreach(v => {
+      assertEquivalent(castDecimal2(f3) > v, f3 > decimal(v))
+      assertEquivalent(castDecimal2(f3) >= v, f3 >= decimal(v))
+      assertEquivalent(castDecimal2(f3) === v, f3 === decimal(v))
+      assertEquivalent(castDecimal2(f3) <=> v, f3 <=> decimal(v))
+      assertEquivalent(castDecimal2(f3) <= v, f3 <= decimal(v))
+      assertEquivalent(castDecimal2(f3) < v, f3 < decimal(v))
+    })
+  }
+
+  test("unwrap cast when literal is within range (min, max) AND has round up 
or down") {
+    // Cases for rounding down
+    var doubleValue = 100.6
+    assertEquivalent(castDouble(f) > doubleValue, f > doubleValue.toShort)
+    assertEquivalent(castDouble(f) > doubleValue, f > doubleValue.toShort)
+    assertEquivalent(castDouble(f) === doubleValue, falseIfNotNull(f))
+    assertEquivalent(castDouble(f) <=> doubleValue, false)
+    assertEquivalent(castDouble(f) <= doubleValue, f <= doubleValue.toShort)
+    assertEquivalent(castDouble(f) < doubleValue, f <= doubleValue.toShort)
+
+    // Cases for rounding up: 3.14 will be rounded to 3.14000010... after 
casting to float

Review comment:
       @cloud-fan Sorry i was wrong in the above comment (somehow I was 
thinking casting from double to short there). 
   
   Yes, it appears that casting from double to float can be either rounding up 
or down, depending on value:
   
   ```scala
   scala> val x = 0.39999999
   x: Double = 0.39999999
   
   scala> val y = x.toFloat
   y: Float = 0.39999998
   
   scala > val x = 0.49999999
   y: Double = 0.49999999
   
   scala> val y = x.toFloat
   y: Float = 0.5
   ```
   To clarify, here the round up is after casting roundtrip: double 3.14 -> 
float 3.14 -> double 3.14000010...




----------------------------------------------------------------
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:
us...@infra.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscr...@spark.apache.org
For additional commands, e-mail: reviews-h...@spark.apache.org

Reply via email to