2017-03-09 03:34:09 +00:00
|
|
|
#include <string.h> // memcpy
|
|
|
|
|
2017-04-01 09:19:00 +00:00
|
|
|
#include <Columns/ColumnArray.h>
|
|
|
|
#include <Columns/ColumnsNumber.h>
|
|
|
|
#include <Columns/ColumnString.h>
|
2017-04-18 03:03:39 +00:00
|
|
|
#include <Columns/ColumnTuple.h>
|
|
|
|
#include <Columns/ColumnNullable.h>
|
2017-07-21 06:35:58 +00:00
|
|
|
#include <Columns/ColumnConst.h>
|
2017-04-01 09:19:00 +00:00
|
|
|
#include <Columns/ColumnsCommon.h>
|
|
|
|
|
2017-07-06 13:54:55 +00:00
|
|
|
#include <DataStreams/ColumnGathererStream.h>
|
|
|
|
|
2017-04-01 09:19:00 +00:00
|
|
|
#include <Common/Exception.h>
|
|
|
|
#include <Common/Arena.h>
|
|
|
|
#include <Common/SipHash.h>
|
2017-07-13 20:58:19 +00:00
|
|
|
#include <Common/typeid_cast.h>
|
2017-03-09 03:34:09 +00:00
|
|
|
|
|
|
|
|
2015-07-06 19:24:51 +00:00
|
|
|
|
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
|
2017-03-09 03:34:09 +00:00
|
|
|
namespace ErrorCodes
|
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
extern const int ILLEGAL_COLUMN;
|
|
|
|
extern const int NOT_IMPLEMENTED;
|
|
|
|
extern const int BAD_ARGUMENTS;
|
|
|
|
extern const int PARAMETER_OUT_OF_BOUND;
|
|
|
|
extern const int SIZES_OF_COLUMNS_DOESNT_MATCH;
|
2017-07-27 23:23:13 +00:00
|
|
|
extern const int LOGICAL_ERROR;
|
2017-03-09 03:34:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-03-20 14:17:09 +00:00
|
|
|
ColumnArray::ColumnArray(MutableColumnPtr && nested_column, MutableColumnPtr && offsets_column)
|
|
|
|
: data(std::move(nested_column)), offsets(std::move(offsets_column))
|
2017-03-09 03:34:09 +00:00
|
|
|
{
|
2018-03-21 19:39:14 +00:00
|
|
|
if (!typeid_cast<const ColumnOffsets *>(offsets.get()))
|
2017-12-13 19:14:12 +00:00
|
|
|
throw Exception("offsets_column must be a ColumnUInt64", ErrorCodes::ILLEGAL_COLUMN);
|
2017-03-09 03:34:09 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
/** NOTE
|
2017-12-09 17:40:31 +00:00
|
|
|
* Arrays with constant value are possible and used in implementation of higher order functions (see FunctionReplicate).
|
2017-04-01 07:20:54 +00:00
|
|
|
* But in most cases, arrays with constant value are unexpected and code will work wrong. Use with caution.
|
|
|
|
*/
|
2017-03-09 03:34:09 +00:00
|
|
|
}
|
|
|
|
|
2018-03-20 14:17:09 +00:00
|
|
|
ColumnArray::ColumnArray(MutableColumnPtr && nested_column)
|
|
|
|
: data(std::move(nested_column))
|
2017-12-15 19:46:24 +00:00
|
|
|
{
|
|
|
|
if (!data->empty())
|
|
|
|
throw Exception("Not empty data passed to ColumnArray, but no offsets passed", ErrorCodes::ILLEGAL_COLUMN);
|
|
|
|
|
2017-12-15 21:32:25 +00:00
|
|
|
offsets = ColumnOffsets::create();
|
2017-12-15 19:46:24 +00:00
|
|
|
}
|
|
|
|
|
2017-03-09 03:34:09 +00:00
|
|
|
|
2017-12-07 22:11:51 +00:00
|
|
|
std::string ColumnArray::getName() const { return "Array(" + getData().getName() + ")"; }
|
2017-03-09 03:34:09 +00:00
|
|
|
|
|
|
|
|
2017-12-14 01:43:19 +00:00
|
|
|
MutableColumnPtr ColumnArray::cloneResized(size_t to_size) const
|
2017-03-09 03:34:09 +00:00
|
|
|
{
|
2017-12-15 19:46:24 +00:00
|
|
|
auto res = ColumnArray::create(getData().cloneEmpty());
|
2017-03-09 03:34:09 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
if (to_size == 0)
|
2017-12-15 19:17:15 +00:00
|
|
|
return std::move(res);
|
2017-03-09 03:34:09 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
size_t from_size = size();
|
2017-03-09 03:34:09 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
if (to_size <= from_size)
|
|
|
|
{
|
|
|
|
/// Just cut column.
|
2017-03-09 03:34:09 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
res->getOffsets().assign(getOffsets().begin(), getOffsets().begin() + to_size);
|
|
|
|
res->getData().insertRangeFrom(getData(), 0, getOffsets()[to_size - 1]);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/// Copy column and append empty arrays for extra elements.
|
2017-03-09 03:34:09 +00:00
|
|
|
|
2017-12-15 21:32:25 +00:00
|
|
|
Offset offset = 0;
|
2017-04-01 07:20:54 +00:00
|
|
|
if (from_size > 0)
|
|
|
|
{
|
|
|
|
res->getOffsets().assign(getOffsets().begin(), getOffsets().end());
|
2017-12-15 19:17:15 +00:00
|
|
|
res->getData().insertRangeFrom(getData(), 0, getData().size());
|
2017-04-01 07:20:54 +00:00
|
|
|
offset = getOffsets().back();
|
|
|
|
}
|
2017-03-09 03:34:09 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
res->getOffsets().resize(to_size);
|
|
|
|
for (size_t i = from_size; i < to_size; ++i)
|
|
|
|
res->getOffsets()[i] = offset;
|
|
|
|
}
|
2017-03-09 03:34:09 +00:00
|
|
|
|
2017-12-15 19:17:15 +00:00
|
|
|
return std::move(res);
|
2017-03-09 03:34:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
size_t ColumnArray::size() const
|
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
return getOffsets().size();
|
2017-03-09 03:34:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Field ColumnArray::operator[](size_t n) const
|
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
size_t offset = offsetAt(n);
|
|
|
|
size_t size = sizeAt(n);
|
|
|
|
Array res(size);
|
2017-03-09 03:34:09 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
for (size_t i = 0; i < size; ++i)
|
|
|
|
res[i] = getData()[offset + i];
|
2017-03-09 03:34:09 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
return res;
|
2017-03-09 03:34:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void ColumnArray::get(size_t n, Field & res) const
|
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
size_t offset = offsetAt(n);
|
|
|
|
size_t size = sizeAt(n);
|
|
|
|
res = Array(size);
|
|
|
|
Array & res_arr = DB::get<Array &>(res);
|
2017-03-09 03:34:09 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
for (size_t i = 0; i < size; ++i)
|
|
|
|
getData().get(offset + i, res_arr[i]);
|
2017-03-09 03:34:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
StringRef ColumnArray::getDataAt(size_t n) const
|
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
/** Returns the range of memory that covers all elements of the array.
|
|
|
|
* Works for arrays of fixed length values.
|
|
|
|
* For arrays of strings and arrays of arrays, the resulting chunk of memory may not be one-to-one correspondence with the elements,
|
|
|
|
* since it contains only the data laid in succession, but not the offsets.
|
|
|
|
*/
|
2017-03-09 03:34:09 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
size_t array_size = sizeAt(n);
|
|
|
|
if (array_size == 0)
|
|
|
|
return StringRef();
|
2017-03-09 03:34:09 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
size_t offset_of_first_elem = offsetAt(n);
|
|
|
|
StringRef first = getData().getDataAtWithTerminatingZero(offset_of_first_elem);
|
2017-03-09 03:34:09 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
size_t offset_of_last_elem = getOffsets()[n] - 1;
|
|
|
|
StringRef last = getData().getDataAtWithTerminatingZero(offset_of_last_elem);
|
2017-03-09 03:34:09 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
return StringRef(first.data, last.data + last.size - first.data);
|
2017-03-09 03:34:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void ColumnArray::insertData(const char * pos, size_t length)
|
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
/** Similarly - only for arrays of fixed length values.
|
|
|
|
*/
|
2017-12-15 19:46:24 +00:00
|
|
|
IColumn * data_ = &getData();
|
2017-12-09 10:14:45 +00:00
|
|
|
if (!data_->isFixedAndContiguous())
|
2017-04-01 07:20:54 +00:00
|
|
|
throw Exception("Method insertData is not supported for " + getName(), ErrorCodes::NOT_IMPLEMENTED);
|
2017-03-09 03:34:09 +00:00
|
|
|
|
2017-12-09 10:14:45 +00:00
|
|
|
size_t field_size = data_->sizeOfValueIfFixed();
|
2017-03-09 03:34:09 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
const char * end = pos + length;
|
|
|
|
size_t elems = 0;
|
|
|
|
for (; pos + field_size <= end; pos += field_size, ++elems)
|
|
|
|
data_->insertData(pos, field_size);
|
2017-03-09 03:34:09 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
if (pos != end)
|
|
|
|
throw Exception("Incorrect length argument for method ColumnArray::insertData", ErrorCodes::BAD_ARGUMENTS);
|
2017-03-09 03:34:09 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
getOffsets().push_back((getOffsets().size() == 0 ? 0 : getOffsets().back()) + elems);
|
2017-03-09 03:34:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
StringRef ColumnArray::serializeValueIntoArena(size_t n, Arena & arena, char const *& begin) const
|
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
size_t array_size = sizeAt(n);
|
|
|
|
size_t offset = offsetAt(n);
|
2017-03-09 03:34:09 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
char * pos = arena.allocContinue(sizeof(array_size), begin);
|
|
|
|
memcpy(pos, &array_size, sizeof(array_size));
|
2017-03-09 03:34:09 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
size_t values_size = 0;
|
|
|
|
for (size_t i = 0; i < array_size; ++i)
|
|
|
|
values_size += getData().serializeValueIntoArena(offset + i, arena, begin).size;
|
2017-03-09 03:34:09 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
return StringRef(begin, sizeof(array_size) + values_size);
|
2017-03-09 03:34:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
const char * ColumnArray::deserializeAndInsertFromArena(const char * pos)
|
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
size_t array_size = *reinterpret_cast<const size_t *>(pos);
|
|
|
|
pos += sizeof(array_size);
|
2017-03-09 03:34:09 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
for (size_t i = 0; i < array_size; ++i)
|
|
|
|
pos = getData().deserializeAndInsertFromArena(pos);
|
2017-03-09 03:34:09 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
getOffsets().push_back((getOffsets().size() == 0 ? 0 : getOffsets().back()) + array_size);
|
|
|
|
return pos;
|
2017-03-09 03:34:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void ColumnArray::updateHashWithValue(size_t n, SipHash & hash) const
|
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
size_t array_size = sizeAt(n);
|
|
|
|
size_t offset = offsetAt(n);
|
2017-03-09 03:34:09 +00:00
|
|
|
|
2018-03-03 15:36:20 +00:00
|
|
|
hash.update(array_size);
|
2017-04-01 07:20:54 +00:00
|
|
|
for (size_t i = 0; i < array_size; ++i)
|
|
|
|
getData().updateHashWithValue(offset + i, hash);
|
2017-03-09 03:34:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void ColumnArray::insert(const Field & x)
|
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
const Array & array = DB::get<const Array &>(x);
|
|
|
|
size_t size = array.size();
|
|
|
|
for (size_t i = 0; i < size; ++i)
|
|
|
|
getData().insert(array[i]);
|
|
|
|
getOffsets().push_back((getOffsets().size() == 0 ? 0 : getOffsets().back()) + size);
|
2017-03-09 03:34:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void ColumnArray::insertFrom(const IColumn & src_, size_t n)
|
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
const ColumnArray & src = static_cast<const ColumnArray &>(src_);
|
|
|
|
size_t size = src.sizeAt(n);
|
|
|
|
size_t offset = src.offsetAt(n);
|
2017-03-09 03:34:09 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
getData().insertRangeFrom(src.getData(), offset, size);
|
|
|
|
getOffsets().push_back((getOffsets().size() == 0 ? 0 : getOffsets().back()) + size);
|
2017-03-09 03:34:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void ColumnArray::insertDefault()
|
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
getOffsets().push_back(getOffsets().size() == 0 ? 0 : getOffsets().back());
|
2017-03-09 03:34:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void ColumnArray::popBack(size_t n)
|
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
auto & offsets = getOffsets();
|
|
|
|
size_t nested_n = offsets.back() - offsetAt(offsets.size() - n);
|
|
|
|
if (nested_n)
|
|
|
|
getData().popBack(nested_n);
|
|
|
|
offsets.resize_assume_reserved(offsets.size() - n);
|
2017-03-09 03:34:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int ColumnArray::compareAt(size_t n, size_t m, const IColumn & rhs_, int nan_direction_hint) const
|
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
const ColumnArray & rhs = static_cast<const ColumnArray &>(rhs_);
|
2017-03-09 03:34:09 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
/// Suboptimal
|
|
|
|
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 = getData().compareAt(offsetAt(n) + i, rhs.offsetAt(m) + i, *rhs.data.get(), nan_direction_hint))
|
|
|
|
return res;
|
2017-03-09 03:34:09 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
return lhs_size < rhs_size
|
|
|
|
? -1
|
|
|
|
: (lhs_size == rhs_size
|
|
|
|
? 0
|
|
|
|
: 1);
|
2017-03-09 03:34:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
namespace
|
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
template <bool positive>
|
|
|
|
struct less
|
|
|
|
{
|
|
|
|
const ColumnArray & parent;
|
|
|
|
int nan_direction_hint;
|
2017-03-09 03:34:09 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
less(const ColumnArray & parent_, int nan_direction_hint_)
|
|
|
|
: parent(parent_), nan_direction_hint(nan_direction_hint_) {}
|
2017-03-09 03:34:09 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
bool operator()(size_t lhs, size_t rhs) const
|
|
|
|
{
|
|
|
|
if (positive)
|
|
|
|
return parent.compareAt(lhs, rhs, parent, nan_direction_hint) < 0;
|
|
|
|
else
|
|
|
|
return parent.compareAt(lhs, rhs, parent, nan_direction_hint) > 0;
|
|
|
|
}
|
|
|
|
};
|
2017-03-09 03:34:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void ColumnArray::reserve(size_t n)
|
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
getOffsets().reserve(n);
|
|
|
|
getData().reserve(n); /// The average size of arrays is not taken into account here. Or it is considered to be no more than 1.
|
2017-03-09 03:34:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
size_t ColumnArray::byteSize() const
|
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
return getData().byteSize() + getOffsets().size() * sizeof(getOffsets()[0]);
|
2017-03-09 03:34:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-07-13 16:49:09 +00:00
|
|
|
size_t ColumnArray::allocatedBytes() const
|
2017-03-09 03:34:09 +00:00
|
|
|
{
|
2017-07-13 16:53:34 +00:00
|
|
|
return getData().allocatedBytes() + getOffsets().allocated_bytes();
|
2017-03-09 03:34:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool ColumnArray::hasEqualOffsets(const ColumnArray & other) const
|
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
if (offsets == other.offsets)
|
|
|
|
return true;
|
2017-03-09 03:34:09 +00:00
|
|
|
|
2017-12-15 21:32:25 +00:00
|
|
|
const Offsets & offsets1 = getOffsets();
|
|
|
|
const Offsets & offsets2 = other.getOffsets();
|
2017-04-01 07:20:54 +00:00
|
|
|
return offsets1.size() == offsets2.size() && 0 == memcmp(&offsets1[0], &offsets2[0], sizeof(offsets1[0]) * offsets1.size());
|
2017-03-09 03:34:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-03-20 14:17:09 +00:00
|
|
|
ColumnPtr ColumnArray::convertToFullColumnIfConst() const
|
2017-03-09 03:34:09 +00:00
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
ColumnPtr new_data;
|
2017-03-09 03:34:09 +00:00
|
|
|
|
2017-12-15 19:46:24 +00:00
|
|
|
if (ColumnPtr full_column = getData().convertToFullColumnIfConst())
|
2017-04-01 07:20:54 +00:00
|
|
|
new_data = full_column;
|
|
|
|
else
|
|
|
|
new_data = data;
|
2017-03-09 03:34:09 +00:00
|
|
|
|
2017-12-14 01:43:19 +00:00
|
|
|
return ColumnArray::create(new_data, offsets);
|
2017-03-09 03:34:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void ColumnArray::getExtremes(Field & min, Field & max) const
|
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
min = Array();
|
|
|
|
max = Array();
|
2017-09-07 13:22:25 +00:00
|
|
|
|
|
|
|
size_t col_size = size();
|
|
|
|
|
|
|
|
if (col_size == 0)
|
|
|
|
return;
|
|
|
|
|
|
|
|
size_t min_idx = 0;
|
|
|
|
size_t max_idx = 0;
|
|
|
|
|
|
|
|
for (size_t i = 1; i < col_size; ++i)
|
|
|
|
{
|
|
|
|
if (compareAt(i, min_idx, *this, /* nan_direction_hint = */ 1) < 0)
|
|
|
|
min_idx = i;
|
2017-09-14 11:52:22 +00:00
|
|
|
else if (compareAt(i, max_idx, *this, /* nan_direction_hint = */ -1) > 0)
|
2017-09-07 13:22:25 +00:00
|
|
|
max_idx = i;
|
|
|
|
}
|
|
|
|
|
|
|
|
get(min_idx, min);
|
|
|
|
get(max_idx, max);
|
2017-03-09 03:34:09 +00:00
|
|
|
}
|
|
|
|
|
2015-07-06 19:24:51 +00:00
|
|
|
|
2015-11-29 17:06:30 +00:00
|
|
|
void ColumnArray::insertRangeFrom(const IColumn & src, size_t start, size_t length)
|
2015-07-06 19:24:51 +00:00
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
if (length == 0)
|
|
|
|
return;
|
2015-07-06 19:24:51 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
const ColumnArray & src_concrete = static_cast<const ColumnArray &>(src);
|
2015-11-29 17:06:30 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
if (start + length > src_concrete.getOffsets().size())
|
|
|
|
throw Exception("Parameter out of bound in ColumnArray::insertRangeFrom method.",
|
|
|
|
ErrorCodes::PARAMETER_OUT_OF_BOUND);
|
2015-07-06 19:24:51 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
size_t nested_offset = src_concrete.offsetAt(start);
|
|
|
|
size_t nested_length = src_concrete.getOffsets()[start + length - 1] - nested_offset;
|
2015-07-06 19:24:51 +00:00
|
|
|
|
2017-12-17 05:21:04 +00:00
|
|
|
getData().insertRangeFrom(src_concrete.getData(), nested_offset, nested_length);
|
2015-07-06 19:24:51 +00:00
|
|
|
|
2017-12-15 21:32:25 +00:00
|
|
|
Offsets & cur_offsets = getOffsets();
|
|
|
|
const Offsets & src_offsets = src_concrete.getOffsets();
|
2015-07-06 19:24:51 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
if (start == 0 && cur_offsets.empty())
|
|
|
|
{
|
|
|
|
cur_offsets.assign(src_offsets.begin(), src_offsets.begin() + length);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
size_t old_size = cur_offsets.size();
|
|
|
|
size_t prev_max_offset = old_size ? cur_offsets.back() : 0;
|
|
|
|
cur_offsets.resize(old_size + length);
|
2015-07-06 19:24:51 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
for (size_t i = 0; i < length; ++i)
|
|
|
|
cur_offsets[old_size + i] = src_offsets[start + i] - nested_offset + prev_max_offset;
|
|
|
|
}
|
2015-07-06 19:24:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-03-20 14:17:09 +00:00
|
|
|
ColumnPtr ColumnArray::filter(const Filter & filt, ssize_t result_size_hint) const
|
2015-11-21 03:19:43 +00:00
|
|
|
{
|
2017-04-18 03:03:39 +00:00
|
|
|
if (typeid_cast<const ColumnUInt8 *>(data.get())) return filterNumber<UInt8>(filt, result_size_hint);
|
|
|
|
if (typeid_cast<const ColumnUInt16 *>(data.get())) return filterNumber<UInt16>(filt, result_size_hint);
|
|
|
|
if (typeid_cast<const ColumnUInt32 *>(data.get())) return filterNumber<UInt32>(filt, result_size_hint);
|
|
|
|
if (typeid_cast<const ColumnUInt64 *>(data.get())) return filterNumber<UInt64>(filt, result_size_hint);
|
|
|
|
if (typeid_cast<const ColumnInt8 *>(data.get())) return filterNumber<Int8>(filt, result_size_hint);
|
|
|
|
if (typeid_cast<const ColumnInt16 *>(data.get())) return filterNumber<Int16>(filt, result_size_hint);
|
|
|
|
if (typeid_cast<const ColumnInt32 *>(data.get())) return filterNumber<Int32>(filt, result_size_hint);
|
|
|
|
if (typeid_cast<const ColumnInt64 *>(data.get())) return filterNumber<Int64>(filt, result_size_hint);
|
|
|
|
if (typeid_cast<const ColumnFloat32 *>(data.get())) return filterNumber<Float32>(filt, result_size_hint);
|
|
|
|
if (typeid_cast<const ColumnFloat64 *>(data.get())) return filterNumber<Float64>(filt, result_size_hint);
|
|
|
|
if (typeid_cast<const ColumnString *>(data.get())) return filterString(filt, result_size_hint);
|
|
|
|
if (typeid_cast<const ColumnTuple *>(data.get())) return filterTuple(filt, result_size_hint);
|
|
|
|
if (typeid_cast<const ColumnNullable *>(data.get())) return filterNullable(filt, result_size_hint);
|
2017-04-01 07:20:54 +00:00
|
|
|
return filterGeneric(filt, result_size_hint);
|
2015-11-21 03:19:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T>
|
2018-03-20 14:17:09 +00:00
|
|
|
ColumnPtr ColumnArray::filterNumber(const Filter & filt, ssize_t result_size_hint) const
|
2015-11-21 03:19:43 +00:00
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
if (getOffsets().size() == 0)
|
2017-12-15 19:46:24 +00:00
|
|
|
return ColumnArray::create(data);
|
2015-11-21 03:19:43 +00:00
|
|
|
|
2017-12-15 19:46:24 +00:00
|
|
|
auto res = ColumnArray::create(data->cloneEmpty());
|
2015-11-21 03:19:43 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
auto & res_elems = static_cast<ColumnVector<T> &>(res->getData()).getData();
|
2017-12-15 21:32:25 +00:00
|
|
|
Offsets & res_offsets = res->getOffsets();
|
2015-11-21 03:19:43 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
filterArraysImpl<T>(static_cast<const ColumnVector<T> &>(*data).getData(), getOffsets(), res_elems, res_offsets, filt, result_size_hint);
|
2017-12-15 19:46:24 +00:00
|
|
|
return std::move(res);
|
2015-11-21 03:19:43 +00:00
|
|
|
}
|
|
|
|
|
2018-03-20 14:17:09 +00:00
|
|
|
ColumnPtr ColumnArray::filterString(const Filter & filt, ssize_t result_size_hint) const
|
2015-11-21 03:19:43 +00:00
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
size_t col_size = getOffsets().size();
|
|
|
|
if (col_size != filt.size())
|
|
|
|
throw Exception("Size of filter doesn't match size of column.", ErrorCodes::SIZES_OF_COLUMNS_DOESNT_MATCH);
|
2015-11-21 03:19:43 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
if (0 == col_size)
|
2017-12-15 19:46:24 +00:00
|
|
|
return ColumnArray::create(data);
|
2015-11-21 03:19:43 +00:00
|
|
|
|
2017-12-15 19:46:24 +00:00
|
|
|
auto res = ColumnArray::create(data->cloneEmpty());
|
2015-11-21 03:19:43 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
const ColumnString & src_string = typeid_cast<const ColumnString &>(*data);
|
|
|
|
const ColumnString::Chars_t & src_chars = src_string.getChars();
|
2017-12-15 21:32:25 +00:00
|
|
|
const Offsets & src_string_offsets = src_string.getOffsets();
|
|
|
|
const Offsets & src_offsets = getOffsets();
|
2015-11-21 03:19:43 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
ColumnString::Chars_t & res_chars = typeid_cast<ColumnString &>(res->getData()).getChars();
|
2017-12-15 21:32:25 +00:00
|
|
|
Offsets & res_string_offsets = typeid_cast<ColumnString &>(res->getData()).getOffsets();
|
|
|
|
Offsets & res_offsets = res->getOffsets();
|
2015-11-21 03:19:43 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
if (result_size_hint < 0) /// Other cases are not considered.
|
|
|
|
{
|
|
|
|
res_chars.reserve(src_chars.size());
|
|
|
|
res_string_offsets.reserve(src_string_offsets.size());
|
|
|
|
res_offsets.reserve(col_size);
|
|
|
|
}
|
2015-11-21 03:19:43 +00:00
|
|
|
|
2017-12-15 21:32:25 +00:00
|
|
|
Offset prev_src_offset = 0;
|
|
|
|
Offset prev_src_string_offset = 0;
|
2015-11-21 03:19:43 +00:00
|
|
|
|
2017-12-15 21:32:25 +00:00
|
|
|
Offset prev_res_offset = 0;
|
|
|
|
Offset prev_res_string_offset = 0;
|
2015-11-21 03:19:43 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
for (size_t i = 0; i < col_size; ++i)
|
|
|
|
{
|
|
|
|
/// Number of rows in the array.
|
|
|
|
size_t array_size = src_offsets[i] - prev_src_offset;
|
2015-11-21 03:19:43 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
if (filt[i])
|
|
|
|
{
|
|
|
|
/// If the array is not empty - copy content.
|
|
|
|
if (array_size)
|
|
|
|
{
|
|
|
|
size_t chars_to_copy = src_string_offsets[array_size + prev_src_offset - 1] - prev_src_string_offset;
|
|
|
|
size_t res_chars_prev_size = res_chars.size();
|
|
|
|
res_chars.resize(res_chars_prev_size + chars_to_copy);
|
|
|
|
memcpy(&res_chars[res_chars_prev_size], &src_chars[prev_src_string_offset], chars_to_copy);
|
2015-11-21 03:19:43 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
for (size_t j = 0; j < array_size; ++j)
|
|
|
|
res_string_offsets.push_back(src_string_offsets[j + prev_src_offset] + prev_res_string_offset - prev_src_string_offset);
|
2015-11-21 03:19:43 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
prev_res_string_offset = res_string_offsets.back();
|
|
|
|
}
|
2015-11-21 03:19:43 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
prev_res_offset += array_size;
|
|
|
|
res_offsets.push_back(prev_res_offset);
|
|
|
|
}
|
2015-11-21 03:19:43 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
if (array_size)
|
|
|
|
{
|
|
|
|
prev_src_offset += array_size;
|
|
|
|
prev_src_string_offset = src_string_offsets[prev_src_offset - 1];
|
|
|
|
}
|
|
|
|
}
|
2015-11-21 03:19:43 +00:00
|
|
|
|
2017-12-15 19:46:24 +00:00
|
|
|
return std::move(res);
|
2015-11-21 03:19:43 +00:00
|
|
|
}
|
|
|
|
|
2018-03-20 14:17:09 +00:00
|
|
|
ColumnPtr ColumnArray::filterGeneric(const Filter & filt, ssize_t result_size_hint) const
|
2015-07-06 19:24:51 +00:00
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
size_t size = getOffsets().size();
|
|
|
|
if (size != filt.size())
|
|
|
|
throw Exception("Size of filter doesn't match size of column.", ErrorCodes::SIZES_OF_COLUMNS_DOESNT_MATCH);
|
2015-07-06 19:24:51 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
if (size == 0)
|
2017-12-15 19:46:24 +00:00
|
|
|
return ColumnArray::create(data);
|
2015-07-06 19:24:51 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
Filter nested_filt(getOffsets().back());
|
|
|
|
for (size_t i = 0; i < size; ++i)
|
|
|
|
{
|
|
|
|
if (filt[i])
|
|
|
|
memset(&nested_filt[offsetAt(i)], 1, sizeAt(i));
|
|
|
|
else
|
|
|
|
memset(&nested_filt[offsetAt(i)], 0, sizeAt(i));
|
|
|
|
}
|
2015-07-06 19:24:51 +00:00
|
|
|
|
2018-01-14 01:25:35 +00:00
|
|
|
auto res = ColumnArray::create(data->cloneEmpty());
|
2015-12-05 07:01:18 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
ssize_t nested_result_size_hint = 0;
|
|
|
|
if (result_size_hint < 0)
|
|
|
|
nested_result_size_hint = result_size_hint;
|
|
|
|
else if (result_size_hint && result_size_hint < 1000000000 && data->size() < 1000000000) /// Avoid overflow.
|
|
|
|
nested_result_size_hint = result_size_hint * data->size() / size;
|
2015-12-05 07:01:18 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
res->data = data->filter(nested_filt, nested_result_size_hint);
|
2015-07-06 19:24:51 +00:00
|
|
|
|
2017-12-15 21:32:25 +00:00
|
|
|
Offsets & res_offsets = res->getOffsets();
|
2017-04-01 07:20:54 +00:00
|
|
|
if (result_size_hint)
|
|
|
|
res_offsets.reserve(result_size_hint > 0 ? result_size_hint : size);
|
2015-07-06 19:24:51 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
size_t current_offset = 0;
|
|
|
|
for (size_t i = 0; i < size; ++i)
|
|
|
|
{
|
|
|
|
if (filt[i])
|
|
|
|
{
|
|
|
|
current_offset += sizeAt(i);
|
|
|
|
res_offsets.push_back(current_offset);
|
|
|
|
}
|
|
|
|
}
|
2015-07-06 19:24:51 +00:00
|
|
|
|
2017-12-15 19:46:24 +00:00
|
|
|
return std::move(res);
|
2015-07-06 19:24:51 +00:00
|
|
|
}
|
|
|
|
|
2018-03-20 14:17:09 +00:00
|
|
|
ColumnPtr ColumnArray::filterNullable(const Filter & filt, ssize_t result_size_hint) const
|
2017-04-18 03:03:39 +00:00
|
|
|
{
|
|
|
|
if (getOffsets().size() == 0)
|
2017-12-15 19:46:24 +00:00
|
|
|
return ColumnArray::create(data);
|
2017-04-18 03:03:39 +00:00
|
|
|
|
|
|
|
const ColumnNullable & nullable_elems = static_cast<const ColumnNullable &>(*data);
|
|
|
|
|
2017-12-14 03:56:56 +00:00
|
|
|
auto array_of_nested = ColumnArray::create(nullable_elems.getNestedColumnPtr(), offsets);
|
2017-04-18 03:03:39 +00:00
|
|
|
auto filtered_array_of_nested_owner = array_of_nested->filter(filt, result_size_hint);
|
2017-12-15 19:46:24 +00:00
|
|
|
auto & filtered_array_of_nested = static_cast<const ColumnArray &>(*filtered_array_of_nested_owner);
|
2017-12-15 02:36:40 +00:00
|
|
|
auto & filtered_offsets = filtered_array_of_nested.getOffsetsPtr();
|
2017-04-18 03:03:39 +00:00
|
|
|
|
2017-12-14 01:43:19 +00:00
|
|
|
auto res_null_map = ColumnUInt8::create();
|
2017-12-15 19:46:24 +00:00
|
|
|
|
|
|
|
filterArraysImplOnlyData(nullable_elems.getNullMapData(), getOffsets(), res_null_map->getData(), filt, result_size_hint);
|
|
|
|
|
|
|
|
return ColumnArray::create(
|
2017-12-14 01:43:19 +00:00
|
|
|
ColumnNullable::create(
|
2017-04-18 03:03:39 +00:00
|
|
|
filtered_array_of_nested.getDataPtr(),
|
2017-12-15 19:53:11 +00:00
|
|
|
std::move(res_null_map)),
|
2017-04-18 03:03:39 +00:00
|
|
|
filtered_offsets);
|
|
|
|
}
|
|
|
|
|
2018-03-20 14:17:09 +00:00
|
|
|
ColumnPtr ColumnArray::filterTuple(const Filter & filt, ssize_t result_size_hint) const
|
2017-04-18 03:03:39 +00:00
|
|
|
{
|
|
|
|
if (getOffsets().size() == 0)
|
2017-12-15 19:46:24 +00:00
|
|
|
return ColumnArray::create(data);
|
2017-04-18 03:03:39 +00:00
|
|
|
|
|
|
|
const ColumnTuple & tuple = static_cast<const ColumnTuple &>(*data);
|
|
|
|
|
|
|
|
/// Make temporary arrays for each components of Tuple, then filter and collect back.
|
|
|
|
|
|
|
|
size_t tuple_size = tuple.getColumns().size();
|
|
|
|
|
|
|
|
if (tuple_size == 0)
|
|
|
|
throw Exception("Logical error: empty tuple", ErrorCodes::LOGICAL_ERROR);
|
|
|
|
|
|
|
|
Columns temporary_arrays(tuple_size);
|
|
|
|
for (size_t i = 0; i < tuple_size; ++i)
|
2018-03-20 14:17:09 +00:00
|
|
|
temporary_arrays[i] = ColumnArray(tuple.getColumns()[i]->assumeMutable(), getOffsetsPtr()->assumeMutable())
|
|
|
|
.filter(filt, result_size_hint);
|
2017-04-18 03:03:39 +00:00
|
|
|
|
2017-12-08 00:50:25 +00:00
|
|
|
Columns tuple_columns(tuple_size);
|
2017-04-18 03:03:39 +00:00
|
|
|
for (size_t i = 0; i < tuple_size; ++i)
|
2017-12-15 19:46:24 +00:00
|
|
|
tuple_columns[i] = static_cast<const ColumnArray &>(*temporary_arrays[i]).getDataPtr();
|
2017-04-18 03:03:39 +00:00
|
|
|
|
2017-12-14 01:43:19 +00:00
|
|
|
return ColumnArray::create(
|
|
|
|
ColumnTuple::create(tuple_columns),
|
2017-12-15 19:46:24 +00:00
|
|
|
static_cast<const ColumnArray &>(*temporary_arrays.front()).getOffsetsPtr());
|
2017-04-18 03:03:39 +00:00
|
|
|
}
|
|
|
|
|
2015-07-06 19:24:51 +00:00
|
|
|
|
2018-03-20 14:17:09 +00:00
|
|
|
ColumnPtr ColumnArray::permute(const Permutation & perm, size_t limit) const
|
2015-07-06 19:24:51 +00:00
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
size_t size = getOffsets().size();
|
2015-07-06 19:24:51 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
if (limit == 0)
|
|
|
|
limit = size;
|
|
|
|
else
|
|
|
|
limit = std::min(size, limit);
|
2015-07-06 19:24:51 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
if (perm.size() < limit)
|
|
|
|
throw Exception("Size of permutation is less than required.", ErrorCodes::SIZES_OF_COLUMNS_DOESNT_MATCH);
|
2015-07-06 19:24:51 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
if (limit == 0)
|
2017-12-15 19:46:24 +00:00
|
|
|
return ColumnArray::create(data);
|
2015-07-06 19:24:51 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
Permutation nested_perm(getOffsets().back());
|
2015-07-06 19:24:51 +00:00
|
|
|
|
2017-12-15 19:46:24 +00:00
|
|
|
auto res = ColumnArray::create(data->cloneEmpty());
|
2015-07-06 19:24:51 +00:00
|
|
|
|
2017-12-15 21:32:25 +00:00
|
|
|
Offsets & res_offsets = res->getOffsets();
|
2017-04-01 07:20:54 +00:00
|
|
|
res_offsets.resize(limit);
|
|
|
|
size_t current_offset = 0;
|
2015-07-06 19:24:51 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
for (size_t i = 0; i < limit; ++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]);
|
|
|
|
res_offsets[i] = current_offset;
|
|
|
|
}
|
2015-07-06 19:24:51 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
if (current_offset != 0)
|
|
|
|
res->data = data->permute(nested_perm, current_offset);
|
2015-07-06 19:24:51 +00:00
|
|
|
|
2017-12-15 19:46:24 +00:00
|
|
|
return std::move(res);
|
2015-07-06 19:24:51 +00:00
|
|
|
}
|
|
|
|
|
2017-03-12 12:56:59 +00:00
|
|
|
void ColumnArray::getPermutation(bool reverse, size_t limit, int nan_direction_hint, Permutation & res) const
|
2015-07-06 19:24:51 +00:00
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
size_t s = size();
|
|
|
|
if (limit >= s)
|
|
|
|
limit = 0;
|
2015-07-06 19:24:51 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
res.resize(s);
|
|
|
|
for (size_t i = 0; i < s; ++i)
|
|
|
|
res[i] = i;
|
2015-07-06 19:24:51 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
if (limit)
|
|
|
|
{
|
|
|
|
if (reverse)
|
|
|
|
std::partial_sort(res.begin(), res.begin() + limit, res.end(), less<false>(*this, nan_direction_hint));
|
|
|
|
else
|
|
|
|
std::partial_sort(res.begin(), res.begin() + limit, res.end(), less<true>(*this, nan_direction_hint));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (reverse)
|
|
|
|
std::sort(res.begin(), res.end(), less<false>(*this, nan_direction_hint));
|
|
|
|
else
|
|
|
|
std::sort(res.begin(), res.end(), less<true>(*this, nan_direction_hint));
|
|
|
|
}
|
2015-07-06 19:24:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-03-20 14:17:09 +00:00
|
|
|
ColumnPtr ColumnArray::replicate(const Offsets & replicate_offsets) const
|
2015-07-06 19:24:51 +00:00
|
|
|
{
|
2017-04-18 03:03:39 +00:00
|
|
|
if (typeid_cast<const ColumnUInt8 *>(data.get())) return replicateNumber<UInt8>(replicate_offsets);
|
|
|
|
if (typeid_cast<const ColumnUInt16 *>(data.get())) return replicateNumber<UInt16>(replicate_offsets);
|
|
|
|
if (typeid_cast<const ColumnUInt32 *>(data.get())) return replicateNumber<UInt32>(replicate_offsets);
|
|
|
|
if (typeid_cast<const ColumnUInt64 *>(data.get())) return replicateNumber<UInt64>(replicate_offsets);
|
|
|
|
if (typeid_cast<const ColumnInt8 *>(data.get())) return replicateNumber<Int8>(replicate_offsets);
|
|
|
|
if (typeid_cast<const ColumnInt16 *>(data.get())) return replicateNumber<Int16>(replicate_offsets);
|
|
|
|
if (typeid_cast<const ColumnInt32 *>(data.get())) return replicateNumber<Int32>(replicate_offsets);
|
|
|
|
if (typeid_cast<const ColumnInt64 *>(data.get())) return replicateNumber<Int64>(replicate_offsets);
|
|
|
|
if (typeid_cast<const ColumnFloat32 *>(data.get())) return replicateNumber<Float32>(replicate_offsets);
|
|
|
|
if (typeid_cast<const ColumnFloat64 *>(data.get())) return replicateNumber<Float64>(replicate_offsets);
|
|
|
|
if (typeid_cast<const ColumnString *>(data.get())) return replicateString(replicate_offsets);
|
2017-07-21 06:35:58 +00:00
|
|
|
if (typeid_cast<const ColumnConst *>(data.get())) return replicateConst(replicate_offsets);
|
2017-04-18 03:03:39 +00:00
|
|
|
if (typeid_cast<const ColumnNullable *>(data.get())) return replicateNullable(replicate_offsets);
|
|
|
|
if (typeid_cast<const ColumnTuple *>(data.get())) return replicateTuple(replicate_offsets);
|
|
|
|
return replicateGeneric(replicate_offsets);
|
2015-07-06 19:24:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
template <typename T>
|
2018-03-20 14:17:09 +00:00
|
|
|
ColumnPtr ColumnArray::replicateNumber(const Offsets & replicate_offsets) const
|
2015-07-06 19:24:51 +00:00
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
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);
|
2015-07-06 19:24:51 +00:00
|
|
|
|
2017-12-14 01:43:19 +00:00
|
|
|
MutableColumnPtr res = cloneEmpty();
|
2015-07-06 19:24:51 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
if (0 == col_size)
|
|
|
|
return res;
|
2015-07-06 19:24:51 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
ColumnArray & res_ = typeid_cast<ColumnArray &>(*res);
|
2015-07-06 19:24:51 +00:00
|
|
|
|
2017-12-15 21:32:25 +00:00
|
|
|
const typename ColumnVector<T>::Container & src_data = typeid_cast<const ColumnVector<T> &>(*data).getData();
|
|
|
|
const Offsets & src_offsets = getOffsets();
|
2015-07-06 19:24:51 +00:00
|
|
|
|
2017-12-15 21:32:25 +00:00
|
|
|
typename ColumnVector<T>::Container & res_data = typeid_cast<ColumnVector<T> &>(res_.getData()).getData();
|
|
|
|
Offsets & res_offsets = res_.getOffsets();
|
2015-07-06 19:24:51 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
res_data.reserve(data->size() / col_size * replicate_offsets.back());
|
|
|
|
res_offsets.reserve(replicate_offsets.back());
|
2015-07-06 19:24:51 +00:00
|
|
|
|
2017-12-15 21:32:25 +00:00
|
|
|
Offset prev_replicate_offset = 0;
|
|
|
|
Offset prev_data_offset = 0;
|
|
|
|
Offset current_new_offset = 0;
|
2015-07-06 19:24:51 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
for (size_t i = 0; i < col_size; ++i)
|
|
|
|
{
|
|
|
|
size_t size_to_replicate = replicate_offsets[i] - prev_replicate_offset;
|
|
|
|
size_t value_size = src_offsets[i] - prev_data_offset;
|
2015-07-06 19:24:51 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
for (size_t j = 0; j < size_to_replicate; ++j)
|
|
|
|
{
|
|
|
|
current_new_offset += value_size;
|
|
|
|
res_offsets.push_back(current_new_offset);
|
2015-07-06 19:24:51 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
res_data.resize(res_data.size() + value_size);
|
|
|
|
memcpy(&res_data[res_data.size() - value_size], &src_data[prev_data_offset], value_size * sizeof(T));
|
|
|
|
}
|
2015-07-06 19:24:51 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
prev_replicate_offset = replicate_offsets[i];
|
|
|
|
prev_data_offset = src_offsets[i];
|
|
|
|
}
|
2015-07-06 19:24:51 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
return res;
|
2015-07-06 19:24:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-03-20 14:17:09 +00:00
|
|
|
ColumnPtr ColumnArray::replicateString(const Offsets & replicate_offsets) const
|
2015-07-06 19:24:51 +00:00
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
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);
|
2015-07-06 19:24:51 +00:00
|
|
|
|
2017-12-14 01:43:19 +00:00
|
|
|
MutableColumnPtr res = cloneEmpty();
|
2015-07-06 19:24:51 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
if (0 == col_size)
|
|
|
|
return res;
|
2015-07-06 19:24:51 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
ColumnArray & res_ = static_cast<ColumnArray &>(*res);
|
2015-07-06 19:24:51 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
const ColumnString & src_string = typeid_cast<const ColumnString &>(*data);
|
|
|
|
const ColumnString::Chars_t & src_chars = src_string.getChars();
|
2017-12-15 21:32:25 +00:00
|
|
|
const Offsets & src_string_offsets = src_string.getOffsets();
|
|
|
|
const Offsets & src_offsets = getOffsets();
|
2015-07-06 19:24:51 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
ColumnString::Chars_t & res_chars = typeid_cast<ColumnString &>(res_.getData()).getChars();
|
2017-12-15 21:32:25 +00:00
|
|
|
Offsets & res_string_offsets = typeid_cast<ColumnString &>(res_.getData()).getOffsets();
|
|
|
|
Offsets & res_offsets = res_.getOffsets();
|
2015-07-06 19:24:51 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
res_chars.reserve(src_chars.size() / col_size * replicate_offsets.back());
|
|
|
|
res_string_offsets.reserve(src_string_offsets.size() / col_size * replicate_offsets.back());
|
|
|
|
res_offsets.reserve(replicate_offsets.back());
|
2015-07-06 19:24:51 +00:00
|
|
|
|
2017-12-15 21:32:25 +00:00
|
|
|
Offset prev_replicate_offset = 0;
|
2015-07-06 19:24:51 +00:00
|
|
|
|
2017-12-15 21:32:25 +00:00
|
|
|
Offset prev_src_offset = 0;
|
|
|
|
Offset prev_src_string_offset = 0;
|
2015-07-06 19:24:51 +00:00
|
|
|
|
2017-12-15 21:32:25 +00:00
|
|
|
Offset current_res_offset = 0;
|
|
|
|
Offset current_res_string_offset = 0;
|
2015-07-06 19:24:51 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
for (size_t i = 0; i < col_size; ++i)
|
|
|
|
{
|
|
|
|
/// How much to replicate the array.
|
|
|
|
size_t size_to_replicate = replicate_offsets[i] - prev_replicate_offset;
|
|
|
|
/// The number of rows in the array.
|
|
|
|
size_t value_size = src_offsets[i] - prev_src_offset;
|
|
|
|
/// Number of characters in rows of the array, including zero/null bytes.
|
|
|
|
size_t sum_chars_size = value_size == 0 ? 0 : (src_string_offsets[prev_src_offset + value_size - 1] - prev_src_string_offset);
|
2015-07-06 19:24:51 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
for (size_t j = 0; j < size_to_replicate; ++j)
|
|
|
|
{
|
|
|
|
current_res_offset += value_size;
|
|
|
|
res_offsets.push_back(current_res_offset);
|
2015-07-06 19:24:51 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
size_t prev_src_string_offset_local = prev_src_string_offset;
|
|
|
|
for (size_t k = 0; k < value_size; ++k)
|
|
|
|
{
|
|
|
|
/// Size of one row.
|
|
|
|
size_t chars_size = src_string_offsets[k + prev_src_offset] - prev_src_string_offset_local;
|
2015-07-06 19:24:51 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
current_res_string_offset += chars_size;
|
|
|
|
res_string_offsets.push_back(current_res_string_offset);
|
2015-07-06 19:24:51 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
prev_src_string_offset_local += chars_size;
|
|
|
|
}
|
2016-06-28 19:43:32 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
/// Copies the characters of the array of rows.
|
|
|
|
res_chars.resize(res_chars.size() + sum_chars_size);
|
|
|
|
memcpySmallAllowReadWriteOverflow15(
|
|
|
|
&res_chars[res_chars.size() - sum_chars_size], &src_chars[prev_src_string_offset], sum_chars_size);
|
|
|
|
}
|
2015-07-06 19:24:51 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
prev_replicate_offset = replicate_offsets[i];
|
|
|
|
prev_src_offset = src_offsets[i];
|
|
|
|
prev_src_string_offset += sum_chars_size;
|
|
|
|
}
|
2015-07-06 19:24:51 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
return res;
|
2015-07-06 19:24:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-03-20 14:17:09 +00:00
|
|
|
ColumnPtr ColumnArray::replicateConst(const Offsets & replicate_offsets) const
|
2015-07-06 19:24:51 +00:00
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
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);
|
2015-07-06 19:24:51 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
if (0 == col_size)
|
|
|
|
return cloneEmpty();
|
2015-07-06 19:24:51 +00:00
|
|
|
|
2017-12-15 21:32:25 +00:00
|
|
|
const Offsets & src_offsets = getOffsets();
|
2015-07-06 19:24:51 +00:00
|
|
|
|
2017-12-15 21:32:25 +00:00
|
|
|
auto res_column_offsets = ColumnOffsets::create();
|
|
|
|
Offsets & res_offsets = res_column_offsets->getData();
|
2017-04-01 07:20:54 +00:00
|
|
|
res_offsets.reserve(replicate_offsets.back());
|
2015-07-06 19:24:51 +00:00
|
|
|
|
2017-12-15 21:32:25 +00:00
|
|
|
Offset prev_replicate_offset = 0;
|
|
|
|
Offset prev_data_offset = 0;
|
|
|
|
Offset current_new_offset = 0;
|
2015-07-06 19:24:51 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
for (size_t i = 0; i < col_size; ++i)
|
|
|
|
{
|
|
|
|
size_t size_to_replicate = replicate_offsets[i] - prev_replicate_offset;
|
|
|
|
size_t value_size = src_offsets[i] - prev_data_offset;
|
2015-07-06 19:24:51 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
for (size_t j = 0; j < size_to_replicate; ++j)
|
|
|
|
{
|
|
|
|
current_new_offset += value_size;
|
|
|
|
res_offsets.push_back(current_new_offset);
|
|
|
|
}
|
2015-07-06 19:24:51 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
prev_replicate_offset = replicate_offsets[i];
|
|
|
|
prev_data_offset = src_offsets[i];
|
|
|
|
}
|
2015-07-06 19:24:51 +00:00
|
|
|
|
2017-12-15 19:53:11 +00:00
|
|
|
return ColumnArray::create(getData().cloneResized(current_new_offset), std::move(res_column_offsets));
|
2015-07-06 19:24:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-03-20 14:17:09 +00:00
|
|
|
ColumnPtr ColumnArray::replicateGeneric(const Offsets & replicate_offsets) const
|
2017-04-18 03:03:39 +00:00
|
|
|
{
|
|
|
|
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);
|
|
|
|
|
2017-12-14 01:43:19 +00:00
|
|
|
MutableColumnPtr res = cloneEmpty();
|
2017-04-18 03:03:39 +00:00
|
|
|
ColumnArray & res_concrete = static_cast<ColumnArray &>(*res);
|
|
|
|
|
|
|
|
if (0 == col_size)
|
|
|
|
return res;
|
|
|
|
|
2017-12-15 21:32:25 +00:00
|
|
|
IColumn::Offset prev_offset = 0;
|
2017-04-18 03:03:39 +00:00
|
|
|
for (size_t i = 0; i < col_size; ++i)
|
|
|
|
{
|
2017-07-21 06:35:58 +00:00
|
|
|
size_t size_to_replicate = replicate_offsets[i] - prev_offset;
|
|
|
|
prev_offset = replicate_offsets[i];
|
2017-04-18 03:03:39 +00:00
|
|
|
|
|
|
|
for (size_t j = 0; j < size_to_replicate; ++j)
|
|
|
|
res_concrete.insertFrom(*this, i);
|
|
|
|
}
|
|
|
|
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-03-20 14:17:09 +00:00
|
|
|
ColumnPtr ColumnArray::replicateNullable(const Offsets & replicate_offsets) const
|
2017-04-18 03:03:39 +00:00
|
|
|
{
|
|
|
|
const ColumnNullable & nullable = static_cast<const ColumnNullable &>(*data);
|
|
|
|
|
|
|
|
/// Make temporary arrays for each components of Nullable. Then replicate them independently and collect back to result.
|
|
|
|
/// NOTE Offsets are calculated twice and it is redundant.
|
|
|
|
|
2018-03-20 14:17:09 +00:00
|
|
|
auto array_of_nested = ColumnArray(nullable.getNestedColumnPtr()->assumeMutable(), getOffsetsPtr()->assumeMutable())
|
|
|
|
.replicate(replicate_offsets);
|
|
|
|
auto array_of_null_map = ColumnArray(nullable.getNullMapColumnPtr()->assumeMutable(), getOffsetsPtr()->assumeMutable())
|
|
|
|
.replicate(replicate_offsets);
|
2017-04-18 03:03:39 +00:00
|
|
|
|
2017-12-14 01:43:19 +00:00
|
|
|
return ColumnArray::create(
|
|
|
|
ColumnNullable::create(
|
2018-03-20 14:17:09 +00:00
|
|
|
static_cast<const ColumnArray &>(*array_of_nested).getDataPtr(),
|
|
|
|
static_cast<const ColumnArray &>(*array_of_null_map).getDataPtr()),
|
|
|
|
static_cast<const ColumnArray &>(*array_of_nested).getOffsetsPtr());
|
2017-04-18 03:03:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-03-20 14:17:09 +00:00
|
|
|
ColumnPtr ColumnArray::replicateTuple(const Offsets & replicate_offsets) const
|
2017-04-18 03:03:39 +00:00
|
|
|
{
|
|
|
|
const ColumnTuple & tuple = static_cast<const ColumnTuple &>(*data);
|
|
|
|
|
|
|
|
/// Make temporary arrays for each components of Tuple. In the same way as for Nullable.
|
|
|
|
|
|
|
|
size_t tuple_size = tuple.getColumns().size();
|
|
|
|
|
|
|
|
if (tuple_size == 0)
|
|
|
|
throw Exception("Logical error: empty tuple", ErrorCodes::LOGICAL_ERROR);
|
|
|
|
|
|
|
|
Columns temporary_arrays(tuple_size);
|
|
|
|
for (size_t i = 0; i < tuple_size; ++i)
|
2018-03-20 14:17:09 +00:00
|
|
|
temporary_arrays[i] = ColumnArray(tuple.getColumns()[i]->assumeMutable(), getOffsetsPtr()->assumeMutable())
|
|
|
|
.replicate(replicate_offsets);
|
2017-04-18 03:03:39 +00:00
|
|
|
|
2017-12-08 00:50:25 +00:00
|
|
|
Columns tuple_columns(tuple_size);
|
2017-04-18 03:03:39 +00:00
|
|
|
for (size_t i = 0; i < tuple_size; ++i)
|
2017-12-15 19:46:24 +00:00
|
|
|
tuple_columns[i] = static_cast<const ColumnArray &>(*temporary_arrays[i]).getDataPtr();
|
2017-04-18 03:03:39 +00:00
|
|
|
|
2017-12-14 01:43:19 +00:00
|
|
|
return ColumnArray::create(
|
|
|
|
ColumnTuple::create(tuple_columns),
|
2017-12-15 19:46:24 +00:00
|
|
|
static_cast<const ColumnArray &>(*temporary_arrays.front()).getOffsetsPtr());
|
2017-04-18 03:03:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-07-06 13:54:55 +00:00
|
|
|
void ColumnArray::gather(ColumnGathererStream & gatherer)
|
|
|
|
{
|
|
|
|
gatherer.gather(*this);
|
|
|
|
}
|
|
|
|
|
2015-07-06 19:24:51 +00:00
|
|
|
}
|