diff options
| author | vitalyisaev <[email protected]> | 2023-11-14 09:58:56 +0300 |
|---|---|---|
| committer | vitalyisaev <[email protected]> | 2023-11-14 10:20:20 +0300 |
| commit | c2b2dfd9827a400a8495e172a56343462e3ceb82 (patch) | |
| tree | cd4e4f597d01bede4c82dffeb2d780d0a9046bd0 /contrib/clickhouse/src/TableFunctions/TableFunctionDictionary.cpp | |
| parent | d4ae8f119e67808cb0cf776ba6e0cf95296f2df7 (diff) | |
YQ Connector: move tests from yql to ydb (OSS)
Перенос папки с тестами на Коннектор из папки yql в папку ydb (синхронизируется с github).
Diffstat (limited to 'contrib/clickhouse/src/TableFunctions/TableFunctionDictionary.cpp')
| -rw-r--r-- | contrib/clickhouse/src/TableFunctions/TableFunctionDictionary.cpp | 95 |
1 files changed, 95 insertions, 0 deletions
diff --git a/contrib/clickhouse/src/TableFunctions/TableFunctionDictionary.cpp b/contrib/clickhouse/src/TableFunctions/TableFunctionDictionary.cpp new file mode 100644 index 00000000000..f0060acb411 --- /dev/null +++ b/contrib/clickhouse/src/TableFunctions/TableFunctionDictionary.cpp @@ -0,0 +1,95 @@ +#include <TableFunctions/TableFunctionDictionary.h> + +#include <Parsers/ASTLiteral.h> + +#include <DataTypes/DataTypeArray.h> +#include <DataTypes/DataTypeString.h> +#include <DataTypes/DataTypesNumber.h> + +#include <Interpreters/Context.h> +#include <Interpreters/ExternalDictionariesLoader.h> +#include <Interpreters/evaluateConstantExpression.h> + +#include <Storages/StorageDictionary.h> +#include <Storages/checkAndGetLiteralArgument.h> + +#include <TableFunctions/TableFunctionFactory.h> + +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) +{ + // 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"); +} + +ColumnsDescription TableFunctionDictionary::getActualTableStructure(ContextPtr context, bool /*is_insert_query*/) const +{ + const ExternalDictionariesLoader & external_loader = context->getExternalDictionariesLoader(); + 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)); + +} + +StoragePtr TableFunctionDictionary::executeImpl( + const ASTPtr &, ContextPtr context, const std::string & table_name, ColumnsDescription, bool is_insert_query) const +{ + StorageID dict_id(getDatabaseName(), table_name); + auto dictionary_table_structure = getActualTableStructure(context, is_insert_query); + + auto result = std::make_shared<StorageDictionary>( + dict_id, dictionary_name, std::move(dictionary_table_structure), String{}, StorageDictionary::Location::Custom, context); + + return result; +} + +void registerTableFunctionDictionary(TableFunctionFactory & factory) +{ + factory.registerFunction<TableFunctionDictionary>(); +} + +} |
