2011-08-07 02:08:22 +00:00
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
|
|
#include <string.h> // memcpy
|
|
|
|
|
|
2013-09-16 05:44:47 +00:00
|
|
|
|
#include <DB/Common/PODArray.h>
|
2015-10-04 03:20:03 +00:00
|
|
|
|
#include <DB/Common/Arena.h>
|
2016-04-15 00:33:21 +00:00
|
|
|
|
#include <DB/Common/memcpySmall.h>
|
2013-05-05 15:25:25 +00:00
|
|
|
|
#include <DB/Columns/IColumn.h>
|
2015-11-29 17:06:30 +00:00
|
|
|
|
#include <DB/IO/ReadHelpers.h>
|
2016-01-11 21:46:36 +00:00
|
|
|
|
#include <DB/IO/WriteHelpers.h>
|
2011-08-07 02:08:22 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
namespace DB
|
|
|
|
|
{
|
|
|
|
|
|
2016-01-11 21:46:36 +00:00
|
|
|
|
namespace ErrorCodes
|
|
|
|
|
{
|
|
|
|
|
extern const int TOO_LARGE_STRING_SIZE;
|
2016-02-16 16:39:39 +00:00
|
|
|
|
extern const int SIZE_OF_FIXED_STRING_DOESNT_MATCH;
|
2016-01-11 21:46:36 +00:00
|
|
|
|
extern const int SIZES_OF_COLUMNS_DOESNT_MATCH;
|
|
|
|
|
extern const int PARAMETER_OUT_OF_BOUND;
|
|
|
|
|
}
|
|
|
|
|
|
2011-08-07 02:08:22 +00:00
|
|
|
|
/** Cтолбeц значений типа "строка фиксированной длины".
|
|
|
|
|
* Если вставить строку меньшей длины, то она будет дополнена нулевыми байтами.
|
|
|
|
|
*/
|
2014-06-04 01:00:09 +00:00
|
|
|
|
class ColumnFixedString final : public IColumn
|
2011-08-07 02:08:22 +00:00
|
|
|
|
{
|
2013-05-05 15:25:25 +00:00
|
|
|
|
public:
|
2016-05-28 10:35:44 +00:00
|
|
|
|
using Chars_t = PaddedPODArray<UInt8>;
|
2013-05-05 15:25:25 +00:00
|
|
|
|
|
2011-08-07 02:08:22 +00:00
|
|
|
|
private:
|
2013-05-05 15:25:25 +00:00
|
|
|
|
/// Байты строк, уложенные подряд. Строки хранятся без завершающего нулевого байта.
|
2016-05-28 05:31:36 +00:00
|
|
|
|
/** NOTE Требуется, чтобы смещение и тип chars в объекте был таким же, как у data в ColumnUInt8.
|
2015-03-10 20:16:13 +00:00
|
|
|
|
* Это используется в функции packFixed (AggregationCommon.h)
|
|
|
|
|
*/
|
2013-05-05 15:25:25 +00:00
|
|
|
|
Chars_t chars;
|
2015-03-10 20:16:13 +00:00
|
|
|
|
/// Размер строк.
|
|
|
|
|
const size_t n;
|
2011-08-07 02:08:22 +00:00
|
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
/** Создать пустой столбец строк фиксированной длины n */
|
2013-05-05 15:25:25 +00:00
|
|
|
|
ColumnFixedString(size_t n_) : n(n_) {}
|
2011-08-07 02:08:22 +00:00
|
|
|
|
|
2014-10-31 22:15:17 +00:00
|
|
|
|
std::string getName() const override { return "ColumnFixedString"; }
|
2011-08-28 00:31:30 +00:00
|
|
|
|
|
2014-10-31 22:15:17 +00:00
|
|
|
|
ColumnPtr cloneEmpty() const override
|
2011-08-07 02:08:22 +00:00
|
|
|
|
{
|
2016-05-28 05:31:36 +00:00
|
|
|
|
return std::make_shared<ColumnFixedString>(n);
|
2011-08-07 02:08:22 +00:00
|
|
|
|
}
|
2013-05-05 15:25:25 +00:00
|
|
|
|
|
2014-10-31 22:15:17 +00:00
|
|
|
|
size_t size() const override
|
2013-05-05 15:25:25 +00:00
|
|
|
|
{
|
|
|
|
|
return chars.size() / n;
|
|
|
|
|
}
|
2013-07-19 20:12:02 +00:00
|
|
|
|
|
2014-10-31 22:15:17 +00:00
|
|
|
|
size_t sizeOfField() const override
|
2013-07-19 20:12:02 +00:00
|
|
|
|
{
|
|
|
|
|
return n;
|
|
|
|
|
}
|
|
|
|
|
|
2014-10-31 22:15:17 +00:00
|
|
|
|
bool isFixed() const override
|
2013-07-19 20:12:02 +00:00
|
|
|
|
{
|
|
|
|
|
return true;
|
|
|
|
|
}
|
2014-08-20 04:57:03 +00:00
|
|
|
|
|
2014-10-31 22:15:17 +00:00
|
|
|
|
size_t byteSize() const override
|
2013-05-05 15:25:25 +00:00
|
|
|
|
{
|
|
|
|
|
return chars.size() + sizeof(n);
|
|
|
|
|
}
|
2014-08-20 04:57:03 +00:00
|
|
|
|
|
2014-10-31 22:15:17 +00:00
|
|
|
|
Field operator[](size_t index) const override
|
2011-08-07 02:08:22 +00:00
|
|
|
|
{
|
2013-05-05 15:25:25 +00:00
|
|
|
|
return String(reinterpret_cast<const char *>(&chars[n * index]), n);
|
2011-08-07 02:08:22 +00:00
|
|
|
|
}
|
|
|
|
|
|
2014-10-31 22:15:17 +00:00
|
|
|
|
void get(size_t index, Field & res) const override
|
2013-01-07 06:47:15 +00:00
|
|
|
|
{
|
2013-05-05 15:25:25 +00:00
|
|
|
|
res.assignString(reinterpret_cast<const char *>(&chars[n * index]), n);
|
2013-01-07 06:47:15 +00:00
|
|
|
|
}
|
|
|
|
|
|
2014-10-31 22:15:17 +00:00
|
|
|
|
StringRef getDataAt(size_t index) const override
|
2012-10-07 06:30:10 +00:00
|
|
|
|
{
|
2013-05-05 15:25:25 +00:00
|
|
|
|
return StringRef(&chars[n * index], n);
|
2012-10-07 06:30:10 +00:00
|
|
|
|
}
|
|
|
|
|
|
2014-10-31 22:15:17 +00:00
|
|
|
|
void insert(const Field & x) override
|
2011-08-07 02:08:22 +00:00
|
|
|
|
{
|
2013-01-07 06:47:15 +00:00
|
|
|
|
const String & s = DB::get<const String &>(x);
|
2012-09-24 06:43:50 +00:00
|
|
|
|
|
|
|
|
|
if (s.size() > n)
|
2012-11-25 21:16:37 +00:00
|
|
|
|
throw Exception("Too large string '" + s + "' for FixedString column", ErrorCodes::TOO_LARGE_STRING_SIZE);
|
2014-08-20 04:57:03 +00:00
|
|
|
|
|
2013-05-05 15:25:25 +00:00
|
|
|
|
size_t old_size = chars.size();
|
2013-12-03 10:01:04 +00:00
|
|
|
|
chars.resize_fill(old_size + n);
|
2013-05-05 15:25:25 +00:00
|
|
|
|
memcpy(&chars[old_size], s.data(), s.size());
|
2011-08-07 02:08:22 +00:00
|
|
|
|
}
|
|
|
|
|
|
2014-10-31 22:15:17 +00:00
|
|
|
|
void insertFrom(const IColumn & src_, size_t index) override
|
2012-12-16 00:52:06 +00:00
|
|
|
|
{
|
|
|
|
|
const ColumnFixedString & src = static_cast<const ColumnFixedString &>(src_);
|
|
|
|
|
|
|
|
|
|
if (n != src.getN())
|
2016-02-16 16:39:39 +00:00
|
|
|
|
throw Exception("Size of FixedString doesn't match", ErrorCodes::SIZE_OF_FIXED_STRING_DOESNT_MATCH);
|
2012-12-16 00:52:06 +00:00
|
|
|
|
|
2013-05-05 15:25:25 +00:00
|
|
|
|
size_t old_size = chars.size();
|
|
|
|
|
chars.resize(old_size + n);
|
2016-04-15 00:33:21 +00:00
|
|
|
|
memcpySmallAllowReadWriteOverflow15(&chars[old_size], &src.chars[n * index], n);
|
2012-12-16 00:52:06 +00:00
|
|
|
|
}
|
|
|
|
|
|
2014-10-31 22:15:17 +00:00
|
|
|
|
void insertData(const char * pos, size_t length) override
|
2013-02-16 20:15:45 +00:00
|
|
|
|
{
|
2013-12-30 17:25:50 +00:00
|
|
|
|
if (length > n)
|
|
|
|
|
throw Exception("Too large string for FixedString column", ErrorCodes::TOO_LARGE_STRING_SIZE);
|
|
|
|
|
|
2013-05-05 15:25:25 +00:00
|
|
|
|
size_t old_size = chars.size();
|
2013-12-30 17:25:50 +00:00
|
|
|
|
chars.resize_fill(old_size + n);
|
|
|
|
|
memcpy(&chars[old_size], pos, length);
|
2013-02-16 20:15:45 +00:00
|
|
|
|
}
|
|
|
|
|
|
2014-10-31 22:15:17 +00:00
|
|
|
|
void insertDefault() override
|
2011-08-07 02:08:22 +00:00
|
|
|
|
{
|
2013-12-03 10:01:04 +00:00
|
|
|
|
chars.resize_fill(chars.size() + n);
|
2011-08-07 02:08:22 +00:00
|
|
|
|
}
|
2011-09-04 00:22:19 +00:00
|
|
|
|
|
2016-02-16 16:39:39 +00:00
|
|
|
|
void popBack(size_t elems) override
|
|
|
|
|
{
|
|
|
|
|
chars.resize_assume_reserved(chars.size() - n * elems);
|
|
|
|
|
}
|
|
|
|
|
|
2015-10-04 03:17:36 +00:00
|
|
|
|
StringRef serializeValueIntoArena(size_t index, Arena & arena, char const *& begin) const override
|
|
|
|
|
{
|
|
|
|
|
auto pos = arena.allocContinue(n, begin);
|
|
|
|
|
memcpy(pos, &chars[n * index], n);
|
|
|
|
|
return StringRef(pos, n);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const char * deserializeAndInsertFromArena(const char * pos) override
|
|
|
|
|
{
|
|
|
|
|
size_t old_size = chars.size();
|
|
|
|
|
chars.resize(old_size + n);
|
|
|
|
|
memcpy(&chars[old_size], pos, n);
|
|
|
|
|
return pos + n;
|
|
|
|
|
}
|
|
|
|
|
|
2014-10-31 22:15:17 +00:00
|
|
|
|
int compareAt(size_t p1, size_t p2, const IColumn & rhs_, int nan_direction_hint) const override
|
2011-09-04 00:22:19 +00:00
|
|
|
|
{
|
|
|
|
|
const ColumnFixedString & rhs = static_cast<const ColumnFixedString &>(rhs_);
|
2013-05-05 15:25:25 +00:00
|
|
|
|
return memcmp(&chars[p1 * n], &rhs.chars[p2 * n], n);
|
2011-09-04 00:22:19 +00:00
|
|
|
|
}
|
2011-09-26 11:05:38 +00:00
|
|
|
|
|
2013-09-16 05:44:47 +00:00
|
|
|
|
template <bool positive>
|
2011-09-26 11:05:38 +00:00
|
|
|
|
struct less
|
|
|
|
|
{
|
|
|
|
|
const ColumnFixedString & parent;
|
|
|
|
|
less(const ColumnFixedString & parent_) : parent(parent_) {}
|
|
|
|
|
bool operator()(size_t lhs, size_t rhs) const
|
|
|
|
|
{
|
2013-09-16 05:44:47 +00:00
|
|
|
|
/// TODO: memcmp тормозит.
|
2014-05-23 02:18:01 +00:00
|
|
|
|
int res = memcmp(&parent.chars[lhs * parent.n], &parent.chars[rhs * parent.n], parent.n);
|
|
|
|
|
return positive ? (res < 0) : (res > 0);
|
2011-09-26 11:05:38 +00:00
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2014-10-31 22:15:17 +00:00
|
|
|
|
void getPermutation(bool reverse, size_t limit, Permutation & res) const override
|
2011-09-26 11:05:38 +00:00
|
|
|
|
{
|
|
|
|
|
size_t s = 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;
|
|
|
|
|
|
2014-05-23 01:32:32 +00:00
|
|
|
|
if (limit >= s)
|
2013-09-16 05:44:47 +00:00
|
|
|
|
limit = 0;
|
|
|
|
|
|
|
|
|
|
if (limit)
|
|
|
|
|
{
|
|
|
|
|
if (reverse)
|
|
|
|
|
std::partial_sort(res.begin(), res.begin() + limit, res.end(), less<false>(*this));
|
|
|
|
|
else
|
|
|
|
|
std::partial_sort(res.begin(), res.begin() + limit, res.end(), less<true>(*this));
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
if (reverse)
|
|
|
|
|
std::sort(res.begin(), res.end(), less<false>(*this));
|
|
|
|
|
else
|
|
|
|
|
std::sort(res.begin(), res.end(), less<true>(*this));
|
|
|
|
|
}
|
2011-09-26 11:05:38 +00:00
|
|
|
|
}
|
2012-08-27 05:13:14 +00:00
|
|
|
|
|
2015-11-29 17:06:30 +00:00
|
|
|
|
void insertRangeFrom(const IColumn & src, size_t start, size_t length) override
|
2013-05-03 15:10:56 +00:00
|
|
|
|
{
|
2015-11-29 17:06:30 +00:00
|
|
|
|
const ColumnFixedString & src_concrete = static_cast<const ColumnFixedString &>(src);
|
|
|
|
|
|
|
|
|
|
if (start + length > src_concrete.size())
|
|
|
|
|
throw Exception("Parameters start = "
|
|
|
|
|
+ toString(start) + ", length = "
|
|
|
|
|
+ toString(length) + " are out of bound in ColumnFixedString::insertRangeFrom method"
|
|
|
|
|
" (size() = " + toString(src_concrete.size()) + ").",
|
|
|
|
|
ErrorCodes::PARAMETER_OUT_OF_BOUND);
|
|
|
|
|
|
|
|
|
|
size_t old_size = chars.size();
|
|
|
|
|
chars.resize(old_size + length * n);
|
|
|
|
|
memcpy(&chars[old_size], &src_concrete.chars[start * n], length * n);
|
2013-05-03 15:10:56 +00:00
|
|
|
|
}
|
|
|
|
|
|
2015-12-05 07:01:18 +00:00
|
|
|
|
ColumnPtr filter(const IColumn::Filter & filt, ssize_t result_size_hint) const override
|
2013-05-03 15:10:56 +00:00
|
|
|
|
{
|
2013-05-05 15:25:25 +00:00
|
|
|
|
size_t col_size = size();
|
|
|
|
|
if (col_size != filt.size())
|
2013-05-03 15:10:56 +00:00
|
|
|
|
throw Exception("Size of filter doesn't match size of column.", ErrorCodes::SIZES_OF_COLUMNS_DOESNT_MATCH);
|
|
|
|
|
|
2016-05-28 05:31:36 +00:00
|
|
|
|
std::shared_ptr<ColumnFixedString> res = std::make_shared<ColumnFixedString>(n);
|
2015-12-05 07:01:18 +00:00
|
|
|
|
|
|
|
|
|
if (result_size_hint)
|
2016-05-28 05:31:36 +00:00
|
|
|
|
res->chars.reserve(result_size_hint > 0 ? result_size_hint * n : chars.size());
|
2013-05-03 15:10:56 +00:00
|
|
|
|
|
2013-05-05 15:25:25 +00:00
|
|
|
|
size_t offset = 0;
|
|
|
|
|
for (size_t i = 0; i < col_size; ++i, offset += n)
|
|
|
|
|
{
|
2013-05-03 15:10:56 +00:00
|
|
|
|
if (filt[i])
|
2013-05-05 15:25:25 +00:00
|
|
|
|
{
|
2016-05-28 05:31:36 +00:00
|
|
|
|
res->chars.resize(res->chars.size() + n);
|
|
|
|
|
memcpySmallAllowReadWriteOverflow15(&res->chars[res->chars.size() - n], &chars[offset], n);
|
2013-05-05 15:25:25 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2013-05-03 15:10:56 +00:00
|
|
|
|
|
|
|
|
|
return res;
|
|
|
|
|
}
|
|
|
|
|
|
2014-10-31 22:15:17 +00:00
|
|
|
|
ColumnPtr permute(const Permutation & perm, size_t limit) const override
|
2013-05-03 15:10:56 +00:00
|
|
|
|
{
|
2013-05-05 15:25:25 +00:00
|
|
|
|
size_t col_size = size();
|
2013-05-03 15:10:56 +00:00
|
|
|
|
|
2013-09-16 05:44:47 +00:00
|
|
|
|
if (limit == 0)
|
|
|
|
|
limit = col_size;
|
|
|
|
|
else
|
|
|
|
|
limit = std::min(col_size, limit);
|
|
|
|
|
|
|
|
|
|
if (perm.size() < limit)
|
|
|
|
|
throw Exception("Size of permutation is less than required.", ErrorCodes::SIZES_OF_COLUMNS_DOESNT_MATCH);
|
|
|
|
|
|
|
|
|
|
if (limit == 0)
|
2016-05-28 05:31:36 +00:00
|
|
|
|
return std::make_shared<ColumnFixedString>(n);
|
2013-05-03 15:10:56 +00:00
|
|
|
|
|
2016-05-28 05:31:36 +00:00
|
|
|
|
std::shared_ptr<ColumnFixedString> res = std::make_shared<ColumnFixedString>(n);
|
2013-05-05 15:25:25 +00:00
|
|
|
|
|
2016-05-28 05:31:36 +00:00
|
|
|
|
Chars_t & res_chars = res->chars;
|
2013-05-05 15:25:25 +00:00
|
|
|
|
|
2013-09-16 05:44:47 +00:00
|
|
|
|
res_chars.resize(n * limit);
|
2013-05-05 15:25:25 +00:00
|
|
|
|
|
|
|
|
|
size_t offset = 0;
|
2013-09-16 05:44:47 +00:00
|
|
|
|
for (size_t i = 0; i < limit; ++i, offset += n)
|
2016-04-15 00:33:21 +00:00
|
|
|
|
memcpySmallAllowReadWriteOverflow15(&res_chars[offset], &chars[perm[i] * n], n);
|
2013-05-05 15:25:25 +00:00
|
|
|
|
|
2013-05-03 15:10:56 +00:00
|
|
|
|
return res;
|
|
|
|
|
}
|
|
|
|
|
|
2014-10-31 22:15:17 +00:00
|
|
|
|
ColumnPtr replicate(const Offsets_t & offsets) const override
|
2012-08-27 05:13:14 +00:00
|
|
|
|
{
|
|
|
|
|
size_t col_size = size();
|
|
|
|
|
if (col_size != offsets.size())
|
|
|
|
|
throw Exception("Size of offsets doesn't match size of column.", ErrorCodes::SIZES_OF_COLUMNS_DOESNT_MATCH);
|
|
|
|
|
|
2016-05-28 05:31:36 +00:00
|
|
|
|
std::shared_ptr<ColumnFixedString> res = std::make_shared<ColumnFixedString>(n);
|
2014-08-20 04:57:03 +00:00
|
|
|
|
|
|
|
|
|
if (0 == col_size)
|
|
|
|
|
return res;
|
|
|
|
|
|
2016-05-28 05:31:36 +00:00
|
|
|
|
Chars_t & res_chars = res->chars;
|
2013-05-03 05:23:14 +00:00
|
|
|
|
res_chars.reserve(n * offsets.back());
|
2012-08-27 05:13:14 +00:00
|
|
|
|
|
|
|
|
|
Offset_t prev_offset = 0;
|
|
|
|
|
for (size_t i = 0; i < col_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)
|
|
|
|
|
for (size_t k = 0; k < n; ++k)
|
2013-05-05 15:25:25 +00:00
|
|
|
|
res_chars.push_back(chars[i * n + k]);
|
2012-08-27 05:13:14 +00:00
|
|
|
|
}
|
|
|
|
|
|
2013-05-03 05:23:14 +00:00
|
|
|
|
return res;
|
2012-08-27 05:13:14 +00:00
|
|
|
|
}
|
2013-05-05 15:25:25 +00:00
|
|
|
|
|
2014-10-31 22:15:17 +00:00
|
|
|
|
void getExtremes(Field & min, Field & max) const override
|
2013-09-06 20:28:22 +00:00
|
|
|
|
{
|
|
|
|
|
min = String();
|
|
|
|
|
max = String();
|
|
|
|
|
}
|
|
|
|
|
|
2015-12-05 07:01:18 +00:00
|
|
|
|
void reserve(size_t size) override
|
|
|
|
|
{
|
|
|
|
|
chars.reserve(n * size);
|
|
|
|
|
};
|
|
|
|
|
|
2013-05-05 15:25:25 +00:00
|
|
|
|
|
|
|
|
|
Chars_t & getChars() { return chars; }
|
|
|
|
|
const Chars_t & getChars() const { return chars; }
|
|
|
|
|
|
|
|
|
|
size_t getN() const { return n; }
|
2011-08-07 02:08:22 +00:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|