Repository: ClickHouse/ClickHouse
File: src/Storages/ObjectStorage/DataLakes/Iceberg/Utils.cpp (C++)

Code before:
        else
        {
            column_name = args[0]->as<ASTIdentifier>()->name();
        }

        if (!clickhouse_name_to_iceberg.contains(clickhouse_name))
            throw Exception(ErrorCodes::BAD_ARGUMENTS, "Unsupported function {} for iceberg", clickhouse_name);
        const auto & transform_base = clickhouse_name_to_iceberg.at(clickhouse_name);

        String transform = transform_base;
        if (arg.has_value())
            transform += "[" + std::to_string(arg.value()) + "]";

        return {transform, column_name};
    };

    if (auto * func = object->as<ASTFunction>(); func->name != "tuple")
    {
        return parse_function(object);
    }

    auto function_desc = object->children[0]->children[i]->children[0];
    if (auto * identifier = function_desc->as<ASTIdentifier>(); identifier)
        return {"identity", identifier->name()};

    return parse_function(function_desc);
}

std::pair<Poco::JSON::Object::Ptr, String> createEmptyMetadataFile(
    String path_location,
    const ColumnsDescription & columns,
    ASTPtr partition_by,
    ASTPtr order_by,
    ContextPtr context,
    UInt64 format_version)
{
    std::unordered_map<String, Int32> column_name_to_source_id;
    static Poco::UUIDGenerator uuid_generator;

    Poco::JSON::Object::Ptr new_metadata_file_content = new Poco::JSON::Object;
    new_metadata_file_content->set(Iceberg::f_format_version, format_version);
    new_metadata_file_content->set(Iceberg::f_table_uuid, uuid_generator.createRandom().toString());
    new_metadata_file_content->set(Iceberg::f_location, path_location);
    if (format_version > 1)
        new_metadata_file_content->set(Iceberg::f_last_sequence_number, 0);

    auto now = std::chrono::system_clock::now();
    auto ms = duration_cast<std::chrono::milliseconds>(now.time_since_epoch());
    new_metadata_file_content->set(Iceberg::f_last_updated_ms, ms.count());
    new_metadata_file_content->set(Iceberg::f_last_column_id, columns.size());
    new_metadata_file_content->set(Iceberg::f_current_schema_id, 0);

Review comment (bug):
I'd prefer a full exception instead of just assert to avoid crashes in non-debug build, but ok

Code after:
        else
            column_name = args[0]->as<ASTIdentifier>()->name();

        if (!clickhouse_name_to_iceberg.contains(clickhouse_name))
            throw Exception(ErrorCodes::BAD_ARGUMENTS, "Unsupported function {} for iceberg", clickhouse_name);
        const auto & transform_base = clickhouse_name_to_iceberg.at(clickhouse_name);

        String transform = transform_base;
        if (arg.has_value())
            transform += "[" + std::to_string(arg.value()) + "]";

        return {transform, column_name};
    };

    if (auto * func = object->as<ASTFunction>(); func->name != "tuple")
    {
        return parse_function(object);
    }

    chassert(!object->children.empty());
    chassert(object->children[0]->as<ASTExpressionList>());
    auto function_desc = object->children[0]->children[i];
    if (auto * identifier = function_desc->as<ASTIdentifier>(); identifier)
        return {"identity", identifier->name()};

    chassert(!function_desc->children.empty());
    function_desc = function_desc->children[0];
    if (auto * identifier = function_desc->as<ASTIdentifier>(); identifier)
        return {"identity", identifier->name()};

    return parse_function(function_desc);
}

std::pair<Poco::JSON::Object::Ptr, String> createEmptyMetadataFile(
    String path_location,
    const ColumnsDescription & columns,
    ASTPtr partition_by,
    ASTPtr order_by,
    ContextPtr context,
    UInt64 format_version)
{
    std::unordered_map<String, Int32> column_name_to_source_id;
    static Poco::UUIDGenerator uuid_generator;

    Poco::JSON::Object::Ptr new_metadata_file_content = new Poco::JSON::Object;
    new_metadata_file_content->set(Iceberg::f_format_version, format_version);
    new_metadata_file_content->set(Iceberg::f_table_uuid, uuid_generator.createRandom().toString());
    new_metadata_file_content->set(Iceberg::f_location, path_location);
    if (format_version > 1)
        new_metadata_file_content->set(Iceberg::f_last_sequence_number, 0);