2011-09-19 01:42:16 +00:00
|
|
|
|
#pragma once
|
2010-03-12 18:25:35 +00:00
|
|
|
|
|
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
|
|
#include <DB/Core/Exception.h>
|
|
|
|
|
#include <DB/Core/ErrorCodes.h>
|
|
|
|
|
|
|
|
|
|
#include <DB/Columns/IColumn.h>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
namespace DB
|
|
|
|
|
{
|
|
|
|
|
|
2012-12-11 20:32:08 +00:00
|
|
|
|
|
2013-11-01 20:10:43 +00:00
|
|
|
|
/** Штука для сравнения чисел.
|
|
|
|
|
* Целые числа сравниваются как обычно.
|
|
|
|
|
* Числа с плавающей запятой сравниваются так, что NaN-ы всегда оказываются в конце
|
|
|
|
|
* (если этого не делать, то сортировка не работала бы вообще).
|
|
|
|
|
*/
|
|
|
|
|
template <typename T>
|
|
|
|
|
struct CompareHelper
|
|
|
|
|
{
|
|
|
|
|
static bool less(T a, T b) { return a < b; }
|
|
|
|
|
static bool greater(T a, T b) { return a > b; }
|
|
|
|
|
|
|
|
|
|
/** Сравнивает два числа. Выдаёт число меньше нуля, равное нулю, или больше нуля, если a < b, a == b, a > b, соответственно.
|
|
|
|
|
* Если одно из значений является NaN, то:
|
|
|
|
|
* - если nan_direction_hint == -1 - NaN считаются меньше всех чисел;
|
|
|
|
|
* - если nan_direction_hint == 1 - NaN считаются больше всех чисел;
|
|
|
|
|
* По-сути: nan_direction_hint == -1 говорит, что сравнение идёт для сортировки по убыванию.
|
|
|
|
|
*/
|
2013-11-07 19:50:53 +00:00
|
|
|
|
static int compare(T a, T b, int nan_direction_hint)
|
|
|
|
|
{
|
|
|
|
|
return a > b ? 1 : (a < b ? -1 : 0);
|
|
|
|
|
}
|
2013-11-01 20:10:43 +00:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
|
struct FloatCompareHelper
|
|
|
|
|
{
|
|
|
|
|
static bool less(T a, T b)
|
|
|
|
|
{
|
|
|
|
|
if (unlikely(isnan(b)))
|
|
|
|
|
return !isnan(a);
|
|
|
|
|
return a < b;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static bool greater(T a, T b)
|
|
|
|
|
{
|
|
|
|
|
if (unlikely(isnan(b)))
|
|
|
|
|
return !isnan(a);
|
|
|
|
|
return a > b;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static int compare(T a, T b, int nan_direction_hint)
|
|
|
|
|
{
|
|
|
|
|
bool isnan_a = isnan(a);
|
|
|
|
|
bool isnan_b = isnan(b);
|
|
|
|
|
if (unlikely(isnan_a || isnan_b))
|
|
|
|
|
{
|
|
|
|
|
if (isnan_a && isnan_b)
|
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
|
|
return isnan_a
|
|
|
|
|
? nan_direction_hint
|
|
|
|
|
: -nan_direction_hint;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return (T(0) < (a - b)) - ((a - b) < T(0));
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
template <> struct CompareHelper<Float32> : public FloatCompareHelper<Float32> {};
|
|
|
|
|
template <> struct CompareHelper<Float64> : public FloatCompareHelper<Float64> {};
|
|
|
|
|
|
|
|
|
|
|
2013-12-16 05:27:38 +00:00
|
|
|
|
/** Шаблон столбцов, которые используют для хранения простой массив.
|
2010-03-12 18:25:35 +00:00
|
|
|
|
*/
|
2013-01-21 06:43:38 +00:00
|
|
|
|
template <typename T>
|
2013-02-08 23:41:05 +00:00
|
|
|
|
class ColumnVectorBase : public IColumn
|
2010-03-12 18:25:35 +00:00
|
|
|
|
{
|
2013-05-03 05:23:14 +00:00
|
|
|
|
private:
|
|
|
|
|
typedef ColumnVectorBase<T> Self;
|
2010-03-12 18:25:35 +00:00
|
|
|
|
public:
|
|
|
|
|
typedef T value_type;
|
2013-12-08 02:29:40 +00:00
|
|
|
|
typedef PODArray<value_type> Container_t;
|
2010-03-12 18:25:35 +00:00
|
|
|
|
|
2013-02-08 23:41:05 +00:00
|
|
|
|
ColumnVectorBase() {}
|
|
|
|
|
ColumnVectorBase(size_t n) : data(n) {}
|
2011-08-28 00:31:30 +00:00
|
|
|
|
|
2011-08-21 03:41:37 +00:00
|
|
|
|
bool isNumeric() const { return IsNumber<T>::value; }
|
2013-07-19 20:12:02 +00:00
|
|
|
|
bool isFixed() const { return IsNumber<T>::value; }
|
2011-08-21 03:41:37 +00:00
|
|
|
|
|
2011-09-26 12:50:50 +00:00
|
|
|
|
size_t sizeOfField() const { return sizeof(T); }
|
|
|
|
|
|
2010-03-12 18:25:35 +00:00
|
|
|
|
size_t size() const
|
|
|
|
|
{
|
|
|
|
|
return data.size();
|
|
|
|
|
}
|
2013-01-07 06:47:15 +00:00
|
|
|
|
|
2012-10-07 06:30:10 +00:00
|
|
|
|
StringRef getDataAt(size_t n) const
|
|
|
|
|
{
|
|
|
|
|
return StringRef(reinterpret_cast<const char *>(&data[n]), sizeof(data[n]));
|
|
|
|
|
}
|
2010-03-12 18:25:35 +00:00
|
|
|
|
|
2012-12-16 00:52:06 +00:00
|
|
|
|
void insertFrom(const IColumn & src, size_t n)
|
|
|
|
|
{
|
2013-05-03 05:23:14 +00:00
|
|
|
|
data.push_back(static_cast<const Self &>(src).getData()[n]);
|
2012-12-16 00:52:06 +00:00
|
|
|
|
}
|
|
|
|
|
|
2013-02-16 20:15:45 +00:00
|
|
|
|
void insertData(const char * pos, size_t length)
|
|
|
|
|
{
|
|
|
|
|
data.push_back(*reinterpret_cast<const T *>(pos));
|
|
|
|
|
}
|
|
|
|
|
|
2010-05-20 19:29:04 +00:00
|
|
|
|
void insertDefault()
|
|
|
|
|
{
|
|
|
|
|
data.push_back(T());
|
|
|
|
|
}
|
|
|
|
|
|
2012-05-17 19:15:53 +00:00
|
|
|
|
size_t byteSize() const
|
2011-08-27 22:43:31 +00:00
|
|
|
|
{
|
|
|
|
|
return data.size() * sizeof(data[0]);
|
|
|
|
|
}
|
|
|
|
|
|
2013-11-01 20:10:43 +00:00
|
|
|
|
int compareAt(size_t n, size_t m, const IColumn & rhs_, int nan_direction_hint) const
|
2011-09-04 00:22:19 +00:00
|
|
|
|
{
|
2013-11-01 20:10:43 +00:00
|
|
|
|
return CompareHelper<T>::compare(data[n], static_cast<const Self &>(rhs_).data[m], nan_direction_hint);
|
2011-09-04 00:22:19 +00:00
|
|
|
|
}
|
|
|
|
|
|
2011-09-26 11:05:38 +00:00
|
|
|
|
struct less
|
|
|
|
|
{
|
2013-05-03 05:23:14 +00:00
|
|
|
|
const Self & parent;
|
|
|
|
|
less(const Self & parent_) : parent(parent_) {}
|
2013-11-01 20:10:43 +00:00
|
|
|
|
bool operator()(size_t lhs, size_t rhs) const { return CompareHelper<T>::less(parent.data[lhs], parent.data[rhs]); }
|
2011-09-26 11:05:38 +00:00
|
|
|
|
};
|
|
|
|
|
|
2013-09-16 05:44:47 +00:00
|
|
|
|
struct greater
|
|
|
|
|
{
|
|
|
|
|
const Self & parent;
|
|
|
|
|
greater(const Self & parent_) : parent(parent_) {}
|
2013-11-01 20:10:43 +00:00
|
|
|
|
bool operator()(size_t lhs, size_t rhs) const { return CompareHelper<T>::greater(parent.data[lhs], parent.data[rhs]); }
|
2013-09-16 05:44:47 +00:00
|
|
|
|
};
|
|
|
|
|
|
2013-12-08 02:29:40 +00:00
|
|
|
|
void getPermutation(bool reverse, size_t limit, Permutation & res) const
|
2011-09-26 11:05:38 +00:00
|
|
|
|
{
|
|
|
|
|
size_t s = data.size();
|
2013-12-08 02:29:40 +00:00
|
|
|
|
res.resize(s);
|
2011-09-26 11:05:38 +00:00
|
|
|
|
for (size_t i = 0; i < s; ++i)
|
|
|
|
|
res[i] = i;
|
|
|
|
|
|
2013-09-16 05:44:47 +00:00
|
|
|
|
if (limit > s)
|
|
|
|
|
limit = 0;
|
|
|
|
|
|
|
|
|
|
if (limit)
|
|
|
|
|
{
|
|
|
|
|
if (reverse)
|
|
|
|
|
std::partial_sort(res.begin(), res.begin() + limit, res.end(), greater(*this));
|
|
|
|
|
else
|
|
|
|
|
std::partial_sort(res.begin(), res.begin() + limit, res.end(), less(*this));
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
if (reverse)
|
|
|
|
|
std::sort(res.begin(), res.end(), greater(*this));
|
|
|
|
|
else
|
|
|
|
|
std::sort(res.begin(), res.end(), less(*this));
|
|
|
|
|
}
|
2011-09-26 11:05:38 +00:00
|
|
|
|
}
|
|
|
|
|
|
2013-02-03 18:39:09 +00:00
|
|
|
|
void reserve(size_t n)
|
|
|
|
|
{
|
|
|
|
|
data.reserve(n);
|
|
|
|
|
}
|
|
|
|
|
|
2010-03-12 18:25:35 +00:00
|
|
|
|
/** Более эффективные методы манипуляции */
|
|
|
|
|
Container_t & getData()
|
|
|
|
|
{
|
|
|
|
|
return data;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const Container_t & getData() const
|
|
|
|
|
{
|
|
|
|
|
return data;
|
|
|
|
|
}
|
|
|
|
|
|
2011-09-19 03:34:23 +00:00
|
|
|
|
protected:
|
2010-03-12 18:25:35 +00:00
|
|
|
|
Container_t data;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
2013-02-08 23:41:05 +00:00
|
|
|
|
/** Реализация для числовых типов.
|
|
|
|
|
* (Есть ещё ColumnAggregateFunction.)
|
|
|
|
|
*/
|
|
|
|
|
template <typename T>
|
|
|
|
|
class ColumnVector : public ColumnVectorBase<T>
|
|
|
|
|
{
|
2013-05-03 05:23:14 +00:00
|
|
|
|
private:
|
|
|
|
|
typedef ColumnVector<T> Self;
|
2013-02-08 23:41:05 +00:00
|
|
|
|
public:
|
|
|
|
|
ColumnVector() {}
|
|
|
|
|
ColumnVector(size_t n) : ColumnVectorBase<T>(n) {}
|
|
|
|
|
|
|
|
|
|
std::string getName() const { return "ColumnVector<" + TypeName<T>::get() + ">"; }
|
|
|
|
|
|
|
|
|
|
ColumnPtr cloneEmpty() const
|
|
|
|
|
{
|
|
|
|
|
return new ColumnVector<T>;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Field operator[](size_t n) const
|
|
|
|
|
{
|
|
|
|
|
return typename NearestFieldType<T>::Type(this->data[n]);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void get(size_t n, Field & res) const
|
|
|
|
|
{
|
|
|
|
|
res = typename NearestFieldType<T>::Type(this->data[n]);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void insert(const Field & x)
|
|
|
|
|
{
|
|
|
|
|
this->data.push_back(DB::get<typename NearestFieldType<T>::Type>(x));
|
|
|
|
|
}
|
2013-05-03 05:23:14 +00:00
|
|
|
|
|
|
|
|
|
ColumnPtr cut(size_t start, size_t length) const
|
|
|
|
|
{
|
|
|
|
|
if (start + length > this->data.size())
|
|
|
|
|
throw Exception("Parameters start = "
|
2013-06-21 20:34:19 +00:00
|
|
|
|
+ toString(start) + ", length = "
|
|
|
|
|
+ toString(length) + " are out of bound in IColumnVector<T>::cut() method"
|
|
|
|
|
" (data.size() = " + toString(this->data.size()) + ").",
|
2013-05-03 05:23:14 +00:00
|
|
|
|
ErrorCodes::PARAMETER_OUT_OF_BOUND);
|
|
|
|
|
|
|
|
|
|
Self * res = new Self(length);
|
|
|
|
|
memcpy(&res->getData()[0], &this->data[start], length * sizeof(this->data[0]));
|
|
|
|
|
return res;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ColumnPtr filter(const IColumn::Filter & filt) const
|
|
|
|
|
{
|
|
|
|
|
size_t size = this->data.size();
|
|
|
|
|
if (size != filt.size())
|
|
|
|
|
throw Exception("Size of filter doesn't match size of column.", ErrorCodes::SIZES_OF_COLUMNS_DOESNT_MATCH);
|
|
|
|
|
|
|
|
|
|
Self * res_ = new Self;
|
|
|
|
|
ColumnPtr res = res_;
|
|
|
|
|
typename Self::Container_t & res_data = res_->getData();
|
|
|
|
|
res_data.reserve(size);
|
|
|
|
|
|
|
|
|
|
for (size_t i = 0; i < size; ++i)
|
|
|
|
|
if (filt[i])
|
|
|
|
|
res_data.push_back(this->data[i]);
|
|
|
|
|
|
|
|
|
|
return res;
|
|
|
|
|
}
|
|
|
|
|
|
2013-09-16 05:44:47 +00:00
|
|
|
|
ColumnPtr permute(const IColumn::Permutation & perm, size_t limit) const
|
2013-05-03 05:23:14 +00:00
|
|
|
|
{
|
|
|
|
|
size_t size = this->data.size();
|
|
|
|
|
|
2013-09-16 05:44:47 +00:00
|
|
|
|
if (limit == 0)
|
|
|
|
|
limit = size;
|
|
|
|
|
else
|
|
|
|
|
limit = std::min(size, limit);
|
|
|
|
|
|
|
|
|
|
if (perm.size() < limit)
|
|
|
|
|
throw Exception("Size of permutation is less than required.", ErrorCodes::SIZES_OF_COLUMNS_DOESNT_MATCH);
|
|
|
|
|
|
|
|
|
|
Self * res_ = new Self(limit);
|
2013-05-03 05:23:14 +00:00
|
|
|
|
ColumnPtr res = res_;
|
|
|
|
|
typename Self::Container_t & res_data = res_->getData();
|
2013-09-16 05:44:47 +00:00
|
|
|
|
for (size_t i = 0; i < limit; ++i)
|
2013-05-03 05:23:14 +00:00
|
|
|
|
res_data[i] = this->data[perm[i]];
|
|
|
|
|
|
|
|
|
|
return res;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ColumnPtr replicate(const IColumn::Offsets_t & offsets) const
|
|
|
|
|
{
|
|
|
|
|
size_t size = this->data.size();
|
|
|
|
|
if (size != offsets.size())
|
|
|
|
|
throw Exception("Size of offsets doesn't match size of column.", ErrorCodes::SIZES_OF_COLUMNS_DOESNT_MATCH);
|
|
|
|
|
|
|
|
|
|
Self * res_ = new Self;
|
|
|
|
|
ColumnPtr res = res_;
|
|
|
|
|
typename Self::Container_t & res_data = res_->getData();
|
|
|
|
|
res_data.reserve(offsets.back());
|
|
|
|
|
|
|
|
|
|
IColumn::Offset_t prev_offset = 0;
|
|
|
|
|
for (size_t i = 0; i < size; ++i)
|
|
|
|
|
{
|
|
|
|
|
size_t size_to_replicate = offsets[i] - prev_offset;
|
|
|
|
|
prev_offset = offsets[i];
|
|
|
|
|
|
|
|
|
|
for (size_t j = 0; j < size_to_replicate; ++j)
|
|
|
|
|
res_data.push_back(this->data[i]);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return res;
|
|
|
|
|
}
|
2013-09-06 20:28:22 +00:00
|
|
|
|
|
|
|
|
|
void getExtremes(Field & min, Field & max) const
|
|
|
|
|
{
|
|
|
|
|
size_t size = this->data.size();
|
|
|
|
|
|
|
|
|
|
if (size == 0)
|
|
|
|
|
{
|
|
|
|
|
min = typename NearestFieldType<T>::Type(0);
|
|
|
|
|
max = typename NearestFieldType<T>::Type(0);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
T cur_min = this->data[0];
|
|
|
|
|
T cur_max = this->data[0];
|
|
|
|
|
|
|
|
|
|
for (size_t i = 1; i < size; ++i)
|
|
|
|
|
{
|
|
|
|
|
if (this->data[i] < cur_min)
|
|
|
|
|
cur_min = this->data[i];
|
|
|
|
|
|
|
|
|
|
if (this->data[i] > cur_max)
|
|
|
|
|
cur_max = this->data[i];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
min = typename NearestFieldType<T>::Type(cur_min);
|
|
|
|
|
max = typename NearestFieldType<T>::Type(cur_max);
|
|
|
|
|
}
|
2013-02-08 23:41:05 +00:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
2010-03-12 18:25:35 +00:00
|
|
|
|
}
|