Skip to content

Reading LowCardinality(Nullable(String)) column is not supported (for the data received from ClickHouse) #504

Open

Description

@rklepov

Please see #339

This one confirms that it works

Perhaps, it would be helpful to know the real type of column at run-time with:

column->GetType().GetName()

Originally posted by @Enmk in #326

The test Map_LowCardinalityTString_LowCardinalityTNullableString might succeed, but this is only true because you create the column with the type ColumnLowCardinalityT<ColumnNullableT<ColumnString>> explicitly in the test.

However, for the real data received from ClickHouse the type of the underlying column will be simply ColumnLowCardinality. This is probably due to the logic implemented in #182:

case Type::Nullable:
    return std::make_shared<ColumnLowCardinality>(
        std::make_shared<ColumnNullable>(
            CreateColumnFromAst(GetASTChildElement(nested, 0), settings),
            std::make_shared<ColumnUInt8>()
        )
    );

Therefore the dynamic_cast inside column->As<ColumnLowCardinalityT<ColumnNullableT<ColumnString>>>() will yield nullptr.

This is easily illustrated with a simple typeid check (assume that column is an instance of a ColumnRef pointer).

This is what we have for LowCardinality(String) column:

// ColumnRef column;

auto type_name = column->GetType().GetName(); // "LowCardinality(String)"

auto type_id = typeid(*column).name(); // clickhouse::ColumnLowCardinalityT<clickhouse::ColumnString> ("N10clickhouse21ColumnLowCardinalityTINS_12ColumnStringEEE")

And this is the result for LowCardinality(Nullable(String)):

// ColumnRef column;

auto type_name = column->GetType().GetName(); // "LowCardinality(Nullable(String))"

auto type_id = typeid(*column).name(); // clickhouse::ColumnLowCardinality ("N10clickhouse20ColumnLowCardinalityE")

The conclusion is that it's not possible to read LowCardinality(Nullable(String)) column in the current version of clickhouse-cpp (v2.6.1):

  1. column->As<ColumnLowCardinalityT<ColumnNullableT<ColumnString>>>() doesn't work for the reasons explained above.
  2. While column->As<ColumnLowCardinality>() does work, it's of no help: trying to wrap the result into ColumnLowCardinalityT<ColumnNullableT<ColumnString>> will also fail because the nested type is ColumnNullable (not ColumnNullableT<ColumnString>).
  3. Declaring ColumnLowCardinalityT<ColumnNullable> is not possible because there's no ValueType alias in ColumnNullable.

Activity

  1. polyglotAI-bot commented on Nov 18, 2026

    @polyglotAI-bot
    Collaborator

    Recommendation: Close — already fixed
    Fixed on master by #541 (merged 2026-08-11): As<ColumnLowCardinalityT<ColumnNullableT<ColumnString>>>() now wraps the column that Select returns. The fix is not released yet: the latest release, v2.6.2, does not contain it.

    Analysis: The factory still creates a base ColumnLowCardinality with a base ColumnNullable dictionary for LowCardinality(Nullable(String)) (clickhouse/columns/factory.cpp:253-270). On v2.6.2, Column::As<T>() is only a dynamic_pointer_cast, so it returns nullptr for that column. On master, Column::As<T>() wraps the column when T has a Wrap method (clickhouse/columns/column.h:199-208), and ColumnLowCardinalityT::Wrap wraps a base dictionary into the typed dictionary (clickhouse/columns/lowcardinality.h:241-246). The README list of supported types (README.md:188) still names only LowCardinality(String) and LowCardinality(FixedString(N)).

    Reproduction: complete program and output
    #include <clickhouse/client.h>
    
    #include <cstdlib>
    #include <iostream>
    #include <string>
    #include <typeinfo>
    
    using namespace clickhouse;
    
    int main() {
        const char* host = std::getenv("CLICKHOUSE_HOST");
        const char* port = std::getenv("CLICKHOUSE_PORT");
        Client client(ClientOptions()
                          .SetHost(host ? host : "localhost")
                          .SetPort(port ? std::stoi(port) : 9000));
    
        client.Execute("DROP TABLE IF EXISTS issue504");
        client.Execute("CREATE TABLE issue504 (v LowCardinality(Nullable(String))) ENGINE = Memory");
        client.Execute("INSERT INTO issue504 VALUES ('a'), (NULL), ('b'), ('a')");
    
        client.Select("SELECT v FROM issue504", [](const Block& block) {
            if (block.GetRowCount() == 0) return;
            ColumnRef column = block[0];
            std::cout << "type name: " << column->GetType().GetName() << "\n";
            std::cout << "is ColumnLowCardinality: "
                      << (dynamic_cast<ColumnLowCardinality*>(column.get()) != nullptr) << "\n";
            try {
                auto lc = column->As<ColumnLowCardinalityT<ColumnNullableT<ColumnString>>>();
                if (!lc) {
                    std::cout << "As<ColumnLowCardinalityT<ColumnNullableT<ColumnString>>>: nullptr\n";
                    return;
                }
                std::cout << "As<ColumnLowCardinalityT<ColumnNullableT<ColumnString>>>: values =";
                for (size_t i = 0; i < lc->Size(); ++i) {
                    auto v = lc->At(i);
                    std::cout << " " << (v ? std::string(*v) : std::string("NULL"));
                }
                std::cout << "\n";
            } catch (const std::exception& e) {
                std::cout << "exception: " << e.what() << "\n";
            }
        });
    
        client.Execute("DROP TABLE IF EXISTS issue504");
        return 0;
    }

    Output on master (58453c2), ClickHouse 26.10.4.207:

    type name: LowCardinality(Nullable(String))
    is ColumnLowCardinality: 1
    As<ColumnLowCardinalityT<ColumnNullableT<ColumnString>>>: values = a NULL b a
    exit=0
    

    Output on v2.6.2 (001025c), ClickHouse 26.10.4.207:

    type name: LowCardinality(Nullable(String))
    is ColumnLowCardinality: 1
    As<ColumnLowCardinalityT<ColumnNullableT<ColumnString>>>: nullptr
    exit=0
    

    Automated triage by Polyglot AI. A maintainer makes the final decision.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions