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
|
#include "mkql_round.h"
#include <ydb/library/yql/minikql/computation/mkql_computation_node_holders.h>
#include <ydb/library/yql/minikql/computation/presort.h>
#include <ydb/library/yql/minikql/mkql_node_cast.h>
#include <ydb/library/yql/minikql/mkql_node_builder.h>
#include <ydb/library/yql/minikql/mkql_type_builder.h>
#include <ydb/library/yql/minikql/mkql_string_util.h>
#include <ydb/library/yql/public/udf/udf_data_type.h>
#include <ydb/library/yql/utils/utf8.h>
#include <algorithm>
namespace NKikimr {
namespace NMiniKQL {
using namespace NYql::NUdf;
namespace {
template<typename From, typename To>
class TRoundIntegralWrapper : public TMutableComputationNode<TRoundIntegralWrapper<From, To>> {
using TSelf = TRoundIntegralWrapper<From, To>;
using TBase = TMutableComputationNode<TSelf>;
typedef TBase TBaseComputation;
public:
TRoundIntegralWrapper(TComputationMutables& mutables, IComputationNode* source, bool down)
: TBaseComputation(mutables)
, Source(source)
, Down(down)
{
}
NUdf::TUnboxedValuePod DoCalculate(TComputationContext& ctx) const {
auto value = Source->GetValue(ctx).Get<From>();
auto toMin = std::numeric_limits<To>::min();
auto toMax = std::numeric_limits<To>::max();
if constexpr (std::is_signed<From>::value && std::is_unsigned<To>::value) {
if (value < 0) {
return Down ? TUnboxedValuePod() : TUnboxedValuePod(toMin);
}
if (value > toMax) {
return Down ? TUnboxedValuePod(toMax) : TUnboxedValuePod();
}
return TUnboxedValuePod(static_cast<To>(value));
}
if constexpr (std::is_unsigned<From>::value && std::is_signed<To>::value) {
if (value > toMax) {
return Down ? TUnboxedValuePod(toMax) : TUnboxedValuePod();
}
return TUnboxedValuePod(static_cast<To>(value));
}
if (value < toMin) {
return Down ? TUnboxedValuePod() : TUnboxedValuePod(toMin);
}
if (value > toMax) {
return Down ? TUnboxedValuePod(toMax) : TUnboxedValuePod();
}
return TUnboxedValuePod(static_cast<To>(value));
}
private:
void RegisterDependencies() const final {
this->DependsOn(Source);
}
IComputationNode* const Source;
const bool Down;
};
class TRoundDateTypeWrapper : public TMutableComputationNode<TRoundDateTypeWrapper> {
using TSelf = TRoundDateTypeWrapper;
using TBase = TMutableComputationNode<TSelf>;
typedef TBase TBaseComputation;
public:
TRoundDateTypeWrapper(TComputationMutables& mutables, IComputationNode* source, bool down, EDataSlot from, EDataSlot to)
: TBaseComputation(mutables)
, Source(source)
, Down(down)
, From(from)
, To(to)
{
}
NUdf::TUnboxedValuePod DoCalculate(TComputationContext& ctx) const {
constexpr ui64 usInDay = 86400000000ull;
constexpr ui32 usInSec = 1000000u;
ui64 us;
if (From == EDataSlot::Timestamp) {
us = Source->GetValue(ctx).Get<NUdf::TDataType<TTimestamp>::TLayout>();
} else {
Y_ENSURE(From == EDataSlot::Datetime);
us = Source->GetValue(ctx).Get<NUdf::TDataType<TDatetime>::TLayout>();
us *= usInSec;
}
TUnboxedValuePod result;
if (To == EDataSlot::Date) {
NUdf::TDataType<TTimestamp>::TLayout rounded = (us + (Down ? 0 : (usInDay - 1u))) / usInDay;
if (rounded >= MAX_DATE) {
return {};
}
result = TUnboxedValuePod(rounded);
} else {
Y_ENSURE(To == EDataSlot::Datetime);
NUdf::TDataType<TDatetime>::TLayout rounded = (us + (Down ? 0 : (usInSec - 1u))) / usInSec;
if (rounded >= MAX_DATETIME) {
return {};
}
result = TUnboxedValuePod(rounded);
}
return result;
}
private:
void RegisterDependencies() const final {
this->DependsOn(Source);
}
IComputationNode* const Source;
const bool Down;
const EDataSlot From;
const EDataSlot To;
};
class TRoundStringWrapper : public TMutableComputationNode<TRoundStringWrapper> {
using TSelf = TRoundStringWrapper;
using TBase = TMutableComputationNode<TSelf>;
typedef TBase TBaseComputation;
public:
TRoundStringWrapper(TComputationMutables& mutables, IComputationNode* source, bool down)
: TBaseComputation(mutables)
, Source(source)
, Down(down)
{
}
NUdf::TUnboxedValuePod DoCalculate(TComputationContext& ctx) const {
TUnboxedValue input = Source->GetValue(ctx);
auto output = NYql::RoundToNearestValidUtf8(input.AsStringRef(), Down);
if (!output) {
return {};
}
return MakeString(*output);
}
private:
void RegisterDependencies() const final {
this->DependsOn(Source);
}
IComputationNode* const Source;
const bool Down;
};
template<typename From>
IComputationNode* FromIntegral(TComputationMutables& mutables, IComputationNode* source, bool down, EDataSlot target) {
switch (target) {
case EDataSlot::Int8: return new TRoundIntegralWrapper<From, i8>(mutables, source, down);
case EDataSlot::Uint8: return new TRoundIntegralWrapper<From, ui8>(mutables, source, down);
case EDataSlot::Int16: return new TRoundIntegralWrapper<From, i16>(mutables, source, down);
case EDataSlot::Uint16: return new TRoundIntegralWrapper<From, ui16>(mutables, source, down);
case EDataSlot::Int32: return new TRoundIntegralWrapper<From, i32>(mutables, source, down);
case EDataSlot::Uint32: return new TRoundIntegralWrapper<From, ui32>(mutables, source, down);
case EDataSlot::Int64: return new TRoundIntegralWrapper<From, i64>(mutables, source, down);
case EDataSlot::Uint64: return new TRoundIntegralWrapper<From, ui64>(mutables, source, down);
default: Y_ENSURE(false, "Unsupported integral rounding");
}
return nullptr;
}
} // namespace
IComputationNode* WrapRound(TCallable& callable, const TComputationNodeFactoryContext& ctx) {
MKQL_ENSURE(callable.GetInputsCount() == 1, "Expecting exactly one argument");
auto type = callable.GetInput(0).GetStaticType();
MKQL_ENSURE(type->IsData(), "Expecting data as argument");
auto returnType = callable.GetType()->GetReturnType();
MKQL_ENSURE(returnType->IsOptional(), "Expecting optional as return type");
auto targetType = static_cast<TOptionalType*>(returnType)->GetItemType();
MKQL_ENSURE(targetType->IsData(), "Expecting Data as target type");
auto from = GetDataSlot(static_cast<TDataType*>(type)->GetSchemeType());
auto to = GetDataSlot(static_cast<TDataType*>(targetType)->GetSchemeType());
bool down = callable.GetType()->GetName() == "RoundDown";
auto source = LocateNode(ctx.NodeLocator, callable, 0);
switch (from) {
case EDataSlot::Int8: return FromIntegral<i8>(ctx.Mutables, source, down, to);
case EDataSlot::Uint8: return FromIntegral<ui8>(ctx.Mutables, source, down, to);
case EDataSlot::Int16: return FromIntegral<i16>(ctx.Mutables, source, down, to);
case EDataSlot::Uint16: return FromIntegral<ui16>(ctx.Mutables, source, down, to);
case EDataSlot::Int32: return FromIntegral<i32>(ctx.Mutables, source, down, to);
case EDataSlot::Uint32: return FromIntegral<ui32>(ctx.Mutables, source, down, to);
case EDataSlot::Int64: return FromIntegral<i64>(ctx.Mutables, source, down, to);
case EDataSlot::Uint64: return FromIntegral<ui64>(ctx.Mutables, source, down, to);
case EDataSlot::Datetime:
case EDataSlot::Timestamp:
Y_ENSURE(GetDataTypeInfo(to).Features & DateType);
return new TRoundDateTypeWrapper(ctx.Mutables, source, down, from, to);
case EDataSlot::String:
Y_ENSURE(to == EDataSlot::Utf8);
return new TRoundStringWrapper(ctx.Mutables, source, down);
default:
Y_ENSURE(false,
"Unsupported rounding from " << GetDataTypeInfo(from).Name << " to " << GetDataTypeInfo(to).Name);
}
return nullptr;
}
}
}
|