SEPURI-SAI-KRISHNA opened a new pull request, #60439:
URL: https://github.com/apache/spark/pull/60439

   ### What changes were proposed in this pull request?
   
   `SimpleMetricsCachedBatchSerializer.buildFilter` skips a cached batch for 
`StartsWith(a, l)` unless
   
   ```scala
   statsFor(a).lowerBound.substr(0, Length(l)) <= l && l <= 
statsFor(a).upperBound.substr(0, Length(l))
   ```
   
   This PR applies this pruning only when the collation of the column supports 
binary ordering. For any other collation the batch is read and the filter is 
evaluated on its rows, as for every predicate that `buildFilter` does not 
handle.
   
   ### Why are the changes needed?
   
   Caching a table changes the result of a `startswith` filter on a string 
column with a collation other than `UTF8_BINARY`: matching rows are dropped.
   
   ```sql
   CREATE TABLE t (a STRING COLLATE UNICODE_CI_AI, b STRING COLLATE UNICODE,
     c STRING COLLATE UTF8_LCASE, d STRING COLLATE UNICODE_CI) USING parquet;
   -- 'œuvre', 'été' with a decomposed first letter, 'İstanbul', 'final' with 
the fi ligature
   INSERT INTO t VALUES ('œuvre', 'été', 'İstanbul', 'final');
   
   SELECT count(*) FROM t WHERE startswith(a, 'oe');        -- 1
   SELECT count(*) FROM t WHERE startswith(b, 'ét');   -- 1
   SELECT count(*) FROM t WHERE startswith(c, 'i̇s');  -- 1
   SELECT count(*) FROM t WHERE startswith(d, 'fin');       -- 1
   
   CACHE TABLE t;
   
   SELECT count(*) FROM t WHERE startswith(a, 'oe');        -- 0
   SELECT count(*) FROM t WHERE startswith(b, 'ét');   -- 0
   SELECT count(*) FROM t WHERE startswith(c, 'i̇s');  -- 0
   SELECT count(*) FROM t WHERE startswith(d, 'fin');       -- 0
   ```
   
   With `spark.sql.inMemoryColumnarStorage.partitionPruning=false` the cached 
table returns 1 again.
   
   The check compares the first `length(l)` characters of the bounds of the 
batch with the literal. That is correct only if a string starts with `l` 
exactly when its first `length(l)` characters are `l`, and if strings are 
ordered by their characters. Both hold only for `UTF8_BINARY`. In the other 
collations a prefix can match a different number of characters:
   
   - `UNICODE_CI_AI`: `'œuvre'` starts with `'oe'`, but its first two 
characters are `'œu'`, which is greater than `'oe'`.
   - `UNICODE`: a decomposed `'é'` (e + U+0301) is equal to the precomposed 
one, so the first two characters of the value are only its first letter.
   - `UTF8_LCASE`: `'İ'` (U+0130) matches the two characters `'i'` + U+0307.
   - `UNICODE_CI`: the ligature `'fi'` matches `'fi'`.
   - `UTF8_BINARY_RTRIM`: the trailing space of the prefix `'a '` is ignored, 
so `'ab'` starts with it.
   
   The other predicates that `buildFilter` handles (`=`, `<`, `IN`, ...) are 
not affected, because the bounds of a batch are gathered with the collation of 
the column (`StringColumnStats`). The pruning by `StartsWith` is older than 
collations and was not restricted when they were added in 4.0.0. The default 
and the Arrow cache serializers share this code.
   
   ### Does this PR introduce _any_ user-facing change?
   
   Yes. A `startswith` filter on a cached string column with a collation other 
than `UTF8_BINARY` no longer drops matching rows. The released versions since 
4.0.0 are affected (reproduced on 4.0.3).
   
   ### How was this patch tested?
   
   A new test in `PartitionBatchPruningSuite` with the cases above and their 
reverses. It fails without the change. The existing tests of the suite still 
check that `StartsWith` prunes batches of a `UTF8_BINARY` column.
   
   Also ran `InMemoryColumnarQuerySuite`, `CachedBatchSerializerSuite`, 
`ArrowCachedBatchSerializerSuite` and `CachedTableSuite`.
   
   ### Was this patch authored or co-authored using generative AI tooling?
   
   Generated-by: Claude Code (Claude Fable 5.2)
   


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