Skip to content

fix(go/adbc/sqldriver): return owned copies of string/binary values - #4625

Merged
amoeba merged 2 commits into
apache:mainfrom
iremcaginyurtturk:fix-sqldriver-string-copy
Jul 29, 2026
Merged

amoeba merged 2 commits into
apache:mainfrom
iremcaginyurtturk:fix-sqldriver-string-copy

Conversation

@iremcaginyurtturk

@iremcaginyurtturk iremcaginyurtturk commented Jul 29, 2026 •

Copy link
Copy Markdown
Contributor

Problem

rows.Next returned array.String.Value (and LargeString/Binary/LargeBinary) directly as the driver.Value. Those are zero-copy values that alias the underlying Arrow data buffer. database/sql retains the driver.Values after Next returns — and after the underlying record batch (and its buffer) is released. For ADBC results the batch buffers are C-owned, so once the batch is released the value reads back as garbage.

Minimal reproduction: reading a VARCHAR from a DuckDB ADBC query through this sqldriver returns a corrupted string (e.g. "\xb6|:u\x14\x7f\x00", stale memory) instead of the actual text. Reading the same result directly at the Arrow level (before release) is correct, which localizes the problem to rows.Next handing back an alias rather than an owned value.

What changed

Copy string/binary values into Go-owned memory (strings.Clone / bytes.Clone) so they outlive the record batch.

Why it surfaced now

The zero-copy return predates this (it's been in rows.Next since the original database/sql wrapper, #97). It was surfaced by the arrow-go v18.7 bump — specifically apache/arrow-go#793, which replaced the nativeCRecordBatchReader finalizer with deterministic atomic Retain/Release. C memory is now freed promptly when the batch is released instead of lazily at GC, so the latent aliasing became a use-after-free.

array.String.Value (and LargeString/Binary/LargeBinary) returns a value
that aliases the Arrow data buffer (zero-copy). rows.Next handed those
straight to database/sql, which retains driver.Values after Next returns
and after the underlying record batch is released. For ADBC results the
record's buffers are C-owned, so once the batch is released the value
reads back as garbage.

Copy string/binary values into Go-owned memory before returning them so
they outlive the batch.

Surfaced by the arrow-go v18.7 bump, which changed buffer release timing
enough to make the latent aliasing fatal; the zero-copy return predates it.

Signed-off-by: iremcaginyurtturk <[email protected]>
Fixes the golangci-lint SA1019 failure (array.NewRecord is deprecated).

Signed-off-by: iremcaginyurtturk <[email protected]>
@iremcaginyurtturk

Copy link
Copy Markdown
Contributor Author

Hi @lidavidm 🙏 I’m blocked downstream by this fix. Could you approve the CI run and take a quick look when you have a moment?

@amoeba

amoeba commented Jul 29, 2026

Copy link
Copy Markdown
Member

Hi @iremcaginyurtturk, I approved CI and @lidavidm can merge later today. Will you be unblocked after merge or do you need a new release to pick up this fix?

@iremcaginyurtturk

Copy link
Copy Markdown
Contributor Author

Thanks @amoeba, and @lidavidm for reviewing! 🙏 Merge is enough for now. I can pin the merge commit, so no new release required to unblock me. A tagged release would be a nice-to-have later for a cleaner pin, but no rush at all.

@amoeba
amoeba merged commit 74d5395 into apache:main Jul 29, 2026
37 of 41 checks passed
@amoeba

amoeba commented Jul 29, 2026

Copy link
Copy Markdown
Member

Merged, thanks!

iremcaginyurtturk added a commit to bruin-data/ingestr that referenced this pull request Jul 29, 2026
Pins github.com/apache/arrow-adbc/go/adbc to the merged commit of
apache/arrow-adbc#4625, which fixes the zero-copy string/binary values
returned by the sqldriver. Under arrow-go v18.7 (required transitively by
the iceberg-go pin) those aliased the freed Arrow buffer and read back as
garbage, breaking every DuckDB/ADBC read (SchemaEvolution, CDC merge,
stress convergence, ...). Regenerate licenses.lock.yml for the bump.
@lidavidm lidavidm added this to the ADBC Libraries 25 milestone Oct 20, 2026
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.

3 participants