sdf-jkl opened a new pull request, #12488:
URL: https://github.com/apache/arrow-rs/pull/12488

   # Which issue does this PR close?
   
   - Relates to #4729.
   - Relates to #27.
   
   This is a proof of concept to make the idea concrete before opening a 
discussion issue. It is not meant to merge as is.
   
   # Rationale for this change
   
   arrow-rs treats dictionary encoding as a `DataType`, so a storage choice 
leaks into schemas: the same strings are `Utf8` in one batch and 
`Dictionary<Int32, Utf8>` in the next, and every kernel and downstream system 
has to handle both. #4729 discussed this in 2023. Folding dictionaries into the 
plain array types met strong objections there, and the follow-up, a logical 
type layer in DataFusion (apache/datafusion#12622), has stalled.
   
   This POC tries the other direction: plain arrays keep their exact layouts, 
and encodings become separate types behind one trait, with dictionary as one of 
them. An encoded array has a logical type and decodes to a plain Arrow array on 
demand. The caller can ask for a specific layout, such as `Utf8View` or 
`Dictionary<UInt16, Utf8>`, and the encoding decodes only as far as that layout 
needs. This is the same contract as Vortex's Arrow export 
(`execute_arrow(target: Option<&Field>)`), so logically typed systems could 
pass encoded arrays through arrow-rs without picking a physical type first.
   
   It also gives Parquet-encoded data an in-memory form without re-encoding. An 
ALP page stays as its page bytes, about 4x smaller than `Float64Array` at the 
median on the ALP paper's datasets, and decodes when read.
   
   ```rust
   // A dictionary array becomes a DictArray whose data_type() is Utf8, not 
Dictionary.
   let encoded = arrow_encoded::from_arrow(Arc::new(dict_array.clone()))?;
   
   // Decode to whatever layout the consumer wants.
   let view = encoded.decode(Some(&Field::new("s", DataType::Utf8View, true)))?;
   
   // Asking for the original layout reuses the key buffer and values array, no 
copy.
   let same = encoded.decode(Some(&Field::new("s", 
dict_array.data_type().clone(), true)))?;
   ```
   
   # What changes are included in this PR?
   
   A new experimental crate, `arrow-encoded`:
   
   - `EncodedArray` trait: `encoding()`, `data_type()` (the type `decode(None)` 
produces, never a dictionary), `len()`, `logical_nulls()`, `children()` for 
encodings built from other encoded arrays, zero-copy `slice()`, 
`decode(target)`, and `get_array_memory_size()`.
   - `logically_equal(a, b)`: true when two types hold the same logical values. 
Dictionary and run-end encoded types compare as their value type, the string, 
binary and variable-size list families compare equal across offset widths and 
views, decimals compare by precision and scale whatever their width, and nested 
types recurse. This is the check that decides which targets `decode` accepts, 
and it would fit in `arrow-schema` if the idea goes forward.
   - `validate_target` and `cast_to_target`: shared target handling for 
encodings. Casts run with `safe: false`, so a lossy cast is an error rather 
than silent nulls.
   - `PlainArray`: the identity encoding over any Arrow array without a 
dictionary in its type.
   - `DictArray`: integer keys over values that may themselves be encoded. 
Decoding to a matching `Dictionary<K, V>` reuses the buffers. Other targets 
decode the values straight to the target layout before gathering.
   - `from_arrow`: wraps any Arrow array in its encoding, `DictArray` for 
dictionaries and `PlainArray` otherwise.
   
   In `parquet`, behind a new `encoded_experimental` feature (included in 
`experimental`):
   
   - `parquet::arrow::encoded::ParquetAlpArray`: a `Float32` or `Float64` array 
backed by one ALP page's values section, with an optional `NullBuffer` placing 
the non-null values on rows. Slicing is zero-copy, and decoding a slice skips 
straight to the vectors it covers.
   
   A `DictArray` whose values are a `ParquetAlpArray` works as is, which shows 
encodings composing across crates.
   
   POC shortcuts and open questions:
   
   - `DictArray` re-validates its keys each time it builds an Arrow dictionary.
   - Extension type targets, including Variant, get no special handling yet. 
Run-end encoded targets go through `arrow-cast` and are untested.
   - There are no compute kernels. The ALP comparison kernel in #12469 is the 
first candidate.
   - Encoded arrays cannot go into a `RecordBatch`. How they would is the main 
question for a design discussion, and it is what deprecating 
`DataType::Dictionary` would need.
   - The new crate is not yet in the release scripts.
   
   # Are these changes tested?
   
   Yes. `arrow-encoded` has tests for the logical type predicate in both 
directions, target validation and casting, dictionary decoding to the same, 
other and plain layouts, the zero-copy case, slicing, nulls from both keys and 
values, and invalid keys. `ParquetAlpArray` is tested on f32 and f64 pages with 
NaN, -0.0, infinities and other exceptions, with nulls and slices across vector 
boundaries, on pages read back from an `ArrowWriter` file, and as the values of 
a `DictArray`. Deliberately breaking the row to value mapping and the zero-copy 
dictionary path each makes a test fail.
   
   # Are there any significant user-facing changes?
   
   No changes to existing APIs. New public items, all experimental:
   
   - `arrow-encoded`: `EncodedArray`, `EncodedArrayRef`, `EncodingId`, 
`PlainArray`, `DictArray`, `from_arrow`, `logically_equal`, `validate_target`, 
`cast_to_target`.
   - `parquet`: the `encoded_experimental` feature and 
`parquet::arrow::encoded::ParquetAlpArray`.
   
   # AI usage disclosure
   
   Claude Code helped explore prior art (#4729, the DataFusion logical types 
epic, Vortex's Arrow export) and wrote the code and tests under my direction. 
This draft is open for my own review of the diff before any discussion.
   
   🤖 Generated with [Claude Code](https://claude.com/claude-code)
   


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