Skip to content

Add support for Time/Time64 columns#467

Open
slabko wants to merge 4 commits intomasterfrom
time-time64-support
Open

Add support for Time/Time64 columns#467
slabko wants to merge 4 commits intomasterfrom
time-time64-support

Conversation

@slabko
Copy link
Contributor

@slabko slabko commented Mar 18, 2026

This PR implements support for Time and Time64 columns. The columns return int32_t and int64_t, respectively, and do not wrap the values into any other types. This gives users the flexibility to choose their own preferred representation of the data. For example, they can use std::chrono::duration, std::chrono::hh_mm_ss, or other libraries such as the Howard Hinnant Date library.

The examples below use std::chrono::zoned_time and std::chrono::hh_mm_ss, which are available in C++20.

// Create table
client.Execute(
    R"(CREATE OR REPLACE TABLE time (
        id UInt64,
        time Time,
        time64_0 Time64(0),
        time64_3 Time64(3),
        time64_6 Time64(6),
    )
    ENGINE MergeTree
    ORDER BY id)");

// Prepare values to insert
auto now = std::chrono::system_clock::now();
auto local_now = std::chrono::zoned_time(std::chrono::current_zone(), now).get_local_time();
auto day = std::chrono::floor<std::chrono::days>(local_now);
auto time_of_day = local_now - day;
auto time = std::chrono::duration_cast<std::chrono::seconds>(time_of_day);
auto time64_0 = std::chrono::duration_cast<std::chrono::seconds>(time_of_day);
auto time64_3 = std::chrono::duration_cast<std::chrono::milliseconds>(time_of_day);
auto time64_6 = std::chrono::duration_cast<std::chrono::microseconds>(time_of_day);

// Insert
clickhouse::Block block = client.BeginInsert("INSERT INTO time VALUES");
block[0]->AsStrict<clickhouse::ColumnUInt64>()->Append(2);
block[1]->AsStrict<clickhouse::ColumnTime>()->Append(static_cast<int32_t>(time.count()));
block[2]->AsStrict<clickhouse::ColumnTime64>()->Append(time64_0.count());
block[3]->AsStrict<clickhouse::ColumnTime64>()->Append(time64_3.count());
block[4]->AsStrict<clickhouse::ColumnTime64>()->Append(time64_6.count());
block.RefreshRowCount();
client.SendInsertBlock(block);
client.EndInsert();

// Select
client.Select(
    "SELECT * FROM time",
    [](const clickhouse::Block & block)
    {
        for (size_t i = 0; i < block.GetRowCount(); ++i) {
            auto col_time = block[1]->AsStrict<clickhouse::ColumnTime>();
            auto col_time64_0 = block[2]->AsStrict<clickhouse::ColumnTime64>();
            auto col_time64_3 = block[3]->AsStrict<clickhouse::ColumnTime64>();
            auto col_time64_6 = block[4]->AsStrict<clickhouse::ColumnTime64>();

            std::chrono::seconds time{col_time->At(i)};
            std::chrono::seconds time64_0{col_time64_0->At(i)};
            std::chrono::milliseconds time64_3{col_time64_3->At(i)};
            std::chrono::microseconds time64_6{col_time64_6->At(i)};

            std::cout << std::format(
                "{}\t{}\t{}\t{}\n", 
                std::chrono::hh_mm_ss{time},
                std::chrono::hh_mm_ss{time64_0},
                std::chrono::hh_mm_ss{time64_3},
                std::chrono::hh_mm_ss{time64_6}
            );
        }
    });

@slabko slabko force-pushed the time-time64-support branch 7 times, most recently from d44abd4 to cc78927 Compare March 19, 2026 10:16
@slabko slabko requested a review from Copilot March 19, 2026 14:31
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR adds first-class support for ClickHouse Time / Time64 types in the C++ client library, including type parsing, column implementations, ItemView validation/printing, and unit/integration tests to verify roundtrip + client behavior.

Changes:

  • Introduce Type::Time / Type::Time64 (incl. Time64Type) and extend the type parser to recognize both.
  • Add new columns ColumnTime and ColumnTime64, wire them into the column factory, and update ItemView validation + test utilities output formatting.
  • Expand UT coverage (ItemView, column behavior, parsing, client roundtrip) and adjust CI workflows (ClickHouse image/secrets).

Reviewed changes

Copilot reviewed 23 out of 23 changed files in this pull request and generated 5 comments.

Show a summary per file
File Description
ut/value_generators.h Adds MakeTime / MakeTime64 generators for new time-based tests.
ut/value_generators.cpp Implements MakeTime() value set including positive/negative cases.
ut/utils_ut.cpp Extends ItemView ostream serialization tests for Time/Time64.
ut/utils.cpp Adds Time/Time64 formatting cases to ItemView ostream output.
ut/type_parser_ut.cpp Adds parser tests for Time and Time64(precision).
ut/roundtrip_column.cpp Enables server setting needed to roundtrip Time/Time64.
ut/itemview_ut.cpp Adds ItemView type-size tests for Time and Time64.
ut/columns_ut.cpp Adds column unit tests for appending/constructing ColumnTime/ColumnTime64.
ut/client_ut.cpp Adds integration tests verifying insertion/selection and string rendering for Time/Time64.
ut/Column_ut.cpp Adds generic column roundtrip test cases for Time/Time64 at multiple precisions.
clickhouse/types/types.h Extends Type::Code and introduces Time64Type + factory methods.
clickhouse/types/types.cpp Implements Time/Time64 naming and Time64Type validation + naming.
clickhouse/types/type_parser.cpp Registers Time/Time64 keywords in the type-code map.
clickhouse/columns/time.h New ColumnTime and ColumnTime64 public column APIs.
clickhouse/columns/time.cpp Implements storage, IO, slicing, swapping, and ItemView exposure for time columns.
clickhouse/columns/itemview.cpp Extends ItemView size validation for Time (4 bytes) and Time64 (8 bytes).
clickhouse/columns/factory.cpp Wires Time/Time64 into CreateColumnByType terminal factory.
clickhouse/client.h Exposes time columns via the public client header includes.
clickhouse/CMakeLists.txt Adds columns/time.cpp to the library build sources.
.github/workflows/windows_msvc.yml Updates CI secrets/host wiring for Windows MSVC runs.
.github/workflows/windows_mingw.yml Updates CI secrets/host wiring for Windows MinGW runs.
.github/workflows/macos.yml Updates CI secrets/host wiring for macOS runs.
.github/workflows/linux.yml Updates ClickHouse server image and container startup options for Linux CI.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@slabko slabko force-pushed the time-time64-support branch from cc78927 to 45fcc5e Compare March 19, 2026 18:02
@slabko slabko force-pushed the time-time64-support branch from 45fcc5e to a125311 Compare March 19, 2026 20:49
@slabko slabko marked this pull request as ready for review March 20, 2026 06:15
@slabko slabko requested a review from mzitnik as a code owner March 20, 2026 06:15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants