feat: Don't re-emit unchanged dictionaries when writing streams; fix nested dictionary and file writing #928
Codecov Report
:x: Patch coverage is
74.64789% with 18 lines in your changes missing coverage. Please review.:white_check_mark: Project coverage is 78.55%. Comparing base (
d01fdbb) to head (a9a082c).| Files with missing lines | Patch % | Lines |
|---|---|---|
| src/nanoarrow/ipc/writer.c | 74.57% | 3 Missing and 12 partials :warning: |
| src/nanoarrow/ipc/encoder.c | 75.00% | 0 Missing and 3 partials :warning: |
<details><summary>Additional details and impacted files</summary>
@@ Coverage Diff @@
## main #928 +/- ##
==========================================
+ Coverage 78.45% 78.55% +0.09%
==========================================
Files 106 106
Lines 17064 17116 +52
Branches 2021 2043 +22
==========================================
+ Hits 13388 13445 +57
+ Misses 2432 2422 -10
- Partials 1244 1249 +5
</details>
:umbrella: View full report in Codecov by Harness.
:loudspeaker: Have feedback on the report? Share it here.
<details><summary> :rocket: New features to boost your workflow: </summary>
</details>
Thank you for this!
I will take a closer look in the next few days, but can I ask that the delta dictionary and/or appender piece be separated from the IPC write piece? All three are independently useful and I think it will be a smoother process if they're PRed separately as well.
Yep I'll break them out!
Rusty
Split completed as requested:
ArrowArrayAppendArrayView() functionality as a one-commit core-array PR.main and now contains only dictionary IPC encoding/writing (no decoder changes, appender, quadratic-delta helper, or automatic delta emission).Query-farm:feat/ipc-dictionary-delta-decoding; I will submit it after #930 merges so its upstream diff contains only decoder work.The original full branch is preserved as Query-farm:split/pr928-full.
I don't think we support views yet for IPC decoding at all yet? https://github.com/apache/arrow-nanoarrow/issues/824
For the PR that does contain this, a good pattern is to define a function that can fail and doesn't own any input pointers:
ArrowErrorCode ArrowIpcDoAppend(struct ArrowArray* combined, struct ArrowArrayView array_view, struct ArrowError* error) {
NANOARROW_RETURN_NOT_OK(...);
return NANOARROW_OK;
}
then here you can just check once and release the temporary
ArrowErrorCode result = ArrowIpcDoAppend(...);
if (result != NANOARROW_OK) {
// release stuff
return result;
}
Can you reuse enum ArrowType for this one?
I think we can skip this test unless there's something special about rejecting a type in the decoder implementation
The usual streaming loop should be using ArrowIpcDecoderDecodeArrayViewWithDictionaries(), which in a perfect world never was shared (however, it probably had a shared backing buffer on its first decode because most decoding happens from a shared buffer).
Rather than trying to unshare, can we check whether this array can be appended to (and if possibly delay the sharing of it until it is requested as an array that is not just a view?). Totally ok if not possible, but if so, we should punt on the trying to unshare piece and implement that in a follow up.
The same pattern applies here
This is a great and super useful change, but a separate one from the decoding or encoding of delta dictionaries
Just curious: what feature is needed to roundtrip the generated extension beyond what's here?
It would help to put the comment about canonicalizing the padding helping to minimize reemitting a dictionary here.
Other parts of nanoarrow typically avoid this by zeroing out the last byte of a bitmap when reserving but it is sometimes hard to guarantee that everywhere.
The same nested function strategy for avoiding these repeated result checks applies here, too
Rather than compare by value (possibly slower than just writing another dictionary to the stream), probably comparing the buffer pointers makes more sense. If it's a shared array it should be pointing to the same data.
You can use the nested function approach for shared cleanup (we don't use goto in nanoarrow, or at least we haven't yet)
I don't think that the writer should emit delta dictionaries in this way...this is a kind of calculation that application code should do, which may have access to things like C++ that can do it more efficiently.
In any case, emitting delta dictionaries is a third scope here for a separate PR
Thank you for this!
I think this PR contains 3 orthogonal changes:
I did a first pass on everything and I think we can get all these merged soon!
Removed. The delta decoding moved to #955, and there is no view-specific handling there.
Done in #955: ArrowIpcDictionaryConcatenate() can fail and doesn't own its inputs, and the caller checks once and releases the temporary.
Done in #955: the test parameters are now enum ArrowType values.
Removed in #955.
Punted. #955 drops the unshare helper (and the common/utils.c change) and always builds a new combined array, so each delta copies the dictionary. Appending in place when the dictionary isn't shared can come in a follow-up.
Same pattern applied in #955 (the reserve is inside the helper).
Agreed. It stays in this PR, which is now only the non-delta writer and encoder changes.
Nothing beyond this PR: the file's dict_exts column is a dictionary-encoded extension type, and the test previously wrote with ArrowIpcWriterWriteArrayView(), which doesn't write dictionaries. Now that the roundtrip goes through ArrowIpcWriterWriteArrayStream() it passes, so I switched it (and the shared-dictionary file) to OK.
This code is gone. With comparison by buffer address there is no need to canonicalize bitmap padding.
Gone along with the materialized copies; there are no more result chains in the writer.
Done: dictionaries are now compared by shape and buffer addresses, recursively. To keep that safe, WriteArrayStream() holds the previous batch until the next one is written, so a released buffer's address can't be reused for different values mid-comparison. There's a test with a producer that recycles memory this way. WriteDictionaryBatch() only sees a borrowed view and always writes.
No more goto. The only shared cleanup is in the existing outer WriteArrayStream() function.
Agreed. Delta emission is removed from the writer entirely. Applications that want deltas can write them with ArrowIpcWriterWriteDictionaryBatch(..., is_delta = 1, ...), so I'm not planning a separate PR for it.
Thanks for the thorough review! I've split this as you suggested:
DICTIONARY_REPLACEMENT, dictionary batch metadata and file mode. It is rebuilt from current main and contains no decoder or delta changes.ArrowIpcWriterWriteDictionaryBatch(..., is_delta = 1, ...) covers that.The two PRs are independent and can be reviewed and merged in either order. I've replied to each inline comment with where it was addressed.
I think the only exception here is for StringView or BinaryView, where its final buffer is a buffer of sizes that I think is typically generated on each export and may not be the same.
(IPC for nanoarrow doesn't currently support this but someday it will and is probably worth fixing now)
Codex flagged for me here that ENOTSUP would be more consistent with the other things that can't be done in IPC file writing, although I don't personally mind either one
Thank you!
Two things probably worth fixing here, plus possibly worth a merge of main since there's one IPC-related fix there since this was last pushed.
Good catch. ArrowIpcWriterArraysShareBuffers() now gets the ArrowArrayView alongside the arrays (for the storage types) and skips the last buffer of a BinaryView/StringView array. It still compares the views and variadic data buffers, which hold the values. I noted this in the WriteArrayStream() docs too. I couldn't add a test that reaches it, because the encoder can't write view types yet.
Switched to ENOTSUP, to match the delta case right above it, and updated the docs for WriteDictionaryBatch() and WriteArrayStream() and the two tests.
Thanks! Both comments are addressed, and I've merged main (which includes #960 and #955). ctest passes locally (397 tests), and the writer tests are clean under ASan/UBSan.
Sorry, this needs a merge of main 😬 . I tried to fix push a fix but GitHub wouldn't let me 🙁
@paleolimbot no problem, I'll do it after lunch.
paleolimbot
rustyconover
This PR improves dictionary writing in the IPC writer/encoder. Per review, it has been split: delta dictionary decoding is now #955, and the writer no longer computes or emits delta dictionaries (applications can still write deltas explicitly with
ArrowIpcWriterWriteDictionaryBatch(..., is_delta = 1, ...)).ArrowIpcWriterWriteArrayStream()no longer writes every dictionary before every batch. A dictionary is skipped when it has the same length/offset/null count and points to the same buffers (recursively, including nested dictionaries) as the previous batch's dictionary — e.g., when the producer shares one dictionary across batches, as the nanoarrow IPC reader does. Comparison is by address rather than by value. To make that safe (a released buffer's address can't be reused for different values while it is being compared), the writer keeps the previous batch alive until the next one is written.ArrowIpcWriterWriteDictionaryBatch()only sees a borrowed view and always writes.ArrowIpcEncoderSetDictionaryReplacement(); the writer declaresDICTIONARY_REPLACEMENTin streams, and the reader no longer rejects streams that declare it.ArrowIpcEncoderSetMessageMetadata()metadata to DictionaryBatch messages.EINVAL; deltas still returnENOTSUP). Unchanged dictionaries fromWriteArrayStream()are skipped.Tests cover a shared dictionary being written once (stream and file), changing a nested dictionary re-emitting its parent, and a producer that recycles a released dictionary's memory for different values. Locally (aarch64 Linux, Debug, ZSTD/LZ4), ctest passes with and without Arrow C++ 21.0.0, and the writer/encoder/reader/files tests are clean under valgrind.
🤖 Generated with Claude Code