Anirban-Chand commented on issue #13570:
URL: https://github.com/apache/gluten/issues/13570#issuecomment-6958412643

   Thanks @fabientra for the thorough verification across batch configurations 
and for answering all the questions in detail! Your findings and data points 
make the picture completely clear.
   
   ### Root Cause Analysis (RCA)
   
   The issue is confirmed to be an uninitialized memory defect in Velox’s 
buffer management:
   
   1. **The defect in `dwio::common::ensureCapacity` 
(`velox/dwio/common/BufferUtil.h`)**:
      When `ensureCapacity` is called with `preserveOldData = false` and 
`clearBits = true`, if the capacity needs to grow, it allocates a new buffer 
`newData`. Because `preserveOldData` is false, it does not copy old data into 
`newData`. However, it then only zeroes bytes starting from `oldSize` up to 
`newCapacity`:
      ```cpp
      if (clearBits && newCapacity > oldSize) {
        std::memset(data->asMutable<int8_t>() + oldSize, 0L, newCapacity - 
oldSize);
      }
      ```
      As a result, bytes `[0, oldSize)` in `newData` are left completely 
uninitialized, containing whatever recycled bits the memory allocator returned.
   
   2. **Trigger in `IcebergSplitReader::next` 
(`velox/connectors/hive/iceberg/IcebergSplitReader.cpp`)**:
      `IcebergSplitReader::next` calls:
      ```cpp
      dwio::common::ensureCapacity<int8_t>(
          deleteBitmap_, numBytes, connectorQueryCtx_->memoryPool(), 
/*preserveOldData=*/false, /*clearBits=*/true);
      ```
      When the scan batch size scales up (e.g., from ~2,500 rows based on 
initial row-size estimates to 4,096 rows after filtering via 
`maxFilteringRatio_`), `deleteBitmap_` expands. The uninitialized `[0, 
oldSize)` range in the new bitmap contains random set bits, which are 
interpreted as deleted row markers (`1 = deleted`). Consequently, those rows in 
that batch are silently dropped.
   
   3. **Why the behavior matched observations**:
      - **Silent loss**: Deletions are valid query operations, so no errors or 
task failures occur.
      - **Repeated patterns across runs**: Memory allocators often reuse pages 
with deterministic contents across runs on the same engine/allocator setup.
      - **Workarounds eliminate the bug**: Setting `preferredBatchBytes=1g` or 
`maxBatchSize=1024` forces a constant batch size throughout the split, so 
`deleteBitmap_` never reallocates, preventing uninitialized memory from ever 
entering the bitmap.
      - **Filter dependence**: Non-partition predicates trigger selectivity 
scaling (`maxFilteringRatio_`), which induces the batch size increase.
   
   ### Next Steps & Fix
   
   The root-level fix belongs in Velox (`BufferUtil.h`): when `preserveOldData` 
is `false`, any newly allocated buffer has zero valid preserved bytes, so 
`oldSize` must be treated as `0` to ensure `clearBits` zeroes out the full 
range `[0, newCapacity)`:
   
   ```diff
          if (preserveOldData) {
            std::memcpy(
                newData->template asMutable<uint8_t>(),
                data->as<uint8_t>(),
                oldSize);
   +      } else {
   +        oldSize = 0;
          }
   ```
   
   I am setting up a PR against upstream Velox with corresponding unit tests in 
`BufferUtilTest` covering `ensureCapacity` with `preserveOldData=false, 
clearBits=true`. Once merged/cherry-picked, we will sync it into Gluten.


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