Skip to content

Map PostgreSQL numeric(p,0) with p>76 to Int256 in MaterializedPostgreSQL - #107431

Merged
alexey-milovidov merged 5 commits into
masterfrom
matpg-numeric-int256
Jun 14, 2026
Merged

alexey-milovidov merged 5 commits into
masterfrom
matpg-numeric-int256

Conversation

@alexey-milovidov

Copy link
Copy Markdown
Member

PostgreSQL numeric can have a precision higher than ClickHouse Decimal256 supports (76 digits). A common case is numeric(78, 0), used to store 256-bit integers (uint256). MaterializedPostgreSQL previously failed to create such a table with:

Precision 78 and scale 0 are too big and not supported

This PR maps numeric(p, 0) with p > 76 to ClickHouse Int256, which natively holds 256-bit integers (PostgreSQL numeric is signed, hence Int256).

Wide-integer text parsing in ClickHouse intentionally does not detect overflow (readIntText.h skips the check for big integers). To avoid silently storing a wrapped-around value, the parsed value is verified by round-tripping it back to text on insert; a value that does not fit into Int256 is rejected with a clear out of range of Int256 error.

Mechanically this adds a vtInt256 value type to ExternalResultDescription and handles it in insertPostgreSQLValue (other sources keep their existing default: handling, so this is a no-op for them).

The integration test replicates a numeric(78, 0) column holding the maximum Int256 value, a negative value and zero, checks the resulting column is Int256 with matching values, and that ongoing replication works.

Closes: #59224
Related: #49045

Changelog category (leave one):

  • Improvement

Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):

MaterializedPostgreSQL now maps PostgreSQL numeric(p, 0) columns with precision greater than 76 (e.g. numeric(78, 0) used for 256-bit integers) to ClickHouse Int256 instead of failing with "Precision too big". Values that do not fit into Int256 are rejected with a clear error.

Documentation entry for user-facing changes

  • Documentation is written (mandatory for new features)

🤖 Generated with Claude Code

…tgreSQL

PostgreSQL `numeric` can have a precision higher than ClickHouse `Decimal256`
supports (76 digits); a common use case is `numeric(78, 0)` to store 256-bit
integers. Previously this failed with "Precision N and scale M are too big and
not supported".

Map such columns (precision > 76, scale 0) to `Int256`, which natively holds
256-bit integers. Because wide-integer text parsing does not detect overflow,
the value is verified by round-tripping it back to text on insert, so a value
that does not fit into `Int256` is rejected with a clear error instead of being
silently wrapped around.

This also adds `vtInt256` to `ExternalResultDescription` and handles it in
`insertPostgreSQLValue`.

Closes: #59224
Related: #49045

Co-Authored-By: Claude Opus 4.8 <[email protected]>
@clickhouse-gh

clickhouse-gh Bot commented Jun 14, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [77afd1e]

Summary: ✅


AI Review

Summary

This PR maps PostgreSQL numeric(p, 0) columns and arrays with precision greater than 76 to ClickHouse Int256, adds read-time range checks to avoid wrapped wide-integer values, and updates the Markdown type mapping docs. The implementation and tests cover the main schema/value paths after the earlier fixes, but one public documentation surface remains stale.

Findings

⚠️ Majors

  • [docs/en/engines/database-engines/postgresql.md:51] The Markdown docs now mention the numeric(p, 0) / Int256 special case, but the registered PostgreSQL database engine Documentation block in src/Databases/PostgreSQL/DatabasePostgreSQL.cpp still says DECIMAL, NUMERIC map only to Decimal. That text is exposed through system.database_engines.description, so users can see contradictory type mappings depending on the documentation surface.
    Suggested fix: mirror this numeric(p, 0) / Int256 note in the source-level Documentation block too.
Final Verdict

Status: ⚠️ Request changes

Minimum required action: update the source-level PostgreSQL engine documentation to match the Markdown documentation.

@clickhouse-gh clickhouse-gh Bot added the pr-improvement Pull request with some product improvements label Jun 14, 2026
…rt arrays

The previous version was unreachable for the exact case it targeted: for a typed
`numeric(78, 0)`, `DataTypeFactory::instance().get(type)` was called first and
threw `Wrong precision: it must be between 1 and 76` before the `scale == 0`
branch could map it to `Int256`.

Parse the precision and scale directly from the type string (e.g. "numeric(78,0)")
so the over-precision, scale-0 case can be mapped to `Int256` without going
through the Decimal factory.

Also handle `numeric(p, 0)[]` arrays, which become `Array(Int256)`: add an
`Int256` branch (with the same round-trip overflow check) to
`preparePostgreSQLArrayInfo`, which previously threw "Type conversion to Int256
is not supported".

Co-Authored-By: Claude Opus 4.8 <[email protected]>
alexey-milovidov and others added 3 commits June 14, 2026 16:16
The manual precision/scale parser added for the `numeric(p, 0)` -> `Int256`
mapping constructed `DataTypeDecimal<...>` directly, which only checks
`scale <= maxPrecision`. This dropped the `scale > precision` rejection that
`createDecimal` (reached via `DataTypeFactory` before) used to provide, so a
PostgreSQL `numeric(5, 7)` would become an invalid `Decimal32(5, 7)`.

Use `createDecimal<DataTypeDecimal>` for `precision <= 76` to restore the
validation and dispatch, keeping the `Int256` special case only for
`precision > 76 && scale == 0`.

Add a regression test (`test_numeric_int256_validation`) covering both the
`scale > precision` rejection and the `Int256` overflow guard, exercised
synchronously via the `postgresql` table function.

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
Update the PostgreSQL data-type mapping table to mention that
`numeric(p, 0)` with precision greater than 76 (e.g. `numeric(78, 0)`) is
inferred as `Int256`, and that out-of-range values are rejected.

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
@clickhouse-gh

clickhouse-gh Bot commented Jun 14, 2026

Copy link
Copy Markdown
Contributor

LLVM Coverage Report

Metric Baseline Current Δ
Lines 84.80% 84.70% -0.10%
Functions 92.40% 92.40% +0.00%
Branches 77.30% 77.30% +0.00%

Changed lines: Changed C/C++ lines covered by tests: 32/49 (65.31%) | Lost baseline coverage: none · Uncovered code

Full report · Diff report

@alexey-milovidov alexey-milovidov left a comment

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Looks good.

@alexey-milovidov alexey-milovidov self-assigned this Jun 14, 2026
@alexey-milovidov
alexey-milovidov added this pull request to the merge queue Jun 14, 2026
Merged via the queue into master with commit 6e470f5 Jun 14, 2026
166 checks passed
@alexey-milovidov
alexey-milovidov deleted the matpg-numeric-int256 branch June 14, 2026 22:52
@robot-ch-test-poll2 robot-ch-test-poll2 added the pr-synced-to-cloud The PR is synced to the cloud repo label Jun 14, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

pr-improvement Pull request with some product improvements pr-synced-to-cloud The PR is synced to the cloud repo

Projects

None yet

Development

Successfully merging this pull request may close these issues.

MaterializedPostgreSQL engine type mapping for uint256

2 participants