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
|
#include "mkql_builtins_compare.h"
#include "mkql_builtins_datetime.h"
#include "mkql_builtins_decimal.h" // Y_IGNORE
#include "mkql_builtins_string_kernels.h"
#include <yql/essentials/minikql/mkql_type_ops.h>
namespace NKikimr {
namespace NMiniKQL {
namespace {
template <typename T1, typename T2,
std::enable_if_t<std::is_integral<T1>::value && std::is_integral<T2>::value && std::is_signed<T1>::value == std::is_signed<T2>::value, bool> Aggr>
Y_FORCE_INLINE bool Less(T1 x, T2 y) {
return x < y;
}
template <typename T1, typename T2,
std::enable_if_t<std::is_integral<T1>::value && std::is_integral<T2>::value && std::is_signed<T1>::value && std::is_unsigned<T2>::value, bool> Aggr>
Y_FORCE_INLINE bool Less(T1 x, T2 y) {
return x < T1(0) || static_cast<std::make_unsigned_t<T1>>(x) < y;
}
template <typename T1, typename T2,
std::enable_if_t<std::is_integral<T1>::value && std::is_integral<T2>::value && std::is_unsigned<T1>::value && std::is_signed<T2>::value, bool> Aggr>
Y_FORCE_INLINE bool Less(T1 x, T2 y) {
return T2(0) < y && x < static_cast<std::make_unsigned_t<T2>>(y);
}
template <typename T1, typename T2,
std::enable_if_t<std::is_floating_point<T1>::value || std::is_floating_point<T2>::value, bool> Aggr>
Y_FORCE_INLINE bool Less(T1 x, T2 y) {
using F1 = std::conditional_t<std::is_floating_point<T1>::value, T1, T2>;
using F2 = std::conditional_t<std::is_floating_point<T2>::value, T2, T1>;
using FT = std::conditional_t<(sizeof(F1) > sizeof(F2)), F1, F2>;
const auto l = static_cast<FT>(x);
const auto r = static_cast<FT>(y);
if constexpr (Aggr) {
if (std::isunordered(l, r))
return !std::isnan(l);
}
return l < r;
}
#ifndef MKQL_DISABLE_CODEGEN
Value* GenLessUnsigned(Value* lhs, Value* rhs, BasicBlock* block) {
return CmpInst::Create(Instruction::ICmp, ICmpInst::ICMP_ULT, lhs, rhs, "less", block);
}
Value* GenLessSigned(Value* lhs, Value* rhs, BasicBlock* block) {
return CmpInst::Create(Instruction::ICmp, ICmpInst::ICMP_SLT, lhs, rhs, "less", block);
}
template <bool Aggr>
Value* GenLessFloats(Value* lhs, Value* rhs, BasicBlock* block);
template <>
Value* GenLessFloats<false>(Value* lhs, Value* rhs, BasicBlock* block) {
return CmpInst::Create(Instruction::FCmp, FCmpInst::FCMP_OLT, lhs, rhs, "less", block);
}
template <>
Value* GenLessFloats<true>(Value* lhs, Value* rhs, BasicBlock* block) {
const auto ult = CmpInst::Create(Instruction::FCmp, FCmpInst::FCMP_ULT, lhs, rhs, "less", block);
const auto ord = CmpInst::Create(Instruction::FCmp, FCmpInst::FCMP_ORD, ConstantFP::get(lhs->getType(), 0.0), lhs, "ordered", block);
return BinaryOperator::CreateAnd(ult, ord, "and", block);
}
template <typename T1, typename T2>
Value* GenLessIntegralLeftSigned(Value* x, Value* y, LLVMContext &context, BasicBlock* block) {
const auto zero = ConstantInt::get(x->getType(), 0);
const auto neg = CmpInst::Create(Instruction::ICmp, ICmpInst::ICMP_SLT, x, zero, "negative", block);
using T = std::conditional_t<(sizeof(std::make_unsigned_t<T1>) > sizeof(T2)), std::make_unsigned_t<T1>, T2>;
const auto comp = GenLessUnsigned(StaticCast<T1, T>(x, context, block), StaticCast<T2, T>(y, context, block), block);
return SelectInst::Create(neg, ConstantInt::getTrue(context), comp, "result", block);
}
template <typename T1, typename T2>
Value* GenLessIntegralRightSigned(Value* x, Value* y, LLVMContext &context, BasicBlock* block) {
const auto zero = ConstantInt::get(y->getType(), 0);
const auto neg = CmpInst::Create(Instruction::ICmp, ICmpInst::ICMP_SLE, y, zero, "negative", block);
using T = std::conditional_t<(sizeof(T1) > sizeof(std::make_unsigned_t<T2>)), T1, std::make_unsigned_t<T2>>;
const auto comp = GenLessUnsigned(StaticCast<T1, T>(x, context, block), StaticCast<T2, T>(y, context, block), block);
return SelectInst::Create(neg, ConstantInt::getFalse(context), comp, "result", block);
}
template <typename T1, typename T2,
std::enable_if_t<std::is_unsigned<T1>::value && std::is_unsigned<T2>::value, bool> Aggr>
inline Value* GenLess(Value* x, Value* y, LLVMContext &context, BasicBlock* block) {
using T = std::conditional_t<(sizeof(T1) > sizeof(T2)), T1, T2>;
return GenLessUnsigned(StaticCast<T1, T>(x, context, block), StaticCast<T2, T>(y, context, block), block);
}
template <typename T1, typename T2,
std::enable_if_t<std::is_signed<T1>::value && std::is_signed<T2>::value &&
std::is_integral<T1>::value && std::is_integral<T2>::value, bool> Aggr>
inline Value* GenLess(Value* x, Value* y, LLVMContext &context, BasicBlock* block) {
using T = std::conditional_t<(sizeof(T1) > sizeof(T2)), T1, T2>;
return GenLessSigned(StaticCast<T1, T>(x, context, block), StaticCast<T2, T>(y, context, block), block);
}
template <typename T1, typename T2,
std::enable_if_t<std::is_integral<T1>::value && std::is_integral<T2>::value
&& std::is_signed<T1>::value && std::is_unsigned<T2>::value, bool> Aggr>
inline Value* GenLess(Value* x, Value* y, LLVMContext &context, BasicBlock* block) {
return GenLessIntegralLeftSigned<T1, T2>(x, y, context, block);
}
template <typename T1, typename T2,
std::enable_if_t<std::is_integral<T1>::value && std::is_integral<T2>::value
&& std::is_unsigned<T1>::value && std::is_signed<T2>::value, bool> Aggr>
inline Value* GenLess(Value* x, Value* y, LLVMContext &context, BasicBlock* block) {
return GenLessIntegralRightSigned<T1, T2>(x, y, context, block);
}
template <typename T1, typename T2,
std::enable_if_t<std::is_floating_point<T1>::value || std::is_floating_point<T2>::value, bool> Aggr>
inline Value* GenLess(Value* x, Value* y, LLVMContext &context, BasicBlock* block) {
using F1 = std::conditional_t<std::is_floating_point<T1>::value, T1, T2>;
using F2 = std::conditional_t<std::is_floating_point<T2>::value, T2, T1>;
using FT = std::conditional_t<(sizeof(F1) > sizeof(F2)), F1, F2>;
return GenLessFloats<Aggr>(StaticCast<T1, FT>(x, context, block), StaticCast<T2, FT>(y, context, block), block);
}
#endif
struct TAggrLess {
static bool Simple(bool left, bool right)
{
return !left && right;
}
#ifndef MKQL_DISABLE_CODEGEN
static constexpr CmpInst::Predicate SimplePredicate = ICmpInst::ICMP_ULT;
#endif
};
template<typename TLeft, typename TRight, bool Aggr>
struct TLess : public TCompareArithmeticBinary<TLeft, TRight, TLess<TLeft, TRight, Aggr>>, public TAggrLess {
static bool Do(TLeft left, TRight right)
{
return Less<TLeft, TRight, Aggr>(left, right);
}
#ifndef MKQL_DISABLE_CODEGEN
static Value* Gen(Value* left, Value* right, const TCodegenContext& ctx, BasicBlock*& block)
{
return GenLess<TLeft, TRight, Aggr>(left, right, ctx.Codegen.GetContext(), block);
}
#endif
};
template<typename TLeft, typename TRight, typename TOutput>
struct TLessOp;
template<typename TLeft, typename TRight>
struct TLessOp<TLeft, TRight, bool> : public TLess<TLeft, TRight, false> {
static constexpr auto NullMode = TKernel::ENullMode::Default;
};
template<typename TLeft, typename TRight, bool Aggr>
struct TDiffDateLess : public TCompareArithmeticBinary<typename TLeft::TLayout, typename TRight::TLayout, TDiffDateLess<TLeft, TRight, Aggr>>, public TAggrLess {
static bool Do(typename TLeft::TLayout left, typename TRight::TLayout right)
{
return std::is_same<TLeft, TRight>::value ?
Less<typename TLeft::TLayout, typename TRight::TLayout, Aggr>(left, right):
Less<TScaledDate, TScaledDate, Aggr>(ToScaledDate<TLeft>(left), ToScaledDate<TRight>(right));
}
#ifndef MKQL_DISABLE_CODEGEN
static Value* Gen(Value* left, Value* right, const TCodegenContext& ctx, BasicBlock*& block)
{
auto& context = ctx.Codegen.GetContext();
return std::is_same<TLeft, TRight>::value ?
GenLess<typename TLeft::TLayout, typename TRight::TLayout, Aggr>(left, right, context, block):
GenLess<TScaledDate, TScaledDate, Aggr>(GenToScaledDate<TLeft>(left, context, block), GenToScaledDate<TRight>(right, context, block), context, block);
}
#endif
};
template<typename TLeft, typename TRight, typename TOutput>
struct TDiffDateLessOp;
template<typename TLeft, typename TRight>
struct TDiffDateLessOp<TLeft, TRight, NUdf::TDataType<bool>> : public TDiffDateLess<TLeft, TRight, false> {
static constexpr auto NullMode = TKernel::ENullMode::Default;
};
template<typename TLeft, typename TRight, bool Aggr>
struct TAggrTzDateLess : public TCompareArithmeticBinaryWithTimezone<TLeft, TRight, TAggrTzDateLess<TLeft, TRight, Aggr>>, public TAggrLess {
static bool Do(TLeft left, TRight right)
{
return Less<TLeft, TRight, Aggr>(left, right);
}
static bool DoTz(ui16 left, ui16 right)
{
return Less<ui16, ui16, Aggr>(left, right);
}
#ifndef MKQL_DISABLE_CODEGEN
static Value* Gen(Value* left, Value* right, const TCodegenContext& ctx, BasicBlock*& block)
{
return GenLess<TLeft, TRight, Aggr>(left, right, ctx.Codegen.GetContext(), block);
}
static Value* GenTz(Value* left, Value* right, const TCodegenContext& ctx, BasicBlock*& block)
{
return GenLess<ui16, ui16, Aggr>(left, right, ctx.Codegen.GetContext(), block);
}
#endif
};
template<NUdf::EDataSlot Slot>
struct TCustomLess : public TAggrLess {
static NUdf::TUnboxedValuePod Execute(NUdf::TUnboxedValuePod left, NUdf::TUnboxedValuePod right) {
return NUdf::TUnboxedValuePod(CompareCustomsWithCleanup<Slot>(left, right) < 0);
}
#ifndef MKQL_DISABLE_CODEGEN
static Value* Generate(Value* left, Value* right, const TCodegenContext& ctx, BasicBlock*& block)
{
auto& context = ctx.Codegen.GetContext();
const auto res = CallBinaryUnboxedValueFunction(&CompareCustoms<Slot>, Type::getInt32Ty(context), left, right, ctx.Codegen, block);
const auto comp = CmpInst::Create(Instruction::ICmp, ICmpInst::ICMP_SLT, res, ConstantInt::get(res->getType(), 0), "less", block);
ValueCleanup(EValueRepresentation::String, left, ctx, block);
ValueCleanup(EValueRepresentation::String, right, ctx, block);
return MakeBoolean(comp, context, block);
}
#endif
};
struct TDecimalLess {
static NUdf::TUnboxedValuePod Execute(const NUdf::TUnboxedValuePod& left, const NUdf::TUnboxedValuePod& right) {
const auto l = left.GetInt128();
const auto r = right.GetInt128();
return NUdf::TUnboxedValuePod(NYql::NDecimal::IsComparable(l) && NYql::NDecimal::IsComparable(r) && l < r);
}
#ifndef MKQL_DISABLE_CODEGEN
static Value* Generate(Value* left, Value* right, const TCodegenContext& ctx, BasicBlock*& block)
{
auto& context = ctx.Codegen.GetContext();
const auto l = GetterForInt128(left, block);
const auto r = GetterForInt128(right, block);
const auto lok = NDecimal::GenIsComparable(l, context, block);
const auto rok = NDecimal::GenIsComparable(r, context, block);
const auto both = BinaryOperator::CreateAnd(lok, rok, "both", block);
const auto ls = GenLessSigned(l, r, block);
const auto res = BinaryOperator::CreateAnd(both, ls, "res", block);
return MakeBoolean(res, context, block);
}
#endif
};
struct TDecimalAggrLess : public TAggrLess {
static NUdf::TUnboxedValuePod Execute(const NUdf::TUnboxedValuePod& left, const NUdf::TUnboxedValuePod& right) {
const auto l = left.GetInt128();
const auto r = right.GetInt128();
return NUdf::TUnboxedValuePod(l < r);
}
#ifndef MKQL_DISABLE_CODEGEN
static Value* Generate(Value* left, Value* right, const TCodegenContext& ctx, BasicBlock*& block)
{
auto& context = ctx.Codegen.GetContext();
const auto l = GetterForInt128(left, block);
const auto r = GetterForInt128(right, block);
const auto ls = GenLessSigned(l, r, block);
return MakeBoolean(ls, context, block);
}
#endif
};
}
void RegisterLess(IBuiltinFunctionRegistry& registry) {
const auto name = "Less";
RegisterComparePrimitive<TLess, TCompareArgsOpt>(registry, name);
RegisterCompareDatetime<TDiffDateLess, TCompareArgsOpt>(registry, name);
RegisterCompareBigDatetime<TDiffDateLess, TCompareArgsOpt>(registry, name);
RegisterCompareStrings<TCustomLess, TCompareArgsOpt>(registry, name);
RegisterCompareCustomOpt<NUdf::TDataType<NUdf::TDecimal>, NUdf::TDataType<NUdf::TDecimal>, TDecimalLess, TCompareArgsOpt>(registry, name);
const auto aggrName = "AggrLess";
RegisterAggrComparePrimitive<TLess, TCompareArgsOpt>(registry, aggrName);
RegisterAggrCompareDatetime<TDiffDateLess, TCompareArgsOpt>(registry, aggrName);
RegisterAggrCompareTzDatetime<TAggrTzDateLess, TCompareArgsOpt>(registry, aggrName);
RegisterAggrCompareBigDatetime<TDiffDateLess, TCompareArgsOpt>(registry, aggrName);
RegisterAggrCompareBigTzDatetime<TAggrTzDateLess, TCompareArgsOpt>(registry, aggrName);
RegisterAggrCompareStrings<TCustomLess, TCompareArgsOpt>(registry, aggrName);
RegisterAggrCompareCustomOpt<NUdf::TDataType<NUdf::TDecimal>, TDecimalAggrLess, TCompareArgsOpt>(registry, aggrName);
}
void RegisterLess(TKernelFamilyMap& kernelFamilyMap) {
auto family = std::make_unique<TKernelFamilyBase>();
AddNumericComparisonKernels<TLessOp>(*family);
AddDateComparisonKernels<TDiffDateLessOp>(*family);
AddDecimalComparisonKernels<TDecimalLess>(*family);
RegisterStringKernelLess(*family);
kernelFamilyMap["Less"] = std::move(family);
}
} // namespace NMiniKQL
} // namespace NKikimr
|