From 53dd55e2e25b7caa9d8cd86f832ca6d49d58f60a Mon Sep 17 00:00:00 2001 From: HawkForge <75696486+HawkForge@users.noreply.github.com> Date: Sun, 6 Dec 2026 16:16:12 +0600 Subject: [PATCH] Reject Decimal precision outside 1..38 The server sends Decimal256(S) as Decimal(76, S), and Decimal(P, S) with P > 38 as is. ColumnDecimal has no 32-byte storage and used 16 bytes per value, so Select() could return wrong values with no error and Insert() hung until the server receive timeout. DecimalType now throws ValidationError for a precision outside 1..38, so CreateColumnByType() and ColumnDecimal reject such types before any data is read or sent. Closes #620 --- clickhouse/types/types.cpp | 5 ++++- ut/CreateColumnByType_ut.cpp | 9 +++++++++ ut/client_ut.cpp | 8 ++++++++ ut/types_ut.cpp | 9 +++++++++ 4 files changed, 30 insertions(+), 1 deletion(-) diff --git a/clickhouse/types/types.cpp b/clickhouse/types/types.cpp index 9b52255f..e0231ecd 100644 --- a/clickhouse/types/types.cpp +++ b/clickhouse/types/types.cpp @@ -317,7 +317,10 @@ DecimalType::DecimalType(size_t precision, size_t scale) : Type(Decimal), precision_(precision), scale_(scale) { - // TODO: assert(precision <= 38 && precision > 0); + // Decimal256 (precision 39..76) is not supported: ColumnDecimal stores at most 16 bytes per value. + if (precision < 1 || precision > 38) { + throw ValidationError("Decimal precision must be between 1 and 38, got " + std::to_string(precision)); + } } static std::string EscapeStringLiteral(std::string_view in) diff --git a/ut/CreateColumnByType_ut.cpp b/ut/CreateColumnByType_ut.cpp index b84d6e0d..087e1aa6 100644 --- a/ut/CreateColumnByType_ut.cpp +++ b/ut/CreateColumnByType_ut.cpp @@ -51,6 +51,15 @@ INSTANTIATE_TEST_SUITE_P(InnerType, CreateColumnBySimpleAggregateFunctionType, : std::make_tuple("SimpleAggregateFunction(anyLast, Tuple(UInt64, String))", "Tuple(UInt64, String)") )); +TEST(CreateColumnByType, DecimalPrecisionOverLimit) { + // The server sends Decimal256(S) as Decimal(76, S). The library has no 32-byte Decimal column. + EXPECT_NE(nullptr, CreateColumnByType("Decimal(38, 2)")); + EXPECT_THROW(CreateColumnByType("Decimal(39, 2)"), ValidationError); + EXPECT_THROW(CreateColumnByType("Decimal(76, 2)"), ValidationError); + EXPECT_THROW(CreateColumnByType("Nullable(Decimal(76, 2))"), ValidationError); + EXPECT_THROW(CreateColumnByType("Array(Decimal(76, 2))"), ValidationError); +} + TEST(CreateColumnByType, UnmatchedBrackets) { // When type string has unmatched brackets, CreateColumnByType must return nullptr. ASSERT_EQ(nullptr, CreateColumnByType("FixedString(10")); diff --git a/ut/client_ut.cpp b/ut/client_ut.cpp index 79ee9ca8..f0d77d20 100644 --- a/ut/client_ut.cpp +++ b/ut/client_ut.cpp @@ -1024,6 +1024,14 @@ TEST_P(ClientCase, EscapeRoundtrip_Enum) { EXPECT_EQ(enum_items.size(), row); } +TEST_P(ClientCase, SelectDecimal256Throws) { + // The server sends Decimal256(S) as Decimal(76, S) with 32 bytes per value. The client has no such column + // and must fail instead of reading the values as 16 bytes. + EXPECT_THROW( + client_->Select("SELECT toDecimal256(number + 1, 2) FROM numbers(2)", [](const Block&) {}), + ValidationError); +} + TEST_P(ClientCase, Decimal) { client_->Execute( "CREATE TEMPORARY TABLE IF NOT EXISTS " diff --git a/ut/types_ut.cpp b/ut/types_ut.cpp index 6dbe6408..d795a85a 100644 --- a/ut/types_ut.cpp +++ b/ut/types_ut.cpp @@ -159,6 +159,15 @@ TEST(TypesCase, DecimalTypes) { // TODO: implement this test. } +TEST(TypesCase, DecimalPrecisionOutOfRange) { + // Decimal256 (precision 39..76) is not supported, ColumnDecimal stores at most 16 bytes per value. + EXPECT_NO_THROW(Type::CreateDecimal(1, 0)); + EXPECT_NO_THROW(Type::CreateDecimal(38, 2)); + EXPECT_THROW(Type::CreateDecimal(0, 0), ValidationError); + EXPECT_THROW(Type::CreateDecimal(39, 2), ValidationError); + EXPECT_THROW(Type::CreateDecimal(76, 2), ValidationError); +} + TEST(TypesCase, DateTimeTypeNameEscapesTimezone) { // One special byte per timezone; {raw timezone, expected escaped rendering}. const std::vector> cases = {