ClickHouse/src/Functions/FunctionJoinGet.cpp

121 lines
4.4 KiB
C++
Raw Normal View History

#include <Functions/FunctionJoinGet.h>
#include <Columns/ColumnString.h>
2019-01-25 14:50:31 +00:00
#include <Functions/FunctionFactory.h>
#include <Functions/FunctionHelpers.h>
#include <Interpreters/Context.h>
#include <Interpreters/HashJoin.h>
#include <Storages/StorageJoin.h>
namespace DB
{
namespace ErrorCodes
{
extern const int ILLEGAL_TYPE_OF_ARGUMENT;
extern const int NUMBER_OF_ARGUMENTS_DOESNT_MATCH;
}
template <bool or_null>
2021-05-15 17:33:15 +00:00
ColumnPtr ExecutableFunctionJoinGet<or_null>::executeImpl(const ColumnsWithTypeAndName & arguments, const DataTypePtr &, size_t) const
{
ColumnsWithTypeAndName keys;
for (size_t i = 2; i < arguments.size(); ++i)
{
2020-10-17 21:41:50 +00:00
auto key = arguments[i];
keys.emplace_back(std::move(key));
}
return storage_join->joinGet(keys, result_columns).column;
}
template <bool or_null>
2021-05-15 17:33:15 +00:00
ExecutableFunctionPtr FunctionJoinGet<or_null>::prepare(const ColumnsWithTypeAndName &) const
{
Block result_columns {{return_type->createColumn(), return_type, attr_name}};
return std::make_unique<ExecutableFunctionJoinGet<or_null>>(table_lock, storage_join, result_columns);
}
static std::pair<std::shared_ptr<StorageJoin>, String>
2021-06-01 12:20:52 +00:00
getJoin(const ColumnsWithTypeAndName & arguments, ContextPtr context)
{
String join_name;
2020-04-22 05:39:31 +00:00
if (const auto * name_col = checkAndGetColumnConst<ColumnString>(arguments[0].column.get()))
{
join_name = name_col->getValue<String>();
}
else
throw Exception(
"Illegal type " + arguments[0].type->getName() + " of first argument of function joinGet, expected a const string.",
ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT);
2019-01-25 14:50:31 +00:00
size_t dot = join_name.find('.');
String database_name;
if (dot == String::npos)
{
database_name = context->getCurrentDatabase();
2019-01-25 14:50:31 +00:00
dot = 0;
}
else
{
database_name = join_name.substr(0, dot);
++dot;
}
String table_name = join_name.substr(dot);
if (table_name.empty())
throw Exception("joinGet does not allow empty table name", ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT);
auto table = DatabaseCatalog::instance().getTable({database_name, table_name}, std::const_pointer_cast<Context>(context));
2019-01-25 14:50:31 +00:00
auto storage_join = std::dynamic_pointer_cast<StorageJoin>(table);
if (!storage_join)
throw Exception("Table " + join_name + " should have engine StorageJoin", ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT);
String attr_name;
2020-04-22 05:39:31 +00:00
if (const auto * name_col = checkAndGetColumnConst<ColumnString>(arguments[1].column.get()))
{
attr_name = name_col->getValue<String>();
}
else
throw Exception(
"Illegal type " + arguments[1].type->getName() + " of second argument of function joinGet, expected a const string.",
ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT);
2019-01-25 14:50:31 +00:00
return std::make_pair(storage_join, attr_name);
}
2020-04-07 14:52:32 +00:00
template <bool or_null>
2021-05-15 17:33:15 +00:00
FunctionBasePtr JoinGetOverloadResolver<or_null>::buildImpl(const ColumnsWithTypeAndName & arguments, const DataTypePtr &) const
2019-01-25 14:50:31 +00:00
{
if (arguments.size() < 3)
throw Exception(
"Number of arguments for function " + getName() + " doesn't match: passed " + toString(arguments.size())
+ ", should be greater or equal to 3",
ErrorCodes::NUMBER_OF_ARGUMENTS_DOESNT_MATCH);
auto [storage_join, attr_name] = getJoin(arguments, getContext());
DataTypes data_types(arguments.size() - 2);
2021-04-22 15:14:58 +00:00
DataTypes argument_types(arguments.size());
for (size_t i = 0; i < arguments.size(); ++i)
{
if (i >= 2)
data_types[i - 2] = arguments[i].type;
argument_types[i] = arguments[i].type;
}
auto return_type = storage_join->joinGetCheckAndGetReturnType(data_types, attr_name, or_null);
auto table_lock = storage_join->lockForShare(getContext()->getInitialQueryId(), getContext()->getSettingsRef().lock_acquire_timeout);
2021-04-22 15:14:58 +00:00
return std::make_unique<FunctionJoinGet<or_null>>(table_lock, storage_join, attr_name, argument_types, return_type);
}
void registerFunctionJoinGet(FunctionFactory & factory)
{
2020-04-07 14:52:32 +00:00
// joinGet
factory.registerFunction<JoinGetOverloadResolver<false>>();
// joinGetOrNull
factory.registerFunction<JoinGetOverloadResolver<true>>();
}
2020-04-07 14:52:32 +00:00
template class ExecutableFunctionJoinGet<true>;
template class ExecutableFunctionJoinGet<false>;
template class FunctionJoinGet<true>;
template class FunctionJoinGet<false>;
template class JoinGetOverloadResolver<true>;
template class JoinGetOverloadResolver<false>;
}