ClickHouse/src/Interpreters/RowRefs.cpp

168 lines
4.9 KiB
C++
Raw Normal View History

2019-03-30 21:30:21 +00:00
#include <Interpreters/RowRefs.h>
#include <Core/Block.h>
2020-09-15 09:55:57 +00:00
#include <common/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
{
namespace ErrorCodes
{
extern const int BAD_TYPE_OF_FIELD;
}
2019-04-01 16:44:15 +00:00
namespace
{
/// maps enum values to types
template <typename F>
2020-06-01 09:38:46 +00:00
void callWithType(TypeIndex which, F && f)
2019-03-30 21:30:21 +00:00
{
switch (which)
{
2020-06-01 09:38:46 +00:00
case TypeIndex::UInt8: return f(UInt8());
case TypeIndex::UInt16: return f(UInt16());
case TypeIndex::UInt32: return f(UInt32());
case TypeIndex::UInt64: return f(UInt64());
case TypeIndex::Int8: return f(Int8());
case TypeIndex::Int16: return f(Int16());
case TypeIndex::Int32: return f(Int32());
case TypeIndex::Int64: return f(Int64());
case TypeIndex::Float32: return f(Float32());
case TypeIndex::Float64: return f(Float64());
case TypeIndex::Decimal32: return f(Decimal32());
case TypeIndex::Decimal64: return f(Decimal64());
case TypeIndex::Decimal128: return f(Decimal128());
case TypeIndex::DateTime64: return f(DateTime64());
2020-06-01 09:38:46 +00:00
default:
break;
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
2020-06-01 09:38:46 +00:00
AsofRowRefs::AsofRowRefs(TypeIndex 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(TypeIndex 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>>;
const 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
}
const RowRef * AsofRowRefs::findAsof(TypeIndex 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>>;
const 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
}
std::optional<TypeIndex> AsofRowRefs::getTypeSize(const IColumn & asof_column, size_t & size)
2019-03-30 21:30:21 +00:00
{
TypeIndex idx = asof_column.getDataType();
2020-06-01 09:38:46 +00:00
switch (idx)
{
2020-06-01 09:38:46 +00:00
case TypeIndex::UInt8:
size = sizeof(UInt8);
return idx;
case TypeIndex::UInt16:
size = sizeof(UInt16);
return idx;
case TypeIndex::UInt32:
size = sizeof(UInt32);
return idx;
case TypeIndex::UInt64:
size = sizeof(UInt64);
return idx;
case TypeIndex::Int8:
size = sizeof(Int8);
return idx;
case TypeIndex::Int16:
size = sizeof(Int16);
return idx;
case TypeIndex::Int32:
size = sizeof(Int32);
return idx;
case TypeIndex::Int64:
size = sizeof(Int64);
return idx;
//case TypeIndex::Int128:
case TypeIndex::Float32:
size = sizeof(Float32);
return idx;
case TypeIndex::Float64:
size = sizeof(Float64);
return idx;
case TypeIndex::Decimal32:
size = sizeof(Decimal32);
return idx;
case TypeIndex::Decimal64:
size = sizeof(Decimal64);
return idx;
case TypeIndex::Decimal128:
size = sizeof(Decimal128);
return idx;
case TypeIndex::DateTime64:
size = sizeof(DateTime64);
return idx;
2020-06-01 09:38:46 +00:00
default:
break;
}
2019-04-01 16:44:15 +00:00
throw Exception("ASOF join not supported for type: " + std::string(asof_column.getFamilyName()), ErrorCodes::BAD_TYPE_OF_FIELD);
2019-03-30 21:30:21 +00:00
}
2019-03-31 10:56:54 +00:00
}