2017-04-01 09:19:00 +00:00
|
|
|
#include <Columns/ColumnConst.h>
|
|
|
|
#include <Columns/ColumnVector.h>
|
2017-07-13 20:58:19 +00:00
|
|
|
#include <Common/typeid_cast.h>
|
2019-08-21 02:28:04 +00:00
|
|
|
#include <Common/assert_cast.h>
|
2023-06-28 15:48:54 +00:00
|
|
|
#include <Common/Exception.h>
|
2017-02-11 20:20:57 +00:00
|
|
|
|
|
|
|
#include <type_traits>
|
|
|
|
|
2022-12-11 18:57:30 +00:00
|
|
|
#include <libdivide-config.h>
|
2017-02-11 20:20:57 +00:00
|
|
|
#include <libdivide.h>
|
|
|
|
|
|
|
|
|
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
|
2023-06-28 15:48:54 +00:00
|
|
|
namespace ErrorCodes
|
|
|
|
{
|
|
|
|
extern const int LOGICAL_ERROR;
|
|
|
|
}
|
|
|
|
|
2017-02-11 20:20:57 +00:00
|
|
|
template <typename T>
|
|
|
|
IColumn::Selector createBlockSelector(
|
|
|
|
const IColumn & column,
|
2017-09-01 17:21:03 +00:00
|
|
|
const std::vector<UInt64> & slots)
|
2017-02-11 20:20:57 +00:00
|
|
|
{
|
|
|
|
const auto total_weight = slots.size();
|
2023-06-28 15:48:54 +00:00
|
|
|
if (total_weight == 0)
|
|
|
|
throw Exception(ErrorCodes::LOGICAL_ERROR, "weight is zero");
|
2020-07-05 00:28:08 +00:00
|
|
|
|
2017-02-11 20:20:57 +00:00
|
|
|
size_t num_rows = column.size();
|
|
|
|
IColumn::Selector selector(num_rows);
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2017-02-11 20:20:57 +00:00
|
|
|
/** Modulo of division of negative numbers to positive number in C++11 is negative (so called truncated division).
|
|
|
|
* This is not suitable for our task. So we will process signed numbers as unsigned.
|
|
|
|
* It is not near like remainder of division, but is suitable for our task.
|
|
|
|
*/
|
2020-08-19 11:52:17 +00:00
|
|
|
using UnsignedT = make_unsigned_t<T>;
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2017-02-11 20:20:57 +00:00
|
|
|
/// const columns contain only one value, therefore we do not need to read it at every iteration
|
2019-06-27 19:28:52 +00:00
|
|
|
if (isColumnConst(column))
|
2017-02-11 20:20:57 +00:00
|
|
|
{
|
2019-08-21 02:28:04 +00:00
|
|
|
const auto data = assert_cast<const ColumnConst &>(column).getValue<T>();
|
2017-02-11 20:20:57 +00:00
|
|
|
const auto shard_num = slots[static_cast<UnsignedT>(data) % total_weight];
|
|
|
|
selector.assign(num_rows, shard_num);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/// libdivide support only UInt32 and UInt64.
|
2017-12-25 04:01:46 +00:00
|
|
|
using TUInt32Or64 = std::conditional_t<sizeof(UnsignedT) <= 4, UInt32, UInt64>;
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2022-10-07 10:46:45 +00:00
|
|
|
libdivide::divider<TUInt32Or64> divider(static_cast<TUInt32Or64>(total_weight));
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2017-02-11 20:20:57 +00:00
|
|
|
const auto & data = typeid_cast<const ColumnVector<T> &>(column).getData();
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2017-02-11 20:20:57 +00:00
|
|
|
for (size_t i = 0; i < num_rows; ++i)
|
|
|
|
selector[i] = slots[static_cast<TUInt32Or64>(data[i]) - (static_cast<TUInt32Or64>(data[i]) / divider) * total_weight];
|
|
|
|
}
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2017-02-11 20:20:57 +00:00
|
|
|
return selector;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-04-18 03:03:39 +00:00
|
|
|
/// Explicit instantiations to avoid code bloat in headers.
|
2017-12-01 21:13:25 +00:00
|
|
|
template IColumn::Selector createBlockSelector<UInt8>(const IColumn & column, const std::vector<UInt64> & slots);
|
|
|
|
template IColumn::Selector createBlockSelector<UInt16>(const IColumn & column, const std::vector<UInt64> & slots);
|
|
|
|
template IColumn::Selector createBlockSelector<UInt32>(const IColumn & column, const std::vector<UInt64> & slots);
|
|
|
|
template IColumn::Selector createBlockSelector<UInt64>(const IColumn & column, const std::vector<UInt64> & slots);
|
|
|
|
template IColumn::Selector createBlockSelector<Int8>(const IColumn & column, const std::vector<UInt64> & slots);
|
|
|
|
template IColumn::Selector createBlockSelector<Int16>(const IColumn & column, const std::vector<UInt64> & slots);
|
|
|
|
template IColumn::Selector createBlockSelector<Int32>(const IColumn & column, const std::vector<UInt64> & slots);
|
|
|
|
template IColumn::Selector createBlockSelector<Int64>(const IColumn & column, const std::vector<UInt64> & slots);
|
2017-02-11 20:20:57 +00:00
|
|
|
|
|
|
|
}
|