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 */
|
2011-08-09 19:19:00 +00:00
|
|
|
|
ColumnArray(ColumnPtr nested_column)
|
2012-08-26 04:40:29 +00:00
|
|
|
|
: data(nested_column), offsets(new ColumnOffsets_t)
|
2010-05-13 16:17:10 +00:00
|
|
|
|
{
|
|
|
|
|
data->clear();
|
|
|
|
|
}
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
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);
|
|
|
|
|
}
|
|
|
|
|
|
2010-05-13 16:17:10 +00:00
|
|
|
|
void cut(size_t start, size_t length)
|
|
|
|
|
{
|
2012-09-20 18:40:58 +00:00
|
|
|
|
if (length == 0)
|
|
|
|
|
{
|
|
|
|
|
data->cut(0, 0);
|
|
|
|
|
Offsets_t().swap(getOffsets());
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
|
data->cut(nested_offset, nested_length);
|
|
|
|
|
|
2010-05-13 16:17:10 +00:00
|
|
|
|
if (start == 0)
|
2012-08-26 04:40:29 +00:00
|
|
|
|
getOffsets().resize(length);
|
2010-05-13 16:17:10 +00:00
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
Offsets_t tmp(length);
|
2011-08-21 03:41:37 +00:00
|
|
|
|
|
|
|
|
|
for (size_t i = 0; i < length; ++i)
|
2012-08-26 04:40:29 +00:00
|
|
|
|
tmp[i] = getOffsets()[start + i] - nested_offset;
|
2011-08-21 03:41:37 +00:00
|
|
|
|
|
2012-08-26 04:40:29 +00:00
|
|
|
|
tmp.swap(getOffsets());
|
2010-05-13 16:17:10 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2010-05-20 19:29:04 +00:00
|
|
|
|
void insert(const Field & x)
|
|
|
|
|
{
|
2013-01-05 20:03:19 +00:00
|
|
|
|
const Array & array = 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
|
|
|
|
}
|
|
|
|
|
|
2010-05-13 16:17:10 +00:00
|
|
|
|
void clear()
|
|
|
|
|
{
|
|
|
|
|
data->clear();
|
2012-08-26 04:40:29 +00:00
|
|
|
|
getOffsets().clear();
|
2010-05-13 16:17:10 +00:00
|
|
|
|
}
|
|
|
|
|
|
2011-08-22 20:24:45 +00:00
|
|
|
|
void filter(const Filter & filt)
|
|
|
|
|
{
|
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)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
/// Не слишком оптимально. Можно сделать специализацию для массивов известных типов.
|
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));
|
|
|
|
|
data->filter(nested_filt);
|
|
|
|
|
|
|
|
|
|
Offsets_t tmp;
|
|
|
|
|
tmp.reserve(size);
|
|
|
|
|
|
|
|
|
|
size_t current_offset = 0;
|
|
|
|
|
for (size_t i = 0; i < size; ++i)
|
|
|
|
|
{
|
|
|
|
|
if (filt[i])
|
|
|
|
|
{
|
|
|
|
|
current_offset += sizeAt(i);
|
|
|
|
|
tmp.push_back(current_offset);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2012-08-26 04:40:29 +00:00
|
|
|
|
tmp.swap(getOffsets());
|
2011-08-22 20:24:45 +00:00
|
|
|
|
}
|
|
|
|
|
|
2012-08-27 05:13:14 +00:00
|
|
|
|
void replicate(const Offsets_t & offsets)
|
|
|
|
|
{
|
|
|
|
|
throw Exception("Replication of column Array is not implemented.", ErrorCodes::NOT_IMPLEMENTED);
|
|
|
|
|
}
|
|
|
|
|
|
2011-09-04 00:22:19 +00:00
|
|
|
|
void permute(const Permutation & perm)
|
|
|
|
|
{
|
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)
|
|
|
|
|
return;
|
|
|
|
|
|
2012-08-26 04:40:29 +00:00
|
|
|
|
Permutation nested_perm(getOffsets().back());
|
2011-09-04 00:22:19 +00:00
|
|
|
|
|
|
|
|
|
Offsets_t tmp_offsets(size);
|
|
|
|
|
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]);
|
|
|
|
|
tmp_offsets[i] = current_offset;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
data->permute(nested_perm);
|
2012-08-26 04:40:29 +00:00
|
|
|
|
tmp_offsets.swap(getOffsets());
|
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;
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
Permutation getPermutation() const
|
|
|
|
|
{
|
|
|
|
|
Permutation nested_perm = data->getPermutation();
|
|
|
|
|
size_t s = size();
|
|
|
|
|
Permutation res(s);
|
|
|
|
|
for (size_t i = 0; i < s; ++i)
|
|
|
|
|
res[i] = i;
|
|
|
|
|
|
|
|
|
|
std::sort(res.begin(), res.end(), less(*this, nested_perm));
|
|
|
|
|
return res;
|
|
|
|
|
}
|
|
|
|
|
|
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; }
|
|
|
|
|
|
2010-05-13 16:17:10 +00:00
|
|
|
|
protected:
|
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
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|