Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion clickhouse/types/types.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
9 changes: 9 additions & 0 deletions ut/CreateColumnByType_ut.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"));
Expand Down
8 changes: 8 additions & 0 deletions ut/client_ut.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 "
Expand Down
9 changes: 9 additions & 0 deletions ut/types_ut.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<std::pair<std::string, std::string>> cases = {
Expand Down
Loading