2020-07-11 07:12:42 +00:00
|
|
|
#include <Columns/ColumnString.h>
|
2024-03-19 16:04:29 +00:00
|
|
|
#include <Core/Block.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>
|
2024-03-19 16:04:29 +00:00
|
|
|
#include <Functions/IFunction.h>
|
2018-11-30 14:49:35 +00:00
|
|
|
#include <Interpreters/Context.h>
|
2024-03-19 16:04:29 +00:00
|
|
|
#include <Interpreters/DatabaseCatalog.h>
|
2020-04-07 09:48:47 +00:00
|
|
|
#include <Interpreters/HashJoin.h>
|
2023-11-06 03:09:28 +00:00
|
|
|
#include <Storages/StorageJoin.h>
|
|
|
|
#include <Storages/TableLockHolder.h>
|
|
|
|
|
2018-11-30 14:49:35 +00:00
|
|
|
|
|
|
|
namespace DB
|
|
|
|
{
|
2023-11-06 03:09:28 +00:00
|
|
|
|
2018-11-30 14:49:35 +00:00
|
|
|
namespace ErrorCodes
|
|
|
|
{
|
|
|
|
extern const int ILLEGAL_TYPE_OF_ARGUMENT;
|
|
|
|
extern const int NUMBER_OF_ARGUMENTS_DOESNT_MATCH;
|
|
|
|
}
|
|
|
|
|
2023-11-06 03:09:28 +00:00
|
|
|
class HashJoin;
|
|
|
|
using StorageJoinPtr = std::shared_ptr<StorageJoin>;
|
|
|
|
|
2023-11-06 22:34:21 +00:00
|
|
|
namespace
|
|
|
|
{
|
|
|
|
|
2023-11-06 03:09:28 +00:00
|
|
|
template <bool or_null>
|
|
|
|
class ExecutableFunctionJoinGet final : public IExecutableFunction, WithContext
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
ExecutableFunctionJoinGet(ContextPtr context_,
|
|
|
|
TableLockHolder table_lock_,
|
|
|
|
StorageJoinPtr storage_join_,
|
|
|
|
const DB::Block & result_columns_)
|
|
|
|
: WithContext(context_)
|
|
|
|
, table_lock(std::move(table_lock_))
|
|
|
|
, storage_join(std::move(storage_join_))
|
|
|
|
, result_columns(result_columns_)
|
|
|
|
{}
|
|
|
|
|
|
|
|
static constexpr auto name = or_null ? "joinGetOrNull" : "joinGet";
|
|
|
|
|
|
|
|
bool useDefaultImplementationForNulls() const override { return false; }
|
|
|
|
bool useDefaultImplementationForLowCardinalityColumns() const override { return false; }
|
|
|
|
bool useDefaultImplementationForConstants() const override { return true; }
|
|
|
|
|
|
|
|
ColumnPtr executeImpl(const ColumnsWithTypeAndName & arguments, const DataTypePtr &, size_t input_rows_count) const override;
|
|
|
|
|
|
|
|
String getName() const override { return name; }
|
|
|
|
|
|
|
|
private:
|
|
|
|
TableLockHolder table_lock;
|
|
|
|
StorageJoinPtr storage_join;
|
|
|
|
DB::Block result_columns;
|
|
|
|
};
|
|
|
|
|
|
|
|
template <bool or_null>
|
|
|
|
class FunctionJoinGet final : public IFunctionBase, WithContext
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
static constexpr auto name = or_null ? "joinGetOrNull" : "joinGet";
|
|
|
|
|
|
|
|
FunctionJoinGet(ContextPtr context_,
|
|
|
|
TableLockHolder table_lock_,
|
|
|
|
StorageJoinPtr storage_join_, String attr_name_,
|
|
|
|
DataTypes argument_types_, DataTypePtr return_type_)
|
|
|
|
: WithContext(context_)
|
|
|
|
, table_lock(std::move(table_lock_))
|
|
|
|
, storage_join(storage_join_)
|
|
|
|
, attr_name(std::move(attr_name_))
|
|
|
|
, argument_types(std::move(argument_types_))
|
|
|
|
, return_type(std::move(return_type_))
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
String getName() const override { return name; }
|
|
|
|
|
|
|
|
bool isSuitableForShortCircuitArgumentsExecution(const DataTypesWithConstInfo & /*arguments*/) const override { return true; }
|
|
|
|
|
|
|
|
const DataTypes & getArgumentTypes() const override { return argument_types; }
|
|
|
|
const DataTypePtr & getResultType() const override { return return_type; }
|
|
|
|
|
|
|
|
ExecutableFunctionPtr prepare(const ColumnsWithTypeAndName &) const override;
|
|
|
|
|
|
|
|
private:
|
|
|
|
TableLockHolder table_lock;
|
|
|
|
StorageJoinPtr storage_join;
|
|
|
|
const String attr_name;
|
|
|
|
DataTypes argument_types;
|
|
|
|
DataTypePtr return_type;
|
|
|
|
};
|
|
|
|
|
|
|
|
template <bool or_null>
|
|
|
|
class JoinGetOverloadResolver final : public IFunctionOverloadResolver, WithContext
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
static constexpr auto name = or_null ? "joinGetOrNull" : "joinGet";
|
|
|
|
static FunctionOverloadResolverPtr create(ContextPtr context_) { return std::make_unique<JoinGetOverloadResolver>(context_); }
|
|
|
|
|
|
|
|
explicit JoinGetOverloadResolver(ContextPtr context_) : WithContext(context_) {}
|
|
|
|
|
|
|
|
bool isDeterministic() const override { return false; }
|
|
|
|
String getName() const override { return name; }
|
|
|
|
|
|
|
|
FunctionBasePtr buildImpl(const ColumnsWithTypeAndName & arguments, const DataTypePtr &) const override;
|
|
|
|
DataTypePtr getReturnTypeImpl(const ColumnsWithTypeAndName &) const override { return {}; } // Not used
|
|
|
|
|
|
|
|
bool useDefaultImplementationForNulls() const override { return false; }
|
|
|
|
bool useDefaultImplementationForLowCardinalityColumns() const override { return false; }
|
|
|
|
|
|
|
|
bool isVariadic() const override { return true; }
|
|
|
|
size_t getNumberOfArguments() const override { return 0; }
|
|
|
|
ColumnNumbers getArgumentsThatAreAlwaysConstant() const override { return {0, 1}; }
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2020-07-11 07:12:42 +00:00
|
|
|
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
|
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-17 21:41:50 +00:00
|
|
|
auto key = arguments[i];
|
2020-10-09 07:41:28 +00:00
|
|
|
keys.emplace_back(std::move(key));
|
2020-07-11 07:12:42 +00:00
|
|
|
}
|
2021-09-29 17:30:07 +00:00
|
|
|
return storage_join->joinGet(keys, result_columns, getContext()).column;
|
2020-07-11 07:12:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
template <bool or_null>
|
2021-05-15 17:33:15 +00:00
|
|
|
ExecutableFunctionPtr FunctionJoinGet<or_null>::prepare(const ColumnsWithTypeAndName &) const
|
2020-07-11 07:12:42 +00:00
|
|
|
{
|
2021-02-25 11:21:06 +00:00
|
|
|
Block result_columns {{return_type->createColumn(), return_type, attr_name}};
|
2021-09-29 17:30:07 +00:00
|
|
|
return std::make_unique<ExecutableFunctionJoinGet<or_null>>(getContext(), table_lock, storage_join, result_columns);
|
2020-07-11 07:12:42 +00:00
|
|
|
}
|
2020-07-23 07:47:17 +00:00
|
|
|
|
2023-11-08 23:22:29 +00:00
|
|
|
std::pair<std::shared_ptr<StorageJoin>, String>
|
2021-06-01 12:20:52 +00:00
|
|
|
getJoin(const ColumnsWithTypeAndName & arguments, ContextPtr context)
|
2020-07-11 07:12:42 +00:00
|
|
|
{
|
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
|
2023-01-23 21:13:58 +00:00
|
|
|
throw Exception(ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT,
|
|
|
|
"Illegal type {} of first argument of function joinGet, expected a const string.",
|
|
|
|
arguments[0].type->getName());
|
2018-11-30 14:49:35 +00:00
|
|
|
|
2021-09-13 19:11:16 +00:00
|
|
|
auto qualified_name = QualifiedTableName::parseFromString(join_name);
|
|
|
|
if (qualified_name.database.empty())
|
|
|
|
qualified_name.database = context->getCurrentDatabase();
|
|
|
|
|
|
|
|
auto table = DatabaseCatalog::instance().getTable({qualified_name.database, qualified_name.table}, std::const_pointer_cast<Context>(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)
|
2023-01-23 21:13:58 +00:00
|
|
|
throw Exception(ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT, "Table {} should have engine StorageJoin", join_name);
|
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
|
2023-01-23 21:13:58 +00:00
|
|
|
throw Exception(ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT,
|
|
|
|
"Illegal type {} of second argument of function joinGet, expected a const string.",
|
|
|
|
arguments[1].type->getName());
|
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>
|
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
|
|
|
{
|
2020-07-11 07:12:42 +00:00
|
|
|
if (arguments.size() < 3)
|
|
|
|
throw Exception(
|
2022-02-11 20:26:46 +00:00
|
|
|
ErrorCodes::NUMBER_OF_ARGUMENTS_DOESNT_MATCH,
|
|
|
|
"Number of arguments for function '{}' doesn't match: passed {}, should be greater or equal to 3",
|
|
|
|
getName() , arguments.size());
|
2021-04-10 23:33:54 +00:00
|
|
|
auto [storage_join, attr_name] = getJoin(arguments, getContext());
|
2020-07-11 07:12:42 +00:00
|
|
|
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;
|
|
|
|
}
|
2022-02-11 20:26:46 +00:00
|
|
|
|
|
|
|
auto return_type = storage_join->joinGetCheckAndGetReturnType(data_types, attr_name, or_null || storage_join->useNulls());
|
2021-04-10 23:33:54 +00:00
|
|
|
auto table_lock = storage_join->lockForShare(getContext()->getInitialQueryId(), getContext()->getSettingsRef().lock_acquire_timeout);
|
2021-04-22 15:14:58 +00:00
|
|
|
|
2022-02-11 20:26:46 +00:00
|
|
|
if (storage_join->useNulls())
|
|
|
|
return std::make_unique<FunctionJoinGet<true>>(getContext(), table_lock, storage_join, attr_name, argument_types, return_type);
|
|
|
|
|
2021-09-29 17:30:07 +00:00
|
|
|
return std::make_unique<FunctionJoinGet<or_null>>(getContext(), table_lock, storage_join, attr_name, argument_types, return_type);
|
2018-11-30 14:49:35 +00:00
|
|
|
}
|
|
|
|
|
2023-11-06 03:09:28 +00:00
|
|
|
}
|
|
|
|
|
2022-07-04 07:01:39 +00:00
|
|
|
REGISTER_FUNCTION(JoinGet)
|
2018-11-30 14:49:35 +00:00
|
|
|
{
|
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
|
|
|
}
|
|
|
|
|
|
|
|
}
|