Repository navigation
fix(duck): size the newline-delimited read bounds from the longest line - #67
Merged
Merged
Conversation
The edges reader sized maximum_object_size from the largest edges.jsonl in the statement, plus a quarter and one MiB. A newline-delimited file's objects are its lines, and DuckDB allocates every read buffer at twice the bound, so one large file set the buffers of every file in a lake batch. On the host's data a batch of 512 sessions holding the 120 MB edges file of session a4916adb read all 512 files with 302 MB buffers. That read alone needed 2,046 MiB at four threads on DuckDB 2.0.0 (1,342 MiB on 1.5.6) under the lake writer's 2 GiB cap, so `lake rebuild` and `lake verify` ran out of memory at registry.py:904. DuckDB 2.0 scans each file of a list as its own parallel scan (read_single_json_file.cpp) and opens files ahead of the scan, so more of those buffers are live at once than on 1.5. The edges and session_events readers now take their bound from the longest line. A file that fits under DuckDB's 16 MiB default with its headroom counts at its size and isn't read; a larger one is read once, a chunk at a time, by the new stored_artifacts.longest_line. The longest line in the 13,071 edges files of the copied corpora (1.9 GB) is 960 bytes, and in their session_events files 8,913 bytes, so the bound is DuckDB's default in practice. At that bound a 120 MiB file of edge lines plus 511 small ones reads in 87 MiB at four threads, against 2,141 MiB at the file-sized bound. The trajectory reader keeps the file-sized bound, because a trajectory.json is one document. A new lake test rebuilds and verifies an eight-session corpus holding a 64 MiB edges.jsonl under a 448 MiB writer cap. It needs 207 MiB with this change and 972 MiB without it, and on main it fails with the same out-of-memory error at registry.py:904. New bound tests cover the line rule, and the spy test now expects one file bound and two line bounds. The docs citations into registry.py move with it.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
On DuckDB 2.0.0,
lake rebuildandlake verifyrun out of memory on the host's real data, inside the edgesread_jsonatregistry.py:904. atif-sql 0.4.0 (DuckDB 1.5.6) passes both on the same data. One commit fixes it.Cause
The edges reader sized
maximum_object_sizefrom the largestedges.jsonlin the statement, plus a quarter and 1 MiB. A newline-delimited file's objects are its lines, though, and DuckDB allocates every read buffer at twice the bound (buffer_capacity = maximum_object_size * 2injson_scan.cpp, the same in 1.5.6 and 2.0.0). So one large file set the buffers of every file in its lake batch. The interactive corpus holds a session whoseedges.jsonldecodes to 120 MB, so its batch of 512 sessions read all 512 files with 302 MB buffers.2.0 holds more of those buffers at once. Its
read_jsonover a file list is a multi-file wrapper around a single-file scan (read_single_json_file.cpp). Every file gets its own scan state, withenable_parallel_scanstrue because one file is fewer than the thread count, and files are opened ahead of the scan. On 1.5, a list at least as long as the thread count read each file whole, with one buffer a thread.Measured on the real snapshot at four threads (the lake writer's setting), the smallest
memory_limitthat read that batch's edges was 1,342 MiB on 1.5.6 and 2,046 MiB on 2.0.0. The writer's cap is 2,048 MiB, and the writer holds other tables while it reads, so the load failed. A batch size of 64 didn't help, because the bound and not the file count sets the buffer size.Fix
The edges and
session_eventsreaders now take their bound from the longest line (_line_size_bound). A file that fits under DuckDB's 16 MiB default with its headroom counts at its own size and isn't read. A larger one is read once, a chunk at a time, by the newstored_artifacts.longest_line. The longest line across the 13,071edges.jsonlfiles of the snapshot (1.9 GB decoded) is 960 bytes. Across theirsession_events.jsonlfiles it's 8,913 bytes. So in practice the bound is DuckDB's default, and a read's buffers no longer grow with its largest file. The trajectory reader keeps the file-sized bound, because atrajectory.jsonis one document.This keeps the 2 GiB writer cap. With the line bound the real 512-session batch needs 79 MiB (it needed 2,046 MiB before), so a larger cap would only hide the next oversized bound.
Tests
TestWriterMemoryCap(test_lake.py) rebuilds and then verifies an eight-session corpus holding a 64 MiBedges.jsonl, under a 448 MiB writer cap. It needs 207 MiB with the fix and 972 MiB without it. On main's source it fails withOutOfMemoryException: failed to allocate data of size 165.3 MiB (340.8 MiB/448.0 MiB used)atregistry.py:904, the same line as the shadow failures. With the fix it passes in about 2.4 s.TestLineSizeBoundcovers the line rule: a large file of short lines stays at the floor, a long line gets the headroom, the longest line of any file wins, a.zstfile is measured decompressed, a file the floor already covers isn't read, the 1 GiB cap holds, and a missing file counts as zero.test_longest_line_spans_read_chunkscovers lines that straddle the 1 MiB read chunks, a last line with no newline, and an empty file, plain and compressed.The docs citations into
registry.pymove with the code (docs:citations:fix). The contract map now says which bound each reader uses.Evidence
On loopbox
duckdb2-20261025(m8a.4xlarge, 64 GiB), tree at a356141, venv frommise run install:ci(duckdb 2.0.0):mise run --force check: exit 0. lint, ty and pyright (0 errors), lint:imports (7 contracts kept), docs:prose, security:vex:check, and test: 2695 passed, 3 skipped, 1 xfailed in 167 s.mise run docs:install && mise run docs:gate: exit 0. docs:citations reports 0 problems in 2,468 citations, and docs:links 0 broken of 1,980 internal links.Shadow rerun on the real snapshot
On loopbox
duckdb2-shadow-20261025(m8a.8xlarge), with the scripts in the job'sshadow/setup/(phaseJ.sh). Every run used a fresh reflink copy of the 2026-10-25 snapshot (five corpora, 13,071 sessions) under a systemd scope pinned to 16 CPUs, with 1.5 rerun back to back. The fix build is this branch's tree fromuv sync --frozen, with the sameuv.lockas main.lake verify, untouched lake (110G)lake rebuild(24G)lake verifyof the rebuilt lake (110G)memory_limitfor the real 512-session edges batch is now 79 MiB, down from 2,046 MiB. The biggest single file alone needs 75 MiB, down from 639 MiB.heavy09_regex_opsdiffers on both corpora, with the same hashes as the first 1.5-against-2.0 run. That's 2.0's partial-match~: 0 matches on 1.5 against 16,406 on 2.0, the same asregexp_matches. No SQL text here uses~.skill05_perceived_countsmatches on every count, and itsavg_confidencediffers only in the 16th digit. It differs the same way between two runs of this branch, so that's summation order.