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

Code before:
            throw Exception(ErrorCodes::BAD_ARGUMENTS, "Invalid iceberg sort order function {}: arguments are missing", clickhouse_name);
        const auto & args = args_list->children;

        std::optional<size_t> arg;
        String column_name;

        if (args.size() == 1)
        {
            const auto * identifier = args[0] ? args[0]->as<ASTIdentifier>() : nullptr;
            if (!identifier)
                throw Exception(
                    ErrorCodes::BAD_ARGUMENTS,
                    "Invalid iceberg sort order function {}: expected a column identifier as an argument",
                    clickhouse_name);
            column_name = identifier->name();
        }
        else if (args.size() == 2)
        {
            const auto * literal = args[0] ? args[0]->as<ASTLiteral>() : nullptr;
            const auto * identifier = args[1] ? args[1]->as<ASTIdentifier>() : nullptr;
            if (!literal || !identifier)
                throw Exception(
                    ErrorCodes::BAD_ARGUMENTS,
                    "Invalid iceberg sort order function {}: expected (integer_literal, column_identifier)",
                    clickhouse_name);

            UInt64 u_param = 0;
            Int64 i_param = 0;
            if (literal->value.tryGet(u_param))
                arg = static_cast<size_t>(u_param);
            else if (literal->value.tryGet(i_param) && i_param >= 0)
                arg = static_cast<size_t>(i_param);
            else
                throw Exception(
                    ErrorCodes::BAD_ARGUMENTS,
                    "Invalid iceberg sort order function {}: expected a non-negative integer literal as first argument",
                    clickhouse_name);

            column_name = identifier->name();
        }
        else
        {
            throw Exception(
                ErrorCodes::BAD_ARGUMENTS,
                "Invalid iceberg sort order function {}: expected 1 or 2 arguments",
                clickhouse_name);
        }

        String transform = it->second;
        if (arg.has_value())
            transform += "[" + std::to_string(arg.value()) + "]";

Review comment (suggestion):
Let's break this error message on two and move each of them to the bottom to know exactly with which argument we encountered a problem

Code after:
    const String & clickhouse_name = func->name;
    const auto it = clickhouse_name_to_iceberg.find(clickhouse_name);
    if (it == clickhouse_name_to_iceberg.end())
        throw Exception(ErrorCodes::BAD_ARGUMENTS, "Unsupported function {} for iceberg", clickhouse_name);

    const auto * args_list = func->arguments ? func->arguments->as<ASTExpressionList>() : nullptr;
    if (!args_list)
        throw Exception(ErrorCodes::BAD_ARGUMENTS, "Invalid iceberg sort order function {}: arguments are missing", clickhouse_name);
    const auto & args = args_list->children;

    std::optional<size_t> arg;
    String column_name;

    if (args.size() == 1)
    {
        column_name = parseColumnArgument(args[0], clickhouse_name, "expected a column identifier as an argument");
    }
    else if (args.size() == 2)
    {
        const auto * literal = args[0] ? args[0]->as<ASTLiteral>() : nullptr;
        if (!literal)
            throw Exception(
                ErrorCodes::BAD_ARGUMENTS,
                "Invalid iceberg sort order function {}: expected (integer_literal, column_identifier), but there is no integer_literal",
                clickhouse_name);

        column_name = parseColumnArgument(args[1], clickhouse_name, "expected (integer_literal, column_identifier), but there is no column_identifier");

        UInt64 u_param = 0;
        Int64 i_param = 0;
        if (literal->value.tryGet(u_param))
            arg = static_cast<size_t>(u_param);
        else if (literal->value.tryGet(i_param) && i_param >= 0)
            arg = static_cast<size_t>(i_param);
        else
            throw Exception(
                ErrorCodes::BAD_ARGUMENTS,
                "Invalid iceberg sort order function {}: expected a non-negative integer literal as first argument",
                clickhouse_name);
    }
    else
    {
        throw Exception(
            ErrorCodes::BAD_ARGUMENTS,
            "Invalid iceberg sort order function {}: expected 1 or 2 arguments, but got {}",
            clickhouse_name, args.size());
    }

    String transform = it->second;
    if (arg.has_value())
        transform += "[" + std::to_string(arg.value()) + "]";