2018-11-30 14:49:35 +00:00
|
|
|
#include <Functions/FunctionJoinGet.h>
|
|
|
|
|
2020-07-11 07:12:42 +00:00
|
|
|
#include <Columns/ColumnString.h>
|
2019-01-25 14:50:31 +00:00
|
|
|
#include <Functions/FunctionFactory.h>
|
2018-11-30 14:49:35 +00:00
|
|
|
#include <Functions/FunctionHelpers.h>
|
|
|
|
#include <Interpreters/Context.h>
|
2020-04-07 09:48:47 +00:00
|
|
|
#include <Interpreters/HashJoin.h>
|
2018-11-30 14:49:35 +00:00
|
|
|
#include <Storages/StorageJoin.h>
|
|
|
|
|
|
|
|
|
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
namespace ErrorCodes
|
|
|
|
{
|
|
|
|
extern const int ILLEGAL_TYPE_OF_ARGUMENT;
|
|
|
|
extern const int NUMBER_OF_ARGUMENTS_DOESNT_MATCH;
|
|
|
|
}
|
|
|
|
|
2020-07-11 07:12:42 +00:00
|
|
|
template <bool or_null>
|
2020-10-14 14:04:50 +00:00
|
|
|
void ExecutableFunctionJoinGet<or_null>::execute(ColumnsWithTypeAndName & columns, const ColumnNumbers & arguments, size_t result, size_t)
|
2020-07-11 07:12:42 +00:00
|
|
|
{
|
2020-10-09 07:41:28 +00:00
|
|
|
ColumnsWithTypeAndName keys;
|
2020-07-11 07:12:42 +00:00
|
|
|
for (size_t i = 2; i < arguments.size(); ++i)
|
|
|
|
{
|
2020-10-14 14:04:50 +00:00
|
|
|
auto key = columns[arguments[i]];
|
2020-10-09 07:41:28 +00:00
|
|
|
keys.emplace_back(std::move(key));
|
2020-07-11 07:12:42 +00:00
|
|
|
}
|
2020-10-14 14:04:50 +00:00
|
|
|
columns[result] = join->joinGet(keys, result_columns);
|
2020-07-11 07:12:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
template <bool or_null>
|
2020-10-14 13:09:11 +00:00
|
|
|
ExecutableFunctionImplPtr FunctionJoinGet<or_null>::prepare(const ColumnsWithTypeAndName &, const ColumnNumbers &, size_t) const
|
2020-07-11 07:12:42 +00:00
|
|
|
{
|
2020-10-09 07:41:28 +00:00
|
|
|
return std::make_unique<ExecutableFunctionJoinGet<or_null>>(join, DB::Block{{return_type->createColumn(), return_type, attr_name}});
|
2020-07-11 07:12:42 +00:00
|
|
|
}
|
2020-07-23 07:47:17 +00:00
|
|
|
|
2020-07-11 07:12:42 +00:00
|
|
|
static auto getJoin(const ColumnsWithTypeAndName & arguments, const Context & context)
|
|
|
|
{
|
2018-11-30 14:49:35 +00:00
|
|
|
String join_name;
|
2020-04-22 05:39:31 +00:00
|
|
|
if (const auto * name_col = checkAndGetColumnConst<ColumnString>(arguments[0].column.get()))
|
2018-11-30 14:49:35 +00:00
|
|
|
{
|
|
|
|
join_name = name_col->getValue<String>();
|
|
|
|
}
|
|
|
|
else
|
2020-07-11 07:12:42 +00:00
|
|
|
throw Exception(
|
|
|
|
"Illegal type " + arguments[0].type->getName() + " of first argument of function joinGet, expected a const string.",
|
|
|
|
ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT);
|
2018-11-30 14:49:35 +00:00
|
|
|
|
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();
|
|
|
|
dot = 0;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
database_name = join_name.substr(0, dot);
|
|
|
|
++dot;
|
|
|
|
}
|
|
|
|
String table_name = join_name.substr(dot);
|
2020-07-11 07:12:42 +00:00
|
|
|
if (table_name.empty())
|
|
|
|
throw Exception("joinGet does not allow empty table name", ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT);
|
2020-05-28 23:01:18 +00:00
|
|
|
auto table = DatabaseCatalog::instance().getTable({database_name, table_name}, context);
|
2019-01-25 14:50:31 +00:00
|
|
|
auto storage_join = std::dynamic_pointer_cast<StorageJoin>(table);
|
2018-11-30 14:49:35 +00:00
|
|
|
if (!storage_join)
|
2020-07-11 07:12:42 +00:00
|
|
|
throw Exception("Table " + join_name + " should have engine StorageJoin", ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT);
|
2018-11-30 14:49:35 +00:00
|
|
|
|
|
|
|
String attr_name;
|
2020-04-22 05:39:31 +00:00
|
|
|
if (const auto * name_col = checkAndGetColumnConst<ColumnString>(arguments[1].column.get()))
|
2018-11-30 14:49:35 +00:00
|
|
|
{
|
|
|
|
attr_name = name_col->getValue<String>();
|
|
|
|
}
|
|
|
|
else
|
2020-07-11 07:12:42 +00:00
|
|
|
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);
|
|
|
|
}
|
2018-11-30 14:49:35 +00:00
|
|
|
|
2020-04-07 14:52:32 +00:00
|
|
|
template <bool or_null>
|
|
|
|
FunctionBaseImplPtr JoinGetOverloadResolver<or_null>::build(const ColumnsWithTypeAndName & arguments, const DataTypePtr &) const
|
2019-01-25 14:50:31 +00:00
|
|
|
{
|
2020-07-11 07:12:42 +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);
|
2019-01-25 14:50:31 +00:00
|
|
|
auto [storage_join, attr_name] = getJoin(arguments, context);
|
|
|
|
auto join = storage_join->getJoin();
|
2020-07-11 07:12:42 +00:00
|
|
|
DataTypes data_types(arguments.size() - 2);
|
|
|
|
for (size_t i = 2; i < arguments.size(); ++i)
|
|
|
|
data_types[i - 2] = arguments[i].type;
|
|
|
|
auto return_type = join->joinGetCheckAndGetReturnType(data_types, attr_name, or_null);
|
2020-06-18 16:10:47 +00:00
|
|
|
auto table_lock = storage_join->lockForShare(context.getInitialQueryId(), context.getSettingsRef().lock_acquire_timeout);
|
2020-04-07 14:52:32 +00:00
|
|
|
return std::make_unique<FunctionJoinGet<or_null>>(table_lock, storage_join, join, attr_name, data_types, return_type);
|
2018-11-30 14:49:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void registerFunctionJoinGet(FunctionFactory & factory)
|
|
|
|
{
|
2020-04-07 14:52:32 +00:00
|
|
|
// joinGet
|
|
|
|
factory.registerFunction<JoinGetOverloadResolver<false>>();
|
|
|
|
// joinGetOrNull
|
|
|
|
factory.registerFunction<JoinGetOverloadResolver<true>>();
|
2018-11-30 14:49:35 +00:00
|
|
|
}
|
|
|
|
|
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>;
|
2018-11-30 14:49:35 +00:00
|
|
|
}
|