aboutsummaryrefslogtreecommitdiffstats
path: root/yql/essentials/minikql/computation/mkql_custom_list.h
blob: 0d36156efad0690b95913d5e6e2bed566553563f (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
#pragma once
#include <yql/essentials/minikql/computation/mkql_computation_node_impl.h>
#include <yql/essentials/minikql/computation/mkql_computation_node_holders.h>

namespace NKikimr {
namespace NMiniKQL {

class TCustomListValue : public TComputationValue<TCustomListValue> {
public:
    TCustomListValue(TMemoryUsageInfo* memInfo)
        : TComputationValue(memInfo)
    {
    }

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

    ui64 GetListLength() const override {
        if (!Length) {
            ui64 length = Iterator ? 1ULL : 0ULL;
            for (const auto it = Iterator ? std::move(Iterator) : NUdf::TBoxedValueAccessor::GetListIterator(*this); 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 = NUdf::TBoxedValueAccessor::GetListIterator(*this);
        HasItems = iter.Skip();
        if (*HasItems) {
            Iterator = std::move(iter);
        }
        return *HasItems;
    }

protected:
    mutable std::optional<ui64> Length;
    mutable std::optional<bool> HasItems;
    mutable NUdf::TUnboxedValue Iterator;
};

class TForwardListValue : public TCustomListValue {
public:
    class TIterator : public TComputationValue<TIterator> {
    public:
        TIterator(TMemoryUsageInfo* memInfo, NUdf::TUnboxedValue&& stream);

    private:
        bool Next(NUdf::TUnboxedValue& value) override;

        const NUdf::TUnboxedValue Stream;
    };

    TForwardListValue(TMemoryUsageInfo* memInfo, NUdf::TUnboxedValue&& stream);

private:
    NUdf::TUnboxedValue GetListIterator() const override;

    mutable NUdf::TUnboxedValue Stream;
};

class TExtendListValue : public TCustomListValue {
public:
    class TIterator : public TComputationValue<TIterator> {
    public:
        TIterator(TMemoryUsageInfo* memInfo, TUnboxedValueVector&& iters);
        ~TIterator();

    private:
        bool Next(NUdf::TUnboxedValue& value) override;
        bool Skip() override;

        const TUnboxedValueVector Iters;
        ui32 Index;
    };

    TExtendListValue(TMemoryUsageInfo* memInfo, TUnboxedValueVector&& lists);

    ~TExtendListValue();

private:
    NUdf::TUnboxedValue GetListIterator() const override;
    ui64 GetListLength() const override;
    bool HasListItems() const override;

    const TUnboxedValueVector Lists;
};

class TExtendStreamValue : public TComputationValue<TExtendStreamValue> {
public:
    using TBase = TComputationValue<TExtendStreamValue>;

    TExtendStreamValue(TMemoryUsageInfo* memInfo, TUnboxedValueVector&& lists);

    ~TExtendStreamValue();

private:
    NUdf::EFetchStatus Fetch(NUdf::TUnboxedValue& value);

    const TUnboxedValueVector Lists;
    ui32 Index = 0;
};

}
}