Skip to content

fix: bind Int128/UInt128/Int256/UInt256 (big.Int) params as numeric values - #1922

Merged
must108 merged 7 commits into
mainfrom
polyglot/fix-bigint-bind-params
Sep 9, 2026
Merged

must108 merged 7 commits into
mainfrom
polyglot/fix-bigint-bind-params

Conversation

@polyglotAI-bot

Copy link
Copy Markdown
Collaborator

Description

Fixes #1917.

*big.Int / big.Int are the Go types behind ClickHouse Int128, UInt128, Int256 and UInt256. In the client-side SQL bind rewrite (?, $1, @name), formatValue() matched *big.Int on the case fmt.Stringer: arm and emitted it as a quoted string literal ('170141183460469231731687303715884105727'). The server then saw a String — toTypeName(?) returned String instead of a number — so type-inference contexts (arithmetic, function dispatch, toTypeName) broke. A big.Int passed by value was worse still: it fell through to fmt.Sprint and produced garbage struct text.

Emitting a bare decimal is not enough: ClickHouse infers an integer literal wider than 64 bits as Float64, losing precision (a WHERE on an Int128 column then matches nothing). The fix instead wraps the exact decimal in the narrowest wide-integer conversion that holds it — toInt128('..'), toUInt128('..'), toInt256('..') or toUInt256('..') — mirroring how the driver already binds times as toDateTime(...) and floats as cast(..., 'Float64'). This keeps both the exact value and an integer type. Server-side {name:Type} query parameters already declare the type, so there the bare decimal is sent unchanged.

Changes

  • bind.go: handle big.Int and *big.Int in formatValue() before the fmt.Stringer arm. New helpers formatBigInt / bigIntConvFunc pick the narrowest of Int128/UInt128/Int256/UInt256 that holds the value, or return an actionable error if it fits none. nil *big.Int → NULL.
  • bind_test.go: TestFormatBigInt (magnitudes, spill boundaries, out-of-range, both format modes, value + pointer), TestBindBigInt (all three placeholder styles, arrays, tuples, maps, nil, and contrast cases that must stay quoted: string, another fmt.Stringer, decimal.Decimal), TestBindBigIntQueryParameter (server-side {name:Type} path).
  • tests/issues/1917_test.go: integration test over Native + HTTP — parameter binds as a wide integer, round-trips exactly, matches an Int128 column, and works via server-side query parameters.
  • tests/std/1917_test.go: the same round-trip through the database/sql surface on both protocols.

Test

Verified against ClickHouse 26.5 that the new tests fail on main (*big.Int → '…' → String; big.Int value → fmt.Sprint garbage) and pass with the fix. toInt128('…') etc. confirmed to yield the exact value and the expected wide-integer type on the server. gofmt/go vet clean on changed files; full root-package unit suite green.

Pre-PR validation gate

  • Deterministic repro confirmed (tests fail on unpatched bind.go, pass with fix)
  • Root cause documented above
  • Fix targets the root cause
  • Test fails without fix, passes with fix
  • No existing tests weakened or broken
  • Convention compliance (AGENTS.md / CONTRIBUTING.md — CHANGELOG.md is auto-generated, not hand-edited; regression test under tests/issues/; t.Cleanup + version-gated server-side subtest)

…alues

A *big.Int (the Go type behind Int128/UInt128/Int256/UInt256) implements
fmt.Stringer, so client-side binding matched the `case fmt.Stringer:` arm in
formatValue and emitted the value as a quoted string literal. The server then
saw a String -- `toTypeName(?)` returned `String`, and server-side
`{name:Int128}` parameters failed to parse the quoted value.

Emitting the bare decimal instead is not enough: ClickHouse reads a bare decimal
literal wider than 64 bits as Float64, losing precision (a WHERE on an Int128
column would match nothing). So in SQL mode the value is wrapped in the narrowest
wide-integer conversion that holds it exactly -- toInt128/toUInt128/toInt256/
toUInt256('<decimal>') -- mirroring how times bind as toDateTime(...) and floats
as cast(..., 'Float64'). In query-parameter text mode the {name:Type} placeholder
already declares the type, so the bare decimal is sent as-is.

Also fixes a nil *big.Int binding as '<nil>' instead of NULL.

Fixes: #1917
Add a tests/std regression test that drives a big.Int bind parameter
through the database/sql surface (rebind -> bind) on both Native and
HTTP, and add tuple and map nesting cases to the unit test, so every
entry point and container type that reaches the big.Int bind path in
issue #1917 is asserted.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

Fixes client-side SQL placeholder binding for big.Int/*big.Int (ClickHouse Int128/UInt128/Int256/UInt256) so values are emitted as correctly-typed wide integers instead of quoted strings (or fmt.Sprint struct output), preserving both type inference and precision.

Changes:

  • Extend formatValue() to detect big.Int/*big.Int before the fmt.Stringer path and format them via the narrowest to(Int|UInt)(128|256)('...') conversion in SQL mode.
  • Add unit tests covering formatting/binding behavior across placeholder styles and query-parameter mode.
  • Add integration tests for native + HTTP and database/sql surfaces to validate server-side typing and round-trips.

Reviewed changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated 3 comments.

File Description
bind.go Adds big.Int formatting/conversion selection logic for SQL-rewrite binding and query-parameter text mode.
bind_test.go Adds unit tests for wide-int formatting/binding behavior, including contrast cases for other fmt.Stringer types.
tests/issues/1917_test.go New integration test validating server-side inferred type, exact round-trip, and column match behavior.
tests/std/1917_test.go New database/sql integration test validating server-side inferred type and exact round-trip.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread tests/issues/1917_test.go Resolved
Comment thread tests/std/1917_test.go Resolved
Comment thread bind.go
Comment on lines +779 to +782
}
}
return "", fmt.Errorf("big.Int value %s is out of range for Int128, UInt128, Int256 and UInt256", v.String())
}

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Thanks — I considered truncating but kept the full value. This branch only fires for values outside [Int256 min, UInt256 max] (>78 digits); it's a single one-shot allocation of a string the caller already holds as a big.Int (no loop, no repeated growth), and AGENTS.md asks that errors be "as actionable as possible" — the exact offending value is the most actionable thing to print. A caller who builds a pathologically huge big.Int has already allocated it, so truncating the echo wouldn't meaningfully bound memory. I'd rather not introduce a magic truncation threshold for that edge, but happy to reconsider if you feel strongly.

Comment thread bind.go Resolved
Comment thread bind_test.go Resolved
Comment thread tests/std/1917_test.go Resolved
@github-actions

github-actions Bot commented Jul 28, 2026 •

Copy link
Copy Markdown
Contributor

🤖 Claude review

Fixes #1917 by binding big.Int / *big.Int (the Go types behind Int128, UInt128, Int256, UInt256) as toInt128('…')-style wide-integer conversions in client-side SQL binding instead of a quoted string, while leaving the server-side {name:Type} path as a bare decimal. Re-review after the previous rounds: every earlier thread is addressed in the current code, and I found no new issues. Approve.

Verified by tracing:

  • *big.Int is matched before the fmt.Stringer arm, so it no longer quotes; big.Int by value no longer falls through to fmt.Sprint; nil *big.Int is NULL (and \N at the top level of a query parameter via isNilParamValue).
  • bigIntConvFunc boundaries are correct at every spill point (Int128 max+1 → UInt128, UInt128 max+1 → Int256, Int128 min-1 → Int256, Int256 max+1 → UInt256), and out-of-range values return an actionable error rather than a bad literal.
  • commonBigIntConvFunc unifies flat slices, Go arrays, []any, and ArraySet to one conversion; when no single type fits it falls back to per-element formatting, so the array error surfaces from the server rather than being masked. The one-level-deep scope for maps/nested arrays/tuples is documented in the doc comment as requested last round.
  • database/sql path: CheckNamedValue accepts *big.Int unchanged, and rebind → bind reaches the same formatter, so the std test exercises the fixed code on both protocols.
  • Tests cover native + HTTP, native API + database/sql, all three placeholder styles, the query-parameter entry point, and the mixed-width WHERE … IN ? case against a UInt256 column. Both integration tests version-gate on 21.12 and register t.Cleanup for every connection.

Behavior change to be aware of (intended): a *big.Int bound against a String column (WHERE s = ? or INSERT … VALUES (?)) used to coerce via the quoted literal and will now fail server-side with an illegal-types error. That is the correct consequence of treating big.Int as numeric, but worth a line in release notes.

Blind spots:

  • I could not run the unit or integration tests in this sandbox; lint and CodeQL are green on the head commit and the ClickHouse test matrix (26.3–26.8) was still in progress at review time.
  • Cross-type scalar comparisons such as an Int128 literal against a UInt256 column with = are not exercised by the tests (only the IN form is). ClickHouse supports these comparisons, so I do not expect a problem, but it is unverified here.

Verdict: ✅ Approve

Inline comments are attached to the relevant lines. This summary updates in place on re-review.

A []*big.Int / big.Int slice (and clickhouse.ArraySet) was formatted with a
per-element wide-integer conversion, so a mixed-width array such as
[]*big.Int{1, uint256Max} became [toInt128('1'), toUInt256('...')]. Those
element types have no common ClickHouse type unless use_variant_as_common_type
is on (off by default before 26.x), so WHERE ... IN ? failed with NO_COMMON_TYPE
on 25.8 (the CI floor) where the old quoted-string form used to coerce cleanly.

Render such arrays with one conversion — the narrowest wide type that holds
every element — via a shared bigIntArray helper, falling back to per-element
formatting when no single type fits (unchanged behavior). Tuples and scalars
are untouched.

Also version-gate the wide-integer integration tests at 21.12 (native and
database/sql), matching tests/bigint_test.go, and merge a split stdlib import
group, per review feedback.
Comment thread bind.go Resolved
polyglotAI-bot and others added 2 commits July 29, 2026 17:58
…eview)

commonBigIntConvFunc unifies only a flat []*big.Int / ArraySet. Mixed-magnitude
big.Int values nested in a map, a nested array, or a tuple used as an IN set
still get per-element toInt128/toUInt256 conversions and can hit NO_COMMON_TYPE.
Document that boundary so a user hitting it from a map/nested bind can find the
reason; the deeper container unification is tracked as a separate follow-up.

@must108 must108 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

LGTM! Ship it

@must108
must108 merged commit 70ac3ec into main Sep 9, 2026
10 of 11 checks passed
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.

[backfill: ClickHouse/clickhouse-go] Int128/UInt128 bind parameters serialized as quoted strings, not integer literals

4 participants