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>
|
2013-03-06 13:44:40 +00:00
|
|
|
#include <DB/DataTypes/IDataType.h>
|
2010-03-12 18:25:35 +00:00
|
|
|
|
|
|
|
|
|
|
|
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>
|
2014-06-04 01:00:09 +00:00
|
|
|
class ColumnConst final : public IColumnConst
|
2010-03-12 18:25:35 +00:00
|
|
|
{
|
|
|
|
public:
|
2011-08-12 20:39:42 +00:00
|
|
|
typedef T Type;
|
2013-12-06 09:01:31 +00:00
|
|
|
typedef typename NearestFieldType<T>::Type FieldType;
|
2014-08-16 23:18:37 +00:00
|
|
|
|
2013-03-27 10:07:23 +00:00
|
|
|
/// Для ColumnConst<Array> data_type_ должен быть ненулевым.
|
|
|
|
/// Для ColumnConst<String> data_type_ должен быть ненулевым, если тип данных FixedString.
|
|
|
|
ColumnConst(size_t s_, const T & data_, DataTypePtr data_type_ = DataTypePtr()) : s(s_), data(data_), data_type(data_type_) {}
|
2014-08-16 23:18:37 +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; }
|
2013-07-19 20:12:02 +00:00
|
|
|
bool isFixed() const { return IsNumber<T>::value; }
|
2011-09-26 12:50:50 +00:00
|
|
|
size_t sizeOfField() const { return sizeof(T); }
|
2013-05-24 10:49:19 +00:00
|
|
|
ColumnPtr cloneResized(size_t s_) const { return new ColumnConst(s_, data); }
|
2010-03-12 18:25:35 +00:00
|
|
|
size_t size() const { return s; }
|
2013-12-06 09:01:31 +00:00
|
|
|
Field operator[](size_t n) const { return FieldType(data); }
|
|
|
|
void get(size_t n, Field & res) const { res = FieldType(data); }
|
2013-05-03 05:23:14 +00:00
|
|
|
|
|
|
|
ColumnPtr cut(size_t start, size_t length) const
|
|
|
|
{
|
|
|
|
return new ColumnConst<T>(length, data, data_type);
|
|
|
|
}
|
2014-08-16 23:18:37 +00:00
|
|
|
|
2010-05-20 19:29:04 +00:00
|
|
|
void insert(const Field & x)
|
|
|
|
{
|
2013-12-06 09:01:31 +00:00
|
|
|
if (x.get<FieldType>() != FieldType(data))
|
|
|
|
throw Exception("Cannot insert different element into constant column " + getName(),
|
|
|
|
ErrorCodes::CANNOT_INSERT_ELEMENT_INTO_CONSTANT_COLUMN);
|
|
|
|
++s;
|
2010-05-20 19:29:04 +00:00
|
|
|
}
|
2013-02-16 20:15:45 +00:00
|
|
|
|
|
|
|
void insertData(const char * pos, size_t length)
|
|
|
|
{
|
|
|
|
throw Exception("Cannot insert element into constant column " + getName(), ErrorCodes::NOT_IMPLEMENTED);
|
|
|
|
}
|
2013-12-06 09:01:31 +00:00
|
|
|
|
|
|
|
void insertFrom(const IColumn & src, size_t n)
|
|
|
|
{
|
|
|
|
if (data != static_cast<const ColumnConst<T> &>(src).data)
|
|
|
|
throw Exception("Cannot insert different element into constant column " + getName(),
|
|
|
|
ErrorCodes::CANNOT_INSERT_ELEMENT_INTO_CONSTANT_COLUMN);
|
|
|
|
++s;
|
|
|
|
}
|
2014-08-16 23:18:37 +00:00
|
|
|
|
2010-05-20 19:29:04 +00:00
|
|
|
void insertDefault() { ++s; }
|
2011-08-28 05:13:24 +00:00
|
|
|
|
2013-05-03 05:23:14 +00:00
|
|
|
ColumnPtr filter(const Filter & filt) const
|
2011-08-28 05:13:24 +00:00
|
|
|
{
|
2012-08-27 05:13:14 +00:00
|
|
|
if (s != filt.size())
|
|
|
|
throw Exception("Size of filter doesn't match size of column.", ErrorCodes::SIZES_OF_COLUMNS_DOESNT_MATCH);
|
2014-08-16 23:18:37 +00:00
|
|
|
|
|
|
|
return new ColumnConst<T>(countBytesInFilter(filt), data, data_type);
|
2011-08-28 05:13:24 +00:00
|
|
|
}
|
2010-03-12 18:25:35 +00:00
|
|
|
|
2013-05-03 05:23:14 +00:00
|
|
|
ColumnPtr replicate(const Offsets_t & offsets) const
|
2012-08-27 05:13:14 +00:00
|
|
|
{
|
|
|
|
if (s != offsets.size())
|
|
|
|
throw Exception("Size of offsets doesn't match size of column.", ErrorCodes::SIZES_OF_COLUMNS_DOESNT_MATCH);
|
|
|
|
|
2014-08-20 04:57:03 +00:00
|
|
|
size_t replicated_size = 0 == s ? 0 : offsets.back();
|
|
|
|
return new ColumnConst<T>(replicated_size, data, data_type);
|
2012-08-27 05:13:14 +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
|
|
|
|
2013-09-16 05:44:47 +00:00
|
|
|
ColumnPtr permute(const Permutation & perm, size_t limit) const
|
2011-09-04 00:22:19 +00:00
|
|
|
{
|
2013-09-16 05:44:47 +00:00
|
|
|
if (limit == 0)
|
|
|
|
limit = s;
|
|
|
|
else
|
|
|
|
limit = std::min(s, limit);
|
2013-05-03 05:23:14 +00:00
|
|
|
|
2013-09-16 05:44:47 +00:00
|
|
|
if (perm.size() < limit)
|
|
|
|
throw Exception("Size of permutation is less than required.", ErrorCodes::SIZES_OF_COLUMNS_DOESNT_MATCH);
|
|
|
|
|
|
|
|
return new ColumnConst<T>(limit, data, data_type);
|
2011-09-04 00:22:19 +00:00
|
|
|
}
|
|
|
|
|
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
|
|
|
{
|
|
|
|
const ColumnConst<T> & rhs = static_cast<const ColumnConst<T> &>(rhs_);
|
2013-11-01 20:10:43 +00:00
|
|
|
return data < rhs.data /// TODO: правильное сравнение NaN-ов в константных столбцах.
|
2011-09-04 00:22:19 +00:00
|
|
|
? -1
|
|
|
|
: (data == rhs.data
|
|
|
|
? 0
|
|
|
|
: 1);
|
|
|
|
}
|
|
|
|
|
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
|
|
|
{
|
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;
|
|
|
|
}
|
|
|
|
|
2012-10-07 06:30:10 +00:00
|
|
|
StringRef getDataAt(size_t n) const;
|
2013-04-13 00:56:07 +00:00
|
|
|
StringRef getDataAtWithTerminatingZero(size_t n) const;
|
2014-05-01 00:17:02 +00:00
|
|
|
UInt64 get64(size_t n) const;
|
2012-10-07 06:30:10 +00:00
|
|
|
|
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
|
|
|
|
2013-09-06 20:28:22 +00:00
|
|
|
void getExtremes(Field & min, Field & max) const
|
|
|
|
{
|
2013-12-06 09:01:31 +00:00
|
|
|
min = FieldType(data);
|
|
|
|
max = FieldType(data);
|
2013-09-06 20:28:22 +00:00
|
|
|
}
|
|
|
|
|
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;
|
2013-03-27 10:07:23 +00:00
|
|
|
DataTypePtr data_type;
|
2010-03-12 18:25:35 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2011-08-21 03:41:37 +00:00
|
|
|
typedef ColumnConst<String> ColumnConstString;
|
2012-08-26 06:48:39 +00:00
|
|
|
typedef ColumnConst<Array> ColumnConstArray;
|
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;
|
|
|
|
|
2012-08-26 06:48:39 +00:00
|
|
|
template <> ColumnPtr ColumnConst<Array>::convertToFullColumn() const;
|
|
|
|
|
2012-10-07 06:30:10 +00:00
|
|
|
|
|
|
|
template <typename T> StringRef ColumnConst<T>::getDataAt(size_t n) const
|
|
|
|
{
|
|
|
|
throw Exception("Method getDataAt is not supported for " + getName(), ErrorCodes::NOT_IMPLEMENTED);
|
|
|
|
}
|
|
|
|
|
|
|
|
template <> inline StringRef ColumnConst<String>::getDataAt(size_t n) const
|
|
|
|
{
|
|
|
|
return StringRef(data);
|
|
|
|
}
|
|
|
|
|
2014-05-01 00:17:02 +00:00
|
|
|
template <typename T> UInt64 ColumnConst<T>::get64(size_t n) const
|
|
|
|
{
|
|
|
|
throw Exception("Method get64 is not supported for " + getName(), ErrorCodes::NOT_IMPLEMENTED);
|
|
|
|
}
|
|
|
|
|
2012-10-07 06:30:10 +00:00
|
|
|
/// Для элементарных типов.
|
2014-05-01 00:17:02 +00:00
|
|
|
template <typename T> StringRef getDataAtImpl(const T & data)
|
2012-10-07 06:30:10 +00:00
|
|
|
{
|
|
|
|
return StringRef(reinterpret_cast<const char *>(&data), sizeof(data));
|
|
|
|
}
|
|
|
|
|
2014-05-01 00:17:02 +00:00
|
|
|
template <typename T> UInt64 get64Impl(const T & data)
|
|
|
|
{
|
|
|
|
union
|
|
|
|
{
|
|
|
|
T src;
|
|
|
|
UInt64 res;
|
|
|
|
};
|
|
|
|
|
|
|
|
res = 0;
|
|
|
|
src = data;
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <> inline StringRef ColumnConst<UInt8 >::getDataAt(size_t n) const { return getDataAtImpl(data); }
|
|
|
|
template <> inline StringRef ColumnConst<UInt16 >::getDataAt(size_t n) const { return getDataAtImpl(data); }
|
|
|
|
template <> inline StringRef ColumnConst<UInt32 >::getDataAt(size_t n) const { return getDataAtImpl(data); }
|
|
|
|
template <> inline StringRef ColumnConst<UInt64 >::getDataAt(size_t n) const { return getDataAtImpl(data); }
|
|
|
|
template <> inline StringRef ColumnConst<Int8 >::getDataAt(size_t n) const { return getDataAtImpl(data); }
|
|
|
|
template <> inline StringRef ColumnConst<Int16 >::getDataAt(size_t n) const { return getDataAtImpl(data); }
|
|
|
|
template <> inline StringRef ColumnConst<Int32 >::getDataAt(size_t n) const { return getDataAtImpl(data); }
|
|
|
|
template <> inline StringRef ColumnConst<Int64 >::getDataAt(size_t n) const { return getDataAtImpl(data); }
|
|
|
|
template <> inline StringRef ColumnConst<Float32 >::getDataAt(size_t n) const { return getDataAtImpl(data); }
|
|
|
|
template <> inline StringRef ColumnConst<Float64 >::getDataAt(size_t n) const { return getDataAtImpl(data); }
|
|
|
|
|
|
|
|
template <> inline UInt64 ColumnConst<UInt8 >::get64(size_t n) const { return get64Impl(data); }
|
|
|
|
template <> inline UInt64 ColumnConst<UInt16 >::get64(size_t n) const { return get64Impl(data); }
|
|
|
|
template <> inline UInt64 ColumnConst<UInt32 >::get64(size_t n) const { return get64Impl(data); }
|
|
|
|
template <> inline UInt64 ColumnConst<UInt64 >::get64(size_t n) const { return get64Impl(data); }
|
|
|
|
template <> inline UInt64 ColumnConst<Int8 >::get64(size_t n) const { return get64Impl(data); }
|
|
|
|
template <> inline UInt64 ColumnConst<Int16 >::get64(size_t n) const { return get64Impl(data); }
|
|
|
|
template <> inline UInt64 ColumnConst<Int32 >::get64(size_t n) const { return get64Impl(data); }
|
|
|
|
template <> inline UInt64 ColumnConst<Int64 >::get64(size_t n) const { return get64Impl(data); }
|
|
|
|
template <> inline UInt64 ColumnConst<Float32 >::get64(size_t n) const { return get64Impl(data); }
|
|
|
|
template <> inline UInt64 ColumnConst<Float64 >::get64(size_t n) const { return get64Impl(data); }
|
2012-10-07 06:30:10 +00:00
|
|
|
|
2012-05-09 13:12:38 +00:00
|
|
|
|
2013-04-13 00:56:07 +00:00
|
|
|
template <typename T> StringRef ColumnConst<T>::getDataAtWithTerminatingZero(size_t n) const
|
|
|
|
{
|
|
|
|
return getDataAt(n);
|
|
|
|
}
|
|
|
|
|
|
|
|
template <> inline StringRef ColumnConst<String>::getDataAtWithTerminatingZero(size_t n) const
|
|
|
|
{
|
|
|
|
return StringRef(data.data(), data.size() + 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-08-21 03:41:37 +00:00
|
|
|
}
|