mkql_timezone.cpp 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. #include "mkql_timezone.h"
  2. #include <yql/essentials/minikql/computation/mkql_computation_node_codegen.h> // Y_IGNORE
  3. #include <yql/essentials/minikql/mkql_node_builder.h>
  4. #include <yql/essentials/minikql/mkql_string_util.h>
  5. #include <yql/essentials/minikql/mkql_type_ops.h>
  6. #include <util/string/cast.h>
  7. namespace NKikimr {
  8. namespace NMiniKQL {
  9. namespace {
  10. class TTimezoneIdWrapper : public TMutableComputationNode<TTimezoneIdWrapper> {
  11. typedef TMutableComputationNode<TTimezoneIdWrapper> TBaseComputation;
  12. public:
  13. TTimezoneIdWrapper(TComputationMutables& mutables, IComputationNode* value)
  14. : TBaseComputation(mutables)
  15. , Value(value)
  16. {}
  17. NUdf::TUnboxedValuePod DoCalculate(TComputationContext& ctx) const {
  18. auto value = Value->GetValue(ctx);
  19. if (!value) {
  20. return {};
  21. }
  22. auto id = FindTimezoneId(value.AsStringRef());
  23. if (!id) {
  24. return {};
  25. }
  26. return NUdf::TUnboxedValuePod(ui16(*id));
  27. }
  28. private:
  29. void RegisterDependencies() const final {
  30. DependsOn(Value);
  31. }
  32. IComputationNode* const Value;
  33. };
  34. class TTimezoneNameWrapper : public TMutableComputationNode<TTimezoneNameWrapper> {
  35. typedef TMutableComputationNode<TTimezoneNameWrapper> TBaseComputation;
  36. public:
  37. TTimezoneNameWrapper(TComputationMutables& mutables, IComputationNode* value)
  38. : TBaseComputation(mutables)
  39. , Value(value)
  40. {}
  41. NUdf::TUnboxedValuePod DoCalculate(TComputationContext& ctx) const {
  42. auto value = Value->GetValue(ctx);
  43. if (!value) {
  44. return {};
  45. }
  46. auto name = FindTimezoneIANAName(value.Get<ui16>());
  47. if (!name) {
  48. return {};
  49. }
  50. return MakeString(*name);
  51. }
  52. private:
  53. void RegisterDependencies() const final {
  54. DependsOn(Value);
  55. }
  56. IComputationNode* const Value;
  57. };
  58. template <bool IsOptional1, bool IsOptional2>
  59. class TAddTimezoneWrapper : public TMutableCodegeneratorNode<TAddTimezoneWrapper<IsOptional1, IsOptional2>> {
  60. typedef TMutableCodegeneratorNode<TAddTimezoneWrapper<IsOptional1, IsOptional2>> TBaseComputation;
  61. public:
  62. TAddTimezoneWrapper(TComputationMutables& mutables, IComputationNode* value, IComputationNode* id)
  63. : TBaseComputation(mutables, EValueRepresentation::Embedded)
  64. , Datetime(value)
  65. , Id(id)
  66. , TimezonesCount(InitTimezones())
  67. , BlackList(GetTzBlackList())
  68. {}
  69. NUdf::TUnboxedValuePod DoCalculate(TComputationContext& ctx) const {
  70. auto value = Datetime->GetValue(ctx);
  71. if (IsOptional1 && !value) {
  72. return {};
  73. }
  74. const auto zone = Id->GetValue(ctx);
  75. if (IsOptional2 && !zone) {
  76. return {};
  77. }
  78. const auto id = zone.Get<ui16>();
  79. if (!IsValidTimezoneId(id)) {
  80. return {};
  81. }
  82. value.SetTimezoneId(id);
  83. return value.Release();
  84. }
  85. #ifndef MKQL_DISABLE_CODEGEN
  86. Value* DoGenerateGetValue(const TCodegenContext& ctx, BasicBlock*& block) const {
  87. auto& context = ctx.Codegen.GetContext();
  88. const auto setz = BasicBlock::Create(context, "setz", ctx.Func);
  89. const auto done = BasicBlock::Create(context, "done", ctx.Func);
  90. const auto value = GetNodeValue(Datetime, ctx, block);
  91. const auto result = PHINode::Create(value->getType(), 2U + (IsOptional1 ? 1U : 0U), "result", done);
  92. if (IsOptional1) {
  93. result->addIncoming(value, block);
  94. const auto good = BasicBlock::Create(context, "good", ctx.Func);
  95. BranchInst::Create(done, good, IsEmpty(value, block, context), block);
  96. block = good;
  97. }
  98. const auto tz = GetNodeValue(Id, ctx, block);
  99. const auto id = GetterFor<ui16>(tz, context, block);
  100. const auto big = CmpInst::Create(Instruction::ICmp, ICmpInst::ICMP_UGE, id, ConstantInt::get(id->getType(), TimezonesCount), "big", block);
  101. auto test = IsOptional2 ? BinaryOperator::CreateOr(IsEmpty(tz, block, context), big, "test", block) : static_cast<Value*>(big);
  102. for (const auto black : BlackList) {
  103. const auto& str = ToString(black);
  104. const auto bad = CmpInst::Create(Instruction::ICmp, ICmpInst::ICMP_EQ, id, ConstantInt::get(id->getType(), black), ("bad_" + str).c_str(), block);
  105. test = BinaryOperator::CreateOr(test, bad, ("test_" + str).c_str(), block);
  106. }
  107. result->addIncoming(ConstantInt::get(value->getType(), 0), block);
  108. BranchInst::Create(done, setz, test, block);
  109. {
  110. block = setz;
  111. const uint64_t init[] = {~0ULL, ~0xFFFFULL};
  112. const auto mask = ConstantInt::get(value->getType(), APInt(128, 2, init));
  113. const auto clean = BinaryOperator::CreateAnd(value, mask, "clean", block);
  114. const auto tzid = BinaryOperator::CreateShl(tz, ConstantInt::get(tz->getType(), 64), "tzid", block);
  115. const auto full = BinaryOperator::CreateOr(clean, tzid, "full", block);
  116. result->addIncoming(full, block);
  117. BranchInst::Create(done, block);
  118. }
  119. block = done;
  120. return result;
  121. }
  122. #endif
  123. private:
  124. void RegisterDependencies() const final {
  125. this->DependsOn(Datetime);
  126. this->DependsOn(Id);
  127. }
  128. IComputationNode* const Datetime;
  129. IComputationNode* const Id;
  130. const ui16 TimezonesCount;
  131. const std::vector<ui16> BlackList;
  132. };
  133. }
  134. IComputationNode* WrapTimezoneId(TCallable& callable, const TComputationNodeFactoryContext& ctx) {
  135. MKQL_ENSURE(callable.GetInputsCount() == 1, "Expected 1 arg");
  136. bool isOptional;
  137. const auto dataType = UnpackOptionalData(callable.GetInput(0), isOptional);
  138. MKQL_ENSURE(dataType->GetSchemeType() == NUdf::TDataType<char*>::Id, "Expected string");
  139. const auto value = LocateNode(ctx.NodeLocator, callable, 0);
  140. return new TTimezoneIdWrapper(ctx.Mutables, value);
  141. }
  142. IComputationNode* WrapTimezoneName(TCallable& callable, const TComputationNodeFactoryContext& ctx) {
  143. MKQL_ENSURE(callable.GetInputsCount() == 1, "Expected 1 arg");
  144. bool isOptional;
  145. const auto dataType = UnpackOptionalData(callable.GetInput(0), isOptional);
  146. MKQL_ENSURE(dataType->GetSchemeType() == NUdf::TDataType<ui16>::Id, "Expected Uint16");
  147. const auto value = LocateNode(ctx.NodeLocator, callable, 0);
  148. return new TTimezoneNameWrapper(ctx.Mutables, value);
  149. }
  150. IComputationNode* WrapAddTimezone(TCallable& callable, const TComputationNodeFactoryContext& ctx) {
  151. MKQL_ENSURE(callable.GetInputsCount() == 2, "Expected 2 arg");
  152. bool isOptional1;
  153. const auto dataType1 = UnpackOptionalData(callable.GetInput(0), isOptional1);
  154. MKQL_ENSURE(NUdf::GetDataTypeInfo(*dataType1->GetDataSlot()).Features & NUdf::DateType, "Expected date type");
  155. bool isOptional2;
  156. const auto dataType2 = UnpackOptionalData(callable.GetInput(1), isOptional2);
  157. MKQL_ENSURE(dataType2->GetSchemeType() == NUdf::TDataType<ui16>::Id, "Expected ui16");
  158. const auto value = LocateNode(ctx.NodeLocator, callable, 0);
  159. const auto id = LocateNode(ctx.NodeLocator, callable, 1);
  160. if (isOptional1 && isOptional2) {
  161. return new TAddTimezoneWrapper<true, true>(ctx.Mutables, value, id);
  162. } else if (isOptional1) {
  163. return new TAddTimezoneWrapper<true, false>(ctx.Mutables, value, id);
  164. } else if (isOptional2) {
  165. return new TAddTimezoneWrapper<false, true>(ctx.Mutables, value, id);
  166. } else {
  167. return new TAddTimezoneWrapper<false, false>(ctx.Mutables, value, id);
  168. }
  169. }
  170. }
  171. }