Decimal(P, S) with P > 38 (Decimal256) is accepted and read as 16-byte values: wrong data with no error, Insert blocks
I maintainer di solito rispondono entro 1 giorno
Nessuno ha ancora preso questa issue.
Valutazione
- Difficoltà
- 3/5
- Tempo stimato
- 1-2 giorni
- Idoneità per principianti
- 72/100
Direzione di ricerca
Read clickhouse/types/type_parser.cpp, clickhouse/columns/factory.cpp, and clickhouse/types/types.cpp, starting with CreateColumnFromAst and the DecimalType precision check. Check existing tests for decimal parsing, column construction, and wrapped column types; add cases for precision above 38 and invalid precision bounds. Done means unsupported Decimal precisions fail before Select reads or Insert sends values, while the supported decimal types still work.
Scritto dal modello di indicizzazione a partire dal testo della issue.
Descrizione
Symptom
ClickHouse sends a Decimal256(S) column as Decimal(76, S), and a Decimal(P, S) column with 39 <= P <= 76 as Decimal(P, S). The server writes 32 bytes per value for these types. The library does not support Decimal256 (#377), but it accepts these type names and uses 16 bytes per value, the same as Decimal128.
Select()without compression can give wrong values with no error. ForSELECT toDecimal256(arrayElement([1, 2, 0, 0], number + 1), 2) FROM numbers(4)the server returns1, 2, 0, 0. The client returns1.00, 0.00, 2.00, 0.00, andSelect()returns normally. Other queries give wrong values and then an error that does not name the type, or only an error with LZ4 (table below).Insert()of aColumnDecimal(76, 2)into aDecimal256(2)column blocks until the serverreceive_timeout(300 s by default), and then throws. The table stays empty.
The explicit name is rejected: CreateColumnByType("Decimal256(2)") returns nullptr. Only the generic name Decimal(P, S), which the server sends, is accepted. The latest release v2.6.2 has the same behavior.
Reproduction
master at 2f8583f, default 2.x build (also with -DCH_USE_3X_API=ON), clang 18.1, Linux x86-64, ClickHouse server 26.10.4.207 on localhost:9000.
The server types and sizes:
$ curl -s localhost:8123 --data-binary "SELECT toTypeName(toDecimal256(1, 2)), toTypeName(CAST(1, 'Decimal(39, 2)')), byteSize(CAST(1, 'Decimal(39, 2)')), byteSize(CAST(1, 'Decimal(38, 2)'))"
Decimal(76, 2) Decimal(39, 2) 32 16
$ curl -s localhost:8123 --data-binary "SELECT toDecimal256(arrayElement([1, 2, 0, 0], number + 1), 2) FROM numbers(4) FORMAT TSV"
1
2
0
0
Reproduction program (C++, 29 lines)
#include <clickhouse/client.h>
#include <clickhouse/columns/factory.h>
#include <iostream>
using namespace clickhouse;
int main() {
std::cout << CreateColumnByType("Decimal(76, 2)")->Type()->GetName() << "\n"; // accepted
std::cout << (CreateColumnByType("Decimal256(2)") ? "column" : "nullptr") << "\n";
Client client(ClientOptions().SetHost("localhost").SetPort(9000));
// Select: wrong values, no error
client.Select("SELECT toDecimal256(arrayElement([1, 2, 0, 0], number + 1), 2) AS d FROM numbers(4)",
[](const Block& b) {
if (b.GetRowCount() == 0) return;
if (auto d = b[0]->As<ColumnDecimal>())
for (size_t r = 0; r < d->Size(); ++r) std::cout << d->StringAt(r) << "\n";
});
// Insert: blocks for 300 s, then throws
client.Execute("DROP TABLE IF EXISTS t_dec256");
client.Execute("CREATE TABLE t_dec256 (d Decimal256(2)) ENGINE = Memory");
auto col = std::make_shared<ColumnDecimal>(76, 2);
col->Append(std::string("1.50"));
col->Append(std::string("-2.25"));
Block block;
block.AppendColumn("d", col);
client.Insert("t_dec256", block);
}
Output:
Decimal(76,2)
nullptr
1.00
0.00
2.00
0.00
Then Insert() blocks. During the wait, system.processes shows the INSERT INTO t_dec256 query with elapsed 15 s and more. After 5 min, Insert() throws:
DB::NetException: Timeout exceeded while reading from socket (peer: [::1]:38554, local: [::1]:9000, 300000 ms)
With SetConnectionRecvTimeout(8s), Insert() throws can't receive string data: Resource temporarily unavailable instead.
Other results (callback values, then the result of Select()):
| Query | Compression | Callback values | Select() |
|---|---|---|---|
SELECT toDecimal256(number + 1, 2) FROM numbers(2) |
none | 1.00, 0.00 (server: 1, 2) |
unimplemented 72 |
SELECT CAST(number + 1, 'Decimal(39, 2)') FROM numbers(2) |
none | 1.00, 0.00 (server: 1, 2) |
unimplemented 72 |
SELECT toDecimal256(-1, 2) |
none | -1.00 |
can't read packet type from input stream |
SELECT toDecimal256(1, 2) |
none | 1.00 |
returns normally |
SELECT CAST(number + 1, 'Decimal(39, 2)') AS d, toUInt8(7) AS x FROM numbers(2) |
none | none | unsupported custom serialization |
SELECT CAST(number + 1, 'Nullable(Decimal256(2))') FROM numbers(2) |
none | column Nullable(Decimal(76,2)) |
unimplemented 72 |
SELECT [toDecimal256(number + 1, 2)] FROM numbers(2) |
none | column Array(Decimal(76,2)) |
unimplemented 72 |
| all of the above | LZ4 | none | some data was not read |
Contrast: Decimal(38, 2) and toDecimal128(...) give the correct values for Select(), and Insert() of a ColumnDecimal(38, 2) into a Decimal128(2) column stores 1.5 and -2.25.
Expected
The library rejects a Decimal precision that it does not support, before it reads or sends data. Select() throws unsupported column type: Decimal(76, 2), as it does for Decimal256(2) and other unsupported type names at the root. ColumnDecimal(76, 2) throws a ValidationError instead of creating a column with the wrong value size.
Root cause
clickhouse/types/type_parser.cpp:72maps the nameDecimaltoType::Decimalfor every precision.clickhouse/columns/factory.cpp:93-94createsColumnDecimal(precision, scale)from the AST with no precision check.clickhouse/columns/decimal.cpp:64-73: the constructor usesColumnInt128(16 bytes) for every precision above 18, soColumnDecimal::LoadBodyandSaveBodyread and write 16 bytes per value.clickhouse/types/types.cpp:320: the precision check in theDecimalTypeconstructor is only a comment,// TODO: assert(precision <= 38 && precision > 0);.
The fix is a precision check (1..38) in CreateColumnFromAst and in the DecimalType constructor. Polyglot AI can open a fix PR if a maintainer assigns this issue to @polyglotAI-bot.
Related
- #377: the feature request for
Decimal256. This issue is the behavior until that support exists. - #589 / #590: the same class of defect, for unknown type names inside containers.
Classification: impact: Data integrity · area: Data types & formats (Decimal256, Decimal(39..76, S)) · native: format
Found by automated analysis (Polyglot AI) while working on #377. Verified against a live server.
- Lingua principale
- C
- Stelle
- 388
- Fork
- 220
- Merge medio
- 1g 25m
- PR unite (30g)
- 16
Preparare l'ambiente
Questo progetto non fornisce container di sviluppo, Dockerfile né guida per i contributori, quindi l'ambiente è a tuo carico: parti dal suo README e consulta la nostra guida al primo contributo per i passaggi generali.
Come iniziare
- Leggi tutta la issue e poi la guida ai contributi del progetto.
- Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
- Fai un fork del repository e lavora su un branch.
- Apri una pull request che faccia riferimento al numero della issue.
Altre issue di ClickHouse/clickhouse-cpp
-
bug data-types
Difficoltà 2/5 1-3 ore Idoneità per principianti 76/100
ClickHouse/clickhouse-cpp#645 ·
I maintainer di solito rispondono entro 1 giorno
-
bug queries
Difficoltà 2/5 1-3 ore Idoneità per principianti 79/100
ClickHouse/clickhouse-cpp#644 ·
I maintainer di solito rispondono entro 1 giorno
-
bug data-types native-format
Difficoltà 2/5 1-3 ore Idoneità per principianti 82/100
ClickHouse/clickhouse-cpp#643 ·
I maintainer di solito rispondono entro 1 giorno
-
bug connectivity native-protocol
Difficoltà 2/5 1-3 ore Idoneità per principianti 78/100
ClickHouse/clickhouse-cpp#638 ·
I maintainer di solito rispondono entro 1 giorno
-
Core SQL test harness: ParseSetAssignments does not skip comments inside SETForse già presa @puretechteam l’ha presa 12 giorni fa. Apertabug
Difficoltà 2/5 1-3 ore Idoneità per principianti 85/100
ClickHouse/clickhouse-cpp#626 · 1 commento ·
I maintainer di solito rispondono entro 1 giorno
Tutte le issue di ClickHouse/clickhouse-cpp
Issue simili
-
CF_CFDP_S_SendFileData computes the file checksum from the SB buffer after CFE_SB_TransmitBufferAperta
Difficoltà 2/5 1-3 ore Idoneità per principianti 78/100
-
needs triage
Difficoltà 2/5 1-3 ore Idoneità per principianti 72/100
389ds/389-ds-base#8251 ·
I maintainer di solito rispondono entro 1 giorno
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 70/100
I maintainer di solito rispondono entro 2 giorni
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 76/100
I maintainer di solito rispondono entro 1 giorno
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 78/100