123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419 |
- #pragma once
- #include "mkql_builtins_impl.h" // Y_IGNORE // Y_IGNORE
- namespace NKikimr {
- namespace NMiniKQL {
- using TScaledDate = i64;
- constexpr TScaledDate DateScale = 86400000000ll;
- constexpr TScaledDate DatetimeScale = 1000000ll;
- template<typename TSrc> inline
- TScaledDate ToScaledDate(typename TSrc::TLayout src);
- template<typename TDst> inline
- typename TDst::TLayout FromScaledDate(TScaledDate src);
- template<> inline
- TScaledDate ToScaledDate<NUdf::TDataType<NUdf::TDate>>(typename NUdf::TDataType<NUdf::TDate>::TLayout src) {
- return src * DateScale;
- }
- template<> inline
- NUdf::TDataType<NUdf::TDate>::TLayout FromScaledDate<NUdf::TDataType<NUdf::TDate>>(TScaledDate src) {
- return src / DateScale;
- }
- template<> inline
- TScaledDate ToScaledDate<NUdf::TDataType<NUdf::TDatetime>>(typename NUdf::TDataType<NUdf::TDatetime>::TLayout src) {
- return src * DatetimeScale;
- }
- template<> inline
- NUdf::TDataType<NUdf::TDatetime>::TLayout FromScaledDate<NUdf::TDataType<NUdf::TDatetime>>(TScaledDate src) {
- return src / DatetimeScale;
- }
- template<> inline
- TScaledDate ToScaledDate<NUdf::TDataType<NUdf::TTimestamp>>(typename NUdf::TDataType<NUdf::TTimestamp>::TLayout src) {
- return src;
- }
- template<> inline
- NUdf::TDataType<NUdf::TTimestamp>::TLayout FromScaledDate<NUdf::TDataType<NUdf::TTimestamp>>(TScaledDate src) {
- return src;
- }
- template<> inline
- TScaledDate ToScaledDate<NUdf::TDataType<NUdf::TInterval>>(typename NUdf::TDataType<NUdf::TInterval>::TLayout src) {
- return src;
- }
- template<> inline
- NUdf::TDataType<NUdf::TInterval>::TLayout FromScaledDate<NUdf::TDataType<NUdf::TInterval>>(TScaledDate src) {
- return src;
- }
- template<> inline
- TScaledDate ToScaledDate<NUdf::TDataType<NUdf::TTzDate>>(typename NUdf::TDataType<NUdf::TTzDate>::TLayout src) {
- return src * DateScale;
- }
- template<> inline
- NUdf::TDataType<NUdf::TTzDate>::TLayout FromScaledDate<NUdf::TDataType<NUdf::TTzDate>>(TScaledDate src) {
- return src / DateScale;
- }
- template<> inline
- TScaledDate ToScaledDate<NUdf::TDataType<NUdf::TTzDatetime>>(typename NUdf::TDataType<NUdf::TTzDatetime>::TLayout src) {
- return src * DatetimeScale;
- }
- template<> inline
- NUdf::TDataType<NUdf::TTzDatetime>::TLayout FromScaledDate<NUdf::TDataType<NUdf::TTzDatetime>>(TScaledDate src) {
- return src / DatetimeScale;
- }
- template<> inline
- TScaledDate ToScaledDate<NUdf::TDataType<NUdf::TTzTimestamp>>(typename NUdf::TDataType<NUdf::TTzTimestamp>::TLayout src) {
- return src;
- }
- template<> inline
- NUdf::TDataType<NUdf::TTzTimestamp>::TLayout FromScaledDate<NUdf::TDataType<NUdf::TTzTimestamp>>(TScaledDate src) {
- return src;
- }
- template<> inline
- TScaledDate ToScaledDate<NUdf::TDataType<NUdf::TDate32>>(typename NUdf::TDataType<NUdf::TDate32>::TLayout src) {
- return src * DateScale;
- }
- template<> inline
- NUdf::TDataType<NUdf::TDate32>::TLayout FromScaledDate<NUdf::TDataType<NUdf::TDate32>>(TScaledDate src) {
- return src / DateScale;
- }
- template<> inline
- TScaledDate ToScaledDate<NUdf::TDataType<NUdf::TDatetime64>>(typename NUdf::TDataType<NUdf::TDatetime64>::TLayout src) {
- return src * DatetimeScale;
- }
- template<> inline
- NUdf::TDataType<NUdf::TDatetime64>::TLayout FromScaledDate<NUdf::TDataType<NUdf::TDatetime64>>(TScaledDate src) {
- return src / DatetimeScale;
- }
- template<> inline
- TScaledDate ToScaledDate<NUdf::TDataType<NUdf::TTimestamp64>>(typename NUdf::TDataType<NUdf::TTimestamp64>::TLayout src) {
- return src;
- }
- template<> inline
- NUdf::TDataType<NUdf::TTimestamp64>::TLayout FromScaledDate<NUdf::TDataType<NUdf::TTimestamp64>>(TScaledDate src) {
- return src;
- }
- template<> inline
- TScaledDate ToScaledDate<NUdf::TDataType<NUdf::TTzDate32>>(typename NUdf::TDataType<NUdf::TTzDate32>::TLayout src) {
- return src * DateScale;
- }
- template<> inline
- NUdf::TDataType<NUdf::TTzDate32>::TLayout FromScaledDate<NUdf::TDataType<NUdf::TTzDate32>>(TScaledDate src) {
- return src / DateScale;
- }
- template<> inline
- TScaledDate ToScaledDate<NUdf::TDataType<NUdf::TTzDatetime64>>(typename NUdf::TDataType<NUdf::TTzDatetime64>::TLayout src) {
- return src * DatetimeScale;
- }
- template<> inline
- NUdf::TDataType<NUdf::TTzDatetime64>::TLayout FromScaledDate<NUdf::TDataType<NUdf::TTzDatetime64>>(TScaledDate src) {
- return src / DatetimeScale;
- }
- template<> inline
- TScaledDate ToScaledDate<NUdf::TDataType<NUdf::TTzTimestamp64>>(typename NUdf::TDataType<NUdf::TTzTimestamp64>::TLayout src) {
- return src;
- }
- template<> inline
- NUdf::TDataType<NUdf::TTzTimestamp64>::TLayout FromScaledDate<NUdf::TDataType<NUdf::TTzTimestamp64>>(TScaledDate src) {
- return src;
- }
- template<> inline
- TScaledDate ToScaledDate<NUdf::TDataType<NUdf::TInterval64>>(typename NUdf::TDataType<NUdf::TInterval64>::TLayout src) {
- return src;
- }
- template<> inline
- NUdf::TDataType<NUdf::TInterval64>::TLayout FromScaledDate<NUdf::TDataType<NUdf::TInterval64>>(TScaledDate src) {
- return src;
- }
- template<typename TDateType>
- inline bool IsBadDateTime(TScaledDate val) {
- static_assert(TDateType::Features & (NYql::NUdf::DateType | NYql::NUdf::TzDateType), "Date type expected");
- if constexpr (TDateType::Features & NYql::NUdf::BigDateType) {
- return val < NUdf::MIN_TIMESTAMP64 || val > NUdf::MAX_TIMESTAMP64;
- } else {
- return val < 0 || val >= TScaledDate(NUdf::MAX_TIMESTAMP);
- }
- }
- template<typename TDateType>
- inline bool IsBadInterval(TScaledDate val) {
- static_assert(TDateType::Features & NYql::NUdf::TimeIntervalType, "Interval type expected");
- if constexpr (TDateType::Features & NYql::NUdf::BigDateType) {
- return val < -NUdf::MAX_INTERVAL64 || val > NUdf::MAX_INTERVAL64;
- } else {
- return val <= -TScaledDate(NUdf::MAX_TIMESTAMP) || val >= TScaledDate(NUdf::MAX_TIMESTAMP);
- }
- }
- template<typename TDateType>
- inline bool IsBadScaledDate(TScaledDate val) {
- if constexpr (TDateType::Features & NYql::NUdf::TimeIntervalType) {
- return IsBadInterval<TDateType>(val);
- } else {
- return IsBadDateTime<TDateType>(val);
- }
- }
- #ifndef MKQL_DISABLE_CODEGEN
- template<typename TLayout>
- inline Value* GenIsInt64Overflow(Value* value, LLVMContext &context, BasicBlock* block) {
- if constexpr (std::is_same_v<ui64, TLayout>) {
- const auto i64Max = ConstantInt::get(value->getType(), static_cast<ui64>(std::numeric_limits<i64>::max()));
- return CmpInst::Create(Instruction::ICmp, ICmpInst::ICMP_UGT, value, i64Max, "ugt", block);
- } else {
- return ConstantInt::getFalse(context);
- }
- }
- template<typename TDateType>
- inline Value* GenIsBadDateTime(Value* val, LLVMContext &context, BasicBlock* block) {
- static_assert(TDateType::Features & (NYql::NUdf::DateType | NYql::NUdf::TzDateType), "Date type expected");
- if constexpr (TDateType::Features & NYql::NUdf::BigDateType) {
- auto lt = CmpInst::Create(Instruction::ICmp, ICmpInst::ICMP_SLT, val, ConstantInt::get(Type::getInt64Ty(context), NUdf::MIN_TIMESTAMP64), "lt", block);
- auto ge = CmpInst::Create(Instruction::ICmp, ICmpInst::ICMP_SGT, val, ConstantInt::get(Type::getInt64Ty(context), NUdf::MAX_TIMESTAMP64), "ge", block);
- return BinaryOperator::CreateOr(lt, ge, "or", block);
- } else {
- auto lt = CmpInst::Create(Instruction::ICmp, ICmpInst::ICMP_SLT, val, ConstantInt::get(Type::getInt64Ty(context), 0), "lt", block);
- auto ge = CmpInst::Create(Instruction::ICmp, ICmpInst::ICMP_SGE, val, ConstantInt::get(Type::getInt64Ty(context), NUdf::MAX_TIMESTAMP), "ge", block);
- return BinaryOperator::CreateOr(lt, ge, "or", block);
- }
- }
- template<typename TDateType>
- inline Value* GenIsBadInterval(Value* val, LLVMContext &context, BasicBlock* block) {
- static_assert(TDateType::Features & NYql::NUdf::TimeIntervalType, "Interval type expected");
- constexpr i64 lowerBound = (TDateType::Features & NYql::NUdf::BigDateType)
- ? (-NUdf::MAX_INTERVAL64 - 1)
- : -(i64)NUdf::MAX_TIMESTAMP;
- constexpr i64 upperBound = (TDateType::Features & NYql::NUdf::BigDateType)
- ? (NUdf::MAX_INTERVAL64 + 1)
- : (i64)NUdf::MAX_TIMESTAMP;
- auto le = CmpInst::Create(Instruction::ICmp, ICmpInst::ICMP_SLE, val, ConstantInt::get(Type::getInt64Ty(context), lowerBound), "le", block);
- auto ge = CmpInst::Create(Instruction::ICmp, ICmpInst::ICMP_SGE, val, ConstantInt::get(Type::getInt64Ty(context), upperBound), "ge", block);
- return BinaryOperator::CreateOr(le, ge, "or", block);
- }
- template<typename TDateType>
- inline Value* GenIsBadScaledDate(Value* val, LLVMContext &context, BasicBlock* block) {
- if constexpr (TDateType::Features & NYql::NUdf::TimeIntervalType) {
- return GenIsBadInterval<TDateType>(val, context, block);
- } else {
- return GenIsBadDateTime<TDateType>(val, context, block);
- }
- }
- template<typename TSrc> inline
- Value* GenToScaledDate(Value* value, LLVMContext &context, BasicBlock* block);
- template<typename TDst> inline
- Value* GenFromScaledDate(Value* value, LLVMContext &context, BasicBlock* block);
- template<> inline
- Value* GenToScaledDate<NUdf::TDataType<NUdf::TDate>>(Value* value, LLVMContext &context, BasicBlock* block) {
- const auto cast = StaticCast<NUdf::TDataType<NUdf::TDate>::TLayout, TScaledDate>(value, context, block);
- const auto mul = BinaryOperator::CreateMul(cast, ConstantInt::get(cast->getType(), DateScale), "mul", block);
- return mul;
- }
- template<> inline
- Value* GenFromScaledDate<NUdf::TDataType<NUdf::TDate>>(Value* value, LLVMContext &context, BasicBlock* block) {
- const auto div = BinaryOperator::CreateSDiv(value, ConstantInt::get(value->getType(), DateScale), "div", block);
- const auto cast = StaticCast<TScaledDate, NUdf::TDataType<NUdf::TDate>::TLayout>(div, context, block);
- return cast;
- }
- template<> inline
- Value* GenToScaledDate<NUdf::TDataType<NUdf::TDatetime>>(Value* value, LLVMContext &context, BasicBlock* block) {
- const auto cast = StaticCast<NUdf::TDataType<NUdf::TDatetime>::TLayout, TScaledDate>(value, context, block);
- const auto mul = BinaryOperator::CreateMul(cast, ConstantInt::get(cast->getType(), DatetimeScale), "mul", block);
- return mul;
- }
- template<> inline
- Value* GenFromScaledDate<NUdf::TDataType<NUdf::TDatetime>>(Value* value, LLVMContext &context, BasicBlock* block) {
- const auto div = BinaryOperator::CreateSDiv(value, ConstantInt::get(value->getType(), DatetimeScale), "div", block);
- const auto cast = StaticCast<TScaledDate, NUdf::TDataType<NUdf::TDatetime>::TLayout>(div, context, block);
- return cast;
- }
- template<> inline
- Value* GenToScaledDate<NUdf::TDataType<NUdf::TTimestamp>>(Value* value, LLVMContext &context, BasicBlock* block) {
- return StaticCast<NUdf::TDataType<NUdf::TTimestamp>::TLayout, TScaledDate>(value, context, block);
- }
- template<> inline
- Value* GenFromScaledDate<NUdf::TDataType<NUdf::TTimestamp>>(Value* value, LLVMContext &context, BasicBlock* block) {
- return StaticCast<TScaledDate, NUdf::TDataType<NUdf::TTimestamp>::TLayout>(value, context, block);
- }
- template<> inline
- Value* GenToScaledDate<NUdf::TDataType<NUdf::TInterval>>(Value* value, LLVMContext &context, BasicBlock* block) {
- return StaticCast<NUdf::TDataType<NUdf::TInterval>::TLayout, TScaledDate>(value, context, block);
- }
- template<> inline
- Value* GenFromScaledDate<NUdf::TDataType<NUdf::TInterval>>(Value* value, LLVMContext &context, BasicBlock* block) {
- return StaticCast<TScaledDate, NUdf::TDataType<NUdf::TInterval>::TLayout>(value, context, block);
- }
- template<> inline
- Value* GenToScaledDate<NUdf::TDataType<NUdf::TTzDate>>(Value* value, LLVMContext &context, BasicBlock* block) {
- const auto cast = StaticCast<NUdf::TDataType<NUdf::TTzDate>::TLayout, TScaledDate>(value, context, block);
- const auto mul = BinaryOperator::CreateMul(cast, ConstantInt::get(cast->getType(), DateScale), "mul", block);
- return mul;
- }
- template<> inline
- Value* GenFromScaledDate<NUdf::TDataType<NUdf::TTzDate>>(Value* value, LLVMContext &context, BasicBlock* block) {
- const auto div = BinaryOperator::CreateSDiv(value, ConstantInt::get(value->getType(), DateScale), "div", block);
- const auto cast = StaticCast<TScaledDate, NUdf::TDataType<NUdf::TTzDate>::TLayout>(div, context, block);
- return cast;
- }
- template<> inline
- Value* GenToScaledDate<NUdf::TDataType<NUdf::TTzDatetime>>(Value* value, LLVMContext &context, BasicBlock* block) {
- const auto cast = StaticCast<NUdf::TDataType<NUdf::TTzDatetime>::TLayout, TScaledDate>(value, context, block);
- const auto mul = BinaryOperator::CreateMul(cast, ConstantInt::get(cast->getType(), DatetimeScale), "mul", block);
- return mul;
- }
- template<> inline
- Value* GenFromScaledDate<NUdf::TDataType<NUdf::TTzDatetime>>(Value* value, LLVMContext &context, BasicBlock* block) {
- const auto div = BinaryOperator::CreateSDiv(value, ConstantInt::get(value->getType(), DatetimeScale), "div", block);
- const auto cast = StaticCast<TScaledDate, NUdf::TDataType<NUdf::TTzDatetime>::TLayout>(div, context, block);
- return cast;
- }
- template<> inline
- Value* GenToScaledDate<NUdf::TDataType<NUdf::TTzTimestamp>>(Value* value, LLVMContext &context, BasicBlock* block) {
- return StaticCast<NUdf::TDataType<NUdf::TTzTimestamp>::TLayout, TScaledDate>(value, context, block);
- }
- template<> inline
- Value* GenFromScaledDate<NUdf::TDataType<NUdf::TTzTimestamp>>(Value* value, LLVMContext &context, BasicBlock* block) {
- return StaticCast<TScaledDate, NUdf::TDataType<NUdf::TTzTimestamp>::TLayout>(value, context, block);
- }
- template<> inline
- Value* GenToScaledDate<NUdf::TDataType<NUdf::TDate32>>(Value* value, LLVMContext &context, BasicBlock* block) {
- const auto cast = StaticCast<NUdf::TDataType<NUdf::TDate32>::TLayout, TScaledDate>(value, context, block);
- const auto mul = BinaryOperator::CreateMul(cast, ConstantInt::get(cast->getType(), DateScale), "mul", block);
- return mul;
- }
- template<> inline
- Value* GenFromScaledDate<NUdf::TDataType<NUdf::TDate32>>(Value* value, LLVMContext &context, BasicBlock* block) {
- const auto div = BinaryOperator::CreateSDiv(value, ConstantInt::get(value->getType(), DateScale), "div", block);
- const auto cast = StaticCast<TScaledDate, NUdf::TDataType<NUdf::TDate32>::TLayout>(div, context, block);
- return cast;
- }
- template<> inline
- Value* GenToScaledDate<NUdf::TDataType<NUdf::TDatetime64>>(Value* value, LLVMContext &context, BasicBlock* block) {
- const auto cast = StaticCast<NUdf::TDataType<NUdf::TDatetime64>::TLayout, TScaledDate>(value, context, block);
- const auto mul = BinaryOperator::CreateMul(cast, ConstantInt::get(cast->getType(), DatetimeScale), "mul", block);
- return mul;
- }
- template<> inline
- Value* GenFromScaledDate<NUdf::TDataType<NUdf::TDatetime64>>(Value* value, LLVMContext &context, BasicBlock* block) {
- const auto div = BinaryOperator::CreateSDiv(value, ConstantInt::get(value->getType(), DatetimeScale), "div", block);
- const auto cast = StaticCast<TScaledDate, NUdf::TDataType<NUdf::TDatetime64>::TLayout>(div, context, block);
- return cast;
- }
- template<> inline
- Value* GenToScaledDate<NUdf::TDataType<NUdf::TTimestamp64>>(Value* value, LLVMContext &context, BasicBlock* block) {
- return StaticCast<NUdf::TDataType<NUdf::TTimestamp64>::TLayout, TScaledDate>(value, context, block);
- }
- template<> inline
- Value* GenFromScaledDate<NUdf::TDataType<NUdf::TTimestamp64>>(Value* value, LLVMContext &context, BasicBlock* block) {
- return StaticCast<TScaledDate, NUdf::TDataType<NUdf::TTimestamp64>::TLayout>(value, context, block);
- }
- template<> inline
- Value* GenToScaledDate<NUdf::TDataType<NUdf::TTzDate32>>(Value* value, LLVMContext &context, BasicBlock* block) {
- const auto cast = StaticCast<NUdf::TDataType<NUdf::TTzDate32>::TLayout, TScaledDate>(value, context, block);
- const auto mul = BinaryOperator::CreateMul(cast, ConstantInt::get(cast->getType(), DateScale), "mul", block);
- return mul;
- }
- template<> inline
- Value* GenFromScaledDate<NUdf::TDataType<NUdf::TTzDate32>>(Value* value, LLVMContext &context, BasicBlock* block) {
- const auto div = BinaryOperator::CreateSDiv(value, ConstantInt::get(value->getType(), DateScale), "div", block);
- const auto cast = StaticCast<TScaledDate, NUdf::TDataType<NUdf::TTzDate32>::TLayout>(div, context, block);
- return cast;
- }
- template<> inline
- Value* GenToScaledDate<NUdf::TDataType<NUdf::TTzDatetime64>>(Value* value, LLVMContext &context, BasicBlock* block) {
- const auto cast = StaticCast<NUdf::TDataType<NUdf::TTzDatetime64>::TLayout, TScaledDate>(value, context, block);
- const auto mul = BinaryOperator::CreateMul(cast, ConstantInt::get(cast->getType(), DatetimeScale), "mul", block);
- return mul;
- }
- template<> inline
- Value* GenFromScaledDate<NUdf::TDataType<NUdf::TTzDatetime64>>(Value* value, LLVMContext &context, BasicBlock* block) {
- const auto div = BinaryOperator::CreateSDiv(value, ConstantInt::get(value->getType(), DatetimeScale), "div", block);
- const auto cast = StaticCast<TScaledDate, NUdf::TDataType<NUdf::TTzDatetime64>::TLayout>(div, context, block);
- return cast;
- }
- template<> inline
- Value* GenToScaledDate<NUdf::TDataType<NUdf::TTzTimestamp64>>(Value* value, LLVMContext &context, BasicBlock* block) {
- return StaticCast<NUdf::TDataType<NUdf::TTzTimestamp64>::TLayout, TScaledDate>(value, context, block);
- }
- template<> inline
- Value* GenFromScaledDate<NUdf::TDataType<NUdf::TTzTimestamp64>>(Value* value, LLVMContext &context, BasicBlock* block) {
- return StaticCast<TScaledDate, NUdf::TDataType<NUdf::TTzTimestamp64>::TLayout>(value, context, block);
- }
- template<> inline
- Value* GenToScaledDate<NUdf::TDataType<NUdf::TInterval64>>(Value* value, LLVMContext &context, BasicBlock* block) {
- return StaticCast<NUdf::TDataType<NUdf::TInterval64>::TLayout, TScaledDate>(value, context, block);
- }
- template<> inline
- Value* GenFromScaledDate<NUdf::TDataType<NUdf::TInterval64>>(Value* value, LLVMContext &context, BasicBlock* block) {
- return StaticCast<TScaledDate, NUdf::TDataType<NUdf::TInterval64>::TLayout>(value, context, block);
- }
- #endif
- }
- }
|