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

Open
#631 0 comments 0 reactions 0 assignees View on GitHub

Maintainers usually reply within 1 day

Nobody has claimed this yet.

Assessment

Difficulty
3/5
Estimated time
1-2 days
Newbie friendliness
70/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Active
Tech stack
cpp
Domain
databases

Research direction

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.

Written by the indexing model from the issue text.

Description

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.

Dominant language
C
Stars
388
Forks
220
Avg merge
1d 25m
Merged PRs (30d)
16

Getting set up

This project ships no dev container, Dockerfile or contributing guide, so setting up is up to you: start from its README, and see our first-contribution guide for the general steps.

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from ClickHouse/clickhouse-cpp

All issues in ClickHouse/clickhouse-cpp

Similar issues

More C issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.