ClickHouse/src/Interpreters/RowRefs.cpp

152 lines
4.3 KiB
C++
Raw Normal View History

2019-03-30 21:30:21 +00:00
#include <Interpreters/RowRefs.h>
#include <Core/Block.h>
#include <Core/Types.h>
2019-04-01 16:44:15 +00:00
#include <Common/typeid_cast.h>
2019-03-30 21:30:21 +00:00
#include <Common/ColumnsHashing.h>
#include <Columns/IColumn.h>
#include <Columns/ColumnVector.h>
#include <Columns/ColumnDecimal.h>
2019-03-30 21:30:21 +00:00
namespace DB
{
2019-04-01 16:44:15 +00:00
namespace
{
/// maps enum values to types
template <typename F>
void callWithType(AsofRowRefs::Type which, F && f)
2019-03-30 21:30:21 +00:00
{
switch (which)
{
2019-05-02 23:46:04 +00:00
case AsofRowRefs::Type::keyu32: return f(UInt32());
case AsofRowRefs::Type::keyu64: return f(UInt64());
case AsofRowRefs::Type::keyi32: return f(Int32());
case AsofRowRefs::Type::keyi64: return f(Int64());
case AsofRowRefs::Type::keyf32: return f(Float32());
case AsofRowRefs::Type::keyf64: return f(Float64());
case AsofRowRefs::Type::keyDecimal32: return f(Decimal32());
case AsofRowRefs::Type::keyDecimal64: return f(Decimal64());
case AsofRowRefs::Type::keyDecimal128: return f(Decimal128());
2019-03-30 21:30:21 +00:00
}
2019-04-01 16:44:15 +00:00
__builtin_unreachable();
}
}
2019-04-01 16:44:15 +00:00
AsofRowRefs::AsofRowRefs(Type type)
{
auto call = [&](const auto & t)
{
using T = std::decay_t<decltype(t)>;
using LookupType = typename Entry<T>::LookupType;
lookups = std::make_unique<LookupType>();
};
callWithType(type, call);
}
void AsofRowRefs::insert(Type type, const IColumn * asof_column, const Block * block, size_t row_num)
2019-04-01 16:44:15 +00:00
{
auto call = [&](const auto & t)
2019-04-01 16:44:15 +00:00
{
using T = std::decay_t<decltype(t)>;
using LookupPtr = typename Entry<T>::LookupPtr;
2019-04-01 16:44:15 +00:00
using ColumnType = std::conditional_t<IsDecimalNumber<T>, ColumnDecimal<T>, ColumnVector<T>>;
auto * column = typeid_cast<const ColumnType *>(asof_column);
T key = column->getElement(row_num);
auto entry = Entry<T>(key, RowRef(block, row_num));
std::get<LookupPtr>(lookups)->insert(entry);
2019-04-01 16:44:15 +00:00
};
2019-04-02 18:50:35 +00:00
callWithType(type, call);
2019-03-30 21:30:21 +00:00
}
2019-10-11 17:56:26 +00:00
const RowRef * AsofRowRefs::findAsof(Type type, ASOF::Inequality inequality, const IColumn * asof_column, size_t row_num) const
2019-03-30 21:30:21 +00:00
{
2019-04-01 16:44:15 +00:00
const RowRef * out = nullptr;
2019-10-11 17:56:26 +00:00
bool ascending = (inequality == ASOF::Inequality::Less) || (inequality == ASOF::Inequality::LessOrEquals);
bool is_strict = (inequality == ASOF::Inequality::Less) || (inequality == ASOF::Inequality::Greater);
auto call = [&](const auto & t)
2019-03-30 21:30:21 +00:00
{
using T = std::decay_t<decltype(t)>;
2019-10-11 17:56:26 +00:00
using EntryType = Entry<T>;
using LookupPtr = typename EntryType::LookupPtr;
2019-03-30 21:30:21 +00:00
using ColumnType = std::conditional_t<IsDecimalNumber<T>, ColumnDecimal<T>, ColumnVector<T>>;
auto * column = typeid_cast<const ColumnType *>(asof_column);
T key = column->getElement(row_num);
auto & typed_lookup = std::get<LookupPtr>(lookups);
2019-04-01 16:44:15 +00:00
2019-10-11 17:56:26 +00:00
if (is_strict)
out = typed_lookup->upperBound(EntryType(key), ascending);
else
out = typed_lookup->lowerBound(EntryType(key), ascending);
2019-04-01 16:44:15 +00:00
};
2019-04-02 18:50:35 +00:00
callWithType(type, call);
2019-04-01 16:44:15 +00:00
return out;
2019-03-30 21:30:21 +00:00
}
2019-04-01 16:44:15 +00:00
std::optional<AsofRowRefs::Type> AsofRowRefs::getTypeSize(const IColumn * asof_column, size_t & size)
2019-03-30 21:30:21 +00:00
{
2019-04-01 16:44:15 +00:00
if (typeid_cast<const ColumnVector<UInt32> *>(asof_column))
{
size = sizeof(UInt32);
2019-05-02 23:46:04 +00:00
return Type::keyu32;
2019-04-01 16:44:15 +00:00
}
else if (typeid_cast<const ColumnVector<UInt64> *>(asof_column))
{
size = sizeof(UInt64);
2019-05-02 23:46:04 +00:00
return Type::keyu64;
}
else if (typeid_cast<const ColumnVector<Int32> *>(asof_column))
{
size = sizeof(Int32);
return Type::keyi32;
}
else if (typeid_cast<const ColumnVector<Int64> *>(asof_column))
{
size = sizeof(Int64);
return Type::keyi64;
2019-04-01 16:44:15 +00:00
}
else if (typeid_cast<const ColumnVector<Float32> *>(asof_column))
{
size = sizeof(Float32);
return Type::keyf32;
}
else if (typeid_cast<const ColumnVector<Float64> *>(asof_column))
{
size = sizeof(Float64);
return Type::keyf64;
}
else if (typeid_cast<const ColumnDecimal<Decimal32> *>(asof_column))
{
size = sizeof(Decimal32);
return Type::keyDecimal32;
}
else if (typeid_cast<const ColumnDecimal<Decimal64> *>(asof_column))
{
size = sizeof(Decimal64);
return Type::keyDecimal64;
}
else if (typeid_cast<const ColumnDecimal<Decimal128> *>(asof_column))
{
size = sizeof(Decimal128);
return Type::keyDecimal128;
}
2019-04-01 16:44:15 +00:00
size = 0;
2019-03-30 21:30:21 +00:00
return {};
}
2019-03-31 10:56:54 +00:00
}