ClickHouse/dbms/Columns/ColumnConst.cpp

124 lines
3.9 KiB
C++
Raw Normal View History

#include <IO/WriteHelpers.h>
2012-08-26 11:14:52 +00:00
#include <Columns/ColumnConst.h>
#include <Columns/ColumnsCommon.h>
2019-10-07 18:56:03 +00:00
#include <Common/PODArray.h>
#include <Common/typeid_cast.h>
#include <Common/WeakHash.h>
2020-03-18 16:36:21 +00:00
#include <Common/HashTable/Hash.h>
2011-11-06 06:22:52 +00:00
namespace DB
{
2013-03-06 13:56:11 +00:00
namespace ErrorCodes
{
extern const int SIZES_OF_COLUMNS_DOESNT_MATCH;
2020-03-19 17:35:08 +00:00
extern const int LOGICAL_ERROR;
}
2019-08-03 11:02:40 +00:00
ColumnConst::ColumnConst(const ColumnPtr & data_, size_t s_)
: data(data_), s(s_)
2017-07-24 01:00:31 +00:00
{
/// Squash Const of Const.
while (const ColumnConst * const_data = typeid_cast<const ColumnConst *>(data.get()))
data = const_data->getDataColumnPtr();
2017-07-24 01:00:31 +00:00
if (data->size() != 1)
throw Exception("Incorrect size of nested column in constructor of ColumnConst: " + toString(data->size()) + ", must be 1.",
ErrorCodes::SIZES_OF_COLUMNS_DOESNT_MATCH);
}
2018-03-20 14:17:09 +00:00
ColumnPtr ColumnConst::convertToFullColumn() const
2017-07-24 01:00:31 +00:00
{
return data->replicate(Offsets(1, s));
2017-07-24 01:00:31 +00:00
}
ColumnPtr ColumnConst::removeLowCardinality() const
{
return ColumnConst::create(data->convertToFullColumnIfLowCardinality(), s);
}
2018-03-20 14:17:09 +00:00
ColumnPtr ColumnConst::filter(const Filter & filt, ssize_t /*result_size_hint*/) const
{
if (s != filt.size())
throw Exception("Size of filter (" + toString(filt.size()) + ") doesn't match size of column (" + toString(s) + ")",
ErrorCodes::SIZES_OF_COLUMNS_DOESNT_MATCH);
return ColumnConst::create(data, countBytesInFilter(filt));
}
2018-03-20 14:17:09 +00:00
ColumnPtr ColumnConst::replicate(const Offsets & offsets) const
{
if (s != offsets.size())
throw Exception("Size of offsets (" + toString(offsets.size()) + ") doesn't match size of column (" + toString(s) + ")",
ErrorCodes::SIZES_OF_COLUMNS_DOESNT_MATCH);
size_t replicated_size = 0 == s ? 0 : offsets.back();
return ColumnConst::create(data, replicated_size);
}
ColumnPtr ColumnConst::permute(const Permutation & perm, size_t limit) const
{
if (limit == 0)
limit = s;
else
limit = std::min(s, limit);
if (perm.size() < limit)
throw Exception("Size of permutation (" + toString(perm.size()) + ") is less than required (" + toString(limit) + ")",
ErrorCodes::SIZES_OF_COLUMNS_DOESNT_MATCH);
return ColumnConst::create(data, limit);
}
ColumnPtr ColumnConst::index(const IColumn & indexes, size_t limit) const
2018-04-18 21:00:47 +00:00
{
if (limit == 0)
limit = indexes.size();
2018-04-18 21:00:47 +00:00
if (indexes.size() < limit)
throw Exception("Size of indexes (" + toString(indexes.size()) + ") is less than required (" + toString(limit) + ")",
2018-04-18 21:00:47 +00:00
ErrorCodes::SIZES_OF_COLUMNS_DOESNT_MATCH);
return ColumnConst::create(data, limit);
}
MutableColumns ColumnConst::scatter(ColumnIndex num_columns, const Selector & selector) const
{
if (s != selector.size())
throw Exception("Size of selector (" + toString(selector.size()) + ") doesn't match size of column (" + toString(s) + ")",
ErrorCodes::SIZES_OF_COLUMNS_DOESNT_MATCH);
std::vector<size_t> counts = countColumnsSizeInSelector(num_columns, selector);
MutableColumns res(num_columns);
for (size_t i = 0; i < num_columns; ++i)
res[i] = cloneResized(counts[i]);
return res;
}
void ColumnConst::getPermutation(bool /*reverse*/, size_t /*limit*/, int /*nan_direction_hint*/, Permutation & res) const
{
res.resize(s);
for (size_t i = 0; i < s; ++i)
res[i] = i;
}
void ColumnConst::updateWeakHash32(WeakHash32 & hash) const
{
if (hash.getData().size() != s)
throw Exception("Size of WeakHash32 does not match size of column: column size is " + std::to_string(s) +
", hash size is " + std::to_string(hash.getData().size()), ErrorCodes::LOGICAL_ERROR);
WeakHash32 element_hash(1);
data->updateWeakHash32(element_hash);
size_t data_hash = element_hash.getData()[0];
for (auto & value : hash.getData())
value = intHashCRC32(data_hash, value);
}
2011-11-06 06:22:52 +00:00
}