q8webmaster opened a new issue, #129:
URL: https://github.com/apache/paimon-trino/issues/129

   ### Summary
   
   When a primary-key table has `deletion-vectors.enabled=true` and 
`file.format=orc`, a query
   whose filter is pushed down to the ORC reader can return wrong results: rows 
that are live are
   dropped, and rows that were deleted or updated (old versions) are returned. 
Queries without a
   pushed-down filter are correct.
   
   ### Cause
   
   `TrinoPageSourceWrapper` applies a file's deletion vector page by page. It 
takes the starting
   row position of each page from `source.getCompletedPositions()`, i.e. the 
number of rows the
   page source has returned so far:
   
   ```java
   int startPosition = (int) source.getCompletedPositions().orElseThrow();
   SourcePage sourcePage = source.getNextSourcePage();
   ...
   deletionVector.isDeleted(startPosition + pagePosition)
   ```
   
   A deletion vector is indexed by the row's position in the data file. The two 
numbers are only
   equal when the reader reads every row. With a pushed-down predicate, Trino's 
ORC reader uses
   the stripe and row-group statistics to skip stripes and row groups 
(`TupleDomainOrcPredicate`
   is built from the split's domains in 
`TrinoPageSourceProvider#createDataPageSource`). After a
   skip, the number of rows returned so far is smaller than the file position, 
so every later row
   is checked against the wrong bit of the deletion vector.
   
   The same code is on `main` and in the open Trino upgrade PRs #125 and #127.
   
   ### Reproduction
   
   1. Create a primary-key table with one bucket, 
`deletion-vectors.enabled=true`,
      `file.format=orc` (ORC row index stride is 10,000 by default).
   2. Write ids `0 .. 49,999` (`id INT` primary key, `v INT`) in one commit, so 
one data file with
      five row groups sorted by `id` ends up on the top level. (Use values for 
`v` that do not
      compress to almost nothing, e.g. `id * 0x9E3779B1`; otherwise the next 
compaction finds the
      change file larger than this file and rewrites both instead of writing a 
deletion vector.)
   3. In a second commit, delete every id divisible by 7 and update every other 
id divisible by
      11 (`v = -v`). The compaction writes a deletion vector on the first file 
that marks rows in
      all row groups.
   4. Compare a pushed-down filter with the same filter hidden from pushdown:
   
   ```sql
   -- not pushed down: correct
   SELECT count(*), count(DISTINCT id), sum(id), sum(v) FROM t WHERE id + 0 > 
25000;
   -- pushed down: the ORC reader skips row groups 0 and 1 (ids 0 .. 19,999)
   SELECT count(*), count(DISTINCT id), sum(id), sum(v) FROM t WHERE id > 25000;
   ```
   
   | query | count(*) | count(DISTINCT id) | sum(id) | sum(v) |
   |---|---|---|---|---|
   | `id + 0 > 25000` (correct) | 21428 | 21428 | 803553571 | 1987230923 |
   | `id > 25000` on `main` | 21428 | **19805** | **803538954** | 
**-5755670142** |
   
   (`v` is a scrambled value per id in this reproduction, and updated rows 
store `-v`.)
   
   The row count happens to match here, but 1,623 ids are returned twice (the 
old version from
   the first file, whose deletion-vector bit was missed, plus the new version 
from the newer
   file), while live rows are dropped and deleted rows come back because the 
bits of other rows
   were applied to them (sum(id) and sum(v) differ).
   
   The same happens with filters on non-key columns whenever their row-group 
statistics let the
   reader skip data, e.g. an equality filter returning fewer rows than the same 
filter written as
   `col || '' = 'x'`.
   
   ### Affected
   
   - ORC data files read through the direct Trino ORC path 
(`TrinoPageSourceProvider` uses it for
     raw-convertible ORC splits) with a deletion vector and a filter that lets 
the ORC reader skip
     a stripe or row group.
   - Not affected: queries without such a filter, Parquet/Avro files (they go 
through Paimon's own
     reader), tables without deletion vectors.
   
   ### Workaround
   
   Hide the filter from pushdown (`col + 0 > x`, `col || '' = 'x'`), or disable 
the deletion
   vector / use Parquet.
   
   ### Fix
   
   Use the position of the page in the file instead of the rows returned so 
far. Trino's
   `OrcRecordReader#getFilePosition()` returns the file position of the first 
row of the last
   page; Trino's own Hive ACID reader uses it for the same purpose 
(`OrcDeletedRows`). A PR with a fix and a
   regression test follows.
   


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

Reply via email to