Repository: ClickHouse/ClickHouse
File: src/Storages/NamedCollectionsHelpers.h (C/C++)

Code before:

#include <fmt/format.h>
#include <fmt/ranges.h>


namespace DB
{

namespace ErrorCodes
{
    extern const int BAD_ARGUMENTS;
}

/// Helper function to get named collection for table engine.
/// Table engines have collection name as first argument of ast and other arguments are key-value overrides.
/// If dependent_table_id is provided, registers the table as a dependency of the named collection.
MutableNamedCollectionPtr tryGetNamedCollectionWithOverrides(
    ASTs asts,
    ContextPtr context,
    bool throw_unknown_collection = true,
    std::vector<std::pair<std::string, ASTPtr>> * complex_args = nullptr,
    const StorageID * dependent_table_id = nullptr);

/// Helper function to get named collection for dictionary source.
/// Dictionaries have collection name as name argument of dict configuration and other arguments are overrides.
/// Also registers the dictionary as a dependency of the named collection, so that
/// DROP NAMED COLLECTION is blocked while the dictionary exists.
MutableNamedCollectionPtr tryGetNamedCollectionWithOverrides(
    const Poco::Util::AbstractConfiguration & config,
    const std::string & config_prefix,
    ContextPtr context);

/// Parses the ast as a key-value pair.
/// Throws an exception if the key cannot be parsed as a string literal.
/// If the value cannot be parsed as a literal or interpreted as a constant expression,
/// falls back to the AST value.
std::pair<std::string, Field> getKeyValueFromAST(ASTPtr ast, ContextPtr context);

/// Parses asts as key value pairs and returns a map of them.
std::map<String, Field> getParamsMapFromAST(ASTs asts, ContextPtr context);

HTTPHeaderEntries getHeadersFromNamedCollection(const NamedCollection & collection);

struct ExternalDatabaseEqualKeysSet
{
    static constexpr std::array<std::pair<std::string_view, std::string_view>, 5> equal_keys{
        std::pair{"username", "user"}, std::pair{"database", "db"}, std::pair{"hostname", "host"}, std::pair{"addresses_expr", "host"}, std::pair{"addresses_expr", "hostname"}};
};
struct MongoDBEqualKeysSet
{
    static constexpr std::array<std::pair<std::string_view, std::string_view>, 4> equal_keys{

Review comment (refactor):
It feels right to add here a comment about which dictionary is added as a dependency of the named collection (how the name of that dictionary is extracted from the `config`)

Code after:

#include <fmt/format.h>
#include <fmt/ranges.h>


namespace DB
{

namespace ErrorCodes
{
    extern const int BAD_ARGUMENTS;
}

/// Helper function to get named collection for table engine.
/// Table engines have collection name as first argument of ast and other arguments are key-value overrides.
/// If dependent_table_id is provided, registers the table as a dependency of the named collection.
MutableNamedCollectionPtr tryGetNamedCollectionWithOverrides(
    ASTs asts,
    ContextPtr context,
    bool throw_unknown_collection = true,
    std::vector<std::pair<std::string, ASTPtr>> * complex_args = nullptr,
    const StorageID * dependent_table_id = nullptr);

/// Helper function to get named collection for dictionary source.
/// Dictionaries have collection name as name argument of dict configuration and other arguments are overrides.
/// Also registers the dictionary as a dependency of the named collection, so that
/// DROP NAMED COLLECTION is blocked while the dictionary exists.
/// The dictionary's identity is derived from config_prefix, which has the form
/// "<dict_root>.source.<type>" (e.g. "dictionary.source.clickhouse"); the first
/// component is used as the dictionary root to call StorageID::fromDictionaryConfig.
MutableNamedCollectionPtr tryGetNamedCollectionWithOverrides(
    const Poco::Util::AbstractConfiguration & config,
    const std::string & config_prefix,
    ContextPtr context);

/// Parses the ast as a key-value pair.
/// Throws an exception if the key cannot be parsed as a string literal.
/// If the value cannot be parsed as a literal or interpreted as a constant expression,
/// falls back to the AST value.
std::pair<std::string, Field> getKeyValueFromAST(ASTPtr ast, ContextPtr context);

/// Parses asts as key value pairs and returns a map of them.
std::map<String, Field> getParamsMapFromAST(ASTs asts, ContextPtr context);

HTTPHeaderEntries getHeadersFromNamedCollection(const NamedCollection & collection);

struct ExternalDatabaseEqualKeysSet
{
    static constexpr std::array<std::pair<std::string_view, std::string_view>, 5> equal_keys{
        std::pair{"username", "user"}, std::pair{"database", "db"}, std::pair{"hostname", "host"}, std::pair{"addresses_expr", "host"}, std::pair{"addresses_expr", "hostname"}};
};