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

Đang mở
#631 0 bình luận 0 reaction 0 người được giao Xem trên GitHub

Maintainer thường phản hồi trong vòng 1 ngày

Chưa có ai nhận issue này.

Đánh giá

Độ khó
3/5
Thời gian dự kiến
1-2 ngày
Mức phù hợp với người mới
70/100
Loại issue
Lỗi
Độ rõ ràng
Đặc tả rõ ràng
Mức độ hoạt động
Sôi nổi
Công nghệ
cpp
Lĩnh vực
databases

Hướng nghiên cứu

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.

Do mô hình lập chỉ mục viết ra từ nội dung của issue.

Mô tả

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.

Ngôn ngữ chính
C
Star
388
Fork
220
Merge trung bình
1 ngày 25 phút
Pull request đã merge (30 ngày)
16

Chuẩn bị môi trường

Dự án này không cung cấp dev container, Dockerfile hay hướng dẫn đóng góp, nên bạn cần tự thiết lập môi trường: hãy bắt đầu từ README và xem hướng dẫn đóng góp lần đầu của chúng tôi để biết các bước chung.

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Issue khác của ClickHouse/clickhouse-cpp

Tất cả issue của ClickHouse/clickhouse-cpp

Issue tương tự

Thêm issue về C

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.