Column::Append: a Time64, DateTime64 or Decimal column with another scale is appended as raw values (wrong data, no error)

Offen
#631 0 Kommentare 0 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen

Maintainer antworten meist innerhalb von 1 Tag

Dieses Issue hat noch niemand übernommen.

Bewertung

Schwierigkeit
3/5
Geschätzter Aufwand
1-2 Tage
Anfängerfreundlichkeit
70/100
Issue-Typ
Bug
Klarheit
Klar beschrieben
Aktivitätsstatus
Aktiv
Tech-Stack
cpp
Bereich
databases

Rechercherichtung

Start from the three Append(ColumnRef) overloads named in the issue: clickhouse/columns/time.cpp:110-114, clickhouse/columns/date.cpp:314-318, and clickhouse/columns/decimal.cpp:205-209. Read how Swap and ColumnArray::AppendAsColumn validate precision/scale, since the issue expects consistent rejection. Build and run the reproduction to confirm the bad rows, then add the check and verify the same program either rejects the mismatched append or preserves values according to the maintainer's chosen behavior.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Beschreibung

bug data-types

Describe the bug

Column::Append(ColumnRef) of ColumnTime64, ColumnDateTime64 and ColumnDecimal accepts a column of the same class with another precision (scale) and copies its raw values. The appended rows then have the scale of the destination column, so each value changes by a power of 10. No error occurs, and Insert() writes the wrong values to the server:

  • Time64(3) ← Time64(6): 1.5 s becomes 1500 s.
  • DateTime64(3) ← DateTime64(6): the timestamp becomes 1000 times larger.
  • Decimal(10,2) ← Decimal(10,4): 1.5 becomes 150. Decimal(38,2) ← Decimal(38,4) gives the same.

Other parts of the library reject the same mismatch: Swap of these classes throws ValidationError when the precisions differ, and ColumnArray::AppendAsColumn throws ValidationError: can't append column of type Time64(6) to column type Time64(3). When the Decimal storage width also differs (for example Decimal(9,2) into Decimal(18,2)), Append adds nothing, as #243 describes for other wrong types.

To reproduce

Reproduction program (C++, 46 lines)
#include <clickhouse/client.h>
#include <cstdlib>
#include <iostream>

using namespace clickhouse;

static void Check(Client__PROG__ client, const std::string__PROG__ type, ColumnRef col) {
    client.Execute("DROP TABLE IF EXISTS append_scale");
    client.Execute("CREATE TABLE append_scale (v " + type + ") ENGINE = Memory");
    Block block;
    block.AppendColumn("v", col);
    client.Insert("append_scale", block);
    std::cout << type << ":";
    client.Select("SELECT toString(v) FROM append_scale", [](const Block__PROG__ b) {
        for (size_t i = 0; i < b.GetRowCount(); ++i)
            std::cout << " " << b[0]->As<ColumnString>()->At(i);
    });
    std::cout << std::endl;
}

int main() {
    const char* host = std::getenv("CLICKHOUSE_HOST");
    Client client(ClientOptions().SetHost(host ? host : "localhost"));
    client.Execute("SET enable_time_time64_type = 1");

    auto t3 = std::make_shared<ColumnTime64>(3);
    auto t6 = std::make_shared<ColumnTime64>(6);
    t3->Append(1'500);                        // 00:00:01.500
    t6->Append(1'500'000);                    // 00:00:01.500000
    t3->Append(t6);
    Check(client, "Time64(3)", t3);

    auto dt3 = std::make_shared<ColumnDateTime64>(3, "UTC");
    auto dt6 = std::make_shared<ColumnDateTime64>(6, "UTC");
    dt3->Append(1'700'000'000'500LL);         // 2023-11-14 22:13:20.500
    dt6->Append(1'700'000'000'500'000LL);     // 2023-11-14 22:13:20.500000
    dt3->Append(dt6);
    Check(client, "DateTime64(3, 'UTC')", dt3);

    auto d2 = std::make_shared<ColumnDecimal>(10, 2);
    auto d4 = std::make_shared<ColumnDecimal>(10, 4);
    d2->Append("1.50");
    d4->Append("1.5000");
    d2->Append(d4);
    Check(client, "Decimal(10, 2)", d2);
}

Output:

Time64(3): 00:00:01.500 00:25:00.000
DateTime64(3, 'UTC'): 2023-11-14 22:13:20.500 9999-12-31 23:59:59.000
Decimal(10, 2): 1.5 150

Expected behavior

The second row of each table has the value of the source row, as the server gives when it converts the source value to the destination type:

SELECT toString(CAST(CAST(1.5 AS Time64(6)) AS Time64(3)))                                           -- 00:00:01.500
SELECT toString(CAST(toDateTime64('2023-11-14 22:13:20.500000', 6, 'UTC') AS DateTime64(3, 'UTC')))  -- 2023-11-14 22:13:20.500
SELECT toString(CAST(toDecimal64('1.5000', 4) AS Decimal(10, 2)))                                    -- 1.5

Or Append throws ValidationError, as Swap and ColumnArray::AppendAsColumn do.

Root cause

ColumnTime64::Append (clickhouse/columns/time.cpp:110-114), ColumnDateTime64::Append (clickhouse/columns/date.cpp:314-318) and ColumnDecimal::Append (clickhouse/columns/decimal.cpp:205-209) check only the column class (As<>()), not the precision or scale, and then append the raw data column.

The fix is a precision and scale check in these three overloads. A maintainer decides what a mismatch does: throw ValidationError (as Swap does) or append nothing (the 2.x behavior for other wrong types, #243). Polyglot AI can open a fix PR if a maintainer assigns this issue to @polyglotAI-bot.

Related

  • #243: the 3.0 plan to make Append throw for a wrong type. This issue is about wrong values, not about an Append that does nothing.

Environment

  • clickhouse-cpp master b2a1f6a (v2.6.2 has the same code)
  • ClickHouse server 26.10.4.207
  • Linux, clang 18

Classification: impact: Data integrity · area: Data types & formats (Time64, DateTime64, Decimal)


Found by automated analysis (Polyglot AI) while working on ClickHouse/clickhouse-cs#767. Verified against a live server.

Vorherrschende Sprache
C
Sterne
388
Forks
220
Ø Merge
1 T. 25 Min.
Gemergte PRs (30 T.)
16

Entwicklungsumgebung

Dieses Projekt bietet weder Dev-Container noch Dockerfile noch Beitragsleitfaden – die Einrichtung liegt bei Ihnen. Beginnen Sie mit der README; die allgemeinen Schritte stehen in unserem Leitfaden für den ersten Beitrag.

Erste Schritte

  1. Lesen Sie das ganze Issue und danach den Beitragsleitfaden des Projekts.
  2. Schreiben Sie ins Issue, dass Sie es übernehmen — das erspart doppelte Arbeit.
  3. Forken Sie das Repository und arbeiten Sie in einem Branch.
  4. Öffnen Sie einen Pull Request, der die Issue-Nummer nennt.

Mehr aus ClickHouse/clickhouse-cpp

Alle Issues in ClickHouse/clickhouse-cpp

Ähnliche Issues

Weitere Issues zu C

Neue Issues direkt in Ihr Postfach

Eine kurze Übersicht über anfängerfreundliche GitHub-Issues.