aboutsummaryrefslogtreecommitdiffstats
path: root/yql/essentials/public/udf/arrow/block_item_comparator.h
blob: ad803799c63bca2923135fdeee5aed49f12f6186 (plain) (blame)
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
#pragma once

#include "block_item.h"

#include <yql/essentials/public/udf/udf_ptr.h>
#include <yql/essentials/public/udf/udf_type_inspection.h>
#include <yql/essentials/public/udf/udf_type_size_check.h>

namespace NYql::NUdf {

// ABI stable
class IBlockItemComparator {
public:
    using TPtr = TUniquePtr<IBlockItemComparator>;

    virtual ~IBlockItemComparator() = default;

    virtual i64 Compare(TBlockItem lhs, TBlockItem rhs) const = 0;
    virtual bool Equals(TBlockItem lhs, TBlockItem rhs) const = 0;
    virtual bool Less(TBlockItem lhs, TBlockItem rhs) const = 0;
    inline bool Greater(TBlockItem lhs, TBlockItem rhs) const {
        return Less(rhs, lhs);
    }
};

UDF_ASSERT_TYPE_SIZE(IBlockItemComparator, 8);

template <typename TDerived, bool Nullable>
class TBlockItemComparatorBase : public IBlockItemComparator {
public:
    const TDerived* Derived() const {
        return static_cast<const TDerived*>(this);
    }

    // returns <0 if lhs < rhs
    i64 Compare(TBlockItem lhs, TBlockItem rhs) const final {
        if constexpr (Nullable) {
            if (lhs) {
                if (rhs) {
                    return Derived()->DoCompare(lhs, rhs);
                } else {
                    return +1;
                }
            } else {
                if (rhs) {
                    return -1;
                } else {
                    return 0;
                }
            }
        } else {
            return Derived()->DoCompare(lhs, rhs);
        }
    }

    bool Equals(TBlockItem lhs, TBlockItem rhs) const final {
        if constexpr (Nullable) {
            if (lhs) {
                if (rhs) {
                    return Derived()->DoEquals(lhs, rhs);
                } else {
                    return false;
                }
            } else {
                if (rhs) {
                    return false;
                } else {
                    return true;
                }
            }
        } else {
            return Derived()->DoEquals(lhs, rhs);
        }
    }

    bool Less(TBlockItem lhs, TBlockItem rhs) const final {
        if constexpr (Nullable) {
            if (lhs) {
                if (rhs) {
                    return Derived()->DoLess(lhs, rhs);
                }
                else {
                    return false;
                }
            } else {
                if (rhs) {
                    return true;
                } else {
                    return false;
                }
            }
        } else {
            return Derived()->DoLess(lhs, rhs);
        }
    }
};

template <typename T, bool Nullable>
class TFixedSizeBlockItemComparator : public TBlockItemComparatorBase<TFixedSizeBlockItemComparator<T, Nullable>, Nullable> {
public:
    i64 DoCompare(TBlockItem lhs, TBlockItem rhs) const {
        if constexpr (std::is_integral<T>::value && sizeof(T) < sizeof(i64)) {
            return i64(lhs.As<T>()) - i64(rhs.As<T>());
        } else {
            if constexpr (std::is_floating_point<T>::value) {
                if (std::isunordered(lhs.As<T>(), rhs.As<T>())) {
                    return i64(std::isnan(lhs.As<T>())) - i64(std::isnan(rhs.As<T>()));
                }
            }

            return (lhs.As<T>() > rhs.As<T>()) - (lhs.As<T>() < rhs.As<T>());
        }
    }

    bool DoEquals(TBlockItem lhs, TBlockItem rhs) const {
        if constexpr (std::is_floating_point<T>::value) {
            if (std::isunordered(lhs.As<T>(), rhs.As<T>())) {
                return std::isnan(lhs.As<T>()) == std::isnan(rhs.As<T>());
            }
        }
        return lhs.As<T>() == rhs.As<T>();
    }

    bool DoLess(TBlockItem lhs, TBlockItem rhs) const {
        if constexpr (std::is_floating_point<T>::value) {
            if (std::isunordered(lhs.As<T>(), rhs.As<T>())) {
                return std::isnan(lhs.As<T>()) < std::isnan(rhs.As<T>());
            }
        }
        return lhs.As<T>() < rhs.As<T>();
    }
};

template <bool Nullable>
class TFixedSizeBlockItemComparator<NYql::NDecimal::TInt128, Nullable> : public TBlockItemComparatorBase<TFixedSizeBlockItemComparator<NYql::NDecimal::TInt128, Nullable>, Nullable> {
public:
    i64 DoCompare(TBlockItem lhs, TBlockItem rhs) const {
        auto l = lhs.GetInt128();
        auto r = rhs.GetInt128();
        return (l > r) - (l < r);
    }

    bool DoEquals(TBlockItem lhs, TBlockItem rhs) const {
        auto l = lhs.GetInt128();
        auto r = rhs.GetInt128();
        return l == r;
    }

    bool DoLess(TBlockItem lhs, TBlockItem rhs) const {
        auto l = lhs.GetInt128();
        auto r = rhs.GetInt128();
        return l < r;
    }
};

template <typename TStringType, bool Nullable>
class TStringBlockItemComparator : public TBlockItemComparatorBase<TStringBlockItemComparator<TStringType, Nullable>, Nullable> {
public:
    i64 DoCompare(TBlockItem lhs, TBlockItem rhs) const {
        return lhs.AsStringRef().Compare(rhs.AsStringRef());
    }

    bool DoEquals(TBlockItem lhs, TBlockItem rhs) const {
        return lhs.AsStringRef() == rhs.AsStringRef();
    }

    bool DoLess(TBlockItem lhs, TBlockItem rhs) const {
        return lhs.AsStringRef() < rhs.AsStringRef();
    }
};

class TSingularTypeBlockItemComparator: public TBlockItemComparatorBase<TSingularTypeBlockItemComparator, /*Nullable=*/false> {
public:
    i64 DoCompare(TBlockItem lhs, TBlockItem rhs) const {
        Y_UNUSED(lhs, rhs);
        return 0;
    }

    bool DoEquals(TBlockItem lhs, TBlockItem rhs) const {
        Y_UNUSED(lhs, rhs);
        return true;
    }

    bool DoLess(TBlockItem lhs, TBlockItem rhs) const {
        Y_UNUSED(lhs, rhs);
        return false;
    }
};

template<typename TTzType, bool Nullable>
class TTzDateBlockItemComparator : public TBlockItemComparatorBase<TTzDateBlockItemComparator<TTzType, Nullable>, Nullable> {
    using TLayout = typename TDataType<TTzType>::TLayout;

public:
    bool DoCompare(TBlockItem lhs, TBlockItem rhs) const {
        const auto x = lhs.Get<TLayout>();
        const auto y = rhs.Get<TLayout>();

        if (x == y) {
            const auto tx = lhs.GetTimezoneId();
            const auto ty = rhs.GetTimezoneId();
            return (tx == ty) ? 0 : (tx < ty ? -1 : 1);
        }

        if (x < y) {
            return -1;
        }

        return 1;
    }

    bool DoEquals(TBlockItem lhs, TBlockItem rhs) const {
        return lhs.Get<TLayout>() == rhs.Get<TLayout>() && lhs.GetTimezoneId() == rhs.GetTimezoneId();
    }

    bool DoLess(TBlockItem lhs, TBlockItem rhs) const {
        return std::forward_as_tuple(lhs.Get<TLayout>(), lhs.GetTimezoneId()) < std::forward_as_tuple(rhs.Get<TLayout>(), rhs.GetTimezoneId());
    }
};


template <bool Nullable>
class TTupleBlockItemComparator : public TBlockItemComparatorBase<TTupleBlockItemComparator<Nullable>, Nullable> {
public:
    TTupleBlockItemComparator(TVector<std::unique_ptr<IBlockItemComparator>>&& children)
        : Children_(std::move(children))
    {}

public:
    i64 DoCompare(TBlockItem lhs, TBlockItem rhs) const {
        for (ui32 i = 0; i < Children_.size(); ++i) {
            auto res = Children_[i]->Compare(lhs.AsTuple()[i], rhs.AsTuple()[i]);
            if (res != 0) {
                return res;
            }
        }

        return 0;
    }

    bool DoEquals(TBlockItem lhs, TBlockItem rhs) const {
        for (ui32 i = 0; i < Children_.size(); ++i) {
            if (!Children_[i]->Equals(lhs.AsTuple()[i], rhs.AsTuple()[i])) {
                return false;
            }
        }

        return true;
    }

    bool DoLess(TBlockItem lhs, TBlockItem rhs) const {
        for (ui32 i = 0; i < Children_.size(); ++i) {
            auto res = Children_[i]->Compare(lhs.AsTuple()[i], rhs.AsTuple()[i]);
            if (res < 0) {
                return true;
            }

            if (res > 0) {
                return false;
            }
        }

        return false;
    }

private:
    const TVector<std::unique_ptr<IBlockItemComparator>> Children_;
};

class TExternalOptionalBlockItemComparator : public TBlockItemComparatorBase<TExternalOptionalBlockItemComparator, true> {
public:
    TExternalOptionalBlockItemComparator(std::unique_ptr<IBlockItemComparator> inner)
        : Inner_(std::move(inner))
    {}

    i64 DoCompare(TBlockItem lhs, TBlockItem rhs) const {
        return Inner_->Compare(lhs.GetOptionalValue(), rhs.GetOptionalValue());
    }

    bool DoEquals(TBlockItem lhs, TBlockItem rhs) const {
        return Inner_->Equals(lhs.GetOptionalValue(), rhs.GetOptionalValue());
    }

    bool DoLess(TBlockItem lhs, TBlockItem rhs) const {
        return Inner_->Less(lhs.GetOptionalValue(), rhs.GetOptionalValue());
    }

private:
    std::unique_ptr<IBlockItemComparator> Inner_;
};

}