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

未關閉
#631 0 則留言 0 個 reaction 已指派 0 人 在 GitHub 檢視

維護者通常 1 天內回覆

還沒有人認領這個 Issue。

評估

難度
3/5
預估耗時
1-2 天
新手友好度
70/100
Issue 類型
缺陷
描述清晰度
描述清楚
活躍度
活躍
技術堆疊
cpp
領域
databases

研究方向

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.

由索引模型根據 Issue 內容生成。

描述

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.

主要語言
C
星號
388
分支
220
平均合併
1 天 25 分鐘
30 天內合併 PR
16

環境準備

這個專案沒有提供開發容器、Dockerfile 或貢獻指南,環境需要你自己搭建:先看它的 README,通用步驟見我們的新手貢獻指南。

從這裡開始

  1. 先讀完整個 Issue,再讀專案的貢獻指南。
  2. 在 Issue 下留言說明你要接手 —— 這能避免兩個人做同樣的事。
  3. Fork 儲存庫,在一個分支上完成修改。
  4. 送出 Pull Request,並在描述裡引用這個 Issue 編號。

ClickHouse/clickhouse-cpp 的其他 Issue

查看 ClickHouse/clickhouse-cpp 的全部 Issue

相似的 Issue

更多 C Issue

把新 issue 寄到你的電子郵件信箱

精選適合新手參與的 GitHub issue 摘要。