2018-11-30 14:49:35 +00:00
|
|
|
#include <Functions/FunctionJoinGet.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>
|
|
|
|
#include <Columns/ColumnString.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;
|
|
|
|
}
|
|
|
|
|
2019-01-25 14:50:31 +00:00
|
|
|
static auto getJoin(const ColumnsWithTypeAndName & arguments, const Context & context)
|
2018-11-30 14:49:35 +00:00
|
|
|
{
|
|
|
|
if (arguments.size() != 3)
|
2019-01-25 14:50:31 +00:00
|
|
|
throw Exception{"Function joinGet takes 3 arguments", ErrorCodes::NUMBER_OF_ARGUMENTS_DOESNT_MATCH};
|
2018-11-30 14:49:35 +00:00
|
|
|
|
|
|
|
String join_name;
|
|
|
|
if (auto name_col = checkAndGetColumnConst<ColumnString>(arguments[0].column.get()))
|
|
|
|
{
|
|
|
|
join_name = name_col->getValue<String>();
|
|
|
|
}
|
|
|
|
else
|
2019-01-25 14:50:31 +00:00
|
|
|
throw Exception{"Illegal type " + arguments[0].type->getName() + " of first argument of function joinGet, expected a const string.",
|
2018-11-30 14:49:35 +00:00
|
|
|
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();
|
|
|
|
dot = 0;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
database_name = join_name.substr(0, dot);
|
|
|
|
++dot;
|
|
|
|
}
|
|
|
|
String table_name = join_name.substr(dot);
|
2020-03-06 20:38:19 +00:00
|
|
|
auto table = DatabaseCatalog::instance().getTable({database_name, table_name});
|
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)
|
|
|
|
throw Exception{"Table " + join_name + " should have engine StorageJoin", ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT};
|
|
|
|
|
|
|
|
String attr_name;
|
|
|
|
if (auto name_col = checkAndGetColumnConst<ColumnString>(arguments[1].column.get()))
|
|
|
|
{
|
|
|
|
attr_name = name_col->getValue<String>();
|
|
|
|
}
|
|
|
|
else
|
2019-01-25 14:50:31 +00:00
|
|
|
throw Exception{"Illegal type " + arguments[1].type->getName()
|
|
|
|
+ " of second argument of function joinGet, expected a const string.",
|
2018-11-30 14:49:35 +00:00
|
|
|
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
|
|
|
|
2019-12-12 14:16:59 +00:00
|
|
|
FunctionBaseImplPtr JoinGetOverloadResolver::build(const ColumnsWithTypeAndName & arguments, const DataTypePtr &) const
|
2019-01-25 14:50:31 +00:00
|
|
|
{
|
|
|
|
auto [storage_join, attr_name] = getJoin(arguments, context);
|
|
|
|
auto join = storage_join->getJoin();
|
2018-11-30 14:49:35 +00:00
|
|
|
DataTypes data_types(arguments.size());
|
|
|
|
|
2020-04-01 12:43:09 +00:00
|
|
|
auto table_lock = storage_join->lockStructureForShare(false, context.getInitialQueryId());
|
2018-11-30 14:49:35 +00:00
|
|
|
for (size_t i = 0; i < arguments.size(); ++i)
|
|
|
|
data_types[i] = arguments[i].type;
|
|
|
|
|
2019-01-25 14:50:31 +00:00
|
|
|
auto return_type = join->joinGetReturnType(attr_name);
|
2019-12-09 14:41:55 +00:00
|
|
|
return std::make_unique<FunctionJoinGet>(table_lock, storage_join, join, attr_name, data_types, return_type);
|
2018-11-30 14:49:35 +00:00
|
|
|
}
|
|
|
|
|
2019-12-12 14:16:59 +00:00
|
|
|
DataTypePtr JoinGetOverloadResolver::getReturnType(const ColumnsWithTypeAndName & arguments) const
|
2019-01-25 14:50:31 +00:00
|
|
|
{
|
|
|
|
auto [storage_join, attr_name] = getJoin(arguments, context);
|
|
|
|
auto join = storage_join->getJoin();
|
|
|
|
return join->joinGetReturnType(attr_name);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-12-09 14:41:55 +00:00
|
|
|
void ExecutableFunctionJoinGet::execute(Block & block, const ColumnNumbers & arguments, size_t result, size_t input_rows_count)
|
2018-11-30 14:49:35 +00:00
|
|
|
{
|
2019-10-16 17:46:00 +00:00
|
|
|
auto ctn = block.getByPosition(arguments[2]);
|
|
|
|
if (isColumnConst(*ctn.column))
|
|
|
|
ctn.column = ctn.column->cloneResized(1);
|
2018-11-30 14:49:35 +00:00
|
|
|
ctn.name = ""; // make sure the key name never collide with the join columns
|
|
|
|
Block key_block = {ctn};
|
|
|
|
join->joinGet(key_block, attr_name);
|
2019-10-16 17:46:00 +00:00
|
|
|
auto & result_ctn = key_block.getByPosition(1);
|
|
|
|
if (isColumnConst(*ctn.column))
|
|
|
|
result_ctn.column = ColumnConst::create(result_ctn.column, input_rows_count);
|
|
|
|
block.getByPosition(result) = result_ctn;
|
2018-11-30 14:49:35 +00:00
|
|
|
}
|
|
|
|
|
2019-12-09 14:41:55 +00:00
|
|
|
ExecutableFunctionImplPtr FunctionJoinGet::prepare(const Block &, const ColumnNumbers &, size_t) const
|
|
|
|
{
|
|
|
|
return std::make_unique<ExecutableFunctionJoinGet>(join, attr_name);
|
|
|
|
}
|
|
|
|
|
2018-11-30 14:49:35 +00:00
|
|
|
void registerFunctionJoinGet(FunctionFactory & factory)
|
|
|
|
{
|
2019-12-12 14:16:59 +00:00
|
|
|
factory.registerFunction<JoinGetOverloadResolver>();
|
2018-11-30 14:49:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|