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

141 lines
3.3 KiB
C++
Raw Normal View History

2011-08-07 02:08:22 +00:00
#pragma once
#include <string.h> // memcpy
#include <DB/Common/PODArray.h>
#include <DB/Columns/IColumn.h>
2011-08-07 02:08:22 +00:00
namespace DB
{
/** A column of values of "fixed-length string" type.
2017-03-09 00:56:38 +00:00
* If you insert a smaller string, it will be padded with zero bytes.
2011-08-07 02:08:22 +00:00
*/
class ColumnFixedString final : public IColumn
2011-08-07 02:08:22 +00:00
{
public:
using Chars_t = PaddedPODArray<UInt8>;
2011-08-07 02:08:22 +00:00
private:
2017-03-09 04:26:17 +00:00
/// Bytes of rows, laid in succession. The strings are stored without a trailing zero byte.
/** NOTE It is required that the offset and type of chars in the object be the same as that of `data in ColumnUInt8`.
2017-03-09 00:56:38 +00:00
* Used in `packFixed` function (AggregationCommon.h)
2015-03-10 20:16:13 +00:00
*/
Chars_t chars;
2017-03-09 04:26:17 +00:00
/// The size of the rows.
2015-03-10 20:16:13 +00:00
const size_t n;
2011-08-07 02:08:22 +00:00
2017-03-11 00:38:30 +00:00
template <bool positive>
struct less;
2011-08-07 02:08:22 +00:00
public:
2017-03-09 04:26:17 +00:00
/** Create an empty column of strings of fixed-length `n` */
ColumnFixedString(size_t n_) : n(n_) {}
2011-08-07 02:08:22 +00:00
std::string getName() const override { return "ColumnFixedString"; }
2011-08-28 00:31:30 +00:00
2017-03-11 00:38:30 +00:00
ColumnPtr cloneResized(size_t size) const override;
size_t size() const override
{
return chars.size() / n;
}
size_t sizeOfField() const override
{
return n;
}
bool isFixed() const override
{
return true;
}
2014-08-20 04:57:03 +00:00
size_t byteSize() const override
{
return chars.size() + sizeof(n);
}
2014-08-20 04:57:03 +00:00
size_t allocatedSize() const override
{
return chars.allocated_size() + sizeof(n);
}
Field operator[](size_t index) const override
2011-08-07 02:08:22 +00:00
{
return String(reinterpret_cast<const char *>(&chars[n * index]), n);
2011-08-07 02:08:22 +00:00
}
void get(size_t index, Field & res) const override
{
res.assignString(reinterpret_cast<const char *>(&chars[n * index]), n);
}
StringRef getDataAt(size_t index) const override
{
return StringRef(&chars[n * index], n);
}
2017-03-11 00:38:30 +00:00
void insert(const Field & x) override;
2011-08-07 02:08:22 +00:00
2017-03-11 00:38:30 +00:00
void insertFrom(const IColumn & src_, size_t index) override;
2017-03-11 00:38:30 +00:00
void insertData(const char * pos, size_t length) override;
void insertDefault() override
2011-08-07 02:08:22 +00:00
{
chars.resize_fill(chars.size() + n);
2011-08-07 02:08:22 +00:00
}
2011-09-04 00:22:19 +00:00
void popBack(size_t elems) override
{
chars.resize_assume_reserved(chars.size() - n * elems);
}
2017-03-11 00:38:30 +00:00
StringRef serializeValueIntoArena(size_t index, Arena & arena, char const *& begin) const override;
2017-03-11 00:38:30 +00:00
const char * deserializeAndInsertFromArena(const char * pos) override;
2017-03-11 00:38:30 +00:00
void updateHashWithValue(size_t index, SipHash & hash) const override;
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_);
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
2017-03-11 00:38:30 +00:00
void getPermutation(bool reverse, size_t limit, Permutation & res) const override;
2011-09-26 11:05:38 +00:00
2017-03-11 00:38:30 +00:00
void insertRangeFrom(const IColumn & src, size_t start, size_t length) override;
2017-03-11 00:38:30 +00:00
ColumnPtr filter(const IColumn::Filter & filt, ssize_t result_size_hint) const override;
2017-03-11 00:38:30 +00:00
ColumnPtr permute(const Permutation & perm, size_t limit) const override;
2017-03-11 00:38:30 +00:00
ColumnPtr replicate(const Offsets_t & offsets) const override;
Columns scatter(ColumnIndex num_columns, const Selector & selector) const override
{
return scatterImpl<ColumnFixedString>(num_columns, selector);
}
void reserve(size_t size) override
{
chars.reserve(n * size);
};
2017-03-11 00:38:30 +00:00
void getExtremes(Field & min, Field & max) const override;
/// Specialized part of interface, not from IColumn.
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
};
}