ClickHouse/src/TableFunctions/TableFunctionDictionary.cpp

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

100 lines
3.7 KiB
C++
Raw Normal View History

2021-03-19 12:47:27 +00:00
#include <TableFunctions/TableFunctionDictionary.h>
#include <Parsers/ASTLiteral.h>
#include <Access/Common/AccessFlags.h>
2023-05-10 15:42:08 +00:00
#include <DataTypes/DataTypeArray.h>
#include <DataTypes/DataTypeString.h>
#include <DataTypes/DataTypesNumber.h>
2021-03-19 12:47:27 +00:00
#include <Interpreters/ExternalDictionariesLoader.h>
#include <Interpreters/evaluateConstantExpression.h>
2023-11-10 04:34:29 +00:00
#include <Interpreters/Context.h>
2021-03-19 12:47:27 +00:00
#include <Storages/StorageDictionary.h>
#include <Storages/checkAndGetLiteralArgument.h>
2021-03-19 12:47:27 +00:00
#include <TableFunctions/TableFunctionFactory.h>
2023-11-06 03:33:23 +00:00
2021-03-19 12:47:27 +00:00
namespace DB
{
namespace ErrorCodes
{
extern const int LOGICAL_ERROR;
extern const int NUMBER_OF_ARGUMENTS_DOESNT_MATCH;
}
void TableFunctionDictionary::parseArguments(const ASTPtr & ast_function, ContextPtr context)
2021-03-19 12:47:27 +00:00
{
// Parse args
ASTs & args_func = ast_function->children;
if (args_func.size() != 1)
throw Exception(ErrorCodes::LOGICAL_ERROR, "Table function ({}) must have arguments.", quoteString(getName()));
ASTs & args = args_func.at(0)->children;
if (args.size() != 1)
throw Exception(ErrorCodes::NUMBER_OF_ARGUMENTS_DOESNT_MATCH, "Table function ({}) requires 1 arguments", quoteString(getName()));
for (auto & arg : args)
arg = evaluateConstantExpressionOrIdentifierAsLiteral(arg, context);
dictionary_name = checkAndGetLiteralArgument<String>(args[0], "dictionary_name");
2021-03-19 12:47:27 +00:00
}
ColumnsDescription TableFunctionDictionary::getActualTableStructure(ContextPtr context, bool /*is_insert_query*/) const
2021-03-19 12:47:27 +00:00
{
const ExternalDictionariesLoader & external_loader = context->getExternalDictionariesLoader();
2023-05-10 15:42:08 +00:00
std::string resolved_name = external_loader.resolveDictionaryName(dictionary_name, context->getCurrentDatabase());
auto load_result = external_loader.load(resolved_name);
if (load_result)
{
/// for regexp tree dictionary, the table structure will be different with dictionary structure. it is:
/// - id. identifier of the tree node
/// - parent_id.
/// - regexp. the regular expression
/// - keys. the names of attributions of dictionary structure
/// - values. the values of each attribution
const auto dictionary = std::static_pointer_cast<const IDictionary>(load_result);
if (dictionary->getTypeName() == "RegExpTree")
{
return ColumnsDescription(NamesAndTypesList({
{"id", std::make_shared<DataTypeUInt64>()},
{"parent_id", std::make_shared<DataTypeUInt64>()},
{"regexp", std::make_shared<DataTypeString>()},
{"keys", std::make_shared<DataTypeArray>(std::make_shared<DataTypeString>())},
{"values", std::make_shared<DataTypeArray>(std::make_shared<DataTypeString>())}
}));
}
}
/// otherwise, we get table structure by dictionary structure.
auto dictionary_structure = external_loader.getDictionaryStructure(dictionary_name, context);
return ColumnsDescription(StorageDictionary::getNamesAndTypes(dictionary_structure));
2021-03-19 12:47:27 +00:00
}
StoragePtr TableFunctionDictionary::executeImpl(
const ASTPtr &, ContextPtr context, const std::string & table_name, ColumnsDescription, bool is_insert_query) const
2021-03-19 12:47:27 +00:00
{
context->checkAccess(AccessType::dictGet, getDatabaseName(), table_name);
2021-03-19 12:47:27 +00:00
StorageID dict_id(getDatabaseName(), table_name);
auto dictionary_table_structure = getActualTableStructure(context, is_insert_query);
2021-04-21 13:45:13 +00:00
auto result = std::make_shared<StorageDictionary>(
2021-05-02 16:33:45 +00:00
dict_id, dictionary_name, std::move(dictionary_table_structure), String{}, StorageDictionary::Location::Custom, context);
2021-04-21 13:45:13 +00:00
return result;
2021-03-19 12:47:27 +00:00
}
2023-11-06 03:33:23 +00:00
2021-03-19 12:47:27 +00:00
void registerTableFunctionDictionary(TableFunctionFactory & factory)
{
factory.registerFunction<TableFunctionDictionary>();
}
}