2011-09-04 00:22:19 +00:00
|
|
|
|
#pragma once
|
2010-03-12 18:25:35 +00:00
|
|
|
|
|
2011-09-04 00:22:19 +00:00
|
|
|
|
#include <string.h>
|
2010-05-20 19:29:04 +00:00
|
|
|
|
|
2010-05-13 16:13:38 +00:00
|
|
|
|
#include <DB/Columns/ColumnArray.h>
|
|
|
|
|
#include <DB/Columns/ColumnsNumber.h>
|
2010-03-12 18:25:35 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
namespace DB
|
|
|
|
|
{
|
|
|
|
|
|
2010-05-13 16:13:38 +00:00
|
|
|
|
/** Cтолбeц значений типа "строка".
|
|
|
|
|
* Отличается от массива UInt8 только получением элемента (в виде String, а не Array)
|
|
|
|
|
*/
|
|
|
|
|
class ColumnString : public ColumnArray
|
|
|
|
|
{
|
2010-05-20 19:29:04 +00:00
|
|
|
|
private:
|
|
|
|
|
ColumnUInt8::Container_t & char_data;
|
|
|
|
|
|
2010-05-13 16:13:38 +00:00
|
|
|
|
public:
|
|
|
|
|
/** Создать пустой столбец строк, с типом значений */
|
|
|
|
|
ColumnString()
|
2010-05-20 19:29:04 +00:00
|
|
|
|
: ColumnArray(new ColumnUInt8()),
|
|
|
|
|
char_data(dynamic_cast<ColumnUInt8 &>(*data).getData())
|
2010-05-13 16:13:38 +00:00
|
|
|
|
{
|
|
|
|
|
}
|
2010-05-21 19:52:50 +00:00
|
|
|
|
|
2011-08-28 00:31:30 +00:00
|
|
|
|
std::string getName() const { return "ColumnString"; }
|
|
|
|
|
|
2011-08-09 19:19:00 +00:00
|
|
|
|
ColumnPtr cloneEmpty() const
|
2010-05-21 19:52:50 +00:00
|
|
|
|
{
|
|
|
|
|
return new ColumnString;
|
|
|
|
|
}
|
2010-05-13 16:13:38 +00:00
|
|
|
|
|
|
|
|
|
Field operator[](size_t n) const
|
|
|
|
|
{
|
2012-08-26 04:40:29 +00:00
|
|
|
|
size_t offset = n == 0 ? 0 : getOffsets()[n - 1];
|
|
|
|
|
size_t size = getOffsets()[n] - offset - 1;
|
2010-05-13 16:13:38 +00:00
|
|
|
|
const char * s = reinterpret_cast<const char *>(&dynamic_cast<const ColumnUInt8 &>(*data).getData()[offset]);
|
|
|
|
|
return String(s, size);
|
|
|
|
|
}
|
2010-05-20 19:29:04 +00:00
|
|
|
|
|
2012-10-07 06:30:10 +00:00
|
|
|
|
StringRef getDataAt(size_t n) const
|
|
|
|
|
{
|
|
|
|
|
return StringRef(&char_data[offsetAt(n)], sizeAt(n));
|
|
|
|
|
}
|
|
|
|
|
|
2010-05-20 19:29:04 +00:00
|
|
|
|
void insert(const Field & x)
|
|
|
|
|
{
|
2010-05-24 16:52:58 +00:00
|
|
|
|
const String & s = boost::get<const String &>(x);
|
2010-05-20 19:29:04 +00:00
|
|
|
|
size_t old_size = char_data.size();
|
|
|
|
|
size_t size_to_append = s.size() + 1;
|
|
|
|
|
char_data.resize(old_size + size_to_append);
|
|
|
|
|
memcpy(&char_data[old_size], s.c_str(), size_to_append);
|
2012-08-26 04:40:29 +00:00
|
|
|
|
getOffsets().push_back((getOffsets().size() == 0 ? 0 : getOffsets().back()) + size_to_append);
|
2010-05-20 19:29:04 +00:00
|
|
|
|
}
|
|
|
|
|
|
2012-10-07 00:23:18 +00:00
|
|
|
|
void filter(const Filter & filt)
|
|
|
|
|
{
|
|
|
|
|
size_t size = getOffsets().size();
|
|
|
|
|
if (size != filt.size())
|
|
|
|
|
throw Exception("Size of filter doesn't match size of column.", ErrorCodes::SIZES_OF_COLUMNS_DOESNT_MATCH);
|
|
|
|
|
|
|
|
|
|
if (size == 0)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
ColumnUInt8::Container_t tmp_chars;
|
|
|
|
|
Offsets_t tmp_offsets;
|
|
|
|
|
tmp_chars.reserve(char_data.size());
|
|
|
|
|
tmp_offsets.reserve(size);
|
|
|
|
|
|
|
|
|
|
Offset_t current_new_offset = 0;
|
|
|
|
|
|
|
|
|
|
for (size_t i = 0; i < size; ++i)
|
|
|
|
|
{
|
|
|
|
|
if (!filt[i])
|
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
size_t string_offset = i == 0 ? 0 : getOffsets()[i - 1];
|
|
|
|
|
size_t string_size = getOffsets()[i] - string_offset;
|
|
|
|
|
|
|
|
|
|
current_new_offset += string_size;
|
|
|
|
|
tmp_offsets.push_back(current_new_offset);
|
|
|
|
|
|
|
|
|
|
tmp_chars.resize(tmp_chars.size() + string_size);
|
|
|
|
|
memcpy(&tmp_chars[tmp_chars.size() - string_size], &char_data[string_offset], string_size);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
tmp_chars.swap(char_data);
|
|
|
|
|
tmp_offsets.swap(getOffsets());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void permute(const Permutation & perm)
|
|
|
|
|
{
|
|
|
|
|
size_t size = getOffsets().size();
|
|
|
|
|
if (size != perm.size())
|
|
|
|
|
throw Exception("Size of permutation doesn't match size of column.", ErrorCodes::SIZES_OF_COLUMNS_DOESNT_MATCH);
|
|
|
|
|
|
|
|
|
|
if (size == 0)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
ColumnUInt8::Container_t tmp_chars(char_data.size());
|
|
|
|
|
Offsets_t tmp_offsets(size);
|
|
|
|
|
|
|
|
|
|
Offset_t current_new_offset = 0;
|
|
|
|
|
|
|
|
|
|
for (size_t i = 0; i < size; ++i)
|
|
|
|
|
{
|
|
|
|
|
size_t j = perm[i];
|
|
|
|
|
size_t string_offset = j == 0 ? 0 : getOffsets()[j - 1];
|
|
|
|
|
size_t string_size = getOffsets()[j] - string_offset;
|
|
|
|
|
|
|
|
|
|
memcpy(&tmp_chars[current_new_offset], &char_data[string_offset], string_size);
|
|
|
|
|
|
|
|
|
|
current_new_offset += string_size;
|
|
|
|
|
tmp_offsets[i] = current_new_offset;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
tmp_chars.swap(char_data);
|
|
|
|
|
tmp_offsets.swap(getOffsets());
|
|
|
|
|
}
|
|
|
|
|
|
2010-05-20 19:29:04 +00:00
|
|
|
|
void insertDefault()
|
|
|
|
|
{
|
|
|
|
|
char_data.push_back(0);
|
2012-08-26 04:40:29 +00:00
|
|
|
|
getOffsets().push_back(getOffsets().size() == 0 ? 1 : (getOffsets().back() + 1));
|
2010-05-20 19:29:04 +00:00
|
|
|
|
}
|
2011-09-04 00:22:19 +00:00
|
|
|
|
|
|
|
|
|
int compareAt(size_t n, size_t m, const IColumn & rhs_) const
|
|
|
|
|
{
|
|
|
|
|
const ColumnString & rhs = static_cast<const ColumnString &>(rhs_);
|
|
|
|
|
|
2011-09-26 11:05:38 +00:00
|
|
|
|
/** Для производительности, строки сравниваются до первого нулевого байта.
|
|
|
|
|
* (если нулевой байт в середине строки, то то, что после него - игнорируется)
|
|
|
|
|
* Замечу, что завершающий нулевой байт всегда есть.
|
|
|
|
|
*/
|
|
|
|
|
return strcmp(
|
|
|
|
|
reinterpret_cast<const char *>(&char_data[offsetAt(n)]),
|
|
|
|
|
reinterpret_cast<const char *>(&rhs.char_data[rhs.offsetAt(m)]));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
struct less
|
|
|
|
|
{
|
|
|
|
|
const ColumnString & parent;
|
|
|
|
|
less(const ColumnString & parent_) : parent(parent_) {}
|
|
|
|
|
bool operator()(size_t lhs, size_t rhs) const
|
|
|
|
|
{
|
|
|
|
|
return 0 > strcmp(
|
|
|
|
|
reinterpret_cast<const char *>(&parent.char_data[parent.offsetAt(lhs)]),
|
|
|
|
|
reinterpret_cast<const char *>(&parent.char_data[parent.offsetAt(rhs)]));
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
Permutation getPermutation() const
|
|
|
|
|
{
|
2012-08-26 04:40:29 +00:00
|
|
|
|
size_t s = getOffsets().size();
|
2011-09-26 11:05:38 +00:00
|
|
|
|
Permutation res(s);
|
|
|
|
|
for (size_t i = 0; i < s; ++i)
|
|
|
|
|
res[i] = i;
|
|
|
|
|
|
|
|
|
|
std::sort(res.begin(), res.end(), less(*this));
|
|
|
|
|
return res;
|
2011-09-04 00:22:19 +00:00
|
|
|
|
}
|
2012-08-27 05:13:14 +00:00
|
|
|
|
|
|
|
|
|
void replicate(const Offsets_t & replicate_offsets)
|
|
|
|
|
{
|
|
|
|
|
size_t col_size = size();
|
|
|
|
|
if (col_size != replicate_offsets.size())
|
|
|
|
|
throw Exception("Size of offsets doesn't match size of column.", ErrorCodes::SIZES_OF_COLUMNS_DOESNT_MATCH);
|
|
|
|
|
|
|
|
|
|
ColumnUInt8::Container_t tmp_chars;
|
|
|
|
|
Offsets_t tmp_offsets;
|
|
|
|
|
tmp_chars.reserve(char_data.size() / col_size * replicate_offsets.back());
|
|
|
|
|
tmp_offsets.reserve(replicate_offsets.back());
|
|
|
|
|
|
|
|
|
|
Offset_t prev_replicate_offset = 0;
|
|
|
|
|
Offset_t prev_string_offset = 0;
|
|
|
|
|
Offset_t current_new_offset = 0;
|
|
|
|
|
|
|
|
|
|
for (size_t i = 0; i < col_size; ++i)
|
|
|
|
|
{
|
|
|
|
|
size_t size_to_replicate = replicate_offsets[i] - prev_replicate_offset;
|
|
|
|
|
size_t string_size = getOffsets()[i] - prev_string_offset;
|
|
|
|
|
|
|
|
|
|
for (size_t j = 0; j < size_to_replicate; ++j)
|
|
|
|
|
{
|
|
|
|
|
current_new_offset += string_size;
|
|
|
|
|
tmp_offsets.push_back(current_new_offset);
|
2012-09-19 18:45:01 +00:00
|
|
|
|
|
|
|
|
|
tmp_chars.resize(tmp_chars.size() + string_size);
|
|
|
|
|
memcpy(&tmp_chars[tmp_chars.size() - string_size], &char_data[prev_string_offset], string_size);
|
2012-08-27 05:13:14 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
prev_replicate_offset = replicate_offsets[i];
|
|
|
|
|
prev_string_offset = getOffsets()[i];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
tmp_chars.swap(char_data);
|
|
|
|
|
tmp_offsets.swap(getOffsets());
|
|
|
|
|
}
|
2010-05-13 16:13:38 +00:00
|
|
|
|
};
|
2010-03-12 18:25:35 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|