Skip to content

fix: render Thrift complex values in linear time - #597

Open
vuanhphung wants to merge 1 commit into
mainfrom
fix/issue-596-complex-type-rendering
Open

vuanhphung wants to merge 1 commit into
mainfrom
fix/issue-596-complex-type-rendering

Conversation

@vuanhphung

@vuanhphung vuanhphung commented Nov 26, 2026 •

Copy link
Copy Markdown
Collaborator

Fix quadratic string concatenation when Thrift renders ARRAY, MAP and STRUCT results. Containers append recursively into one buffer; nested containers, including complex MAP keys, append to the caller's buffer, so no intermediate strings are built (this also covers #353's root cause). Each top-level container reuses its render buffer only while its capacity is at most 64 KiB and clears it on batch reload and release, so each complex column keeps at most 64 KiB and a large value no longer stays live after it is returned. Values whose render buffer grows past 64 KiB (from about 57 KB of ARRAY output, because append overshoots) regrow it on every row, which stays linear. Output is byte-for-byte unchanged, and scalar columns don't reach the changed code.

Verified 239 golden shape/type cases, including MAP with ARRAY and STRUCT keys at the top level and inside an ARRAY, against this branch and against origin/main's renderer. New tests cover a large-then-small value per shape, a 50 KiB complex map key (the key container keeps no buffer; live heap after GC grows by one top-level buffer of at most 64 KiB), a value under 64 KiB that grew into a larger buffer, small values after runs of large ones, and rendering after an error in list elements, map keys (scalar and complex) and map values; the 5000-wide guard stays under 0.72 MB/render (16 MiB ceiling). Memory tests assert retained capacities directly and pass 100 of 100 fresh runs under -covermode=atomic. Live heap after GC, main vs this PR: 64 MiB value then small ones 0–224 B vs 16–128 B; 50 KiB complex key 0 B vs 57,344 B (one top-level buffer of at most 64 KiB); a STRUCT with four 40 KiB-keyed maps 16 B vs 96 B. make lint, make test, -race -shuffle=on and the kernel-tagged tests pass; -gcflags=-m shows only the result string and its any boxing escaping (plus the complex-key string, which replaces the string keys.Value built before). Live Thrift complex rendering, an over-64-KiB-then-small query, complex-keyed maps (identical text on main and this PR) and kernel/Thrift parity pass.

Benchstat medians, main → this PR, 18 samples per version (three rotated fresh-process rounds of -count=6 -benchtime=200ms, GOMAXPROCS=2). These are steady-state numbers for a warm container: every case is faster (p<0.001), and B/op is equal at width 0 and lower everywhere else. The first complex value in each record batch starts from an empty buffer and can cost a few bytes more than main at widths 0–2.

Width ARRAY MAP STRUCT STRUCT
0 30.1 → 24.6 ns, 18 → 18 B 29.2 → 23.7 ns, 18 → 18 B 28.6 → 23.3 ns, 18 → 18 B 121.3 → 39.1 ns, 80 → 32 B
1 107.1 → 78.5 ns, 40 → 32 B 156.3 → 100.9 ns, 64 → 40 B 139.8 → 86.7 ns, 64 → 40 B 203.8 → 95.0 ns, 96 → 40 B
2 213.1 → 138.2 ns, 112 → 64 B 316.7 → 174.8 ns, 176 → 80 B 290.1 → 147.6 ns, 184 → 80 B 334.2 → 155.8 ns, 240 → 80 B
5 588.6 → 311.6 ns, 584 → 152 B 868.8 → 394.0 ns, 959 → 168 B 783.0 → 316.8 ns, 976 → 168 B 749.0 → 321.6 ns, 824 → 168 B
50 13.9 → 2.9 µs 24.5 → 3.7 µs 25.2 → 3.1 µs 15.2 → 2.9 µs
1536 6.24 ms → 81.9 µs 13.05 ms → 119.0 µs 20.57 ms → 94.0 µs 7.72 ms → 82.5 µs
5000 58.6 ms → 351 µs, 429 MiB → 502 KiB 128.6 ms → 488 µs, 884 MiB → 699 KiB 133.7 ms → 390 µs, 920 MiB → 662 KiB 74.3 ms → 349 µs, 429 MiB → 502 KiB

Once the render buffer grows past 64 KiB (ARRAY width about 3400 and up, from about 57 KB of output), it isn't kept, so each row grows a new one: at widths 3800–5000, 372–699 KiB per render, about 6x the output including the result string, and more than 99.8% below main. MAPs with ARRAY keys are 27–61% faster than main.

Cross-driver state

Driver State Notes
JDBC Supported Native wrappers use StringBuilder; default string MAP formatting also uses a builder. EnableComplexDatatypeSupport=0; DatabricksArray.java:434, DatabricksMap.java:238, DatabricksStruct.java:282, ComplexDataTypeParser.java:676 @b75bdb8.
Python Supported Native containers via Arrow/pandas or as_py(); _disable_pandas=False. src/databricks/sql/result_set.py:97 @75a0d5d. No growing JSON string loop.
Go Not supported → Supported Existing string-valued database/sql surface and scalar formatting unchanged; recursive append with at most 64 KiB kept per complex column replaces concatenation.
Node.js Supported Native objects/arrays via recursive toJSON() / map(). lib/result/ArrowResultConverter.ts:455 @d76fc08. No growing JSON string loop.
Kernel Not applicable Go's internal/arrowscan already renders linearly.

Fixes #596

This pull request and its description were written by Isaac.

@vuanhphung
vuanhphung marked this pull request as ready for review November 26, 2026 18:27

@oss-reviewer-bot oss-reviewer-bot Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔀 Cross-reviewed: two independent reviews (Claude Opus 5.5 · GPT-6.1 Sol), checked by a judge model (Claude Sonnet 5.5).

Verdict: 1 Low

Looks good, with 1 low concern. I traced the recursive append path against the old concatenation code. Null handling, slice offsets, map key quoting, complex map keys and error propagation all match, and the golden tests cover each of these.

Comment thread driver_e2e_test.go Resolved

@oss-reviewer-bot oss-reviewer-bot Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔀 Cross-reviewed: two independent reviews (Claude Opus 5.5 · GPT-6.1 Sol), checked by a judge model (Claude Sonnet 5.5).

✅ No issues identified.

@vuanhphung
vuanhphung force-pushed the fix/issue-596-complex-type-rendering branch from b5f00a4 to 3c548d6 Compare November 26, 2026 18:45

@oss-reviewer-bot oss-reviewer-bot Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔀 Cross-reviewed: two independent reviews (Claude Opus 5.5 · GPT-6.1 Sol), checked by a judge model (Claude Sonnet 5.5).

Verdict: 1 Nit

Looks good: no blocking issues. The rewrite to a single append buffer gives the same output as the old code, including null handling, offsets, and escaped keys and field names. The complex flag is only set on list, map and struct containers, so the complexValueAppender assertion can't panic.

Other findings

  • ⚪ Nit — The parameter complex in appendColumnValue shadows Go's built-in complex. It compiles and behaves correctly, but it is a style nit. Rename it to nested or isComplex. (Found by Claude Opus 5.5 · confirmed by the judge)

@vuanhphung

Copy link
Copy Markdown
Collaborator Author

Re the naming nit: complex is a boolean flag in a scope that never calls Go's numeric complex built-in. This is naming-only, and build/vet pass, so I’m leaving the existing name unchanged.


This comment was generated with GitHub MCP.

@vuanhphung
vuanhphung force-pushed the fix/issue-596-complex-type-rendering branch from 3c548d6 to 485590d Compare November 26, 2026 19:09

@oss-reviewer-bot oss-reviewer-bot Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔀 Cross-reviewed: two independent reviews (Claude Opus 5.5 · GPT-6.1 Sol), checked by a judge model (Claude Sonnet 5.5).

✅ No issues identified.

@oss-reviewer-bot oss-reviewer-bot Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔀 Cross-reviewed: two independent reviews (Claude Opus 5.5 · GPT-6.1 Sol), checked by a judge model (Claude Sonnet 5.5).

✅ No issues identified.

@oss-reviewer-bot oss-reviewer-bot Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔀 Cross-reviewed: two independent reviews (Claude Opus 5.5 · GPT-6.1 Sol), checked by a judge model (Claude Sonnet 5.5).

✅ No issues identified.

@vuanhphung
vuanhphung force-pushed the fix/issue-596-complex-type-rendering branch from 3e3ae23 to f77ef01 Compare November 27, 2026 21:43

@oss-reviewer-bot oss-reviewer-bot Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔀 Cross-reviewed: two independent reviews (Claude Opus 5.5 · GPT-6.1 Sol), checked by a judge model (Claude Sonnet 5.5).

Verdict: 1 Low

Looks good overall, with 1 low concern. The append-based rendering produces the same output as before, nested containers never touch their own buffers, and the buffers are cleared on SetValueArray and Release. The tests and the e2e query exercise the native complex path, which is on by default.

@vuanhphung
vuanhphung force-pushed the fix/issue-596-complex-type-rendering branch from f77ef01 to 02573a1 Compare November 27, 2026 23:20

@oss-reviewer-bot oss-reviewer-bot Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔀 Cross-reviewed: two independent reviews (Claude Opus 5.5 · GPT-6.1 Sol), checked by a judge model (Claude Sonnet 5.5).

✅ No issues identified.

@vuanhphung
vuanhphung force-pushed the fix/issue-596-complex-type-rendering branch from 02573a1 to ac22c3e Compare November 28, 2026 00:50

@oss-reviewer-bot oss-reviewer-bot Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔀 Cross-reviewed: two independent reviews (Claude Opus 5.5 · GPT-6.1 Sol), checked by a judge model (Claude Sonnet 5.5).

✅ No issues identified.

Render nested Thrift ARRAY, MAP and STRUCT values into a shared byte
buffer instead of repeatedly copying growing strings, making wide
results linear while preserving their existing text representation.

Fixes #596

Signed-off-by: Vu Anh Phung <vu.phung@databricks.com>
Co-authored-by: Isaac <no-reply@databricks.com>
@vuanhphung
vuanhphung force-pushed the fix/issue-596-complex-type-rendering branch from ac22c3e to 867b4ce Compare November 28, 2026 01:42

@oss-reviewer-bot oss-reviewer-bot Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔀 Cross-reviewed: two independent reviews (Claude Opus 5.5 · GPT-6.1 Sol), checked by a judge model (Claude Sonnet 5.5).

✅ No issues identified.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Thrift path renders ARRAY/MAP/STRUCT with repeated string concatenation: time and memory grow quadratically with the element count

1 participant