2011-08-07 02:08:22 +00:00
|
|
|
#pragma once
|
|
|
|
|
2017-04-01 09:19:00 +00:00
|
|
|
#include <Common/PODArray.h>
|
2019-03-03 20:08:39 +00:00
|
|
|
#include <Common/memcmpSmall.h>
|
2019-03-14 23:10:51 +00:00
|
|
|
#include <Common/typeid_cast.h>
|
2019-08-21 02:28:04 +00:00
|
|
|
#include <Common/assert_cast.h>
|
2017-04-01 09:19:00 +00:00
|
|
|
#include <Columns/IColumn.h>
|
2019-10-07 18:56:03 +00:00
|
|
|
#include <Columns/IColumnImpl.h>
|
2018-12-27 00:34:49 +00:00
|
|
|
#include <Columns/ColumnVectorHelper.h>
|
2019-09-27 13:44:33 +00:00
|
|
|
#include <Core/Field.h>
|
2011-08-07 02:08:22 +00:00
|
|
|
|
|
|
|
|
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
|
2017-03-09 04:18:41 +00:00
|
|
|
/** 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
|
|
|
*/
|
2019-04-19 20:21:17 +00:00
|
|
|
class ColumnFixedString final : public COWHelper<ColumnVectorHelper, ColumnFixedString>
|
2011-08-07 02:08:22 +00:00
|
|
|
{
|
2013-05-05 15:25:25 +00:00
|
|
|
public:
|
2019-04-19 20:21:17 +00:00
|
|
|
friend class COWHelper<ColumnVectorHelper, ColumnFixedString>;
|
2017-12-14 04:25:22 +00:00
|
|
|
|
2018-11-25 00:08:50 +00:00
|
|
|
using Chars = PaddedPODArray<UInt8>;
|
2013-05-05 15:25:25 +00:00
|
|
|
|
2011-08-07 02:08:22 +00:00
|
|
|
private:
|
2017-04-01 07:20:54 +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`.
|
|
|
|
* Used in `packFixed` function (AggregationCommon.h)
|
|
|
|
*/
|
2018-11-25 00:08:50 +00:00
|
|
|
Chars chars;
|
2017-04-01 07:20:54 +00:00
|
|
|
/// The size of the rows.
|
|
|
|
const size_t n;
|
2011-08-07 02:08:22 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
template <bool positive>
|
|
|
|
struct less;
|
2017-03-11 00:38:30 +00:00
|
|
|
|
2017-04-01 07:20:54 +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
|
|
|
|
2018-06-03 16:51:31 +00:00
|
|
|
ColumnFixedString(const ColumnFixedString & src) : chars(src.chars.begin(), src.chars.end()), n(src.n) {}
|
2017-12-14 03:56:56 +00:00
|
|
|
|
|
|
|
public:
|
2017-12-07 22:11:51 +00:00
|
|
|
std::string getName() const override { return "FixedString(" + std::to_string(n) + ")"; }
|
|
|
|
const char * getFamilyName() const override { return "FixedString"; }
|
2011-08-28 00:31:30 +00:00
|
|
|
|
2017-12-14 03:56:56 +00:00
|
|
|
MutableColumnPtr cloneResized(size_t size) const override;
|
2013-05-05 15:25:25 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
size_t size() const override
|
|
|
|
{
|
|
|
|
return chars.size() / n;
|
|
|
|
}
|
2013-07-19 20:12:02 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
size_t byteSize() const override
|
|
|
|
{
|
|
|
|
return chars.size() + sizeof(n);
|
|
|
|
}
|
2014-08-20 04:57:03 +00:00
|
|
|
|
2017-07-13 16:49:09 +00:00
|
|
|
size_t allocatedBytes() const override
|
2017-04-01 07:20:54 +00:00
|
|
|
{
|
2017-07-13 16:49:09 +00:00
|
|
|
return chars.allocated_bytes() + sizeof(n);
|
2017-04-01 07:20:54 +00:00
|
|
|
}
|
2017-01-17 20:54:32 +00:00
|
|
|
|
2019-03-10 03:16:51 +00:00
|
|
|
void protect() override
|
|
|
|
{
|
|
|
|
chars.protect();
|
|
|
|
}
|
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
Field operator[](size_t index) const override
|
|
|
|
{
|
|
|
|
return String(reinterpret_cast<const char *>(&chars[n * index]), n);
|
|
|
|
}
|
2011-08-07 02:08:22 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
void get(size_t index, Field & res) const override
|
|
|
|
{
|
|
|
|
res.assignString(reinterpret_cast<const char *>(&chars[n * index]), n);
|
|
|
|
}
|
2013-01-07 06:47:15 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
StringRef getDataAt(size_t index) const override
|
|
|
|
{
|
|
|
|
return StringRef(&chars[n * index], n);
|
|
|
|
}
|
2012-10-07 06:30:10 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
void insert(const Field & x) override;
|
2011-08-07 02:08:22 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
void insertFrom(const IColumn & src_, size_t index) override;
|
2012-12-16 00:52:06 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
void insertData(const char * pos, size_t length) override;
|
2013-02-16 20:15:45 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
void insertDefault() override
|
|
|
|
{
|
|
|
|
chars.resize_fill(chars.size() + n);
|
|
|
|
}
|
2011-09-04 00:22:19 +00:00
|
|
|
|
2019-11-08 11:15:20 +00:00
|
|
|
virtual void insertManyDefaults(size_t length) override
|
|
|
|
{
|
|
|
|
chars.resize_fill(chars.size() + n * length);
|
|
|
|
}
|
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
void popBack(size_t elems) override
|
|
|
|
{
|
|
|
|
chars.resize_assume_reserved(chars.size() - n * elems);
|
|
|
|
}
|
2016-02-16 16:39:39 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
StringRef serializeValueIntoArena(size_t index, Arena & arena, char const *& begin) const override;
|
2015-10-04 03:17:36 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
const char * deserializeAndInsertFromArena(const char * pos) override;
|
2015-10-04 03:17:36 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
void updateHashWithValue(size_t index, SipHash & hash) const override;
|
2016-07-10 15:58:58 +00:00
|
|
|
|
2017-12-01 17:49:12 +00:00
|
|
|
int compareAt(size_t p1, size_t p2, const IColumn & rhs_, int /*nan_direction_hint*/) const override
|
2017-04-01 07:20:54 +00:00
|
|
|
{
|
2019-08-21 02:28:04 +00:00
|
|
|
const ColumnFixedString & rhs = assert_cast<const ColumnFixedString &>(rhs_);
|
2019-03-03 20:08:39 +00:00
|
|
|
return memcmpSmallAllowOverflow15(chars.data() + p1 * n, rhs.chars.data() + p2 * n, n);
|
2017-04-01 07:20:54 +00:00
|
|
|
}
|
2011-09-26 11:05:38 +00:00
|
|
|
|
2019-02-18 19:44:26 +00:00
|
|
|
void getPermutation(bool reverse, size_t limit, int nan_direction_hint, Permutation & res) const override;
|
2011-09-26 11:05:38 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
void insertRangeFrom(const IColumn & src, size_t start, size_t length) override;
|
2013-05-05 15:25:25 +00:00
|
|
|
|
2018-03-20 14:17:09 +00:00
|
|
|
ColumnPtr filter(const IColumn::Filter & filt, ssize_t result_size_hint) const override;
|
2013-05-05 15:25:25 +00:00
|
|
|
|
2019-02-18 19:44:26 +00:00
|
|
|
ColumnPtr permute(const Permutation & perm, size_t limit) const override;
|
2013-05-05 15:25:25 +00:00
|
|
|
|
2019-02-18 17:28:53 +00:00
|
|
|
ColumnPtr index(const IColumn & indexes, size_t limit) const override;
|
2018-04-23 16:40:25 +00:00
|
|
|
|
|
|
|
template <typename Type>
|
2019-02-18 17:28:53 +00:00
|
|
|
ColumnPtr indexImpl(const PaddedPODArray<Type> & indexes, size_t limit) const;
|
2018-04-23 16:40:25 +00:00
|
|
|
|
2018-03-20 14:17:09 +00:00
|
|
|
ColumnPtr replicate(const Offsets & offsets) const override;
|
2013-05-05 15:25:25 +00:00
|
|
|
|
2017-12-14 03:56:56 +00:00
|
|
|
MutableColumns scatter(ColumnIndex num_columns, const Selector & selector) const override
|
2017-04-01 07:20:54 +00:00
|
|
|
{
|
|
|
|
return scatterImpl<ColumnFixedString>(num_columns, selector);
|
|
|
|
}
|
2017-02-11 20:20:57 +00:00
|
|
|
|
2017-07-06 13:54:55 +00:00
|
|
|
void gather(ColumnGathererStream & gatherer_stream) override;
|
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
void reserve(size_t size) override
|
|
|
|
{
|
|
|
|
chars.reserve(n * size);
|
2018-06-03 16:51:31 +00:00
|
|
|
}
|
2015-12-05 07:01:18 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
void getExtremes(Field & min, Field & max) const override;
|
2017-03-11 00:38:30 +00:00
|
|
|
|
2019-03-14 23:10:51 +00:00
|
|
|
bool structureEquals(const IColumn & rhs) const override
|
|
|
|
{
|
|
|
|
if (auto rhs_concrete = typeid_cast<const ColumnFixedString *>(&rhs))
|
|
|
|
return n == rhs_concrete->n;
|
|
|
|
return false;
|
|
|
|
}
|
2017-03-11 00:38:30 +00:00
|
|
|
|
2017-12-09 10:14:45 +00:00
|
|
|
bool canBeInsideNullable() const override { return true; }
|
|
|
|
|
|
|
|
bool isFixedAndContiguous() const override { return true; }
|
|
|
|
size_t sizeOfValueIfFixed() const override { return n; }
|
2018-04-25 15:16:48 +00:00
|
|
|
StringRef getRawData() const override { return StringRef(chars.data(), chars.size()); }
|
2017-12-09 10:14:45 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
/// Specialized part of interface, not from IColumn.
|
2018-12-13 13:41:47 +00:00
|
|
|
void insertString(const String & string) { insertData(string.c_str(), string.size()); }
|
2018-11-25 00:08:50 +00:00
|
|
|
Chars & getChars() { return chars; }
|
|
|
|
const Chars & getChars() const { return chars; }
|
2013-05-05 15:25:25 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
size_t getN() const { return n; }
|
2011-08-07 02:08:22 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
}
|