aboutsummaryrefslogtreecommitdiffstats
path: root/yql/essentials/minikql/comp_nodes/ut/mkql_match_recognize_ut.cpp
blob: a9fad1b6ef7eb68d082825ed55b2a57e4c1205dd (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
#include "../mkql_time_order_recover.h"
#include <yql/essentials/minikql/mkql_node.h>
#include <yql/essentials/minikql/mkql_node_cast.h>
#include <yql/essentials/minikql/mkql_program_builder.h>
#include <yql/essentials/minikql/mkql_function_registry.h>
#include <yql/essentials/minikql/computation/mkql_computation_node.h>
#include <yql/essentials/minikql/computation/mkql_computation_node_holders.h>
#include <yql/essentials/minikql/computation/mkql_computation_node_graph_saveload.h>
#include <yql/essentials/minikql/invoke_builtins/mkql_builtins.h>
#include <yql/essentials/minikql/comp_nodes/mkql_factories.h>

#include <library/cpp/testing/unittest/registar.h>

namespace NKikimr::NMiniKQL {
namespace {
    TIntrusivePtr<IRandomProvider> CreateRandomProvider() {
        return CreateDeterministicRandomProvider(1);
    }

    TIntrusivePtr<ITimeProvider> CreateTimeProvider() {
        return CreateDeterministicTimeProvider(10000000);
    }

    struct TSetup {
        TSetup(TScopedAlloc& alloc)
            : Alloc(alloc)
        {
            FunctionRegistry = CreateFunctionRegistry(CreateBuiltinRegistry());
            RandomProvider = CreateRandomProvider();
            TimeProvider = CreateTimeProvider();

            Env.Reset(new TTypeEnvironment(Alloc));
            PgmBuilder.Reset(new TProgramBuilder(*Env, *FunctionRegistry));
        }

        THolder<IComputationGraph> BuildGraph(TRuntimeNode pgm, const std::vector<TNode*>& entryPoints = std::vector<TNode*>()) {
            Explorer.Walk(pgm.GetNode(), *Env);
            TComputationPatternOpts opts(
            Alloc.Ref(),
            *Env, GetBuiltinFactory(),
            FunctionRegistry.Get(),
            NUdf::EValidateMode::None,
            NUdf::EValidatePolicy::Fail, "OFF", EGraphPerProcess::Multi);
            Pattern = MakeComputationPattern(Explorer, pgm, entryPoints, opts);
            TComputationOptsFull compOpts = opts.ToComputationOptions(*RandomProvider, *TimeProvider);
            return Pattern->Clone(compOpts);
        }

        TIntrusivePtr<IFunctionRegistry> FunctionRegistry;
        TIntrusivePtr<IRandomProvider> RandomProvider;
        TIntrusivePtr<ITimeProvider> TimeProvider;
        TScopedAlloc& Alloc;
        THolder<TTypeEnvironment> Env;
        THolder<TProgramBuilder> PgmBuilder;
        TExploringNodeVisitor Explorer;
        IComputationPattern::TPtr Pattern;
    };

    using TTestInputData = std::vector<std::tuple<i64, std::string, ui32, std::string>>;

    THolder<IComputationGraph> BuildGraph(
        TSetup& setup,
        bool streamingMode,
        const TTestInputData& input) {
        TProgramBuilder& pgmBuilder = *setup.PgmBuilder;

        auto structType = pgmBuilder.NewStructType({
            {"time", pgmBuilder.NewDataType(NUdf::TDataType<i64>::Id)},
            {"key", pgmBuilder.NewDataType(NUdf::TDataType<char*>::Id)},
            {"sum", pgmBuilder.NewDataType(NUdf::TDataType<ui32>::Id)},
            {"part", pgmBuilder.NewDataType(NUdf::TDataType<char*>::Id)}});

        TVector<TRuntimeNode> items;
        for (size_t i = 0; i < input.size(); ++i)
        {
            auto time = pgmBuilder.NewDataLiteral<i64>(std::get<0>(input[i]));
            auto key = pgmBuilder.NewDataLiteral<NUdf::EDataSlot::String>(NUdf::TStringRef(std::get<1>(input[i])));
            auto sum = pgmBuilder.NewDataLiteral<ui32>(std::get<2>(input[i]));
            auto part = pgmBuilder.NewDataLiteral<NUdf::EDataSlot::String>(NUdf::TStringRef(std::get<3>(input[i])));

            auto item = pgmBuilder.NewStruct(structType,
                {{"time", time}, {"key", key}, {"sum", sum},  {"part", part}});
            items.push_back(std::move(item));
        }

        const auto list = pgmBuilder.NewList(structType, std::move(items));
        auto inputFlow = pgmBuilder.ToFlow(list);

        TVector<TStringBuf> partitionColumns;
        TVector<std::pair<TStringBuf, TProgramBuilder::TBinaryLambda>> getMeasures = {{
            std::make_pair(
                TStringBuf("key"),
                [&](TRuntimeNode /*measureInputDataArg*/, TRuntimeNode /*matchedVarsArg*/) {
                    return pgmBuilder.NewDataLiteral<ui32>(56);
                }
        )}};
        TVector<std::pair<TStringBuf, TProgramBuilder::TTernaryLambda>> getDefines = {{
            std::make_pair(
                TStringBuf("A"),
                [&](TRuntimeNode /*inputDataArg*/, TRuntimeNode /*matchedVarsArg*/, TRuntimeNode /*currentRowIndexArg*/) {
                    return pgmBuilder.NewDataLiteral<bool>(true);
                }
        )}};

        auto pgmReturn = pgmBuilder.MatchRecognizeCore(
            inputFlow,
            [&](TRuntimeNode item) {
                return pgmBuilder.Member(item, "part");
            },
            partitionColumns,
            getMeasures,
            {
                {NYql::NMatchRecognize::TRowPatternFactor{"A", 3, 3, false, false, false}}
            },
            getDefines,
            streamingMode,
            {NYql::NMatchRecognize::EAfterMatchSkipTo::NextRow, ""}
        );

        auto graph = setup.BuildGraph(pgmReturn);
        return graph;
    }
}

Y_UNIT_TEST_SUITE(MatchRecognizeSaveLoadTest) {
    void TestWithSaveLoadImpl(bool streamingMode) {
        TScopedAlloc alloc(__LOCATION__);
        std::vector<std::tuple<ui32, i64, ui32>> result;
        TSetup setup1(alloc);

        const TTestInputData input = {
            {1000, "A", 101, "P"},
            {1001, "B", 102, "P"},
            {1002, "C", 103, "P"},      // <- match end
            {1003, "D", 103, "P"}};     // <- not processed

        auto graph1 = BuildGraph(setup1,streamingMode, input);

        auto value = graph1->GetValue();

        UNIT_ASSERT(!value.IsFinish() && value);
        auto v = value.GetElement(0).Get<ui32>();

        TString graphState = graph1->SaveGraphState();

        graph1.Reset();

        TSetup setup2(alloc);

        auto graph2 = BuildGraph(setup2, streamingMode, TTestInputData{{1003, "D", 103, "P"}});
        graph2->LoadGraphState(graphState);

        value = graph2->GetValue();
        UNIT_ASSERT(!value.IsFinish() && value);
        v = value.GetElement(0).Get<ui32>();
        UNIT_ASSERT_VALUES_EQUAL(56, v);
    }

    Y_UNIT_TEST(StreamingMode) {
        TestWithSaveLoadImpl(true);
    }

    Y_UNIT_TEST(NotStreamingMode) {
        TestWithSaveLoadImpl(false);
    }
}

} // namespace NKikimr::NMiniKQL