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