2022-12-13 21:07:30 +00:00
|
|
|
#include "NamedCollectionsHelpers.h"
|
2022-12-16 22:57:09 +00:00
|
|
|
#include <Common/NamedCollections/NamedCollections.h>
|
2022-12-13 21:07:30 +00:00
|
|
|
#include <Interpreters/evaluateConstantExpression.h>
|
|
|
|
#include <Storages/checkAndGetLiteralArgument.h>
|
|
|
|
#include <Parsers/ASTIdentifier.h>
|
|
|
|
#include <Parsers/ASTFunction.h>
|
|
|
|
|
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
|
2022-12-22 11:15:34 +00:00
|
|
|
namespace ErrorCodes
|
|
|
|
{
|
|
|
|
extern const int BAD_ARGUMENTS;
|
|
|
|
}
|
|
|
|
|
2022-12-13 21:07:30 +00:00
|
|
|
namespace
|
|
|
|
{
|
|
|
|
NamedCollectionPtr tryGetNamedCollectionFromASTs(ASTs asts)
|
|
|
|
{
|
|
|
|
if (asts.empty())
|
|
|
|
return nullptr;
|
|
|
|
|
|
|
|
const auto * identifier = asts[0]->as<ASTIdentifier>();
|
|
|
|
if (!identifier)
|
|
|
|
return nullptr;
|
|
|
|
|
|
|
|
const auto & collection_name = identifier->name();
|
2022-12-22 14:29:35 +00:00
|
|
|
return NamedCollectionFactory::instance().get(collection_name);
|
2022-12-13 21:07:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
std::optional<std::pair<std::string, Field>> getKeyValueFromAST(ASTPtr ast)
|
|
|
|
{
|
|
|
|
const auto * function = ast->as<ASTFunction>();
|
|
|
|
if (!function || function->name != "equals")
|
|
|
|
return std::nullopt;
|
|
|
|
|
|
|
|
const auto * function_args_expr = assert_cast<const ASTExpressionList *>(function->arguments.get());
|
|
|
|
const auto & function_args = function_args_expr->children;
|
|
|
|
|
|
|
|
if (function_args.size() != 2)
|
|
|
|
return std::nullopt;
|
|
|
|
|
|
|
|
auto literal_key = evaluateConstantExpressionOrIdentifierAsLiteral(
|
|
|
|
function_args[0], Context::getGlobalContextInstance());
|
|
|
|
auto key = checkAndGetLiteralArgument<String>(literal_key, "key");
|
|
|
|
|
|
|
|
auto literal_value = evaluateConstantExpressionOrIdentifierAsLiteral(
|
|
|
|
function_args[1], Context::getGlobalContextInstance());
|
|
|
|
auto value = literal_value->as<ASTLiteral>()->value;
|
|
|
|
|
|
|
|
return std::pair{key, value};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
NamedCollectionPtr tryGetNamedCollectionWithOverrides(ASTs asts)
|
|
|
|
{
|
|
|
|
if (asts.empty())
|
|
|
|
return nullptr;
|
|
|
|
|
|
|
|
auto collection = tryGetNamedCollectionFromASTs(asts);
|
|
|
|
if (!collection)
|
|
|
|
return nullptr;
|
|
|
|
|
|
|
|
if (asts.size() == 1)
|
|
|
|
return collection;
|
|
|
|
|
|
|
|
auto collection_copy = collection->duplicate();
|
|
|
|
|
2023-01-04 14:29:06 +00:00
|
|
|
for (auto * it = std::next(asts.begin()); it != asts.end(); ++it)
|
2022-12-13 21:07:30 +00:00
|
|
|
{
|
2022-12-22 11:15:34 +00:00
|
|
|
auto value_override = getKeyValueFromAST(*it);
|
|
|
|
if (!value_override && !(*it)->as<ASTFunction>())
|
|
|
|
throw Exception(ErrorCodes::BAD_ARGUMENTS, "Expected key-value argument or function");
|
2022-12-13 21:07:30 +00:00
|
|
|
if (!value_override)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
const auto & [key, value] = *value_override;
|
2022-12-14 09:15:25 +00:00
|
|
|
collection_copy->setOrUpdate<String>(key, toString(value));
|
2022-12-13 21:07:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return collection_copy;
|
|
|
|
}
|
|
|
|
|
2022-12-16 22:57:09 +00:00
|
|
|
HTTPHeaderEntries getHeadersFromNamedCollection(const NamedCollection & collection)
|
|
|
|
{
|
|
|
|
HTTPHeaderEntries headers;
|
|
|
|
auto keys = collection.getKeys(0, "headers");
|
|
|
|
for (const auto & key : keys)
|
|
|
|
headers.emplace_back(collection.get<String>(key + ".name"), collection.get<String>(key + ".value"));
|
|
|
|
return headers;
|
|
|
|
}
|
|
|
|
|
2022-12-13 21:07:30 +00:00
|
|
|
}
|