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>
|
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
|
|
|
|
|
{
|
|
|
|
|
throw Exception("Method getDataAt is not supported for " + getName(), ErrorCodes::NOT_IMPLEMENTED);
|
|
|
|
|
}
|
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)
|
|
|
|
|
if (filt[i])
|
|
|
|
|
memset(&nested_filt[offsetAt(i)], 1, 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-05-03 05:23:14 +00:00
|
|
|
|
ColumnPtr replicate(const Offsets_t & offsets) const
|
2012-08-27 05:13:14 +00:00
|
|
|
|
{
|
|
|
|
|
throw Exception("Replication of column Array is not implemented.", ErrorCodes::NOT_IMPLEMENTED);
|
|
|
|
|
}
|
|
|
|
|
|
2013-05-03 05:23:14 +00:00
|
|
|
|
ColumnPtr permute(const Permutation & perm) 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
|
|
|
|
if (size != perm.size())
|
|
|
|
|
throw Exception("Size of permutation 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-09-04 00:22:19 +00:00
|
|
|
|
|
2012-08-26 04:40:29 +00:00
|
|
|
|
Permutation nested_perm(getOffsets().back());
|
2011-09-04 00:22:19 +00:00
|
|
|
|
|
2013-05-03 05:23:14 +00:00
|
|
|
|
ColumnArray * res_ = new ColumnArray(data);
|
|
|
|
|
ColumnPtr res = res_;
|
|
|
|
|
|
|
|
|
|
Offsets_t & res_offsets = res_->getOffsets();
|
|
|
|
|
res_offsets.resize(size);
|
2011-09-04 00:22:19 +00:00
|
|
|
|
size_t current_offset = 0;
|
|
|
|
|
|
|
|
|
|
for (size_t i = 0; i < size; ++i)
|
|
|
|
|
{
|
|
|
|
|
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-05-03 05:23:14 +00:00
|
|
|
|
res_->data = data->permute(nested_perm);
|
|
|
|
|
|
|
|
|
|
return res;
|
2011-09-04 00:22:19 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int compareAt(size_t n, size_t m, const IColumn & rhs_) const
|
|
|
|
|
{
|
|
|
|
|
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)
|
|
|
|
|
if (int res = data->compareAt(offsetAt(n) + i, rhs.offsetAt(m) + i, *rhs.data))
|
|
|
|
|
return res;
|
|
|
|
|
|
|
|
|
|
return lhs_size < rhs_size
|
|
|
|
|
? -1
|
|
|
|
|
: (lhs_size == rhs_size
|
|
|
|
|
? 0
|
|
|
|
|
: 1);
|
|
|
|
|
}
|
|
|
|
|
|
2011-09-26 11:05:38 +00:00
|
|
|
|
struct less
|
|
|
|
|
{
|
|
|
|
|
const ColumnArray & parent;
|
|
|
|
|
const Permutation & nested_perm;
|
|
|
|
|
|
|
|
|
|
less(const ColumnArray & parent_, const Permutation & nested_perm_) : parent(parent_), nested_perm(nested_perm_) {}
|
|
|
|
|
bool operator()(size_t lhs, size_t rhs) const
|
|
|
|
|
{
|
|
|
|
|
size_t lhs_size = parent.sizeAt(lhs);
|
|
|
|
|
size_t rhs_size = parent.sizeAt(rhs);
|
|
|
|
|
size_t min_size = std::min(lhs_size, rhs_size);
|
|
|
|
|
for (size_t i = 0; i < min_size; ++i)
|
|
|
|
|
{
|
|
|
|
|
if (nested_perm[parent.offsetAt(lhs) + i] < nested_perm[parent.offsetAt(rhs) + i])
|
|
|
|
|
return true;
|
|
|
|
|
else if (nested_perm[parent.offsetAt(lhs) + i] > nested_perm[parent.offsetAt(rhs) + i])
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
return lhs_size < rhs_size;
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2013-01-21 06:43:38 +00:00
|
|
|
|
Permutation getPermutation() const
|
2011-09-26 11:05:38 +00:00
|
|
|
|
{
|
2013-01-21 06:43:38 +00:00
|
|
|
|
Permutation nested_perm = data->getPermutation();
|
2011-09-26 11:05:38 +00:00
|
|
|
|
size_t s = size();
|
2013-01-21 06:43:38 +00:00
|
|
|
|
Permutation res(s);
|
2011-09-26 11:05:38 +00:00
|
|
|
|
for (size_t i = 0; i < s; ++i)
|
|
|
|
|
res[i] = i;
|
|
|
|
|
|
|
|
|
|
std::sort(res.begin(), res.end(), less(*this, nested_perm));
|
2013-01-21 06:43:38 +00:00
|
|
|
|
return res;
|
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
|
|
|
|
}
|
|
|
|
|
|
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-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]); }
|
2010-05-13 16:17:10 +00:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|