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

Abierto
#631 0 comentarios 0 reacciones 0 asignados Ver en GitHub

Los mantenedores suelen responder en 1 día

Nadie ha tomado este issue todavía.

Evaluación

Dificultad
3/5
Tiempo estimado
1-2 días
Aptitud para principiantes
70/100
Tipo de issue
Error
Claridad
Bien especificado
Estado de actividad
Activo
Stack tecnológico
cpp
Área
databases

Línea de trabajo

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.

Escrito por el modelo de indexación a partir del texto del issue.

Descripción

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.

Lenguaje dominante
C
Estrellas
388
Forks
220
Merge medio
1 d 25 min
PR fusionados (30 d)
16

Preparar el entorno

Este proyecto no incluye contenedor de desarrollo, Dockerfile ni guía de contribución, así que la configuración corre por tu cuenta: empieza por su README y consulta nuestra guía para la primera contribución para los pasos generales.

Primeros pasos

  1. Lee el issue completo y luego la guía de contribución del proyecto.
  2. Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
  3. Haz un fork del repositorio y trabaja en una rama.
  4. Abre un pull request que haga referencia al número del issue.

Más de ClickHouse/clickhouse-cpp

Todos los issues de ClickHouse/clickhouse-cpp

Issues similares

Más issues de C

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.