2012-08-23 18:45:59 +00:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <DB/Core/Block.h>
|
2015-12-24 14:31:41 +00:00
|
|
|
#include <ext/map.hpp>
|
|
|
|
#include <ext/range.hpp>
|
2012-08-23 18:45:59 +00:00
|
|
|
|
|
|
|
|
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
|
2016-01-11 21:46:36 +00:00
|
|
|
namespace ErrorCodes
|
|
|
|
{
|
|
|
|
extern const int NOT_IMPLEMENTED;
|
|
|
|
extern const int CANNOT_INSERT_VALUE_OF_DIFFERENT_SIZE_INTO_TUPLE;
|
|
|
|
}
|
|
|
|
|
2012-08-23 18:45:59 +00:00
|
|
|
|
2016-07-10 02:14:23 +00:00
|
|
|
/** Column, that is just group of few another columns.
|
|
|
|
*
|
|
|
|
* For constant Tuples, see ColumnConstTuple.
|
|
|
|
* Mixed constant/non-constant columns is prohibited in tuple
|
|
|
|
* for implementation simplicity.
|
2012-08-23 18:45:59 +00:00
|
|
|
*/
|
2014-06-04 01:00:09 +00:00
|
|
|
class ColumnTuple final : public IColumn
|
2012-08-23 18:45:59 +00:00
|
|
|
{
|
|
|
|
private:
|
|
|
|
Block data;
|
|
|
|
Columns columns;
|
|
|
|
|
|
|
|
public:
|
|
|
|
ColumnTuple() {}
|
|
|
|
|
|
|
|
ColumnTuple(Block data_) : data(data_)
|
|
|
|
{
|
|
|
|
size_t size = data.columns();
|
|
|
|
columns.resize(size);
|
|
|
|
for (size_t i = 0; i < size; ++i)
|
2015-11-29 17:06:30 +00:00
|
|
|
columns[i] = data.unsafeGetByPosition(i).column;
|
2012-08-23 18:45:59 +00:00
|
|
|
}
|
2014-10-31 22:15:17 +00:00
|
|
|
|
|
|
|
std::string getName() const override { return "Tuple"; }
|
|
|
|
|
2016-05-28 00:14:10 +00:00
|
|
|
ColumnPtr cloneEmpty() const override
|
2012-08-23 18:45:59 +00:00
|
|
|
{
|
2016-05-28 05:31:36 +00:00
|
|
|
return std::make_shared<ColumnTuple>(data.cloneEmpty());
|
2012-08-23 18:45:59 +00:00
|
|
|
}
|
|
|
|
|
2014-10-31 22:15:17 +00:00
|
|
|
size_t size() const override
|
2012-08-23 18:45:59 +00:00
|
|
|
{
|
|
|
|
return data.rows();
|
|
|
|
}
|
|
|
|
|
2014-10-31 22:15:17 +00:00
|
|
|
Field operator[](size_t n) const override
|
2012-08-23 18:45:59 +00:00
|
|
|
{
|
2015-12-24 14:31:41 +00:00
|
|
|
return Tuple{ext::map<TupleBackend>(columns, [n] (const auto & column) { return (*column)[n]; })};
|
2012-08-23 18:45:59 +00:00
|
|
|
}
|
|
|
|
|
2014-10-31 22:15:17 +00:00
|
|
|
void get(size_t n, Field & res) const override
|
2013-01-07 06:47:15 +00:00
|
|
|
{
|
2015-12-24 14:31:41 +00:00
|
|
|
const size_t size = columns.size();
|
|
|
|
res = Tuple(TupleBackend(size));
|
|
|
|
TupleBackend & res_arr = DB::get<Tuple &>(res).t;
|
|
|
|
for (const auto i : ext::range(0, size))
|
2013-01-07 06:47:15 +00:00
|
|
|
columns[i]->get(n, res_arr[i]);
|
|
|
|
}
|
|
|
|
|
2014-10-31 22:15:17 +00:00
|
|
|
StringRef getDataAt(size_t n) const override
|
2012-10-07 06:30:10 +00:00
|
|
|
{
|
|
|
|
throw Exception("Method getDataAt is not supported for " + getName(), ErrorCodes::NOT_IMPLEMENTED);
|
|
|
|
}
|
|
|
|
|
2014-10-31 22:15:17 +00:00
|
|
|
void insertData(const char * pos, size_t length) override
|
2013-02-16 20:15:45 +00:00
|
|
|
{
|
|
|
|
throw Exception("Method insertData is not supported for " + getName(), ErrorCodes::NOT_IMPLEMENTED);
|
|
|
|
}
|
|
|
|
|
2014-10-31 22:15:17 +00:00
|
|
|
void insert(const Field & x) override
|
2012-08-23 18:45:59 +00:00
|
|
|
{
|
2015-12-24 14:31:41 +00:00
|
|
|
const TupleBackend & tuple = DB::get<const Tuple &>(x).t;
|
2012-08-23 18:45:59 +00:00
|
|
|
|
2015-12-24 14:31:41 +00:00
|
|
|
const size_t size = columns.size();
|
|
|
|
if (tuple.size() != size)
|
2012-08-23 18:45:59 +00:00
|
|
|
throw Exception("Cannot insert value of different size into tuple", ErrorCodes::CANNOT_INSERT_VALUE_OF_DIFFERENT_SIZE_INTO_TUPLE);
|
|
|
|
|
|
|
|
for (size_t i = 0; i < size; ++i)
|
2015-12-24 14:31:41 +00:00
|
|
|
columns[i]->insert(tuple[i]);
|
2012-08-23 18:45:59 +00:00
|
|
|
}
|
|
|
|
|
2014-10-31 22:15:17 +00:00
|
|
|
void insertFrom(const IColumn & src_, size_t n) override
|
2012-12-16 00:52:06 +00:00
|
|
|
{
|
|
|
|
const ColumnTuple & src = static_cast<const ColumnTuple &>(src_);
|
2014-10-31 22:15:17 +00:00
|
|
|
|
2012-12-16 00:52:06 +00:00
|
|
|
size_t size = columns.size();
|
|
|
|
if (src.columns.size() != size)
|
|
|
|
throw Exception("Cannot insert value of different size into tuple", ErrorCodes::CANNOT_INSERT_VALUE_OF_DIFFERENT_SIZE_INTO_TUPLE);
|
2014-10-31 22:15:17 +00:00
|
|
|
|
2012-12-16 00:52:06 +00:00
|
|
|
for (size_t i = 0; i < size; ++i)
|
|
|
|
columns[i]->insertFrom(*src.columns[i], n);
|
|
|
|
}
|
|
|
|
|
2014-10-31 22:15:17 +00:00
|
|
|
void insertDefault() override
|
2012-08-23 18:45:59 +00:00
|
|
|
{
|
2015-10-04 03:17:36 +00:00
|
|
|
for (auto & column : columns)
|
|
|
|
column->insertDefault();
|
|
|
|
}
|
|
|
|
|
2016-02-16 16:39:39 +00:00
|
|
|
void popBack(size_t n) override
|
|
|
|
{
|
|
|
|
for (auto & column : columns)
|
|
|
|
column->popBack(n);
|
|
|
|
}
|
|
|
|
|
2015-10-04 03:17:36 +00:00
|
|
|
|
|
|
|
StringRef serializeValueIntoArena(size_t n, Arena & arena, char const *& begin) const override
|
|
|
|
{
|
|
|
|
size_t values_size = 0;
|
|
|
|
for (auto & column : columns)
|
|
|
|
values_size += column->serializeValueIntoArena(n, arena, begin).size;
|
|
|
|
|
|
|
|
return StringRef(begin, values_size);
|
|
|
|
}
|
|
|
|
|
|
|
|
const char * deserializeAndInsertFromArena(const char * pos) override
|
|
|
|
{
|
|
|
|
for (auto & column : columns)
|
|
|
|
pos = column->deserializeAndInsertFromArena(pos);
|
|
|
|
|
|
|
|
return pos;
|
2012-08-23 18:45:59 +00:00
|
|
|
}
|
|
|
|
|
2016-07-10 15:58:58 +00:00
|
|
|
void updateHashWithValue(size_t n, SipHash & hash) const override
|
|
|
|
{
|
|
|
|
for (auto & column : columns)
|
|
|
|
column->updateHashWithValue(n, hash);
|
|
|
|
}
|
|
|
|
|
2015-11-29 17:06:30 +00:00
|
|
|
void insertRangeFrom(const IColumn & src, size_t start, size_t length) override
|
2013-05-03 05:23:14 +00:00
|
|
|
{
|
|
|
|
for (size_t i = 0; i < columns.size(); ++i)
|
2015-11-29 17:06:30 +00:00
|
|
|
data.unsafeGetByPosition(i).column->insertRangeFrom(
|
|
|
|
*static_cast<const ColumnTuple &>(src).data.unsafeGetByPosition(i).column.get(),
|
|
|
|
start, length);
|
2013-05-03 05:23:14 +00:00
|
|
|
}
|
|
|
|
|
2015-12-05 07:01:18 +00:00
|
|
|
ColumnPtr filter(const Filter & filt, ssize_t result_size_hint) const override
|
2013-05-03 05:23:14 +00:00
|
|
|
{
|
|
|
|
Block res_block = data.cloneEmpty();
|
|
|
|
|
|
|
|
for (size_t i = 0; i < columns.size(); ++i)
|
2015-12-05 07:01:18 +00:00
|
|
|
res_block.unsafeGetByPosition(i).column = data.unsafeGetByPosition(i).column->filter(filt, result_size_hint);
|
2013-05-03 05:23:14 +00:00
|
|
|
|
2016-05-28 05:31:36 +00:00
|
|
|
return std::make_shared<ColumnTuple>(res_block);
|
2013-05-03 05:23:14 +00:00
|
|
|
}
|
|
|
|
|
2014-10-31 22:15:17 +00:00
|
|
|
ColumnPtr permute(const Permutation & perm, size_t limit) const override
|
2013-05-03 05:23:14 +00:00
|
|
|
{
|
|
|
|
Block res_block = data.cloneEmpty();
|
|
|
|
|
|
|
|
for (size_t i = 0; i < columns.size(); ++i)
|
2015-11-29 17:06:30 +00:00
|
|
|
res_block.unsafeGetByPosition(i).column = data.unsafeGetByPosition(i).column->permute(perm, limit);
|
2013-05-03 05:23:14 +00:00
|
|
|
|
2016-05-28 05:31:36 +00:00
|
|
|
return std::make_shared<ColumnTuple>(res_block);
|
2013-05-03 05:23:14 +00:00
|
|
|
}
|
2014-10-31 22:15:17 +00:00
|
|
|
|
|
|
|
ColumnPtr replicate(const Offsets_t & offsets) const override
|
2013-05-03 05:23:14 +00:00
|
|
|
{
|
|
|
|
Block res_block = data.cloneEmpty();
|
|
|
|
|
|
|
|
for (size_t i = 0; i < columns.size(); ++i)
|
2015-11-29 17:06:30 +00:00
|
|
|
res_block.unsafeGetByPosition(i).column = data.unsafeGetByPosition(i).column->replicate(offsets);
|
2013-05-03 05:23:14 +00:00
|
|
|
|
2016-05-28 05:31:36 +00:00
|
|
|
return std::make_shared<ColumnTuple>(res_block);
|
2013-05-03 05:23:14 +00:00
|
|
|
}
|
2012-08-23 18:45:59 +00:00
|
|
|
|
2014-10-31 22:15:17 +00:00
|
|
|
int compareAt(size_t n, size_t m, const IColumn & rhs, int nan_direction_hint) const override
|
2012-08-23 18:45:59 +00:00
|
|
|
{
|
|
|
|
size_t size = columns.size();
|
|
|
|
for (size_t i = 0; i < size; ++i)
|
2013-11-01 20:10:43 +00:00
|
|
|
if (int res = columns[i]->compareAt(n, m, *static_cast<const ColumnTuple &>(rhs).columns[i], nan_direction_hint))
|
2012-08-23 18:45:59 +00:00
|
|
|
return res;
|
2014-10-31 22:15:17 +00:00
|
|
|
|
2012-08-23 18:45:59 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2013-09-16 05:44:47 +00:00
|
|
|
template <bool positive>
|
2012-08-23 18:45:59 +00:00
|
|
|
struct Less
|
|
|
|
{
|
|
|
|
ConstColumnPlainPtrs plain_columns;
|
|
|
|
|
|
|
|
Less(const Columns & columns)
|
|
|
|
{
|
2015-10-04 03:17:36 +00:00
|
|
|
for (const auto & column : columns)
|
|
|
|
plain_columns.push_back(column.get());
|
2012-08-23 18:45:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool operator() (size_t a, size_t b) const
|
|
|
|
{
|
|
|
|
for (ConstColumnPlainPtrs::const_iterator it = plain_columns.begin(); it != plain_columns.end(); ++it)
|
|
|
|
{
|
2013-11-01 20:10:43 +00:00
|
|
|
int res = (*it)->compareAt(a, b, **it, positive ? 1 : -1);
|
2012-08-23 18:45:59 +00:00
|
|
|
if (res < 0)
|
2013-09-16 05:44:47 +00:00
|
|
|
return positive;
|
2012-08-23 18:45:59 +00:00
|
|
|
else if (res > 0)
|
2013-09-16 05:44:47 +00:00
|
|
|
return !positive;
|
2012-08-23 18:45:59 +00:00
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2014-10-31 22:15:17 +00:00
|
|
|
void getPermutation(bool reverse, size_t limit, Permutation & res) const override
|
2012-08-23 18:45:59 +00:00
|
|
|
{
|
|
|
|
size_t rows = size();
|
2013-12-08 02:29:40 +00:00
|
|
|
res.resize(rows);
|
2012-08-23 18:45:59 +00:00
|
|
|
for (size_t i = 0; i < rows; ++i)
|
2013-12-08 02:29:40 +00:00
|
|
|
res[i] = i;
|
2012-08-23 18:45:59 +00:00
|
|
|
|
2014-05-23 01:32:32 +00:00
|
|
|
if (limit >= rows)
|
2013-09-16 05:44:47 +00:00
|
|
|
limit = 0;
|
|
|
|
|
|
|
|
if (limit)
|
|
|
|
{
|
|
|
|
if (reverse)
|
2013-12-08 02:29:40 +00:00
|
|
|
std::partial_sort(res.begin(), res.begin() + limit, res.end(), Less<false>(columns));
|
2013-09-16 05:44:47 +00:00
|
|
|
else
|
2013-12-08 02:29:40 +00:00
|
|
|
std::partial_sort(res.begin(), res.begin() + limit, res.end(), Less<true>(columns));
|
2013-09-16 05:44:47 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (reverse)
|
2013-12-08 02:29:40 +00:00
|
|
|
std::sort(res.begin(), res.end(), Less<false>(columns));
|
2013-09-16 05:44:47 +00:00
|
|
|
else
|
2013-12-08 02:29:40 +00:00
|
|
|
std::sort(res.begin(), res.end(), Less<true>(columns));
|
2013-09-16 05:44:47 +00:00
|
|
|
}
|
2012-08-23 18:45:59 +00:00
|
|
|
}
|
|
|
|
|
2014-10-31 22:15:17 +00:00
|
|
|
void reserve(size_t n) override
|
2013-02-03 18:39:09 +00:00
|
|
|
{
|
2015-10-04 03:17:36 +00:00
|
|
|
for (auto & column : columns)
|
|
|
|
column->reserve(n);
|
2013-02-03 18:39:09 +00:00
|
|
|
}
|
|
|
|
|
2014-10-31 22:15:17 +00:00
|
|
|
size_t byteSize() const override
|
2012-08-23 18:45:59 +00:00
|
|
|
{
|
|
|
|
size_t res = 0;
|
2015-10-04 03:17:36 +00:00
|
|
|
for (const auto & column : columns)
|
|
|
|
res += column->byteSize();
|
2012-08-23 18:45:59 +00:00
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2015-10-28 21:21:18 +00:00
|
|
|
ColumnPtr convertToFullColumnIfConst() const override
|
|
|
|
{
|
|
|
|
Block materialized = data;
|
|
|
|
for (size_t i = 0, size = materialized.columns(); i < size; ++i)
|
|
|
|
if (auto converted = materialized.unsafeGetByPosition(i).column->convertToFullColumnIfConst())
|
|
|
|
materialized.unsafeGetByPosition(i).column = converted;
|
|
|
|
|
2016-05-28 05:31:36 +00:00
|
|
|
return std::make_shared<ColumnTuple>(materialized);
|
2015-10-28 21:21:18 +00:00
|
|
|
}
|
|
|
|
|
2012-08-23 18:45:59 +00:00
|
|
|
|
|
|
|
const Block & getData() const { return data; }
|
2013-10-26 19:00:13 +00:00
|
|
|
Block & getData() { return data; }
|
2015-10-29 02:13:37 +00:00
|
|
|
|
|
|
|
const Columns & getColumns() const { return columns; }
|
|
|
|
Columns & getColumns() { return columns; }
|
2016-07-05 16:23:37 +00:00
|
|
|
|
2016-08-10 19:12:29 +00:00
|
|
|
void getExtremes(Field & min, Field & max) const override
|
2016-07-05 16:23:37 +00:00
|
|
|
{
|
|
|
|
const size_t tuple_size = columns.size();
|
|
|
|
|
|
|
|
min = Tuple(TupleBackend(tuple_size));
|
|
|
|
max = Tuple(TupleBackend(tuple_size));
|
|
|
|
|
|
|
|
auto & min_backend = min.get<Tuple &>().t;
|
|
|
|
auto & max_backend = max.get<Tuple &>().t;
|
|
|
|
|
|
|
|
for (const auto i : ext::range(0, tuple_size))
|
2016-08-10 19:12:29 +00:00
|
|
|
columns[i]->getExtremes(min_backend[i], max_backend[i]);
|
2016-07-05 16:23:37 +00:00
|
|
|
}
|
2012-08-23 18:45:59 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
}
|