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

Code before:
    if (columns.empty())
    {
        if (configuration->isDataLakeConfiguration())
        {
            auto table_structure = configuration->tryGetTableStructureFromMetadata(context);
            if (table_structure)
                columns = table_structure.value();
        }

        if (columns.empty())
        {
            if (format == "auto")
            {
                std::tie(columns, format) = StorageObjectStorage::resolveSchemaAndFormatFromData(
                    object_storage, configuration, format_settings, sample_path, context);
            }
            else
            {
                chassert(!format.empty());
                columns = StorageObjectStorage::resolveSchemaFromData(object_storage, configuration, format_settings, sample_path, context);
            }
        }
    }
    else if (format == "auto")
    {
        format = StorageObjectStorage::resolveFormatFromData(object_storage, configuration, format_settings, sample_path, context);
    }
}

void validateColumns(
    const ColumnsDescription & columns,
    const StorageObjectStorageConfiguration & configuration,
    bool validate_schema_with_remote,
    ObjectStoragePtr object_storage,
    StorageObjectStorageConfigurationPtr config_ptr,
    const std::optional<FormatSettings> * format_settings,
    const std::string * sample_path,
    ContextPtr context,
    const NamesAndTypesList * hive_partition_columns_to_read_from_file_path,
    const ColumnsDescription * columns_in_table_or_function_definition,
    LoggerPtr log)
{
    if (!columns.hasOnlyOrdinary())
    {
        /// We don't allow special columns.
        throw Exception(ErrorCodes::BAD_ARGUMENTS,
            "Special columns like MATERIALIZED, ALIAS or EPHEMERAL are not supported for {} storage.",
            configuration.getTypeName());
    }

    /// If schema validation parameters are not provided, skip schema consistency check.

Review comment (suggestion):
I think we use  `resolveSchemaAndFormat` not only in `StorageObjectStorage.cpp`, but in other places, while after this change validation will not be done in this cases 🤔

Easiest fix would probably be to return this back, and call `validateColumns` only if we did not call `resolveSchemaAndFormat` already. But you can come up with a different solution if you have better ideas.



Code after:

    if (columns.empty())
    {
        if (configuration->isDataLakeConfiguration())
        {
            auto table_structure = configuration->tryGetTableStructureFromMetadata(context);
            if (table_structure)
                columns = table_structure.value();
        }

        if (columns.empty())
        {
            if (format == "auto")
            {
                std::tie(columns, format) = StorageObjectStorage::resolveSchemaAndFormatFromData(
                    object_storage, configuration, format_settings, sample_path, context);
            }
            else
            {
                chassert(!format.empty());
                columns = StorageObjectStorage::resolveSchemaFromData(object_storage, configuration, format_settings, sample_path, context);
            }
        }
    }
    else if (format == "auto")
    {
        format = StorageObjectStorage::resolveFormatFromData(object_storage, configuration, format_settings, sample_path, context);
    }

    validateColumns(columns, configuration);
}

void validateColumns(
    const ColumnsDescription & columns,
    StorageObjectStorageConfigurationPtr configuration,
    bool validate_schema_with_remote,
    ObjectStoragePtr object_storage,
    const std::optional<FormatSettings> * format_settings,
    const std::string * sample_path,
    ContextPtr context,
    const NamesAndTypesList * hive_partition_columns_to_read_from_file_path,
    const ColumnsDescription * columns_in_table_or_function_definition,
    LoggerPtr log)
{
    if (!columns.hasOnlyOrdinary())
    {
        /// We don't allow special columns.
        throw Exception(ErrorCodes::BAD_ARGUMENTS,
            "Special columns like MATERIALIZED, ALIAS or EPHEMERAL are not supported for {} storage.",
            configuration ? configuration->getTypeName() : "object storage");
    }