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

116 lines
2.8 KiB
C
Raw Normal View History

2011-08-21 03:41:37 +00:00
#pragma once
2010-03-12 18:25:35 +00:00
#include <Poco/SharedPtr.h>
2011-08-12 18:28:48 +00:00
#include <DB/Core/Field.h>
2010-05-20 19:29:04 +00:00
#include <DB/Core/Exception.h>
#include <DB/Core/ErrorCodes.h>
2011-11-06 06:22:52 +00:00
#include <DB/Columns/ColumnVector.h>
2010-03-12 18:25:35 +00:00
#include <DB/Columns/IColumn.h>
namespace DB
{
using Poco::SharedPtr;
2011-11-06 06:22:52 +00:00
class IColumnConst : public IColumn
{
public:
bool isConst() const { return true; }
virtual ColumnPtr convertToFullColumn() const = 0;
};
2010-03-12 18:25:35 +00:00
/** шаблон для столбцов-констант (столбцов одинаковых значений).
*/
template <typename T>
2011-11-06 06:22:52 +00:00
class ColumnConst : public IColumnConst
2010-03-12 18:25:35 +00:00
{
public:
2011-08-12 20:39:42 +00:00
typedef T Type;
2011-08-12 18:27:39 +00:00
ColumnConst(size_t s_, const T & data_) : s(s_), data(data_) {}
2010-03-12 18:25:35 +00:00
2011-08-28 00:31:30 +00:00
std::string getName() const { return "ColumnConst<" + 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 { return new ColumnConst(0, data); }
2010-03-12 18:25:35 +00:00
size_t size() const { return s; }
2011-08-12 18:27:39 +00:00
Field operator[](size_t n) const { return typename NearestFieldType<T>::Type(data); }
2010-03-12 18:25:35 +00:00
void cut(size_t start, size_t length) { s = length; }
2010-05-13 16:13:38 +00:00
void clear() { s = 0; }
2011-08-28 05:13:24 +00:00
2010-05-20 19:29:04 +00:00
void insert(const Field & x)
{
2011-08-28 05:13:24 +00:00
throw Exception("Cannot insert element into constant column " + getName(), ErrorCodes::CANNOT_INSERT_ELEMENT_INTO_CONSTANT_COLUMN);
2010-05-20 19:29:04 +00:00
}
2011-08-28 05:13:24 +00:00
2010-05-20 19:29:04 +00:00
void insertDefault() { ++s; }
2011-08-28 05:13:24 +00:00
void filter(const Filter & filt)
{
size_t new_size = 0;
for (Filter::const_iterator it = filt.begin(); it != filt.end(); ++it)
if (*it)
++new_size;
s = new_size;
}
2010-03-12 18:25:35 +00:00
2012-05-17 19:15:53 +00:00
size_t byteSize() const { return sizeof(data) + sizeof(s); }
2011-08-27 22:43:31 +00:00
2011-09-04 00:22:19 +00:00
void permute(const Permutation & perm)
{
if (s != perm.size())
throw Exception("Size of permutation doesn't match size of column.", ErrorCodes::SIZES_OF_COLUMNS_DOESNT_MATCH);
}
int compareAt(size_t n, size_t m, const IColumn & rhs_) const
{
const ColumnConst<T> & rhs = static_cast<const ColumnConst<T> &>(rhs_);
return data < rhs.data
? -1
: (data == rhs.data
? 0
: 1);
}
2011-09-26 11:05:38 +00:00
Permutation getPermutation() const
{
Permutation res(s);
for (size_t i = 0; i < s; ++i)
res[i] = i;
return res;
}
2010-03-12 18:25:35 +00:00
/** Более эффективные методы манипуляции */
T & getData() { return data; }
const T & getData() const { return data; }
/** Преобразование из константы в полноценный столбец */
2011-11-06 06:22:52 +00:00
ColumnPtr convertToFullColumn() const;
2010-03-12 18:25:35 +00:00
private:
2010-05-13 16:13:38 +00:00
size_t s;
2010-03-12 18:25:35 +00:00
T data;
};
2011-08-21 03:41:37 +00:00
typedef ColumnConst<String> ColumnConstString;
2010-03-12 18:25:35 +00:00
2011-11-06 06:22:52 +00:00
template <typename T> ColumnPtr ColumnConst<T>::convertToFullColumn() const
{
ColumnVector<T> * res = new ColumnVector<T>;
res->getData().assign(s, data);
return res;
}
2012-05-09 13:12:38 +00:00
template <> ColumnPtr ColumnConst<String>::convertToFullColumn() const;
// TODO: convertToFullColumn для остальных типов столбцов.
2011-08-21 03:41:37 +00:00
}