2021-03-03 08:36:20 +00:00
|
|
|
#include <memory>
|
|
|
|
#include <Columns/ColumnString.h>
|
|
|
|
#include <Core/Block.h>
|
|
|
|
#include <DataTypes/DataTypeString.h>
|
|
|
|
#include <Functions/FunctionFactory.h>
|
2021-05-17 07:30:42 +00:00
|
|
|
#include <Functions/IFunction.h>
|
2021-03-03 08:36:20 +00:00
|
|
|
#include <Storages/MergeTree/MergeTreePartition.h>
|
|
|
|
|
|
|
|
|
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
namespace ErrorCodes
|
|
|
|
{
|
|
|
|
extern const int NUMBER_OF_ARGUMENTS_DOESNT_MATCH;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-03-09 12:09:35 +00:00
|
|
|
/** partitionId(x, y, ...) is a function that computes partition ids of arguments.
|
2021-03-29 23:02:53 +00:00
|
|
|
* The function is slow and should not be called for large amount of rows.
|
2021-03-03 08:36:20 +00:00
|
|
|
*/
|
2021-03-09 12:09:35 +00:00
|
|
|
class FunctionPartitionId : public IFunction
|
2021-03-03 08:36:20 +00:00
|
|
|
{
|
|
|
|
public:
|
2021-03-09 12:09:35 +00:00
|
|
|
static constexpr auto name = "partitionId";
|
2021-03-03 08:36:20 +00:00
|
|
|
|
2021-06-01 12:20:52 +00:00
|
|
|
static FunctionPtr create(ContextPtr) { return std::make_shared<FunctionPartitionId>(); }
|
2021-03-03 08:36:20 +00:00
|
|
|
|
|
|
|
String getName() const override { return name; }
|
|
|
|
|
|
|
|
bool isVariadic() const override { return true; }
|
|
|
|
|
|
|
|
size_t getNumberOfArguments() const override { return 0; }
|
|
|
|
|
|
|
|
bool isInjective(const ColumnsWithTypeAndName & /*sample_columns*/) const override { return true; }
|
|
|
|
|
|
|
|
bool useDefaultImplementationForNulls() const override { return true; }
|
|
|
|
bool useDefaultImplementationForConstants() const override { return true; }
|
2021-06-22 16:21:23 +00:00
|
|
|
bool isSuitableForShortCircuitArgumentsExecution(const DataTypesWithConstInfo & /*arguments*/) const override { return true; }
|
2021-03-03 08:36:20 +00:00
|
|
|
|
|
|
|
DataTypePtr getReturnTypeImpl(const DataTypes & arguments) const override
|
|
|
|
{
|
|
|
|
if (arguments.empty())
|
|
|
|
throw Exception("Function " + getName() + " requires at least one argument.", ErrorCodes::NUMBER_OF_ARGUMENTS_DOESNT_MATCH);
|
|
|
|
|
|
|
|
return std::make_shared<DataTypeString>();
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual ColumnPtr executeImpl(const ColumnsWithTypeAndName & arguments, const DataTypePtr &, size_t input_rows_count) const override
|
|
|
|
{
|
2021-03-29 23:00:25 +00:00
|
|
|
Block sample_block(arguments);
|
2021-03-03 08:36:20 +00:00
|
|
|
size_t size = arguments.size();
|
|
|
|
|
|
|
|
auto result_column = ColumnString::create();
|
|
|
|
for (size_t j = 0; j < input_rows_count; ++j)
|
|
|
|
{
|
|
|
|
Row row(size);
|
|
|
|
for (size_t i = 0; i < size; ++i)
|
2021-03-29 23:00:25 +00:00
|
|
|
arguments[i].column->get(j, row[i]);
|
2021-03-03 08:36:20 +00:00
|
|
|
MergeTreePartition partition(std::move(row));
|
|
|
|
result_column->insert(partition.getID(sample_block));
|
|
|
|
}
|
|
|
|
return result_column;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2021-03-09 12:09:35 +00:00
|
|
|
void registerFunctionPartitionId(FunctionFactory & factory)
|
2021-03-03 08:36:20 +00:00
|
|
|
{
|
2021-03-09 12:09:35 +00:00
|
|
|
factory.registerFunction<FunctionPartitionId>();
|
2021-03-03 08:36:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|