2010-05-13 16:17:10 +00:00
|
|
|
|
#ifndef DBMS_CORE_COLUMN_ARRAY_H
|
|
|
|
|
#define DBMS_CORE_COLUMN_ARRAY_H
|
|
|
|
|
|
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>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
namespace DB
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
using Poco::SharedPtr;
|
|
|
|
|
|
|
|
|
|
/** Cтолбeц значений типа массив.
|
|
|
|
|
* В памяти он представлен, как один столбец вложенного типа, размер которого равен сумме размеров всех массивов,
|
|
|
|
|
* и как массив смещений в нём, который позволяет достать каждый элемент.
|
|
|
|
|
*/
|
|
|
|
|
class ColumnArray : public IColumn
|
|
|
|
|
{
|
|
|
|
|
public:
|
|
|
|
|
/** По индексу i находится смещение до начала i + 1 -го элемента. */
|
|
|
|
|
typedef std::vector<size_t> Offsets_t;
|
|
|
|
|
|
|
|
|
|
/** Создать пустой столбец массивов, с типом значений, как в столбце nested_column */
|
2011-08-09 19:19:00 +00:00
|
|
|
|
ColumnArray(ColumnPtr nested_column)
|
2010-05-13 16:17:10 +00:00
|
|
|
|
: data(nested_column)
|
|
|
|
|
{
|
|
|
|
|
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
|
|
|
|
|
{
|
|
|
|
|
return offsets.size();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void cut(size_t start, size_t length)
|
|
|
|
|
{
|
2011-08-21 03:41:37 +00:00
|
|
|
|
if (length == 0 || start + length > offsets.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);
|
2011-08-21 03:41:37 +00:00
|
|
|
|
size_t nested_length = offsets[start + length - 1] - nested_offset;
|
|
|
|
|
|
|
|
|
|
data->cut(nested_offset, nested_length);
|
|
|
|
|
|
2010-05-13 16:17:10 +00:00
|
|
|
|
if (start == 0)
|
|
|
|
|
offsets.resize(length);
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
Offsets_t tmp(length);
|
2011-08-21 03:41:37 +00:00
|
|
|
|
|
|
|
|
|
for (size_t i = 0; i < length; ++i)
|
|
|
|
|
tmp[i] = offsets[start + i] - nested_offset;
|
|
|
|
|
|
2010-05-13 16:17:10 +00:00
|
|
|
|
tmp.swap(offsets);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2010-05-20 19:29:04 +00:00
|
|
|
|
void insert(const Field & x)
|
|
|
|
|
{
|
|
|
|
|
Array & array = boost::get<Array &>(x);
|
|
|
|
|
size_t size = array.size();
|
|
|
|
|
for (size_t i = 0; i < size; ++i)
|
|
|
|
|
data->insert(array[i]);
|
|
|
|
|
offsets.push_back((offsets.size() == 0 ? 0 : offsets.back()) + size);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void insertDefault()
|
|
|
|
|
{
|
|
|
|
|
data->insertDefault();
|
|
|
|
|
offsets.push_back(offsets.size() == 0 ? 1 : (offsets.back() + 1));
|
|
|
|
|
}
|
|
|
|
|
|
2010-05-13 16:17:10 +00:00
|
|
|
|
void clear()
|
|
|
|
|
{
|
|
|
|
|
data->clear();
|
|
|
|
|
offsets.clear();
|
|
|
|
|
}
|
|
|
|
|
|
2011-08-22 20:24:45 +00:00
|
|
|
|
void filter(const Filter & filt)
|
|
|
|
|
{
|
|
|
|
|
size_t size = offsets.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;
|
|
|
|
|
|
|
|
|
|
/// Не слишком оптимально. Можно сделать специализацию для массивов известных типов.
|
|
|
|
|
Filter nested_filt(offsets.back());
|
|
|
|
|
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);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
tmp.swap(offsets);
|
|
|
|
|
}
|
|
|
|
|
|
2011-09-04 00:22:19 +00:00
|
|
|
|
void permute(const Permutation & perm)
|
|
|
|
|
{
|
|
|
|
|
size_t size = offsets.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;
|
|
|
|
|
|
|
|
|
|
Permutation nested_perm(offsets.back());
|
|
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
tmp_offsets.swap(offsets);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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
|
|
|
|
{
|
|
|
|
|
return data->byteSize() + offsets.size() * sizeof(offsets[0]);
|
|
|
|
|
}
|
|
|
|
|
|
2010-05-13 16:17:10 +00:00
|
|
|
|
/** Более эффективные методы манипуляции */
|
|
|
|
|
IColumn & getData()
|
|
|
|
|
{
|
|
|
|
|
return *data;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const IColumn & getData() const
|
|
|
|
|
{
|
|
|
|
|
return *data;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Offsets_t & getOffsets()
|
|
|
|
|
{
|
|
|
|
|
return offsets;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const Offsets_t & getOffsets() const
|
|
|
|
|
{
|
|
|
|
|
return offsets;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected:
|
2011-08-09 19:19:00 +00:00
|
|
|
|
ColumnPtr data;
|
2010-05-13 16:17:10 +00:00
|
|
|
|
Offsets_t offsets;
|
2011-08-22 20:24:45 +00:00
|
|
|
|
|
|
|
|
|
inline size_t offsetAt(size_t i) const { return i == 0 ? 0 : offsets[i - 1]; }
|
|
|
|
|
inline size_t sizeAt(size_t i) const { return i == 0 ? offsets[0] : (offsets[i] - offsets[i - 1]); }
|
2010-05-13 16:17:10 +00:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#endif
|