aboutsummaryrefslogtreecommitdiffstats
path: root/yql/essentials/udfs/common/file/file_udf.cpp
blob: fa5f84a2c57188a90b9a8f9e3a2da4eeb555eeda (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
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
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
#include <yql/essentials/public/udf/udf_helpers.h>
#include <yql/essentials/utils/line_split.h>

#include <util/generic/yexception.h>
#include <util/stream/buffered.h>
#include <util/stream/file.h>
#include <util/string/cast.h>
#include <util/ysaveload.h>

#include <functional>

using namespace NKikimr;
using namespace NUdf;

extern const char ByLineFuncName[];
const char ByLineFuncName[] = "ByLines";

namespace {
    namespace Helper {
        template <class TUserType>
        inline bool ConvertToUnboxed(const IValueBuilder& valueBuilder, const TString& curLine, TUnboxedValue& result) {
            Y_UNUSED(valueBuilder);
            TUserType userType;
            if (!TryFromString<TUserType>(curLine, userType)) {
                return false;
            }
            result = TUnboxedValuePod(userType);
            return true;
        }

        template <>
        inline bool ConvertToUnboxed<const char*>(const IValueBuilder& valueBuilder, const TString& curLine, TUnboxedValue& result) {
            result = valueBuilder.NewString(curLine);
            return true;
        }

        template <>
        inline bool ConvertToUnboxed<TUtf8>(const IValueBuilder& valueBuilder, const TString& curLine, TUnboxedValue& result) {
            result = valueBuilder.NewString(curLine);
            return true;
        }

        template <>
        inline bool ConvertToUnboxed<TYson>(const IValueBuilder& valueBuilder, const TString& curLine, TUnboxedValue& result) {
            result = valueBuilder.NewString(curLine);
            return true;
        }

        template <>
        inline bool ConvertToUnboxed<TJson>(const IValueBuilder& valueBuilder, const TString& curLine, TUnboxedValue& result) {
            result = valueBuilder.NewString(curLine);
            return true;
        }

        template <typename T>
        struct TypeToTypeName {
            static const char* Name() {
                return "Unknown";
            }
        };
        template <>
        struct TypeToTypeName<bool> {
            static constexpr const char* Name() {
                return "Bool";
            }
        };
        template <>
        struct TypeToTypeName<i8> {
            static constexpr const char* Name() {
                return "Int8";
            }
        };
        template <>
        struct TypeToTypeName<ui8> {
            static constexpr const char* Name() {
                return "Uint8";
            }
        };
        template <>
        struct TypeToTypeName<i16> {
            static constexpr const char* Name() {
                return "Int16";
            }
        };
        template <>
        struct TypeToTypeName<ui16> {
            static constexpr const char* Name() {
                return "Uint16";
            }
        };
        template <>
        struct TypeToTypeName<ui32> {
            static constexpr const char* Name() {
                return "Uint32";
            }
        };
        template <>
        struct TypeToTypeName<ui64> {
            static constexpr const char* Name() {
                return "Uint64";
            }
        };
        template <>
        struct TypeToTypeName<i32> {
            static constexpr const char* Name() {
                return "Int32";
            }
        };
        template <>
        struct TypeToTypeName<i64> {
            static constexpr const char* Name() {
                return "Int64";
            }
        };
        template <>
        struct TypeToTypeName<float> {
            static constexpr const char* Name() {
                return "Float";
            }
        };
        template <>
        struct TypeToTypeName<double> {
            static constexpr const char* Name() {
                return "Double";
            }
        };
        template <>
        struct TypeToTypeName<const char*> {
            static constexpr const char* Name() {
                return "String";
            }
        };
        template <>
        struct TypeToTypeName<TUtf8> {
            static constexpr const char* Name() {
                return "Utf8";
            }
        };
        template <>
        struct TypeToTypeName<TYson> {
            static constexpr const char* Name() {
                return "Yson";
            }
        };
        template <>
        struct TypeToTypeName<TJson> {
            static constexpr const char* Name() {
                return "Json";
            }
        };
    }

    static const ui64 TAKE_UNLIM = -1;

    bool SkipElements(IBoxedValue& iter, ui64 skip) {
        for (; skip > 0; --skip) {
            if (!TBoxedValueAccessor::Skip(iter)) {
                return false;
            }
        }
        return true;
    }

    typedef std::function<void(const TString& message)> TTerminateFunc;

    class TStreamMeta: public TThrRefBase {
    public:
        typedef TBuffered<TUnbufferedFileInput> TStream;
        typedef TIntrusivePtr<TStreamMeta> TPtr;

        TStreamMeta(TString filePath)
            : FilePath(filePath)
        {
            // work in greedy mode to catch error on creation
            Cached = DoCreateStream();
        }

        std::unique_ptr<TStream> CreateStream(TTerminateFunc terminateFunc) {
            if (Cached) {
                return std::move(Cached);
            }

            terminateFunc("The file iterator was already created. To scan file data multiple times please use ListCollect either over ParseFile or over some lazy function over it, e.g. ListMap");
            Y_ABORT("Terminate unstoppable!");
        }

        bool GetLinesCount(ui64& count) const {
            if (LinesCount == Unknown)
                return false;
            count = LinesCount;
            return true;
        }
        void SetLinesCount(ui64 count) {
            Y_DEBUG_ABORT_UNLESS(LinesCount == Unknown || count == LinesCount, "Set another value of count lines");
            if (LinesCount == Unknown) {
                LinesCount = count;
            }
        }

        const TString& GetFilePath() const {
            return FilePath;
        }

    private:
        std::unique_ptr<TStream> DoCreateStream() {
            static const auto bufferSize = 1 << 12;
            TFile file(FilePath, OpenExisting | RdOnly | Seq);
            if (FileSize == Unknown) {
                FileSize = file.GetLength();
            }
            return std::make_unique<TBuffered<TUnbufferedFileInput>>(bufferSize, file);
        }

        TString FilePath;
        static const ui64 Unknown = -1;
        ui64 FileSize = Unknown;
        ui64 LinesCount = Unknown;
        std::unique_ptr<TStream> Cached;
    };

    class TEmptyIter: public TBoxedValue {
    private:
        bool Skip() override {
            return false;
        }
        bool Next(TUnboxedValue&) override {
            return false;
        }

    public:
        TEmptyIter(TTerminateFunc terminateFunc)
            : TerminateFunc(terminateFunc)
        {
        }

    private:
        const TTerminateFunc TerminateFunc;
    };

    template <class TUserType>
    class TLineByLineBoxedValueIterator: public TBoxedValue {
    public:
        TLineByLineBoxedValueIterator(TStreamMeta::TPtr metaPtr, std::unique_ptr<TStreamMeta::TStream>&& stream, const IValueBuilder& valueBuilder, TTerminateFunc terminateFunc)
            : MetaPtr(metaPtr)
            , ValueBuilder(valueBuilder)
            , Stream(std::move(stream))
            , Splitter(*Stream)
            , TerminateFunc(terminateFunc)
        {
        }

        void SetLimit(ui64 limit = TAKE_UNLIM) {
            Limit = limit;
        }

    private:
        bool SkipLimit() {
            if (Limit != TAKE_UNLIM) {
                if (Limit == 0) {
                    return false;
                }
                --Limit;
            }
            return true;
        }

        bool Skip() final {
            ++CurLineNum;
            return Splitter.Next(CurLine) && SkipLimit();
        }

        bool Next(TUnboxedValue& value) override {
            if (!Skip()) {
                return false;
            }
            if (!Helper::ConvertToUnboxed<TUserType>(ValueBuilder, CurLine, value)) {
                TStringBuilder sb;
                sb << "File::ByLines failed to cast string '" << CurLine << "' to " << Helper::TypeToTypeName<TUserType>::Name() << Endl;
                sb << "- path: " << MetaPtr->GetFilePath() << Endl;
                sb << "- line: " << CurLineNum << Endl;
                TerminateFunc(sb);
                Y_ABORT("Terminate unstoppable!");
            }
            return true;
        }

        TStreamMeta::TPtr MetaPtr;
        const IValueBuilder& ValueBuilder;

        std::unique_ptr<TStreamMeta::TStream> Stream;
        TLineSplitter Splitter;
        TTerminateFunc TerminateFunc;
        TString CurLine;
        ui64 CurLineNum = 0;
        ui64 Limit = TAKE_UNLIM;
        TUnboxedValue Result;
    };

    template <class TUserType>
    class TListByLineBoxedValue: public TBoxedValue {
    public:
        TListByLineBoxedValue(TStreamMeta::TPtr metaPtr, const IValueBuilder& valueBuilder, TTerminateFunc terminateFunc, ui64 skip = 0ULL, ui64 take = TAKE_UNLIM)
            : MetaPtr(metaPtr)
            , ValueBuilder(valueBuilder)
            , TerminateFunc(terminateFunc)
            , Skip(skip)
            , Take(take)
        {}
    private:
        bool HasFastListLength() const override {
            ui64 tmp;
            return MetaPtr->GetLinesCount(tmp);
        }
        ui64 GetListLength() const override {
            ui64 length;
            if (!MetaPtr->GetLinesCount(length)) {
                length = Skip;
                for (const auto iter = GetListIterator(); iter.Skip(); ++length)
                    continue;
                if (Take == TAKE_UNLIM) {
                    MetaPtr->SetLinesCount(length);
                }
            }
            if (length <= Skip) {
                return 0;
            }
            return Min(length - Skip, Take);
        }
        ui64 GetEstimatedListLength() const override {
            /// \todo some optimisation?
            return GetListLength();
        }

        TUnboxedValue GetListIterator() const override {
            try {
                auto stream = MetaPtr->CreateStream(TerminateFunc);
                IBoxedValuePtr iter(new TLineByLineBoxedValueIterator<TUserType>(MetaPtr, std::move(stream), ValueBuilder, TerminateFunc));
                if (!Take || !SkipElements(*iter, Skip)) {
                    return TUnboxedValuePod(new TEmptyIter(TerminateFunc));
                }
                static_cast<TLineByLineBoxedValueIterator<TUserType>*>(iter.Get())->SetLimit(Take);
                return TUnboxedValuePod(std::move(iter));
            } catch (const std::exception& e) {
                TerminateFunc(CurrentExceptionMessage());
                Y_ABORT("Terminate unstoppable!");
            }
        }

        IBoxedValuePtr SkipListImpl(const IValueBuilder& builder, ui64 count) const override {
            return new TListByLineBoxedValue(MetaPtr, builder, TerminateFunc, Skip + count, Take == TAKE_UNLIM ? TAKE_UNLIM : Take - std::min(Take, count));
        }
        IBoxedValuePtr TakeListImpl(const IValueBuilder& builder, ui64 count) const override {
            return new TListByLineBoxedValue(MetaPtr, builder, TerminateFunc, Skip, std::min(Take, count));
        }

        bool HasListItems() const override {
            return true;
        }

        TStreamMeta::TPtr MetaPtr;
        const IValueBuilder& ValueBuilder;
        TTerminateFunc TerminateFunc;
        ui64 Skip = 0ULL;
        ui64 Take = TAKE_UNLIM;
    };

    template <class TUserType>
    class TByLinesFunc: public TBoxedValue {
    private:
        TSourcePosition Pos_;

        TByLinesFunc(TSourcePosition pos)
            : Pos_(pos)
        {}

        TUnboxedValue Run(const IValueBuilder* valueBuilder, const TUnboxedValuePod* args) const override {
            try {
                TString filePath(args[0].AsStringRef());
                TStreamMeta::TPtr metaPtr(new TStreamMeta(filePath));
                auto pos = Pos_;
                auto terminateFunc = [pos](const TString& message) {
                    UdfTerminate((TStringBuilder() << pos << " " << message).data());
                };
                return TUnboxedValuePod(new TListByLineBoxedValue<TUserType>(metaPtr, *valueBuilder, terminateFunc));
            } catch (const std::exception& e) {
                UdfTerminate((TStringBuilder() << Pos_ << " " << e.what()).data());
            }
        }

    public:
        static void DeclareSignature(
            TStringRef name,
            TType* userType,
            IFunctionTypeInfoBuilder& builder,
            bool typesOnly)
        {
            Y_UNUSED(name);
            builder.UserType(userType);
            builder.SimpleSignature<TListType<TUserType>(char*)>();
            if (!typesOnly) {
                builder.Implementation(new TByLinesFunc<TUserType>(builder.GetSourcePosition()));
            }
        }
    };

    class TFolderListFromFile: public TBoxedValue {
    private:
        class TIterator : public TBoxedValue {
        public:
            TIterator(ui32 indexP, ui32 indexT, ui32 indexA, const IValueBuilder& valueBuilder, const TSourcePosition& pos, TString filePath)
                : IndexP_(indexP)
                , IndexT_(indexT)
                , IndexA_(indexA)
                , ValueBuilder_(valueBuilder)
                , Pos_(pos)
                , Input_(filePath)
            {
            }

        private:
            bool Next(NUdf::TUnboxedValue& value) override {
                try {
                    TString type;
                    TString path;
                    TString attrs;
                    ::Load(&Input_, type);
                    if (!type) {
                        return false;
                    }
                    ::Load(&Input_, path);
                    ::Load(&Input_, attrs);

                    NUdf::TUnboxedValue* items = nullptr;
                    value = ValueBuilder_.NewArray(3, items);
                    items[IndexT_] = ValueBuilder_.NewString(type);
                    items[IndexP_] = ValueBuilder_.NewString(path);
                    items[IndexA_] = ValueBuilder_.NewString(attrs);
                }
                catch (const std::exception& e) {
                    UdfTerminate((TStringBuilder() << Pos_ << " " << e.what()).data());
                }
                return true;
            }

        private:
            const ui32 IndexP_;
            const ui32 IndexT_;
            const ui32 IndexA_;
            const IValueBuilder& ValueBuilder_;
            const TSourcePosition Pos_;
            TIFStream Input_;
        };

        class TList: public TBoxedValue {
        public:
            TList(ui32 indexP, ui32 indexT, ui32 indexA, const IValueBuilder& valueBuilder, const TSourcePosition& pos, TString filePath)
                : IndexP_(indexP)
                , IndexT_(indexT)
                , IndexA_(indexA)
                , ValueBuilder_(valueBuilder)
                , Pos_(pos)
                , FilePath_(std::move(filePath))
            {
            }

        protected:
            NUdf::TUnboxedValue GetListIterator() const override {
                return NUdf::TUnboxedValuePod(new TIterator(IndexP_, IndexT_, IndexA_, ValueBuilder_, Pos_, FilePath_));
            }

            bool HasFastListLength() const override {
                return bool(Length);
            }

            ui64 GetListLength() const override {
                if (!Length) {
                    ui64 length = 0ULL;
                    for (const auto it = GetListIterator(); it.Skip();) {
                        ++length;
                    }

                    Length = length;
                }

                return *Length;
            }

            ui64 GetEstimatedListLength() const override {
                return GetListLength();
            }

            bool HasListItems() const override {
                if (HasItems) {
                    return *HasItems;
                }

                if (Length) {
                    HasItems = (*Length != 0);
                    return *HasItems;
                }

                auto iter = GetListIterator();
                HasItems = iter.Skip();
                return *HasItems;
            }

        protected:
            const ui32 IndexP_;
            const ui32 IndexT_;
            const ui32 IndexA_;
            const IValueBuilder& ValueBuilder_;
            const TSourcePosition Pos_;
            const TString FilePath_;
            mutable TMaybe<ui64> Length;
            mutable TMaybe<bool> HasItems;
        };

    public:
        TFolderListFromFile(ui32 indexP, ui32 indexT, ui32 indexA, const TSourcePosition& pos)
            : IndexP_(indexP)
            , IndexT_(indexT)
            , IndexA_(indexA)
            , Pos_(pos)
        {
        }

        static const ::NYql::NUdf::TStringRef& Name() {
            static auto name = ::NYql::NUdf::TStringRef::Of("FolderListFromFile");
            return name;
        }

        TUnboxedValue Run(const IValueBuilder* valueBuilder, const TUnboxedValuePod* args) const override {
            try {
                TString filePath(args[0].AsStringRef());
                return TUnboxedValuePod(new TList(IndexP_, IndexT_, IndexA_, *valueBuilder, Pos_, filePath));
            } catch (const std::exception& e) {
                UdfTerminate((TStringBuilder() << Pos_ << " " << e.what()).data());
            }
        }

        static bool DeclareSignature(const TStringRef& name, TType* userType, IFunctionTypeInfoBuilder& builder, bool typesOnly) {
            if (Name() != name) {
                // the only case when we return false
                return false;
            }

            builder.UserType(userType);

            ui32 indexP, indexT, indexA;
            auto itemType = builder.Struct()
                ->AddField<const char*>("Path", &indexP)
                .AddField<const char*>("Type", &indexT)
                .AddField<TYson>("Attributes", &indexA)
                .Build();
            auto resultType = builder.List()->Item(itemType).Build();

            builder.Args()->Add<const char*>().Done().Returns(resultType);
            if (!typesOnly) {
                builder.Implementation(new TFolderListFromFile(indexP, indexT, indexA, builder.GetSourcePosition()));
            }
            return true;
        }

    private:
        const ui32 IndexP_;
        const ui32 IndexT_;
        const ui32 IndexA_;
        const TSourcePosition Pos_;
    };

    SIMPLE_MODULE(TFileModule,
        TUserDataTypeFuncFactory<false, false, ByLineFuncName, TByLinesFunc, const char*, TUtf8, TYson, TJson, i8, ui8, i16, ui16, ui32, ui64, i32, i64, float, double, bool>,
        TFolderListFromFile
    )

}

REGISTER_MODULES(TFileModule)