ColumnDecimal::Append(Int128) stores a value outside the Decimal(P, S) range with no error: cut to 32/64 bits, or with too many digits
Mantenedores costumam responder em até 1 dia
Ninguém assumiu esta issue ainda.
Avaliação
- Dificuldade
- 2/5
- Tempo estimado
- 1-3 horas
- Facilidade para iniciantes
- 76/100
Direção de pesquisa
Comece por clickhouse/columns/decimal.cpp, especialmente ColumnDecimal::Append(const Int128&), e compare-o com a validação da sobrecarga para strings. Confira os testes existentes das colunas Decimal e adicione cobertura para valores fora do intervalo de precisão, incluindo os tipos de armazenamento mais estreitos. O trabalho estará concluído quando Append lançar ValidationError sem armazenar um valor inválido, de forma consistente com a sobrecarga para strings.
Escrita pelo modelo de indexação a partir do texto da issue.
Descrição
Describe the bug
ColumnDecimal::Append(const Int128&) does not check the value against the range of the column type, (-10^(P-S), 10^(P-S)). It stores every value with no error, and Insert() writes it to the server:
Decimal(9, S)andDecimal(18, S)keep only the low 32 or 64 bits of the scaled value. InDecimal(9, 2), 42949673.01 (2^32 + 5) becomes 0.05, and 21474836.48 (2^31) becomes -21474836.48.Decimal(18, 2)gives the same result for 2^64 + 5 and 2^63.- A value with more than P digits that fits the storage is stored unchanged.
Decimal(9, 0)keeps 1000000000 (10 digits), andDecimal(38, 0)keeps 10^38 (39 digits). The server then returns a value that is outside the column type.
ColumnDecimal::Append(const std::string&) rejects the same values with ValidationError. Both bignum builds (CH_USE_ABSEIL_FOR_BIGNUM=ON and OFF) give the same result. The nested column of Nullable(Decimal(9, 2)) and Array(Decimal(9, 2)) gives the same result too.
To reproduce
Reproduction program (C++, 44 lines)
#include <clickhouse/client.h>
#include <iostream>
using namespace clickhouse;
static void Check(Client& client, const std::string& type, ColumnRef col) {
client.Execute("DROP TABLE IF EXISTS dec_append");
client.Execute("CREATE TABLE dec_append (v " + type + ") ENGINE = Memory");
Block block;
block.AppendColumn("v", col);
client.Insert("dec_append", block); // no error
std::cout << type << ":";
client.Select("SELECT toString(v) FROM dec_append", [](const Block& b) {
for (size_t i = 0; i < b.GetRowCount(); ++i)
std::cout << " " << b[0]->As<ColumnString>()->At(i);
});
std::cout << std::endl;
}
int main() {
Client client(ClientOptions().SetHost("localhost"));
auto d9 = std::make_shared<ColumnDecimal>(9, 2);
d9->Append(Bignum::MakeInt128(0, 4294967301ULL)); // 2^32 + 5 = 42949673.01
d9->Append(Int128(2147483648LL)); // 2^31 = 21474836.48
Check(client, "Decimal(9, 2)", d9);
auto d18 = std::make_shared<ColumnDecimal>(18, 2);
d18->Append(Bignum::MakeInt128(1, 5)); // 2^64 + 5 = 184467440737095516.21
d18->Append(Bignum::MakeInt128(0, 1ULL << 63)); // 2^63 = 92233720368547758.08
Check(client, "Decimal(18, 2)", d18);
auto d9s0 = std::make_shared<ColumnDecimal>(9, 0);
d9s0->Append(Int128(1000000000)); // 10^9: 10 digits
Check(client, "Decimal(9, 0)", d9s0);
auto d38 = std::make_shared<ColumnDecimal>(38, 0);
d38->Append(Bignum::MakeInt128(0x4B3B4CA85A86C47ALL, 0x098A224000000000ULL)); // 10^38: 39 digits
Check(client, "Decimal(38, 0)", d38);
ColumnDecimal contrast(9, 2);
try { contrast.Append(std::string("42949673.01")); }
catch (const ValidationError& e) { std::cout << "Append(\"42949673.01\"): " << e.what() << std::endl; }
}
Output:
Decimal(9, 2): 0.05 -21474836.48
Decimal(18, 2): 0.05 -92233720368547758.08
Decimal(9, 0): 1000000000
Decimal(38, 0): 100000000000000000000000000000000000000
Append("42949673.01"): Value 42949673.01 is too large for scale=2 precision=9
Expected behavior
Append throws ValidationError and stores nothing, as the string overload does. The server rejects these values:
CREATE TABLE dec_sql (d9 Decimal(9, 2), d9s0 Decimal(9, 0), d38 Decimal(38, 0)) ENGINE = Memory
INSERT INTO dec_sql (d9) VALUES (42949673.01)
Code: 69. DB::Exception: Decimal value is too big: 8 digits were read: '42949673'e0. Expected to read decimal with scale 2 and precision 9: ... (ARGUMENT_OUT_OF_BOUND)
INSERT INTO dec_sql (d9s0) VALUES (1000000000)
Code: 69. DB::Exception: Too many digits (10 > 9) in decimal value: ... (ARGUMENT_OUT_OF_BOUND)
INSERT INTO dec_sql (d38) VALUES (100000000000000000000000000000000000000)
Code: 69. DB::Exception: Too many digits (39 > 38) in decimal value: ... (ARGUMENT_OUT_OF_BOUND)
The Decimal docs give the range Decimal(P, S) - (-1 * 10^(P - S), 1 * 10^(P - S)).
Root cause
ColumnDecimal::Append(const Int128&) (clickhouse/columns/decimal.cpp:84-96) has no range check. For precision 1-18 it casts the low 64 bits to int32_t or int64_t (lines 87 and 90), and for precision 19-38 it stores the value as it is (line 93). Only Append(const std::string&) calls ValidateDecimalString (line 102).
The fix is a range check in this overload. Polyglot AI can open a fix PR if a maintainer assigns this issue to @polyglotAI-bot.
Related
- #631:
Append(ColumnRef)of a Decimal column with another scale; another overload of the same class.
Environment
- clickhouse-cpp
master6049285(v2.6.2 has the same code) - ClickHouse server 26.11.7.10
- Linux, clang 18;
CH_USE_ABSEIL_FOR_BIGNUM=ONandOFF
Classification: impact: Data integrity · area: Data types & formats (Decimal)
Found by automated analysis (Polyglot AI) while working on ClickHouse/clickhouse-odbc#686. Verified against a live server.
- Linguagem predominante
- C
- Estrelas
- 388
- Forks
- 220
- Merge médio
- 1d 25min
- PRs com merge (30d)
- 16
Preparar o ambiente
Este projeto não oferece contêiner de desenvolvimento, Dockerfile nem guia de contribuição, então a configuração fica por sua conta: comece pelo README e veja nosso guia da primeira contribuição para os passos gerais.
Primeiros passos
- Leia a issue inteira e depois o guia de contribuição do projeto.
- Comente na issue dizendo que vai assumir — evita que duas pessoas façam o mesmo trabalho.
- Faça um fork do repositório e trabalhe em uma branch.
- Abra um pull request que referencie o número da issue.
Mais de ClickHouse/clickhouse-cpp
-
bug queries
Dificuldade 2/5 1-3 horas Facilidade para iniciantes 79/100
ClickHouse/clickhouse-cpp#644 ·
Mantenedores costumam responder em até 1 dia
-
bug data-types native-format
Dificuldade 2/5 1-3 horas Facilidade para iniciantes 82/100
ClickHouse/clickhouse-cpp#643 ·
Mantenedores costumam responder em até 1 dia
-
bug connectivity native-protocol
Dificuldade 2/5 1-3 horas Facilidade para iniciantes 78/100
ClickHouse/clickhouse-cpp#638 ·
Mantenedores costumam responder em até 1 dia
-
Core SQL test harness: ParseSetAssignments does not skip comments inside SETTalvez já em andamento @puretechteam assumiu há 11 dias. Abertabug
Dificuldade 2/5 1-3 horas Facilidade para iniciantes 85/100
ClickHouse/clickhouse-cpp#626 · 1 comentário ·
Mantenedores costumam responder em até 1 dia
-
Enum type parsing: duplicate ordinals (Enum8('a' = 1, 'b' = 1)) and duplicate labels (Enum8('a' = 1, 'a' = 2)) are silently accepted and lose a memberTalvez já em andamento @SBNREX assumiu há 19 dias. Abertabug data-types native-format
Dificuldade 2/5 1-3 horas Facilidade para iniciantes 75/100
ClickHouse/clickhouse-cpp#607 · 1 comentário ·
Mantenedores costumam responder em até 1 dia
Todas as issues de ClickHouse/clickhouse-cpp
Issues semelhantes
-
Dificuldade 2/5 1-3 horas Facilidade para iniciantes 70/100
Mantenedores costumam responder em até 2 dias
-
Dificuldade 2/5 1-3 horas Facilidade para iniciantes 76/100
Mantenedores costumam responder em até 1 dia
-
Dificuldade 2/5 1-3 horas Facilidade para iniciantes 78/100
-
type:feature
Dificuldade 2/5 1-3 horas Facilidade para iniciantes 78/100
google-coral/coralnpu#175 ·
Mantenedores costumam responder em até 1 dia
-
Dificuldade 2/5 1-3 horas Facilidade para iniciantes 85/100
Mantenedores costumam responder em até 1 dia