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-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
|
|
|
|
|
{
|
|
|
|
|
size_t offset = n == 0 ? 0 : offsets[n - 1];
|
|
|
|
|
size_t size = offsets[n] - offset;
|
|
|
|
|
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-21 03:41:37 +00:00
|
|
|
|
size_t nested_offset = start == 0 ? 0 : offsets[start - 1];
|
|
|
|
|
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();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** Более эффективные методы манипуляции */
|
|
|
|
|
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;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#endif
|