ClickHouse/dbms/include/DB/Columns/ColumnVector.h

242 lines
5.3 KiB
C
Raw Normal View History

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
/** Специализация - заглушка, которая ничего не делает для агрегатных функций.
*/
template <>
2012-12-11 20:32:08 +00:00
class FieldVisitorConvertToNumber<SharedPtr<IAggregateFunction> >
: public StaticVisitor<SharedPtr<IAggregateFunction> >
{
public:
typedef SharedPtr<IAggregateFunction> T;
2012-12-11 20:32:08 +00:00
T operator() (const Null & x) const
{
throw Exception("Cannot convert NULL to Aggregate Function", ErrorCodes::CANNOT_CONVERT_TYPE);
}
T operator() (const String & x) const
{
throw Exception("Cannot convert String to Aggregate Function", ErrorCodes::CANNOT_CONVERT_TYPE);
}
T operator() (const Array & x) const
{
throw Exception("Cannot convert Array to Aggregate Function", ErrorCodes::CANNOT_CONVERT_TYPE);
}
T operator() (const SharedPtr<IAggregateFunction> & x) const
{
return x;
}
2012-12-11 20:32:08 +00:00
T operator() (const UInt64 & x) const
{
throw Exception("Cannot convert UInt64 to Aggregate Function", ErrorCodes::CANNOT_CONVERT_TYPE);
}
T operator() (const Int64 & x) const
{
throw Exception("Cannot convert Int64 to Aggregate Function", ErrorCodes::CANNOT_CONVERT_TYPE);
}
T operator() (const Float64 & x) const
{
throw Exception("Cannot convert Float64 to Aggregate Function", ErrorCodes::CANNOT_CONVERT_TYPE);
}
};
2012-12-11 20:32:08 +00:00
/** Шаблон столбцов, которые используют для хранения непрерывный кусок памяти.
2010-03-12 18:25:35 +00:00
*/
template <typename T, typename Container = PODArray<T> >
2010-03-12 18:25:35 +00:00
class ColumnVector : public IColumn
{
public:
typedef T value_type;
typedef Container Container_t;
typedef ColumnVector<T, Container> Self;
2010-03-12 18:25:35 +00:00
ColumnVector() {}
ColumnVector(size_t n) : data(n) {}
2011-08-28 00:31:30 +00:00
std::string getName() const { return "ColumnVector<" + TypeName<T>::get() + ">"; }
2011-08-21 03:41:37 +00:00
bool isNumeric() const { return IsNumber<T>::value; }
2011-09-26 12:50:50 +00:00
size_t sizeOfField() const { return sizeof(T); }
2011-08-09 19:19:00 +00:00
ColumnPtr cloneEmpty() const
2010-05-21 19:52:50 +00:00
{
return new Self;
2010-05-21 19:52:50 +00:00
}
2010-03-12 18:25:35 +00:00
size_t size() const
{
return data.size();
}
Field operator[](size_t n) const
{
2010-03-18 19:32:14 +00:00
return typename NearestFieldType<T>::Type(data[n]);
2010-03-12 18:25:35 +00:00
}
void get(size_t n, Field & res) const
{
res = typename NearestFieldType<T>::Type(data[n]);
}
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
void cut(size_t start, size_t length)
{
if (start + length > data.size())
2011-08-21 03:41:37 +00:00
throw Exception("Parameters start = "
+ Poco::NumberFormatter::format(start) + ", length = "
+ Poco::NumberFormatter::format(length) + " are out of bound in ColumnVector<>::cut() method"
2011-08-21 03:41:37 +00:00
" (data.size() = " + Poco::NumberFormatter::format(data.size()) + ").",
2010-03-12 18:25:35 +00:00
ErrorCodes::PARAMETER_OUT_OF_BOUND);
if (start == 0)
data.resize(length);
else
{
Container_t tmp(length);
memcpy(&tmp[0], &data[start], length * sizeof(data[0]));
tmp.swap(data);
}
}
2010-05-20 19:29:04 +00:00
void insert(const Field & x)
{
data.push_back(apply_visitor(FieldVisitorConvertToNumber<typename NearestFieldType<T>::Type>(), x));
2010-05-20 19:29:04 +00:00
}
void insertFrom(const IColumn & src, size_t n)
{
data.push_back(static_cast<const Self &>(src).getData()[n]);
}
2010-05-20 19:29:04 +00:00
void insertDefault()
{
data.push_back(T());
}
2010-05-13 16:13:38 +00:00
void clear()
{
data.clear();
}
2011-08-22 20:24:45 +00:00
void filter(const Filter & filt)
{
size_t size = data.size();
if (size != filt.size())
throw Exception("Size of filter doesn't match size of column.", ErrorCodes::SIZES_OF_COLUMNS_DOESNT_MATCH);
Container_t tmp;
tmp.reserve(size);
for (size_t i = 0; i < size; ++i)
if (filt[i])
2012-09-24 02:04:57 +00:00
tmp.push_back(data[i]);
2011-08-22 20:24:45 +00:00
tmp.swap(data);
}
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]);
}
2011-09-04 00:22:19 +00:00
void permute(const Permutation & perm)
{
size_t size = data.size();
if (size != perm.size())
throw Exception("Size of permutation doesn't match size of column.", ErrorCodes::SIZES_OF_COLUMNS_DOESNT_MATCH);
Container_t tmp(size);
for (size_t i = 0; i < size; ++i)
tmp[i] = data[perm[i]];
tmp.swap(data);
}
int compareAt(size_t n, size_t m, const IColumn & rhs_) const
{
const Self & rhs = static_cast<const Self &>(rhs_);
2011-09-04 00:22:19 +00:00
return data[n] < rhs.data[m]
? -1
: (data[n] == rhs.data[m]
? 0
: 1);
}
2011-09-26 11:05:38 +00:00
struct less
{
const Self & parent;
less(const Self & parent_) : parent(parent_) {}
2011-09-26 11:05:38 +00:00
bool operator()(size_t lhs, size_t rhs) const { return parent.data[lhs] < parent.data[rhs]; }
};
void getPermutation(Permutation & res) const
2011-09-26 11:05:38 +00:00
{
size_t s = data.size();
res.resize(s);
2011-09-26 11:05:38 +00:00
for (size_t i = 0; i < s; ++i)
res[i] = i;
std::sort(res.begin(), res.end(), less(*this));
}
2012-08-27 05:13:14 +00:00
void replicate(const Offsets_t & offsets)
{
size_t size = data.size();
if (size != offsets.size())
throw Exception("Size of offsets doesn't match size of column.", ErrorCodes::SIZES_OF_COLUMNS_DOESNT_MATCH);
Container_t tmp;
tmp.reserve(offsets.back());
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)
tmp.push_back(data[i]);
}
tmp.swap(data);
}
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;
};
}