2016-01-13 21:05:11 +00:00
|
|
|
|
#if defined(__x86_64__)
|
|
|
|
|
#include <emmintrin.h>
|
|
|
|
|
#endif
|
2015-11-21 03:19:43 +00:00
|
|
|
|
|
|
|
|
|
#include <DB/Columns/IColumn.h>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
namespace DB
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
size_t countBytesInFilter(const IColumn::Filter & filt)
|
|
|
|
|
{
|
|
|
|
|
size_t count = 0;
|
|
|
|
|
|
|
|
|
|
/** NOTE: По идее, filt должен содержать только нолики и единички.
|
|
|
|
|
* Но, на всякий случай, здесь используется условие > 0 (на знаковые байты).
|
|
|
|
|
* Лучше было бы использовать != 0, то это не позволяет SSE2.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
const Int8 * pos = reinterpret_cast<const Int8 *>(&filt[0]);
|
|
|
|
|
const Int8 * end = pos + filt.size();
|
2016-01-13 21:05:11 +00:00
|
|
|
|
|
|
|
|
|
#if defined(__x86_64__)
|
|
|
|
|
const __m128i zero16 = _mm_setzero_si128();
|
2015-11-21 03:19:43 +00:00
|
|
|
|
const Int8 * end64 = pos + filt.size() / 64 * 64;
|
|
|
|
|
|
|
|
|
|
for (; pos < end64; pos += 64)
|
|
|
|
|
count += __builtin_popcountll(
|
|
|
|
|
static_cast<UInt64>(_mm_movemask_epi8(_mm_cmpgt_epi8(
|
|
|
|
|
_mm_loadu_si128(reinterpret_cast<const __m128i *>(pos)),
|
|
|
|
|
zero16)))
|
|
|
|
|
| (static_cast<UInt64>(_mm_movemask_epi8(_mm_cmpgt_epi8(
|
|
|
|
|
_mm_loadu_si128(reinterpret_cast<const __m128i *>(pos + 16)),
|
|
|
|
|
zero16))) << 16)
|
|
|
|
|
| (static_cast<UInt64>(_mm_movemask_epi8(_mm_cmpgt_epi8(
|
|
|
|
|
_mm_loadu_si128(reinterpret_cast<const __m128i *>(pos + 32)),
|
|
|
|
|
zero16))) << 32)
|
|
|
|
|
| (static_cast<UInt64>(_mm_movemask_epi8(_mm_cmpgt_epi8(
|
|
|
|
|
_mm_loadu_si128(reinterpret_cast<const __m128i *>(pos + 48)),
|
|
|
|
|
zero16))) << 48));
|
2016-01-13 21:05:11 +00:00
|
|
|
|
#endif
|
2015-11-21 03:19:43 +00:00
|
|
|
|
|
|
|
|
|
for (; pos < end; ++pos)
|
|
|
|
|
count += *pos > 0;
|
|
|
|
|
|
|
|
|
|
return count;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2016-01-11 21:46:36 +00:00
|
|
|
|
namespace ErrorCodes
|
|
|
|
|
{
|
|
|
|
|
extern const int SIZES_OF_COLUMNS_DOESNT_MATCH;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2015-11-21 03:19:43 +00:00
|
|
|
|
template <typename T>
|
|
|
|
|
void filterArraysImpl(
|
2016-04-15 00:33:21 +00:00
|
|
|
|
const PaddedPODArray<T> & src_elems, const IColumn::Offsets_t & src_offsets,
|
|
|
|
|
PaddedPODArray<T> & res_elems, IColumn::Offsets_t & res_offsets,
|
2015-12-05 07:01:18 +00:00
|
|
|
|
const IColumn::Filter & filt, ssize_t result_size_hint)
|
2015-11-21 03:19:43 +00:00
|
|
|
|
{
|
|
|
|
|
const size_t size = src_offsets.size();
|
|
|
|
|
if (size != filt.size())
|
|
|
|
|
throw Exception("Size of filter doesn't match size of column.", ErrorCodes::SIZES_OF_COLUMNS_DOESNT_MATCH);
|
|
|
|
|
|
2015-12-05 07:01:18 +00:00
|
|
|
|
if (result_size_hint)
|
|
|
|
|
{
|
|
|
|
|
res_offsets.reserve(result_size_hint > 0 ? result_size_hint : size);
|
|
|
|
|
|
|
|
|
|
if (result_size_hint < 0)
|
|
|
|
|
res_elems.reserve(src_elems.size());
|
|
|
|
|
else if (result_size_hint < 1000000000 && src_elems.size() < 1000000000) /// Избегаем переполнения.
|
2015-12-11 01:21:53 +00:00
|
|
|
|
res_elems.reserve((result_size_hint * src_elems.size() + size - 1) / size);
|
2015-12-05 07:01:18 +00:00
|
|
|
|
}
|
2015-11-21 03:19:43 +00:00
|
|
|
|
|
|
|
|
|
IColumn::Offset_t current_src_offset = 0;
|
|
|
|
|
|
|
|
|
|
const UInt8 * filt_pos = &filt[0];
|
|
|
|
|
const auto filt_end = filt_pos + size;
|
|
|
|
|
|
|
|
|
|
auto offsets_pos = &src_offsets[0];
|
|
|
|
|
const auto offsets_begin = offsets_pos;
|
|
|
|
|
|
|
|
|
|
/// copy array ending at *end_offset_ptr
|
|
|
|
|
const auto copy_array = [&] (const IColumn::Offset_t * offset_ptr)
|
|
|
|
|
{
|
|
|
|
|
const auto offset = offset_ptr == offsets_begin ? 0 : offset_ptr[-1];
|
|
|
|
|
const auto size = *offset_ptr - offset;
|
|
|
|
|
|
|
|
|
|
current_src_offset += size;
|
|
|
|
|
res_offsets.push_back(current_src_offset);
|
|
|
|
|
|
|
|
|
|
const auto elems_size_old = res_elems.size();
|
2015-12-11 01:08:19 +00:00
|
|
|
|
res_elems.resize(elems_size_old + size);
|
2015-11-21 03:19:43 +00:00
|
|
|
|
memcpy(&res_elems[elems_size_old], &src_elems[offset], size * sizeof(T));
|
|
|
|
|
};
|
|
|
|
|
|
2016-01-13 21:05:11 +00:00
|
|
|
|
#if defined(__x86_64__)
|
|
|
|
|
const __m128i zero_vec = _mm_setzero_si128();
|
|
|
|
|
static constexpr size_t SIMD_BYTES = 16;
|
|
|
|
|
const auto filt_end_aligned = filt_pos + size / SIMD_BYTES * SIMD_BYTES;
|
|
|
|
|
|
2015-11-21 03:19:43 +00:00
|
|
|
|
while (filt_pos < filt_end_aligned)
|
|
|
|
|
{
|
|
|
|
|
const auto mask = _mm_movemask_epi8(_mm_cmpgt_epi8(
|
|
|
|
|
_mm_loadu_si128(reinterpret_cast<const __m128i *>(filt_pos)),
|
|
|
|
|
zero_vec));
|
|
|
|
|
|
|
|
|
|
if (mask == 0)
|
|
|
|
|
{
|
|
|
|
|
/// SIMD_BYTES consecutive rows do not pass the filter
|
|
|
|
|
}
|
|
|
|
|
else if (mask == 0xffff)
|
|
|
|
|
{
|
|
|
|
|
/// SIMD_BYTES consecutive rows pass the filter
|
|
|
|
|
const auto first = offsets_pos == offsets_begin;
|
|
|
|
|
|
|
|
|
|
const auto chunk_offset = first ? 0 : offsets_pos[-1];
|
|
|
|
|
const auto chunk_size = offsets_pos[SIMD_BYTES - 1] - chunk_offset;
|
|
|
|
|
|
|
|
|
|
const auto offsets_size_old = res_offsets.size();
|
|
|
|
|
res_offsets.resize(offsets_size_old + SIMD_BYTES);
|
|
|
|
|
memcpy(&res_offsets[offsets_size_old], offsets_pos, SIMD_BYTES * sizeof(IColumn::Offset_t));
|
|
|
|
|
|
|
|
|
|
if (!first)
|
|
|
|
|
{
|
|
|
|
|
/// difference between current and actual offset
|
|
|
|
|
const auto diff_offset = chunk_offset - current_src_offset;
|
|
|
|
|
|
|
|
|
|
if (diff_offset > 0)
|
|
|
|
|
{
|
|
|
|
|
const auto res_offsets_pos = &res_offsets[offsets_size_old];
|
|
|
|
|
|
|
|
|
|
/// adjust offsets
|
|
|
|
|
for (size_t i = 0; i < SIMD_BYTES; ++i)
|
|
|
|
|
res_offsets_pos[i] -= diff_offset;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
current_src_offset += chunk_size;
|
|
|
|
|
|
|
|
|
|
/// copy elements for SIMD_BYTES arrays at once
|
|
|
|
|
const auto elems_size_old = res_elems.size();
|
|
|
|
|
res_elems.resize(elems_size_old + chunk_size);
|
|
|
|
|
memcpy(&res_elems[elems_size_old], &src_elems[chunk_offset], chunk_size * sizeof(T));
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
for (size_t i = 0; i < SIMD_BYTES; ++i)
|
|
|
|
|
if (filt_pos[i])
|
|
|
|
|
copy_array(offsets_pos + i);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
filt_pos += SIMD_BYTES;
|
|
|
|
|
offsets_pos += SIMD_BYTES;
|
|
|
|
|
}
|
2016-01-13 21:05:11 +00:00
|
|
|
|
#endif
|
2015-11-21 03:19:43 +00:00
|
|
|
|
|
|
|
|
|
while (filt_pos < filt_end)
|
|
|
|
|
{
|
|
|
|
|
if (*filt_pos)
|
|
|
|
|
copy_array(offsets_pos);
|
|
|
|
|
|
|
|
|
|
++filt_pos;
|
|
|
|
|
++offsets_pos;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/// Явные инстанцирования - чтобы не размещать реализацию функции выше в заголовочном файле.
|
|
|
|
|
template void filterArraysImpl<UInt8>(
|
2016-04-15 00:33:21 +00:00
|
|
|
|
const PaddedPODArray<UInt8> &, const IColumn::Offsets_t &,
|
|
|
|
|
PaddedPODArray<UInt8> &, IColumn::Offsets_t &,
|
|
|
|
|
const IColumn::Filter &, ssize_t);
|
2015-11-21 03:19:43 +00:00
|
|
|
|
template void filterArraysImpl<UInt16>(
|
2016-04-15 00:33:21 +00:00
|
|
|
|
const PaddedPODArray<UInt16> &, const IColumn::Offsets_t &,
|
|
|
|
|
PaddedPODArray<UInt16> &, IColumn::Offsets_t &,
|
|
|
|
|
const IColumn::Filter &, ssize_t);
|
2015-11-21 03:19:43 +00:00
|
|
|
|
template void filterArraysImpl<UInt32>(
|
2016-04-15 00:33:21 +00:00
|
|
|
|
const PaddedPODArray<UInt32> &, const IColumn::Offsets_t &,
|
|
|
|
|
PaddedPODArray<UInt32> &, IColumn::Offsets_t &,
|
|
|
|
|
const IColumn::Filter &, ssize_t);
|
2015-11-21 03:19:43 +00:00
|
|
|
|
template void filterArraysImpl<UInt64>(
|
2016-04-15 00:33:21 +00:00
|
|
|
|
const PaddedPODArray<UInt64> &, const IColumn::Offsets_t &,
|
|
|
|
|
PaddedPODArray<UInt64> &, IColumn::Offsets_t &,
|
|
|
|
|
const IColumn::Filter &, ssize_t);
|
2015-11-21 03:19:43 +00:00
|
|
|
|
template void filterArraysImpl<Int8>(
|
2016-04-15 00:33:21 +00:00
|
|
|
|
const PaddedPODArray<Int8> &, const IColumn::Offsets_t &,
|
|
|
|
|
PaddedPODArray<Int8> &, IColumn::Offsets_t &,
|
|
|
|
|
const IColumn::Filter &, ssize_t);
|
2015-11-21 03:19:43 +00:00
|
|
|
|
template void filterArraysImpl<Int16>(
|
2016-04-15 00:33:21 +00:00
|
|
|
|
const PaddedPODArray<Int16> &, const IColumn::Offsets_t &,
|
|
|
|
|
PaddedPODArray<Int16> &, IColumn::Offsets_t &,
|
|
|
|
|
const IColumn::Filter &, ssize_t);
|
2015-11-21 03:19:43 +00:00
|
|
|
|
template void filterArraysImpl<Int32>(
|
2016-04-15 00:33:21 +00:00
|
|
|
|
const PaddedPODArray<Int32> &, const IColumn::Offsets_t &,
|
|
|
|
|
PaddedPODArray<Int32> &, IColumn::Offsets_t &,
|
|
|
|
|
const IColumn::Filter &, ssize_t);
|
2015-11-21 03:19:43 +00:00
|
|
|
|
template void filterArraysImpl<Int64>(
|
2016-04-15 00:33:21 +00:00
|
|
|
|
const PaddedPODArray<Int64> &, const IColumn::Offsets_t &,
|
|
|
|
|
PaddedPODArray<Int64> &, IColumn::Offsets_t &,
|
|
|
|
|
const IColumn::Filter &, ssize_t);
|
2015-11-21 03:19:43 +00:00
|
|
|
|
template void filterArraysImpl<Float32>(
|
2016-04-15 00:33:21 +00:00
|
|
|
|
const PaddedPODArray<Float32> &, const IColumn::Offsets_t &,
|
|
|
|
|
PaddedPODArray<Float32> &, IColumn::Offsets_t &,
|
|
|
|
|
const IColumn::Filter &, ssize_t);
|
2015-11-21 03:19:43 +00:00
|
|
|
|
template void filterArraysImpl<Float64>(
|
2016-04-15 00:33:21 +00:00
|
|
|
|
const PaddedPODArray<Float64> &, const IColumn::Offsets_t &,
|
|
|
|
|
PaddedPODArray<Float64> &, IColumn::Offsets_t &,
|
|
|
|
|
const IColumn::Filter &, ssize_t);
|
2015-11-21 03:19:43 +00:00
|
|
|
|
|
|
|
|
|
}
|