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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
|
#include "mkql_fromstring.h"
#include <yql/essentials/minikql/computation/mkql_computation_node_holders.h>
#include <yql/essentials/minikql/computation/mkql_computation_node_codegen.h> // Y_IGNORE
#include <yql/essentials/minikql/mkql_node_cast.h>
#include <yql/essentials/minikql/mkql_node_builder.h>
#include <yql/essentials/minikql/invoke_builtins/mkql_builtins_decimal.h> // Y_IGNORE
#include <yql/essentials/public/udf/udf_terminator.h>
#ifndef MKQL_DISABLE_CODEGEN
Y_PRAGMA_DIAGNOSTIC_PUSH
Y_PRAGMA("GCC diagnostic ignored \"-Wreturn-type-c-linkage\"")
extern "C" NKikimr::NUdf::TUnboxedValuePod DataFromString(const NKikimr::NUdf::TUnboxedValuePod data, NKikimr::NUdf::EDataSlot slot) {
return NKikimr::NMiniKQL::ValueFromString(slot, data.AsStringRef());
}
extern "C" NYql::NDecimal::TInt128 DecimalFromString(const NKikimr::NUdf::TUnboxedValuePod decimal, ui8 precision, ui8 scale) {
return NYql::NDecimal::FromStringEx(decimal.AsStringRef(), precision, scale);
}
Y_PRAGMA_DIAGNOSTIC_POP
#endif
namespace NKikimr {
namespace NMiniKQL {
namespace {
const unsigned ERROR_FRAGMENT_LIMIT = 5000;
[[noreturn]]
void ThrowConvertError(NYql::NUdf::TStringRef data, TStringBuf type) {
TStringBuilder builder;
builder << "could not convert \"";
if (data.Size() < ERROR_FRAGMENT_LIMIT) {
builder << data << "\"";
} else {
builder << TStringBuf(data.Data(), ERROR_FRAGMENT_LIMIT) << "\" (truncated)";
}
builder << " to " << type;
UdfTerminate(builder.data());
}
template <bool IsStrict, bool IsOptional>
class TDecimalFromStringWrapper : public TMutableCodegeneratorNode<TDecimalFromStringWrapper<IsStrict, IsOptional>> {
typedef TMutableCodegeneratorNode<TDecimalFromStringWrapper<IsStrict, IsOptional>> TBaseComputation;
public:
TDecimalFromStringWrapper(TComputationMutables& mutables, IComputationNode* data, ui8 precision, ui8 scale)
: TBaseComputation(mutables, EValueRepresentation::Embedded)
, Data(data)
, Precision(precision)
, Scale(scale)
{
MKQL_ENSURE(precision > 0 && precision <= NYql::NDecimal::MaxPrecision, "Wrong precision.");
MKQL_ENSURE(scale <= precision, "Wrong scale.");
}
NUdf::TUnboxedValuePod DoCalculate(TComputationContext& ctx) const {
const auto& data = Data->GetValue(ctx);
if (IsOptional && !data) {
return NUdf::TUnboxedValuePod();
}
if (const auto v = NYql::NDecimal::FromStringEx(data.AsStringRef(), Precision, Scale); !NYql::NDecimal::IsError(v)) {
return NUdf::TUnboxedValuePod(v);
}
if constexpr (IsStrict) {
Throw(data, Precision, Scale);
} else {
return NUdf::TUnboxedValuePod();
}
}
#ifndef MKQL_DISABLE_CODEGEN
Value* DoGenerateGetValue(const TCodegenContext& ctx, BasicBlock*& block) const {
auto& context = ctx.Codegen.GetContext();
const auto valType = Type::getInt128Ty(context);
const auto psType = Type::getInt8Ty(context);
const auto valTypePtr = PointerType::getUnqual(valType);
const auto name = "DecimalFromString";
ctx.Codegen.AddGlobalMapping(name, reinterpret_cast<const void*>(&DecimalFromString));
const auto fnType = NYql::NCodegen::ETarget::Windows != ctx.Codegen.GetEffectiveTarget() ?
FunctionType::get(valType, { valType, psType, psType }, false):
FunctionType::get(Type::getVoidTy(context), { valTypePtr, valTypePtr, psType, psType }, false);
const auto func = ctx.Codegen.GetModule().getOrInsertFunction(name, fnType);
const auto zero = ConstantInt::get(valType, 0ULL);
const auto precision = ConstantInt::get(psType, Precision);
const auto scale = ConstantInt::get(psType, Scale);
const auto value = GetNodeValue(Data, ctx, block);
const auto fail = BasicBlock::Create(context, "fail", ctx.Func);
const auto good = BasicBlock::Create(context, "good", ctx.Func);
const auto ways = (IsOptional ? 1U : 0U) + (IsStrict ? 0U : 1U);
const auto last = ways > 0U ? BasicBlock::Create(context, "last", ctx.Func) : nullptr;
const auto phi = last ? PHINode::Create(valType, ways + 1U, "result", last) : nullptr;
if constexpr (IsOptional) {
phi->addIncoming(zero, block);
const auto check = CmpInst::Create(Instruction::ICmp, ICmpInst::ICMP_EQ, value, zero, "check", block);
const auto call = BasicBlock::Create(context, "call", ctx.Func);
BranchInst::Create(last, call, check, block);
block = call;
}
Value* decimal;
if (NYql::NCodegen::ETarget::Windows != ctx.Codegen.GetEffectiveTarget()) {
decimal = CallInst::Create(func, { value, precision, scale }, "from_string", block);
} else {
const auto retPtr = new AllocaInst(valType, 0U, "ret_ptr", block);
new StoreInst(value, retPtr, block);
CallInst::Create(func, { retPtr, retPtr, precision, scale }, "", block);
decimal = new LoadInst(valType, retPtr, "res", block);
}
if (Data->IsTemporaryValue())
ValueCleanup(Data->GetRepresentation(), value, ctx, block);
const auto test = NDecimal::GenIsError(decimal, context, block);
BranchInst::Create(fail, good, test, block);
{
block = fail;
if constexpr (IsStrict) {
const auto doFunc = ConstantInt::get(Type::getInt64Ty(context), GetMethodPtr(&TDecimalFromStringWrapper::Throw));
const auto doFuncType = FunctionType::get(Type::getVoidTy(context), {valType, psType, psType}, false);
const auto doFuncPtr = CastInst::Create(Instruction::IntToPtr, doFunc, PointerType::getUnqual(doFuncType), "thrower", block);
CallInst::Create(doFuncType, doFuncPtr, { value, precision, scale }, "", block);
new UnreachableInst(context, block);
} else {
phi->addIncoming(zero, block);
BranchInst::Create(last, block);
}
}
block = good;
if constexpr (IsOptional || !IsStrict) {
phi->addIncoming(SetterForInt128(decimal, block), block);
BranchInst::Create(last, block);
block = last;
return phi;
} else {
return SetterForInt128(decimal, block);
}
}
#endif
private:
void RegisterDependencies() const final {
this->DependsOn(Data);
}
[[noreturn]] static void Throw(const NUdf::TUnboxedValuePod data, ui8 precision, ui8 scale) {
const TString type = TStringBuilder() << "Decimal(" << unsigned(precision) << ", " << unsigned(scale) << ")";
ThrowConvertError(data.AsStringRef(), type);
}
IComputationNode* const Data;
const ui8 Precision, Scale;
};
template <bool IsStrict, bool IsOptional>
class TFromStringWrapper : public TMutableCodegeneratorNode<TFromStringWrapper<IsStrict, IsOptional>> {
typedef TMutableCodegeneratorNode<TFromStringWrapper<IsStrict, IsOptional>> TBaseComputation;
public:
TFromStringWrapper(TComputationMutables& mutables, IComputationNode* data, NUdf::TDataTypeId schemeType)
: TBaseComputation(mutables, GetValueRepresentation(schemeType))
, Data(data)
, SchemeType(NUdf::GetDataSlot(schemeType))
{}
NUdf::TUnboxedValue DoCalculate(TComputationContext& ctx) const {
const auto& data = Data->GetValue(ctx);
if (IsOptional && !data) {
return NUdf::TUnboxedValuePod();
}
if (const auto out = ValueFromString(SchemeType, data.AsStringRef())) {
return out;
}
if constexpr (IsStrict) {
Throw(data, SchemeType);
} else {
return NUdf::TUnboxedValuePod();
}
}
#ifndef MKQL_DISABLE_CODEGEN
Value* DoGenerateGetValue(const TCodegenContext& ctx, BasicBlock*& block) const {
auto& context = ctx.Codegen.GetContext();
const auto valType = Type::getInt128Ty(context);
const auto slotType = Type::getInt32Ty(context);
const auto valTypePtr = PointerType::getUnqual(valType);
const auto name = "DataFromString";
ctx.Codegen.AddGlobalMapping(name, reinterpret_cast<const void*>(&DataFromString));
const auto fnType = NYql::NCodegen::ETarget::Windows != ctx.Codegen.GetEffectiveTarget() ?
FunctionType::get(valType, { valType, slotType }, false):
FunctionType::get(Type::getVoidTy(context), { valTypePtr, valTypePtr, slotType }, false);
const auto func = ctx.Codegen.GetModule().getOrInsertFunction(name, fnType);
const auto zero = ConstantInt::get(valType, 0ULL);
const auto slot = ConstantInt::get(slotType, static_cast<ui32>(SchemeType));
const auto value = GetNodeValue(Data, ctx, block);
const auto fail = IsStrict ? BasicBlock::Create(context, "fail", ctx.Func) : nullptr;
const auto last = IsOptional || fail ? BasicBlock::Create(context, "last", ctx.Func) : nullptr;
const auto phi = IsOptional ? PHINode::Create(valType, 2U, "result", last) : nullptr;
if constexpr (IsOptional) {
phi->addIncoming(zero, block);
const auto check = CmpInst::Create(Instruction::ICmp, ICmpInst::ICMP_EQ, value, zero, "check", block);
const auto call = BasicBlock::Create(context, "call", ctx.Func);
BranchInst::Create(last, call, check, block);
block = call;
}
Value* data;
if (NYql::NCodegen::ETarget::Windows != ctx.Codegen.GetEffectiveTarget()) {
data = CallInst::Create(func, { value, slot }, "from_string", block);
} else {
const auto retPtr = new AllocaInst(valType, 0U, "ret_ptr", block);
new StoreInst(value, retPtr, block);
CallInst::Create(func, { retPtr, retPtr, slot }, "", block);
data = new LoadInst(valType, retPtr, "res", block);
}
if (Data->IsTemporaryValue())
ValueCleanup(Data->GetRepresentation(), value, ctx, block);
if constexpr (IsOptional) {
phi->addIncoming(data, block);
}
if constexpr (IsStrict) {
const auto test = CmpInst::Create(Instruction::ICmp, ICmpInst::ICMP_EQ, data, zero, "test", block);
BranchInst::Create(fail, last, test, block);
block = fail;
const auto doFunc = ConstantInt::get(Type::getInt64Ty(context), GetMethodPtr(&TFromStringWrapper::Throw));
const auto doFuncType = FunctionType::get(Type::getVoidTy(context), {valType, slotType}, false);
const auto doFuncPtr = CastInst::Create(Instruction::IntToPtr, doFunc, PointerType::getUnqual(doFuncType), "thrower", block);
CallInst::Create(doFuncType, doFuncPtr, { value, slot }, "", block);
new UnreachableInst(context, block);
} else if constexpr (IsOptional) {
BranchInst::Create(last, block);
}
if constexpr (IsOptional || IsStrict) {
block = last;
}
return IsOptional ? phi : data;
}
#endif
private:
void RegisterDependencies() const final {
this->DependsOn(Data);
}
[[noreturn]] static void Throw(const NUdf::TUnboxedValuePod data, NUdf::EDataSlot slot) {
ThrowConvertError(data.AsStringRef(), NUdf::GetDataTypeInfo(slot).Name);
}
IComputationNode* const Data;
const NUdf::EDataSlot SchemeType;
};
}
IComputationNode* WrapFromString(TCallable& callable, const TComputationNodeFactoryContext& ctx) {
MKQL_ENSURE(callable.GetInputsCount() >= 2, "Expected 2 args");
bool isOptional;
const auto dataType = UnpackOptionalData(callable.GetInput(0), isOptional);
MKQL_ENSURE(dataType->GetSchemeType() == NUdf::TDataType<char*>::Id || dataType->GetSchemeType() == NUdf::TDataType<NUdf::TUtf8>::Id, "Expected String");
const auto schemeTypeData = AS_VALUE(TDataLiteral, callable.GetInput(1));
const auto schemeType = schemeTypeData->AsValue().Get<ui32>();
const auto data = LocateNode(ctx.NodeLocator, callable, 0);
if (NUdf::TDataType<NUdf::TDecimal>::Id == schemeType) {
MKQL_ENSURE(callable.GetInputsCount() == 4, "Expected 4 args");
const auto precision = AS_VALUE(TDataLiteral, callable.GetInput(2))->AsValue().Get<ui8>();
const auto scale = AS_VALUE(TDataLiteral, callable.GetInput(3))->AsValue().Get<ui8>();
if (isOptional) {
return new TDecimalFromStringWrapper<false, true>(ctx.Mutables, data, precision, scale);
} else {
return new TDecimalFromStringWrapper<false, false>(ctx.Mutables, data, precision, scale);
}
} else {
MKQL_ENSURE(callable.GetInputsCount() == 2, "Expected 2 args");
if (isOptional) {
return new TFromStringWrapper<false, true>(ctx.Mutables, data, static_cast<NUdf::TDataTypeId>(schemeType));
} else {
return new TFromStringWrapper<false, false>(ctx.Mutables, data, static_cast<NUdf::TDataTypeId>(schemeType));
}
}
}
IComputationNode* WrapStrictFromString(TCallable& callable, const TComputationNodeFactoryContext& ctx) {
MKQL_ENSURE(callable.GetInputsCount() >= 2, "Expected 2 args");
bool isOptional;
const auto dataType = UnpackOptionalData(callable.GetInput(0), isOptional);
MKQL_ENSURE(dataType->GetSchemeType() == NUdf::TDataType<char*>::Id || dataType->GetSchemeType() == NUdf::TDataType<NUdf::TUtf8>::Id, "Expected String");
const auto schemeTypeData = AS_VALUE(TDataLiteral, callable.GetInput(1));
const auto schemeType = schemeTypeData->AsValue().Get<ui32>();
const auto data = LocateNode(ctx.NodeLocator, callable, 0);
if (NUdf::TDataType<NUdf::TDecimal>::Id == schemeType) {
MKQL_ENSURE(callable.GetInputsCount() == 4, "Expected 4 args");
const auto precision = AS_VALUE(TDataLiteral, callable.GetInput(2))->AsValue().Get<ui8>();
const auto scale = AS_VALUE(TDataLiteral, callable.GetInput(3))->AsValue().Get<ui8>();
if (isOptional) {
return new TDecimalFromStringWrapper<true, true>(ctx.Mutables, data, precision, scale);
} else {
return new TDecimalFromStringWrapper<true, false>(ctx.Mutables, data, precision, scale);
}
} else {
MKQL_ENSURE(callable.GetInputsCount() == 2, "Expected 2 args");
if (isOptional) {
return new TFromStringWrapper<true, true>(ctx.Mutables, data, static_cast<NUdf::TDataTypeId>(schemeType));
} else {
return new TFromStringWrapper<true, false>(ctx.Mutables, data, static_cast<NUdf::TDataTypeId>(schemeType));
}
}
}
}
}
|