add missing files

This commit is contained in:
lgbo-ustc 2022-02-28 21:06:47 +08:00 committed by liangjiabiao
parent 99cd25d70e
commit c070f76260
2 changed files with 128 additions and 0 deletions

View File

@ -0,0 +1,90 @@
#include <TableFunctions/Hive/TableFunctionHive.h>
#if USE_HIVE
#include <memory>
#include <type_traits>
#include <Common/Exception.h>
#include <Common/ErrorCodes.h>
#include <Parsers/ASTLiteral.h>
#include <Parsers/ParserPartition.h>
#include <Parsers/ExpressionListParsers.h>
#include <Parsers/parseQuery.h>
#include <Interpreters/Context.h>
#include <Interpreters/evaluateConstantExpression.h>
#include <Storages/Hive/HiveSettings.h>
#include <Storages/Hive/StorageHive.h>
#include <TableFunctions/TableFunctionFactory.h>
#include <TableFunctions/parseColumnsListForTableFunction.h>
#include <base/logger_useful.h>
namespace DB
{
namespace ErrorCodes
{
extern const int NUMBER_OF_ARGUMENTS_DOESNT_MATCH;
}
void TableFunctionHive::parseArguments(const ASTPtr & ast_function_, ContextPtr context_)
{
ASTs & args_func = ast_function_->children;
if (args_func.size() != 1)
throw Exception(ErrorCodes::NUMBER_OF_ARGUMENTS_DOESNT_MATCH, "Table function '{}' must have arguments.", getName());
ASTs & args = args_func.at(0)->children;
const auto message = fmt::format(
"The signature of function {} is:\n"
" - hive_url, hive_database, hive_table, structure, partition_by_keys",
getName());
if (args.size() != 5)
throw Exception(ErrorCodes::NUMBER_OF_ARGUMENTS_DOESNT_MATCH, message);
for (auto & arg : args)
arg = evaluateConstantExpressionOrIdentifierAsLiteral(arg, context_);
hive_metastore_url = args[1]->as<ASTLiteral &>().value.safeGet<String>();
hive_database = args[2]->as<ASTLiteral &>().value.safeGet<String>();
hive_table = args[3]->as<ASTLiteral &>().value.safeGet<String>();
table_structure = args[4]->as<ASTLiteral &>().value.safeGet<String>();
partition_by_def = args[5]->as<ASTLiteral &>().value.safeGet<String>();
actual_columns = parseColumnsListFromString(table_structure, context_);
}
ColumnsDescription TableFunctionHive::getActualTableStructure(ContextPtr /*context_*/) const { return actual_columns; }
StoragePtr TableFunctionHive::executeImpl(
const ASTPtr & /*ast_function_*/,
ContextPtr context_,
const std::string & table_name_,
ColumnsDescription /*cached_columns_*/) const
{
const Settings & settings = context_->getSettings();
ParserLambdaExpression partition_by_parser;
ASTPtr partition_by_ast = parseQuery(
partition_by_parser,
"(" + partition_by_def + ")",
"partition by declaration list",
settings.max_query_size,
settings.max_parser_depth);
StoragePtr storage;
storage = StorageHive::create(
hive_metastore_url,
hive_database,
hive_table,
StorageID(getDatabaseName(), table_name_),
actual_columns,
ConstraintsDescription{},
"",
partition_by_ast,
std::make_unique<HiveSettings>(),
context_);
return storage;
}
void registerTableFunctionHive(TableFunctionFactory & factory_) { factory_.registerFunction<TableFunctionHive>(); }
}
#endif

View File

@ -0,0 +1,38 @@
#pragma once
#include <Common/config.h>
#if USE_HIVE
#include <TableFunctions/ITableFunction.h>
#include <Poco/Logger.h>
namespace DB
{
class Context;
class TableFunctionHive : public ITableFunction
{
public:
static constexpr auto name = "hive";
static constexpr auto storage_type_name = "hive";
std::string getName() const override { return name; }
bool hasStaticStructure() const override { return true; }
StoragePtr executeImpl(
const ASTPtr & ast_function, ContextPtr context, const std::string & table_name, ColumnsDescription cached_columns) const override;
const char * getStorageTypeName() const override { return storage_type_name; }
ColumnsDescription getActualTableStructure(ContextPtr) const override;
void parseArguments(const ASTPtr & ast_function_, ContextPtr context_) override;
private:
Poco::Logger * logger = &Poco::Logger::get("TableFunctionHive");
String cluster_name;
String hive_metastore_url;
String hive_database;
String hive_table;
String table_structure;
String partition_by_def;
ColumnsDescription actual_columns;
};
}
#endif