2012-08-20 05:32:50 +00:00
|
|
|
|
#pragma once
|
2010-05-13 16:17:10 +00:00
|
|
|
|
|
2010-05-13 16:18:26 +00:00
|
|
|
|
#include <string.h> // memcpy
|
|
|
|
|
|
2010-05-13 16:17:10 +00:00
|
|
|
|
#include <Poco/SharedPtr.h>
|
|
|
|
|
|
|
|
|
|
#include <DB/Core/Exception.h>
|
|
|
|
|
#include <DB/Core/ErrorCodes.h>
|
|
|
|
|
|
|
|
|
|
#include <DB/Columns/IColumn.h>
|
2012-08-26 04:40:29 +00:00
|
|
|
|
#include <DB/Columns/ColumnsNumber.h>
|
2013-07-28 00:35:29 +00:00
|
|
|
|
#include <DB/Columns/ColumnString.h>
|
2010-05-13 16:17:10 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
namespace DB
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
using Poco::SharedPtr;
|
|
|
|
|
|
|
|
|
|
/** Cтолбeц значений типа массив.
|
|
|
|
|
* В памяти он представлен, как один столбец вложенного типа, размер которого равен сумме размеров всех массивов,
|
|
|
|
|
* и как массив смещений в нём, который позволяет достать каждый элемент.
|
|
|
|
|
*/
|
|
|
|
|
class ColumnArray : public IColumn
|
|
|
|
|
{
|
|
|
|
|
public:
|
|
|
|
|
/** По индексу i находится смещение до начала i + 1 -го элемента. */
|
2012-08-26 04:40:29 +00:00
|
|
|
|
typedef ColumnVector<Offset_t> ColumnOffsets_t;
|
2010-05-13 16:17:10 +00:00
|
|
|
|
|
|
|
|
|
/** Создать пустой столбец массивов, с типом значений, как в столбце nested_column */
|
2013-05-20 15:42:57 +00:00
|
|
|
|
explicit ColumnArray(ColumnPtr nested_column, ColumnPtr offsets_column = NULL)
|
|
|
|
|
: data(nested_column), offsets(offsets_column)
|
2010-05-13 16:17:10 +00:00
|
|
|
|
{
|
2013-05-20 15:42:57 +00:00
|
|
|
|
if (!offsets_column)
|
|
|
|
|
{
|
|
|
|
|
offsets = new ColumnOffsets_t;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
if (!dynamic_cast<ColumnOffsets_t *>(&*offsets_column))
|
|
|
|
|
throw Exception("offsets_column must be a ColumnVector<UInt64>", ErrorCodes::ILLEGAL_COLUMN);
|
|
|
|
|
}
|
2010-05-13 16:17:10 +00:00
|
|
|
|
}
|
2010-05-21 19:52:50 +00:00
|
|
|
|
|
2011-08-28 00:31:30 +00:00
|
|
|
|
std::string getName() const { return "ColumnArray(" + data->getName() + ")"; }
|
|
|
|
|
|
2011-08-09 19:19:00 +00:00
|
|
|
|
ColumnPtr cloneEmpty() const
|
2010-05-21 19:52:50 +00:00
|
|
|
|
{
|
|
|
|
|
return new ColumnArray(data->cloneEmpty());
|
|
|
|
|
}
|
2010-05-13 16:17:10 +00:00
|
|
|
|
|
|
|
|
|
size_t size() const
|
|
|
|
|
{
|
2012-08-26 04:40:29 +00:00
|
|
|
|
return getOffsets().size();
|
2010-05-13 16:17:10 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Field operator[](size_t n) const
|
|
|
|
|
{
|
2011-08-22 20:24:45 +00:00
|
|
|
|
size_t offset = offsetAt(n);
|
|
|
|
|
size_t size = sizeAt(n);
|
2010-05-13 16:17:10 +00:00
|
|
|
|
Array res(size);
|
|
|
|
|
|
|
|
|
|
for (size_t i = 0; i < size; ++i)
|
|
|
|
|
res[i] = (*data)[offset + i];
|
|
|
|
|
|
|
|
|
|
return res;
|
|
|
|
|
}
|
|
|
|
|
|
2013-01-07 06:47:15 +00:00
|
|
|
|
void get(size_t n, Field & res) const
|
|
|
|
|
{
|
|
|
|
|
size_t offset = offsetAt(n);
|
|
|
|
|
size_t size = sizeAt(n);
|
|
|
|
|
res = Array(size);
|
|
|
|
|
Array & res_arr = DB::get<Array &>(res);
|
|
|
|
|
|
|
|
|
|
for (size_t i = 0; i < size; ++i)
|
|
|
|
|
data->get(offset + i, res_arr[i]);
|
|
|
|
|
}
|
|
|
|
|
|
2012-10-07 06:30:10 +00:00
|
|
|
|
StringRef getDataAt(size_t n) const
|
|
|
|
|
{
|
2014-01-08 19:05:50 +00:00
|
|
|
|
/** Работает для массивов чисел.
|
|
|
|
|
* Для массивов строк и массивов массивов полученный кусок памяти может не взаимно-однозначно соответствовать элементам.
|
|
|
|
|
*/
|
|
|
|
|
StringRef begin = data->getDataAt(offsetAt(n));
|
|
|
|
|
StringRef end = data->getDataAt(offsetAt(n) + sizeAt(n));
|
|
|
|
|
return StringRef(begin.data, end.data - begin.data);
|
2012-10-07 06:30:10 +00:00
|
|
|
|
}
|
2013-02-16 20:15:45 +00:00
|
|
|
|
|
|
|
|
|
void insertData(const char * pos, size_t length)
|
|
|
|
|
{
|
|
|
|
|
throw Exception("Method insertData is not supported for " + getName(), ErrorCodes::NOT_IMPLEMENTED);
|
|
|
|
|
}
|
2012-10-07 06:30:10 +00:00
|
|
|
|
|
2013-05-03 05:23:14 +00:00
|
|
|
|
ColumnPtr cut(size_t start, size_t length) const
|
2010-05-13 16:17:10 +00:00
|
|
|
|
{
|
2012-09-20 18:40:58 +00:00
|
|
|
|
if (length == 0)
|
2013-05-03 05:23:14 +00:00
|
|
|
|
return new ColumnArray(data);
|
2012-09-20 18:40:58 +00:00
|
|
|
|
|
|
|
|
|
if (start + length > getOffsets().size())
|
2010-05-13 16:17:10 +00:00
|
|
|
|
throw Exception("Parameter out of bound in IColumnArray::cut() method.",
|
|
|
|
|
ErrorCodes::PARAMETER_OUT_OF_BOUND);
|
|
|
|
|
|
2011-08-22 20:24:45 +00:00
|
|
|
|
size_t nested_offset = offsetAt(start);
|
2012-08-26 04:40:29 +00:00
|
|
|
|
size_t nested_length = getOffsets()[start + length - 1] - nested_offset;
|
2011-08-21 03:41:37 +00:00
|
|
|
|
|
2013-05-03 05:23:14 +00:00
|
|
|
|
ColumnArray * res_ = new ColumnArray(data);
|
|
|
|
|
ColumnPtr res = res_;
|
|
|
|
|
|
|
|
|
|
res_->data = data->cut(nested_offset, nested_length);
|
|
|
|
|
Offsets_t & res_offsets = res_->getOffsets();
|
2011-08-21 03:41:37 +00:00
|
|
|
|
|
2010-05-13 16:17:10 +00:00
|
|
|
|
if (start == 0)
|
2013-05-03 05:23:14 +00:00
|
|
|
|
{
|
|
|
|
|
res_offsets.assign(getOffsets().begin(), getOffsets().begin() + length);
|
|
|
|
|
}
|
2010-05-13 16:17:10 +00:00
|
|
|
|
else
|
|
|
|
|
{
|
2013-05-03 05:23:14 +00:00
|
|
|
|
res_offsets.resize(length);
|
2011-08-21 03:41:37 +00:00
|
|
|
|
|
|
|
|
|
for (size_t i = 0; i < length; ++i)
|
2013-05-03 05:23:14 +00:00
|
|
|
|
res_offsets[i] = getOffsets()[start + i] - nested_offset;
|
2010-05-13 16:17:10 +00:00
|
|
|
|
}
|
2013-05-03 05:23:14 +00:00
|
|
|
|
|
|
|
|
|
return res;
|
2010-05-13 16:17:10 +00:00
|
|
|
|
}
|
|
|
|
|
|
2010-05-20 19:29:04 +00:00
|
|
|
|
void insert(const Field & x)
|
|
|
|
|
{
|
2013-01-07 06:47:15 +00:00
|
|
|
|
const Array & array = DB::get<const Array &>(x);
|
2010-05-20 19:29:04 +00:00
|
|
|
|
size_t size = array.size();
|
|
|
|
|
for (size_t i = 0; i < size; ++i)
|
|
|
|
|
data->insert(array[i]);
|
2012-08-26 04:40:29 +00:00
|
|
|
|
getOffsets().push_back((getOffsets().size() == 0 ? 0 : getOffsets().back()) + size);
|
2010-05-20 19:29:04 +00:00
|
|
|
|
}
|
|
|
|
|
|
2012-12-16 00:52:06 +00:00
|
|
|
|
void insertFrom(const IColumn & src_, size_t n)
|
|
|
|
|
{
|
|
|
|
|
const ColumnArray & src = static_cast<const ColumnArray &>(src_);
|
|
|
|
|
size_t size = src.sizeAt(n);
|
|
|
|
|
size_t offset = src.offsetAt(n);
|
|
|
|
|
|
|
|
|
|
for (size_t i = 0; i < size; ++i)
|
|
|
|
|
data->insertFrom(src.getData(), offset + i);
|
|
|
|
|
|
|
|
|
|
getOffsets().push_back((getOffsets().size() == 0 ? 0 : getOffsets().back()) + size);
|
|
|
|
|
}
|
|
|
|
|
|
2010-05-20 19:29:04 +00:00
|
|
|
|
void insertDefault()
|
|
|
|
|
{
|
|
|
|
|
data->insertDefault();
|
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
|
|
|
|
}
|
|
|
|
|
|
2013-05-03 05:23:14 +00:00
|
|
|
|
ColumnPtr filter(const Filter & filt) const
|
2011-08-22 20:24:45 +00:00
|
|
|
|
{
|
2012-08-26 04:40:29 +00:00
|
|
|
|
size_t size = getOffsets().size();
|
2011-08-22 20:24:45 +00:00
|
|
|
|
if (size != filt.size())
|
|
|
|
|
throw Exception("Size of filter doesn't match size of column.", ErrorCodes::SIZES_OF_COLUMNS_DOESNT_MATCH);
|
|
|
|
|
|
|
|
|
|
if (size == 0)
|
2013-05-03 05:23:14 +00:00
|
|
|
|
return new ColumnArray(data);
|
2011-08-22 20:24:45 +00:00
|
|
|
|
|
|
|
|
|
/// Не слишком оптимально. Можно сделать специализацию для массивов известных типов.
|
2012-08-26 04:40:29 +00:00
|
|
|
|
Filter nested_filt(getOffsets().back());
|
2011-08-22 20:24:45 +00:00
|
|
|
|
for (size_t i = 0; i < size; ++i)
|
2013-12-13 01:51:57 +00:00
|
|
|
|
{
|
2011-08-22 20:24:45 +00:00
|
|
|
|
if (filt[i])
|
|
|
|
|
memset(&nested_filt[offsetAt(i)], 1, sizeAt(i));
|
2013-12-13 01:51:57 +00:00
|
|
|
|
else
|
|
|
|
|
memset(&nested_filt[offsetAt(i)], 0, sizeAt(i));
|
|
|
|
|
}
|
2013-05-03 05:23:14 +00:00
|
|
|
|
|
|
|
|
|
ColumnArray * res_ = new ColumnArray(data);
|
|
|
|
|
ColumnPtr res = res_;
|
|
|
|
|
res_->data = data->filter(nested_filt);
|
|
|
|
|
|
|
|
|
|
Offsets_t & res_offsets = res_->getOffsets();
|
|
|
|
|
res_offsets.reserve(size);
|
2011-08-22 20:24:45 +00:00
|
|
|
|
|
|
|
|
|
size_t current_offset = 0;
|
|
|
|
|
for (size_t i = 0; i < size; ++i)
|
|
|
|
|
{
|
|
|
|
|
if (filt[i])
|
|
|
|
|
{
|
|
|
|
|
current_offset += sizeAt(i);
|
2013-05-03 05:23:14 +00:00
|
|
|
|
res_offsets.push_back(current_offset);
|
2011-08-22 20:24:45 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2013-05-03 05:23:14 +00:00
|
|
|
|
return res;
|
2011-08-22 20:24:45 +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
|
|
|
|
{
|
2012-08-26 04:40:29 +00:00
|
|
|
|
size_t size = getOffsets().size();
|
2011-09-04 00:22:19 +00:00
|
|
|
|
|
2013-09-16 05:44:47 +00:00
|
|
|
|
if (limit == 0)
|
|
|
|
|
limit = size;
|
|
|
|
|
else
|
|
|
|
|
limit = std::min(size, limit);
|
|
|
|
|
|
|
|
|
|
if (perm.size() < limit)
|
|
|
|
|
throw Exception("Size of permutation is less than required.", ErrorCodes::SIZES_OF_COLUMNS_DOESNT_MATCH);
|
|
|
|
|
|
|
|
|
|
if (limit == 0)
|
2013-05-03 05:23:14 +00:00
|
|
|
|
return new ColumnArray(data);
|
2011-09-04 00:22:19 +00:00
|
|
|
|
|
2013-12-16 04:30:21 +00:00
|
|
|
|
Permutation nested_perm(getOffsets().back());
|
2011-09-04 00:22:19 +00:00
|
|
|
|
|
2013-12-16 05:27:38 +00:00
|
|
|
|
ColumnArray * res_ = new ColumnArray(data->cloneEmpty());
|
2013-05-03 05:23:14 +00:00
|
|
|
|
ColumnPtr res = res_;
|
|
|
|
|
|
|
|
|
|
Offsets_t & res_offsets = res_->getOffsets();
|
2013-09-16 05:44:47 +00:00
|
|
|
|
res_offsets.resize(limit);
|
2011-09-04 00:22:19 +00:00
|
|
|
|
size_t current_offset = 0;
|
|
|
|
|
|
2013-09-16 05:44:47 +00:00
|
|
|
|
for (size_t i = 0; i < limit; ++i)
|
2011-09-04 00:22:19 +00:00
|
|
|
|
{
|
|
|
|
|
for (size_t j = 0; j < sizeAt(perm[i]); ++j)
|
|
|
|
|
nested_perm[current_offset + j] = offsetAt(perm[i]) + j;
|
|
|
|
|
current_offset += sizeAt(perm[i]);
|
2013-05-03 05:23:14 +00:00
|
|
|
|
res_offsets[i] = current_offset;
|
2011-09-04 00:22:19 +00:00
|
|
|
|
}
|
|
|
|
|
|
2013-12-16 05:12:55 +00:00
|
|
|
|
if (current_offset != 0)
|
|
|
|
|
res_->data = data->permute(nested_perm, current_offset);
|
2013-05-03 05:23:14 +00:00
|
|
|
|
|
|
|
|
|
return res;
|
2011-09-04 00:22:19 +00:00
|
|
|
|
}
|
|
|
|
|
|
2014-03-25 23:16:20 +00:00
|
|
|
|
int compareAt(size_t n, size_t m, const IColumn & rhs_, int nan_direction_hint) const final
|
2011-09-04 00:22:19 +00:00
|
|
|
|
{
|
|
|
|
|
const ColumnArray & rhs = static_cast<const ColumnArray &>(rhs_);
|
|
|
|
|
|
|
|
|
|
/// Не оптимально
|
|
|
|
|
size_t lhs_size = sizeAt(n);
|
|
|
|
|
size_t rhs_size = rhs.sizeAt(m);
|
|
|
|
|
size_t min_size = std::min(lhs_size, rhs_size);
|
|
|
|
|
for (size_t i = 0; i < min_size; ++i)
|
2014-03-25 23:16:20 +00:00
|
|
|
|
if (int res = data.get()->compareAt(offsetAt(n) + i, rhs.offsetAt(m) + i, *rhs.data.get(), nan_direction_hint))
|
2011-09-04 00:22:19 +00:00
|
|
|
|
return res;
|
|
|
|
|
|
|
|
|
|
return lhs_size < rhs_size
|
|
|
|
|
? -1
|
|
|
|
|
: (lhs_size == rhs_size
|
|
|
|
|
? 0
|
|
|
|
|
: 1);
|
|
|
|
|
}
|
|
|
|
|
|
2013-09-16 05:44:47 +00:00
|
|
|
|
template <bool positive>
|
2011-09-26 11:05:38 +00:00
|
|
|
|
struct less
|
|
|
|
|
{
|
|
|
|
|
const ColumnArray & parent;
|
|
|
|
|
|
2014-03-25 23:16:20 +00:00
|
|
|
|
less(const ColumnArray & parent_) : parent(parent_) {}
|
|
|
|
|
|
2011-09-26 11:05:38 +00:00
|
|
|
|
bool operator()(size_t lhs, size_t rhs) const
|
|
|
|
|
{
|
2014-03-25 23:16:20 +00:00
|
|
|
|
if (positive)
|
|
|
|
|
return parent.compareAt(lhs, rhs, parent, 1) < 0;
|
|
|
|
|
else
|
|
|
|
|
return parent.compareAt(lhs, rhs, parent, -1) > 0;
|
2011-09-26 11:05:38 +00:00
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
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
|
|
|
|
{
|
|
|
|
|
size_t s = size();
|
2014-03-25 23:16:20 +00:00
|
|
|
|
if (limit > s)
|
|
|
|
|
limit = 0;
|
|
|
|
|
|
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;
|
|
|
|
|
|
2013-09-16 05:44:47 +00:00
|
|
|
|
if (limit)
|
|
|
|
|
{
|
|
|
|
|
if (reverse)
|
2014-03-25 23:16:20 +00:00
|
|
|
|
std::partial_sort(res.begin(), res.begin() + limit, res.end(), less<false>(*this));
|
2013-09-16 05:44:47 +00:00
|
|
|
|
else
|
2014-03-25 23:16:20 +00:00
|
|
|
|
std::partial_sort(res.begin(), res.begin() + limit, res.end(), less<true>(*this));
|
2013-09-16 05:44:47 +00:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
if (reverse)
|
2014-03-25 23:16:20 +00:00
|
|
|
|
std::sort(res.begin(), res.end(), less<false>(*this));
|
2013-09-16 05:44:47 +00:00
|
|
|
|
else
|
2014-03-25 23:16:20 +00:00
|
|
|
|
std::sort(res.begin(), res.end(), less<true>(*this));
|
2013-09-16 05:44:47 +00:00
|
|
|
|
}
|
2011-09-26 11:05:38 +00:00
|
|
|
|
}
|
|
|
|
|
|
2013-02-03 18:39:09 +00:00
|
|
|
|
void reserve(size_t n)
|
|
|
|
|
{
|
|
|
|
|
getOffsets().reserve(n);
|
|
|
|
|
getData().reserve(n); /// Средний размер массивов тут никак не учитывается. Или считается, что он не больше единицы.
|
|
|
|
|
}
|
|
|
|
|
|
2012-05-17 19:15:53 +00:00
|
|
|
|
size_t byteSize() const
|
2011-08-27 22:43:31 +00:00
|
|
|
|
{
|
2012-08-26 04:40:29 +00:00
|
|
|
|
return data->byteSize() + getOffsets().size() * sizeof(getOffsets()[0]);
|
2011-08-27 22:43:31 +00:00
|
|
|
|
}
|
2013-09-06 20:28:22 +00:00
|
|
|
|
|
|
|
|
|
void getExtremes(Field & min, Field & max) const
|
|
|
|
|
{
|
|
|
|
|
min = Array();
|
|
|
|
|
max = Array();
|
|
|
|
|
}
|
2013-07-22 12:04:08 +00:00
|
|
|
|
|
2013-07-22 16:49:19 +00:00
|
|
|
|
bool hasEqualOffsets(const ColumnArray & other) const
|
2013-07-22 12:04:08 +00:00
|
|
|
|
{
|
2013-07-22 16:49:19 +00:00
|
|
|
|
if (offsets == other.offsets)
|
2013-07-22 12:04:08 +00:00
|
|
|
|
return true;
|
|
|
|
|
|
|
|
|
|
const Offsets_t & offsets1 = getOffsets();
|
2013-07-22 16:49:19 +00:00
|
|
|
|
const Offsets_t & offsets2 = other.getOffsets();
|
2013-07-22 12:04:08 +00:00
|
|
|
|
return offsets1.size() == offsets2.size() && 0 == memcmp(&offsets1[0], &offsets2[0], sizeof(offsets1[0]) * offsets1.size());
|
|
|
|
|
}
|
2011-08-27 22:43:31 +00:00
|
|
|
|
|
2010-05-13 16:17:10 +00:00
|
|
|
|
/** Более эффективные методы манипуляции */
|
2012-08-26 11:14:52 +00:00
|
|
|
|
IColumn & getData() { return *data; }
|
|
|
|
|
const IColumn & getData() const { return *data; }
|
2010-05-13 16:17:10 +00:00
|
|
|
|
|
2012-08-26 11:14:52 +00:00
|
|
|
|
ColumnPtr & getDataPtr() { return data; }
|
|
|
|
|
const ColumnPtr & getDataPtr() const { return data; }
|
2010-05-13 16:17:10 +00:00
|
|
|
|
|
2013-01-06 19:06:21 +00:00
|
|
|
|
Offsets_t & __attribute__((__always_inline__)) getOffsets()
|
2010-05-13 16:17:10 +00:00
|
|
|
|
{
|
2012-08-26 04:40:29 +00:00
|
|
|
|
return static_cast<ColumnOffsets_t &>(*offsets.get()).getData();
|
2010-05-13 16:17:10 +00:00
|
|
|
|
}
|
|
|
|
|
|
2013-01-06 19:06:21 +00:00
|
|
|
|
const Offsets_t & __attribute__((__always_inline__)) getOffsets() const
|
2010-05-13 16:17:10 +00:00
|
|
|
|
{
|
2012-08-26 04:40:29 +00:00
|
|
|
|
return static_cast<const ColumnOffsets_t &>(*offsets.get()).getData();
|
2010-05-13 16:17:10 +00:00
|
|
|
|
}
|
|
|
|
|
|
2012-08-26 06:48:39 +00:00
|
|
|
|
ColumnPtr & getOffsetsColumn() { return offsets; }
|
|
|
|
|
const ColumnPtr & getOffsetsColumn() const { return offsets; }
|
|
|
|
|
|
2013-07-27 21:50:13 +00:00
|
|
|
|
|
|
|
|
|
ColumnPtr replicate(const Offsets_t & replicate_offsets) const
|
|
|
|
|
{
|
|
|
|
|
/// Не получается реализовать в общем случае.
|
|
|
|
|
|
|
|
|
|
if (dynamic_cast<const ColumnUInt8 *>(&*data)) return replicate<UInt8>(replicate_offsets);
|
|
|
|
|
if (dynamic_cast<const ColumnUInt16 *>(&*data)) return replicate<UInt16>(replicate_offsets);
|
|
|
|
|
if (dynamic_cast<const ColumnUInt32 *>(&*data)) return replicate<UInt32>(replicate_offsets);
|
|
|
|
|
if (dynamic_cast<const ColumnUInt64 *>(&*data)) return replicate<UInt64>(replicate_offsets);
|
|
|
|
|
if (dynamic_cast<const ColumnInt8 *>(&*data)) return replicate<Int8>(replicate_offsets);
|
|
|
|
|
if (dynamic_cast<const ColumnInt16 *>(&*data)) return replicate<Int16>(replicate_offsets);
|
|
|
|
|
if (dynamic_cast<const ColumnInt32 *>(&*data)) return replicate<Int32>(replicate_offsets);
|
|
|
|
|
if (dynamic_cast<const ColumnInt64 *>(&*data)) return replicate<Int64>(replicate_offsets);
|
|
|
|
|
if (dynamic_cast<const ColumnFloat32 *>(&*data)) return replicate<Float32>(replicate_offsets);
|
|
|
|
|
if (dynamic_cast<const ColumnFloat64 *>(&*data)) return replicate<Float64>(replicate_offsets);
|
2013-07-28 00:35:29 +00:00
|
|
|
|
if (dynamic_cast<const ColumnString *>(&*data)) return replicateString(replicate_offsets);
|
2013-07-27 21:50:13 +00:00
|
|
|
|
|
|
|
|
|
throw Exception("Replication of column " + getName() + " is not implemented.", ErrorCodes::NOT_IMPLEMENTED);
|
|
|
|
|
}
|
|
|
|
|
|
2013-05-05 15:25:25 +00:00
|
|
|
|
private:
|
2011-08-09 19:19:00 +00:00
|
|
|
|
ColumnPtr data;
|
2012-08-26 06:48:39 +00:00
|
|
|
|
ColumnPtr offsets; /// Смещения могут быть разделяемыми для нескольких столбцов - для реализации вложенных структур данных.
|
2011-08-22 20:24:45 +00:00
|
|
|
|
|
2013-01-06 19:06:21 +00:00
|
|
|
|
size_t __attribute__((__always_inline__)) offsetAt(size_t i) const { return i == 0 ? 0 : getOffsets()[i - 1]; }
|
|
|
|
|
size_t __attribute__((__always_inline__)) sizeAt(size_t i) const { return i == 0 ? getOffsets()[0] : (getOffsets()[i] - getOffsets()[i - 1]); }
|
2013-07-27 21:50:13 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/// Размножить значения, если вложенный столбец - ColumnArray<T>.
|
|
|
|
|
template <typename T>
|
|
|
|
|
ColumnPtr replicate(const Offsets_t & replicate_offsets) const
|
|
|
|
|
{
|
|
|
|
|
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);
|
|
|
|
|
|
|
|
|
|
ColumnPtr res = cloneEmpty();
|
|
|
|
|
ColumnArray & res_ = dynamic_cast<ColumnArray &>(*res);
|
|
|
|
|
|
|
|
|
|
const typename ColumnVector<T>::Container_t & cur_data = dynamic_cast<const ColumnVector<T> &>(*data).getData();
|
|
|
|
|
const Offsets_t & cur_offsets = getOffsets();
|
|
|
|
|
|
|
|
|
|
typename ColumnVector<T>::Container_t & res_data = dynamic_cast<ColumnVector<T> &>(res_.getData()).getData();
|
|
|
|
|
Offsets_t & res_offsets = res_.getOffsets();
|
|
|
|
|
|
|
|
|
|
res_data.reserve(data->size() / col_size * replicate_offsets.back());
|
|
|
|
|
res_offsets.reserve(replicate_offsets.back());
|
|
|
|
|
|
|
|
|
|
Offset_t prev_replicate_offset = 0;
|
|
|
|
|
Offset_t prev_data_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 value_size = cur_offsets[i] - prev_data_offset;
|
|
|
|
|
|
|
|
|
|
for (size_t j = 0; j < size_to_replicate; ++j)
|
|
|
|
|
{
|
|
|
|
|
current_new_offset += value_size;
|
|
|
|
|
res_offsets.push_back(current_new_offset);
|
|
|
|
|
|
|
|
|
|
res_data.resize(res_data.size() + value_size);
|
|
|
|
|
memcpy(&res_data[res_data.size() - value_size], &cur_data[prev_data_offset], value_size * sizeof(T));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
prev_replicate_offset = replicate_offsets[i];
|
|
|
|
|
prev_data_offset = cur_offsets[i];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return res;
|
|
|
|
|
}
|
2013-07-28 00:35:29 +00:00
|
|
|
|
|
|
|
|
|
/// Размножить значения, если вложенный столбец - ColumnString. Код слишком сложный.
|
|
|
|
|
ColumnPtr replicateString(const Offsets_t & replicate_offsets) const
|
|
|
|
|
{
|
|
|
|
|
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);
|
|
|
|
|
|
|
|
|
|
ColumnPtr res = cloneEmpty();
|
|
|
|
|
ColumnArray & res_ = dynamic_cast<ColumnArray &>(*res);
|
|
|
|
|
|
|
|
|
|
const ColumnString & cur_string = dynamic_cast<const ColumnString &>(*data);
|
|
|
|
|
const ColumnString::Chars_t & cur_chars = cur_string.getChars();
|
|
|
|
|
const Offsets_t & cur_string_offsets = cur_string.getOffsets();
|
|
|
|
|
const Offsets_t & cur_offsets = getOffsets();
|
|
|
|
|
|
|
|
|
|
ColumnString::Chars_t & res_chars = dynamic_cast<ColumnString &>(res_.getData()).getChars();
|
|
|
|
|
Offsets_t & res_string_offsets = dynamic_cast<ColumnString &>(res_.getData()).getOffsets();
|
|
|
|
|
Offsets_t & res_offsets = res_.getOffsets();
|
|
|
|
|
|
|
|
|
|
res_chars.reserve(cur_chars.size() / col_size * replicate_offsets.back());
|
|
|
|
|
res_string_offsets.reserve(cur_string_offsets.size() / col_size * replicate_offsets.back());
|
|
|
|
|
res_offsets.reserve(replicate_offsets.back());
|
|
|
|
|
|
|
|
|
|
Offset_t prev_replicate_offset = 0;
|
|
|
|
|
|
|
|
|
|
Offset_t prev_cur_offset = 0;
|
|
|
|
|
Offset_t prev_cur_string_offset = 0;
|
|
|
|
|
|
|
|
|
|
Offset_t current_res_offset = 0;
|
|
|
|
|
Offset_t current_res_string_offset = 0;
|
|
|
|
|
|
|
|
|
|
for (size_t i = 0; i < col_size; ++i)
|
|
|
|
|
{
|
|
|
|
|
/// Насколько размножить массив.
|
|
|
|
|
size_t size_to_replicate = replicate_offsets[i] - prev_replicate_offset;
|
|
|
|
|
/// Количество строк в массиве.
|
|
|
|
|
size_t value_size = cur_offsets[i] - prev_cur_offset;
|
|
|
|
|
|
|
|
|
|
size_t sum_chars_size = 0;
|
|
|
|
|
|
|
|
|
|
for (size_t j = 0; j < size_to_replicate; ++j)
|
|
|
|
|
{
|
|
|
|
|
current_res_offset += value_size;
|
|
|
|
|
res_offsets.push_back(current_res_offset);
|
|
|
|
|
|
2013-07-31 12:44:25 +00:00
|
|
|
|
sum_chars_size = 0;
|
2013-07-28 00:35:29 +00:00
|
|
|
|
|
|
|
|
|
size_t prev_cur_string_offset_local = prev_cur_string_offset;
|
|
|
|
|
for (size_t k = 0; k < value_size; ++k)
|
|
|
|
|
{
|
|
|
|
|
/// Размер одной строки.
|
|
|
|
|
size_t chars_size = cur_string_offsets[k + prev_cur_offset] - prev_cur_string_offset_local;
|
|
|
|
|
|
|
|
|
|
current_res_string_offset += chars_size;
|
|
|
|
|
res_string_offsets.push_back(current_res_string_offset);
|
|
|
|
|
|
|
|
|
|
/// Копирование символов одной строки.
|
|
|
|
|
res_chars.resize(res_chars.size() + chars_size);
|
|
|
|
|
memcpy(&res_chars[res_chars.size() - chars_size], &cur_chars[prev_cur_string_offset_local], chars_size);
|
|
|
|
|
|
|
|
|
|
sum_chars_size += chars_size;
|
|
|
|
|
prev_cur_string_offset_local += chars_size;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
prev_replicate_offset = replicate_offsets[i];
|
|
|
|
|
prev_cur_offset = cur_offsets[i];
|
|
|
|
|
prev_cur_string_offset += sum_chars_size;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return res;
|
|
|
|
|
}
|
2010-05-13 16:17:10 +00:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|