You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@sedona.apache.org by "jiayuasu (via GitHub)" <gi...@apache.org> on 2026/09/23 06:58:11 UTC

[I] Casting a sliced run-end-encoded array reads values from the wrong offset [sedona-db]

jiayuasu opened a new issue, #1345:
URL: https://github.com/apache/sedona-db/issues/1345

   ## Summary
   
   Casting a sliced run-end-encoded (REE) array reads values from the start of its runs instead of from the slice offset, so the result silently contains another row's values.
   
   ## Reproduce
   
   Everything below happens inside the engine (no slice crosses the C data interface):
   
   ```python
   import pyarrow as pa
   import sedonadb
   
   sd = sedonadb.connect()
   ree = pa.RunEndEncodedArray.from_arrays(
       pa.array([1, 2, 3], pa.int32()), pa.array([10, 20, 30], pa.int64())
   )
   sd.create_data_frame(pa.table({"n": ree})).to_view("ree_full")
   
   sd.sql("SELECT CAST(n AS BIGINT) AS x FROM ree_full").to_arrow_table()
   # x: [10, 20, 30]  -- correct
   
   sd.sql(
       "SELECT CAST(n AS BIGINT) AS x FROM (SELECT n FROM ree_full LIMIT 1 OFFSET 1)"
   ).to_arrow_table()
   # x: [10]  -- expected [20]
   ```
   
   The same happens for an array that arrives already sliced from Python (`ree.slice(1, 1)`, logically `[20]`). Selecting the column *without* a cast returns it intact (offset 1, value 20), and every decoding path returns the wrong row: `CAST` from SQL and from the expression API, `arrow_cast(n, 'Int64')`, and `nvl(n, 0)`. The value type does not matter; durations and timestamps behave the same way.
   
   ## Where it might be
   
   The arrow-rs kernel used here (`arrow-cast` 58.4.0, `run_end_encoded_cast` in `src/cast/run_array.rs`) accounts for `run_array.offset()` when it expands a run array to its logical form, so the offset may be dropped before the cast is reached, or a different path may be taken. I haven't confirmed which with a standalone arrow-rs test.
   
   ## Impact
   
   The result is silently wrong, not an error. Slicing is routine inside the engine (`LIMIT`/`OFFSET`, batch splitting, repartitioning), so any query that decodes an REE column can be affected. As a workaround, `sedonadb-geopandas` no longer decodes REE columns (#1344).
   


-- 
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: issues-unsubscribe@sedona.apache.org.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [I] Casting a sliced run-end-encoded array reads values from the wrong offset [sedona-db]

Posted by "jiayuasu (via GitHub)" <gi...@apache.org>.
jiayuasu closed issue #1345: Casting a sliced run-end-encoded array reads values from the wrong offset
URL: https://github.com/apache/sedona-db/issues/1345


-- 
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: issues-unsubscribe@sedona.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [I] Casting a sliced run-end-encoded array reads values from the wrong offset [sedona-db]

Posted by "jiayuasu (via GitHub)" <gi...@apache.org>.
jiayuasu commented on issue #1345:
URL: https://github.com/apache/sedona-db/issues/1345#issuecomment-5790555368

   Narrowed this down. The bug is in sedona-db, not arrow-rs or DataFusion:
   
   - **arrow-rs is correct.** A standalone crate against `arrow-cast` 58.4.0 (the version sedona-db uses) casts `slice(1, 1)`, `slice(1, 2)`, `slice(2, 1)`, and nested slices such as `slice(0, 2).slice(1, 1)` to Int64 correctly (`[20]`, `[20, 30]`, `[30]`, `[20]`). A round trip through arrow-rs's own C data interface (`to_ffi`/`from_ffi`), sliced before or after import, is also correct.
   - **Stock DataFusion is correct.** `datafusion` 54.0.0 from PyPI, with the same pyarrow array registered via `register_record_batches`, returns `[20]` for `SELECT CAST(n AS BIGINT) FROM (SELECT n FROM t LIMIT 1 OFFSET 1)`.
   - **sedona-db is correct for engine-built arrays.** A run-end-encoded column built in SQL with `arrow_cast(v, 'RunEndEncoded(...)')`, then sliced with `LIMIT 1 OFFSET 1` and cast, returns `[20]`.
   - **It is wrong only for arrays imported from Python** through `create_data_frame` (`import_arrow_array_stream` → `RecordBatchReaderProvider`), whether the slice happens in Python or in the engine. The uncast column still round-trips back to Python intact (offset 1, logical `[20]`); only decoding it inside the engine reads the wrong row.
   
   So the remaining difference is how a pyarrow-exported run-end-encoded batch looks once it's inside sedona-db's reader/provider path. Next step would be to dump the imported `RunArray`'s `offset`, `len`, and run-ends offset right after `ArrowArrayStreamReader` in `import_from.rs`, and again at the scan output, to see where the offset or run-ends slice goes astray.
   


-- 
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: issues-unsubscribe@sedona.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [I] Casting a sliced run-end-encoded array reads values from the wrong offset [sedona-db]

Posted by "jiayuasu (via GitHub)" <gi...@apache.org>.
jiayuasu commented on issue #1345:
URL: https://github.com/apache/sedona-db/issues/1345#issuecomment-5791171572

   Correction to the above: this is **already fixed on current main**, and the narrowing in my previous comment was run against a stale local build (from Aug 13), so its conclusion that the bug is in sedona-db's current import path is wrong.
   
   With the native extension rebuilt from current main, every case in the original report is correct: casting a sliced run-end-encoded array gives `[20]`, both for an array sliced in Python and for `LIMIT 1 OFFSET 1` inside the engine, and arithmetic and comparisons on the sliced column return the right rows. The bug does reproduce on the **released 0.4.1** wheel (`[10]`), so it was fixed somewhere between 0.4.1 and current main and will ship with the next release.
   
   I think this can be closed as fixed on main. (#1344 no longer relies on the engine behaving either way: it never expands a run-end-encoded column.)
   


-- 
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: issues-unsubscribe@sedona.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org