spark-test commented on code in PR #60487:
URL: https://github.com/apache/spark/pull/60487#discussion_r4573561745


##########
sql/core/src/test/scala/org/apache/spark/sql/execution/datasources/v2/V2PredicateSuite.scala:
##########
@@ -302,6 +303,31 @@ class V2PredicateSuite extends SparkFunSuite {
     assert(PredicateUtils.toV1(predicate3).get.toV2 == predicate3)
   }
 
+  test("SPARK-61684: CHAR_READ_SIDE_PADDING compared with literals") {
+    def str(s: String, dt: StringType = StringType): Literal[_] =
+      LiteralValue(UTF8String.fromString(s), dt)
+    def toV1(name: String, length: Int, literals: Literal[_]*): Option[Filter] 
= {
+      val pad = new 
GeneralScalarExpression(CharVarcharUtils.V2_CHAR_READ_SIDE_PADDING,
+        Array[Expression](ref("c"), LiteralValue(length, IntegerType), str(" 
")))
+      PredicateUtils.toV1(new Predicate(name, (pad +: 
literals).toArray[Expression]))
+    }
+
+    // the stored values that a read pads to 'a  '
+    val in = In("c", Array("a", "a ", "a  "))
+    assert(toV1("=", 3, str("a  ")) === Some(in))
+    assert(toV1("<>", 3, str("a  ")) === Some(V1Not(in)))
+    // NULL <=> 'a  ' is false, so it must stay false under a NOT
+    assert(toV1("<=>", 3, str("a  ")) === Some(V1And(IsNotNull("c"), in)))
+    assert(toV1("IN", 3, str("a  "), str("bc ")) ===
+      Some(In("c", Array("a", "a ", "a  ", "bc", "bc "))))
+
+    // not converted: a non-binary collation, a literal not padded to the 
length, too many values
+    assert(toV1("=", 3, str("a  ", StringType("UTF8_LCASE"))).isEmpty)
+    assert(toV1("=", 3, str("a")).isEmpty)
+    assert(toV1("=", 255, str(" " * 255)).get.asInstanceOf[In].values.length 
=== 256)
+    assert(toV1("=", 256, str(" " * 256)).isEmpty)

Review Comment:
   **Nit (P3):** These two assertions use a single literal, so they cannot tell 
the documented total bound from a per-literal one. If the `.sum` at 
PredicateUtils.scala:178 became a `.max`, or only the first literal were 
counted, every assertion in this test would still pass, while `p IN ('a', 'b')` 
on a `CHAR(200)` column would turn into an `In` of 400 values instead of 
staying unconverted. Results would stay correct, but V1 sources would receive 
filters well beyond the 256 values that the comment at 
`MAX_CHAR_PADDING_IN_VALUES` and the migration note describe. An `IN` whose 
literals each fit within the bound but together exceed it pins the total:
   
   ```suggestion
       assert(toV1("=", 255, str(" " * 255)).get.asInstanceOf[In].values.length 
=== 256)
       assert(toV1("=", 256, str(" " * 256)).isEmpty)
       // the bound applies to the total: each literal needs 200 values, 
together 400
       assert(toV1("IN", 200, str("a" + " " * 199), str("b" + " " * 
199)).isEmpty)
   ```
   
   See **Shared repair plan 1** in the review body.



##########
sql/core/src/test/scala/org/apache/spark/sql/execution/datasources/v2/V2PredicateSuite.scala:
##########
@@ -302,6 +303,31 @@ class V2PredicateSuite extends SparkFunSuite {
     assert(PredicateUtils.toV1(predicate3).get.toV2 == predicate3)
   }
 
+  test("SPARK-61684: CHAR_READ_SIDE_PADDING compared with literals") {
+    def str(s: String, dt: StringType = StringType): Literal[_] =
+      LiteralValue(UTF8String.fromString(s), dt)
+    def toV1(name: String, length: Int, literals: Literal[_]*): Option[Filter] 
= {
+      val pad = new 
GeneralScalarExpression(CharVarcharUtils.V2_CHAR_READ_SIDE_PADDING,
+        Array[Expression](ref("c"), LiteralValue(length, IntegerType), str(" 
")))
+      PredicateUtils.toV1(new Predicate(name, (pad +: 
literals).toArray[Expression]))
+    }
+
+    // the stored values that a read pads to 'a  '
+    val in = In("c", Array("a", "a ", "a  "))
+    assert(toV1("=", 3, str("a  ")) === Some(in))

Review Comment:
   **Nit (P3):** Every literal in this test is ASCII, and so is every value the 
new DELETE test stores, so characters, UTF-8 bytes and UTF-16 code units 
coincide and a counting mistake in the conversion would go unnoticed. The 
conversion counts characters (`numChars` at PredicateUtils.scala:175 and 182), 
as read-side padding does. If line 182 counted UTF-8 bytes instead, the `In` 
for any non-ASCII literal would miss matching stored values, such as the fully 
padded one. Counting Java `String` length there, which the line invites because 
it already builds the values from `p.toString`, would do the same for 
characters outside the Basic Multilingual Plane. A V1 DELETE or overwrite would 
then silently keep rows it should remove, and none of these assertions would 
fail. A character outside that plane catches both mistakes, since it takes four 
UTF-8 bytes and two UTF-16 code units, while a character such as U+00E9 catches 
only the byte mistake:
   
   ```suggestion
       assert(toV1("=", 3, str("a  ")) === Some(in))
       // U+1F600 is one character, but 4 bytes in UTF-8 and 2 chars in UTF-16
       val e = "\uD83D\uDE00"
       assert(toV1("=", 3, str(e + "  ")) === Some(In("c", Array(e, e + " ", e 
+ "  "))))
   ```
   
   See **Shared repair plan 1** in the review body.



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