aboutsummaryrefslogtreecommitdiffstats
path: root/yql/essentials/core/ut/yql_expr_discover_ut.cpp
blob: bf59d252dcb77c7bd7f6ff74ab719345e96ffd5a (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
#include "yql_opt_proposed_by_data.h"

#include <contrib/ydb/library/yql/providers/yt/provider/yql_yt_provider.h>
#include <contrib/ydb/library/yql/providers/yt/gateway/file/yql_yt_file.h>
#include <contrib/ydb/library/yql/providers/yt/gateway/file/yql_yt_file_services.h>
#include <yql/essentials/providers/common/provider/yql_provider_names.h>
#include <yql/essentials/ast/yql_ast_annotation.h>
#include <yql/essentials/ast/yql_expr.h>
#include <yql/essentials/core/type_ann/type_ann_core.h>
#include <yql/essentials/core/ut_common/yql_ut_common.h>
#include <yql/essentials/minikql/invoke_builtins/mkql_builtins.h>
#include <yql/essentials/core/services/yql_eval_expr.h>

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

namespace NYql {

Y_UNIT_TEST_SUITE(TDiscoverYqlExpr) {

    static TString Discover(const TString& ast) {
        TAstParseResult astRes = ParseAst(ast);
        UNIT_ASSERT(astRes.IsOk());
        TExprContext exprCtx;
        TExprNode::TPtr exprRoot;
        UNIT_ASSERT(CompileExpr(*astRes.Root, exprRoot, exprCtx, nullptr, nullptr));

        auto functionRegistry = NKikimr::NMiniKQL::CreateFunctionRegistry(NKikimr::NMiniKQL::CreateBuiltinRegistry());
        TTestTablesMapping testTables;
        auto yqlNativeServices = NFile::TYtFileServices::Make(functionRegistry.Get(), testTables);
        auto ytGateway = CreateYtFileGateway(yqlNativeServices);
        auto typeAnnotationContext = MakeIntrusive<TTypeAnnotationContext>();
        typeAnnotationContext->DiscoveryMode = true;
        auto ytState = MakeIntrusive<TYtState>(typeAnnotationContext.Get());
        ytState->Gateway = ytGateway;

        InitializeYtGateway(ytGateway, ytState);
        auto randomProvider = CreateDeterministicRandomProvider(1);
        typeAnnotationContext->AddDataSink(YtProviderName, CreateYtDataSink(ytState));
        auto datasource = CreateYtDataSource(ytState);
        typeAnnotationContext->AddDataSource(YtProviderName, datasource);
        auto intentTransformer = CreateIntentDeterminationTransformer(*typeAnnotationContext);
        TVector<TTransformStage> transformers;
        const auto issueCode = TIssuesIds::DEFAULT_ERROR;
        transformers.push_back(TTransformStage(CreateFunctorTransformer(
            [=](const TExprNode::TPtr& input, TExprNode::TPtr& output, TExprContext& ctx) {
                return EvaluateExpression(input, output, *typeAnnotationContext, ctx, *functionRegistry, nullptr);
            }),
            "EvaluateExpression",
            issueCode));
        transformers.push_back(TTransformStage(
            CreateIODiscoveryTransformer(*typeAnnotationContext),
            "IODiscovery",
            issueCode));
        transformers.push_back(TTransformStage(
            CreateEpochsTransformer(*typeAnnotationContext),
            "Epochs",
            issueCode));
        transformers.push_back(TTransformStage(
            intentTransformer,
            "IntentDetermination",
            issueCode));
        auto fullTransformer = CreateCompositeGraphTransformer(transformers, true);

        TStringStream str;
        if (SyncTransform(*fullTransformer, exprRoot, exprCtx) == IGraphTransformer::TStatus::Ok) {
            NYson::TYsonWriter writer(&str, NYson::EYsonFormat::Text);
            datasource->CollectDiscoveredData(writer);
        } else {
            exprCtx.IssueManager.GetIssues().PrintTo(str);
        }
        return str.Str();
    }

    Y_UNIT_TEST(DiscoverYt) {
        auto s = R"((
(let mr_source (DataSource 'yt 'plato))
(let x (Read! world mr_source
    (Key '('table (String 'Input)))
    '('key 'subkey 'value) '()))
(let world (Left! x))
(let table (Right! x))

(let mr_sink (DataSink 'yt 'plato))
(let world (Write! world mr_sink
    (Key '('table (String 'Output)))
    table '('('mode 'append))))

(let world (Commit! world mr_sink))
(return world)
))";

        auto res = Discover(s);
        UNIT_ASSERT_VALUES_EQUAL(res, "[[\"plato\";\"Input\";[\"read\"]];[\"plato\";\"Output\";[\"modify\"]]]");
    }

    Y_UNIT_TEST(ErrorOnRange) {
        auto s = R"((
(let mr_source (DataSource 'yt 'plato))
(let range (MrTableRange '"" (lambda '($i) (And (>= $i (String '"Input1")) (<= $i (String '"Input2")))) '""))
(let x (Read! world mr_source
    (Key '('table range))
    '('key 'subkey 'value) '()))
(let world (Left! x))
(let table (Right! x))

(let mr_sink (DataSink 'yt 'plato))
(let world (Write! world mr_sink
    (Key '('table (String 'Output)))
    table '('('mode 'append))))

(let world (Commit! world mr_sink))
(return world)
))";

        auto res = Discover(s);
        UNIT_ASSERT_VALUES_EQUAL(res, R"(<main>: Error: Default error
    <main>:5:6: Error: MrTableRange/MrTableRangeStrict is not allowed in Discovery mode, code: 4600
)");
    }

    Y_UNIT_TEST(ErrorOnTime) {
        auto s = R"((
(let mr_source (DataSource 'yt 'plato))
(let x (Read! world mr_source
    (Key '('table (String (EvaluateAtom (SafeCast (CurrentUtcDate) (DataType 'String))))))
    '('key 'subkey 'value) '()))
(let world (Left! x))
(let table (Right! x))

(let mr_sink (DataSink 'yt 'plato))
(let world (Write! world mr_sink
    (Key '('table (String 'Output)))
    table '('('mode 'append))))

(let world (Commit! world mr_sink))
(return world)
))";

        auto res = Discover(s);
        UNIT_ASSERT_VALUES_EQUAL(res, R"(<main>: Error: Default error
    <main>:4:28: Error: At function: EvaluateAtom
        <main>: Error: Type annotation, code: 1030
            <main>:4:42: Error: At function: SafeCast
                <main>:4:52: Error: At function: CurrentUtcDate
                    <main>:4:52: Error: CurrentUtcDate is not allowed in Discovery mode, code: 4600
)");
    }
}

} // namespace NYql