aboutsummaryrefslogtreecommitdiffstats
path: root/yql/essentials/public/purecalc/ut/test_sql.cpp
blob: 64ec760f9ec6d8b692300f42f443f096b75d3648 (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
#include <yql/essentials/public/purecalc/purecalc.h>

#include "fake_spec.h"

#include <yql/essentials/public/purecalc/ut/protos/test_structs.pb.h>

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

Y_UNIT_TEST_SUITE(TestSql) {
    using namespace NYql::NPureCalc;

    Y_UNIT_TEST(TestSqlCompile) {
        auto factory = MakeProgramFactory();

        auto sql = TString(R"(
            SELECT * FROM Input;
        )");

        UNIT_ASSERT_NO_EXCEPTION([&](){
            factory->MakePullStreamProgram(FakeIS(), FakeOS(), sql, ETranslationMode::SQL);
        }());

        UNIT_ASSERT_NO_EXCEPTION([&](){
            factory->MakePullListProgram(FakeIS(), FakeOS(), sql, ETranslationMode::SQL);
        }());

        auto program = factory->MakePushStreamProgram(FakeIS(), FakeOS(), sql, ETranslationMode::SQL);
        auto expectedIssues = TString(R"(<main>: Warning: Type annotation, code: 1030
    generated.sql:2:13: Warning: At function: PersistableRepr
        generated.sql:2:13: Warning: Persistable required. Atom, key, world, datasink, datasource, callable, resource, stream and lambda are not persistable, code: 1104
)");

        UNIT_ASSERT_VALUES_EQUAL(expectedIssues, program->GetIssues().ToString());
    }

    Y_UNIT_TEST(TestSqlCompileSingleUnnamedInput) {
        auto factory = MakeProgramFactory();

        auto sql = TString(R"(
            SELECT * FROM TABLES()
        )");

        UNIT_ASSERT_NO_EXCEPTION([&](){
            factory->MakePullStreamProgram(FakeIS(), FakeOS(), sql, ETranslationMode::SQL);
        }());

        UNIT_ASSERT_NO_EXCEPTION([&](){
            factory->MakePullListProgram(FakeIS(), FakeOS(), sql, ETranslationMode::SQL);
        }());

        UNIT_ASSERT_NO_EXCEPTION([&](){
            factory->MakePushStreamProgram(FakeIS(), FakeOS(), sql, ETranslationMode::SQL);
        }());
    }

    Y_UNIT_TEST(TestSqlCompileNamedMultiinputs) {
        auto factory = MakeProgramFactory();

        auto sql = TString(R"(
            SELECT * FROM Input0
            UNION ALL
            SELECT * FROM Input1
        )");

        UNIT_ASSERT_NO_EXCEPTION([&](){
            factory->MakePullListProgram(FakeIS(2), FakeOS(), sql, ETranslationMode::SQL);
        }());
    }

    Y_UNIT_TEST(TestSqlCompileUnnamedMultiinputs) {
        auto factory = MakeProgramFactory();

        auto sql = TString(R"(
            $t0, $t1, $t2 = PROCESS TABLES();
            SELECT * FROM $t0
            UNION ALL
            SELECT * FROM $t1
            UNION ALL
            SELECT * FROM $t2
        )");

        UNIT_ASSERT_NO_EXCEPTION([&](){
            factory->MakePullListProgram(FakeIS(3), FakeOS(), sql, ETranslationMode::SQL);
        }());
    }

    Y_UNIT_TEST(TestSqlCompileWithWarning) {
        auto factory = MakeProgramFactory();

        auto sql = TString(R"(
            $x = 1;
            $y = 2;
            SELECT $x as Name FROM Input;
        )");

        auto expectedIssues = TString(R"(generated.sql:3:13: Warning: Symbol $y is not used, code: 4527
<main>: Warning: Type annotation, code: 1030
    generated.sql:4:13: Warning: At function: PersistableRepr
        generated.sql:4:13: Warning: Persistable required. Atom, key, world, datasink, datasource, callable, resource, stream and lambda are not persistable, code: 1104
)");

        auto program = factory->MakePushStreamProgram(FakeIS(), FakeOS(), sql, ETranslationMode::SQL);
        UNIT_ASSERT_VALUES_EQUAL(expectedIssues, program->GetIssues().ToString());
    }

    Y_UNIT_TEST(TestSqlWrongTableName) {
        auto factory = MakeProgramFactory();

        auto sql = TString(R"(
            SELECT * FROM WrongTable;
        )");

        UNIT_ASSERT_EXCEPTION_CONTAINS([&](){
            factory->MakePullStreamProgram(FakeIS(), FakeOS(), sql, ETranslationMode::SQL);
        }(), TCompileError, "Failed to optimize");

        UNIT_ASSERT_EXCEPTION_CONTAINS([&](){
            factory->MakePullListProgram(FakeIS(), FakeOS(), sql, ETranslationMode::SQL);
        }(), TCompileError, "Failed to optimize");

        UNIT_ASSERT_EXCEPTION_CONTAINS([&](){
            factory->MakePushStreamProgram(FakeIS(), FakeOS(), sql, ETranslationMode::SQL);
        }(), TCompileError, "Failed to optimize");
    }

    Y_UNIT_TEST(TestAllocateLargeStringOnEvaluate) {
        auto factory = MakeProgramFactory();

        auto sql = TString(R"(
            $data = Length(EvaluateExpr("long string" || " very loooong string"));
            SELECT $data as Name FROM Input;
        )");

        UNIT_ASSERT_NO_EXCEPTION([&](){
            factory->MakePullStreamProgram(FakeIS(), FakeOS(), sql, ETranslationMode::SQL);
        }());

        UNIT_ASSERT_NO_EXCEPTION([&](){
            factory->MakePullListProgram(FakeIS(), FakeOS(), sql, ETranslationMode::SQL);
        }());

        UNIT_ASSERT_NO_EXCEPTION([&](){
            factory->MakePushStreamProgram(FakeIS(), FakeOS(), sql, ETranslationMode::SQL);
        }());
    }

    Y_UNIT_TEST(TestInvalidSql) {
        auto factory = MakeProgramFactory();

        auto sql = TString(R"(
            Just some invalid SQL;
        )");

        UNIT_ASSERT_EXCEPTION_CONTAINS([&](){
            factory->MakePullStreamProgram(FakeIS(), FakeOS(), sql, ETranslationMode::SQL);
        }(), TCompileError, "failed to parse SQL");

        UNIT_ASSERT_EXCEPTION_CONTAINS([&](){
            factory->MakePullListProgram(FakeIS(), FakeOS(), sql, ETranslationMode::SQL);
        }(), TCompileError, "failed to parse SQL");

        UNIT_ASSERT_EXCEPTION_CONTAINS([&](){
            factory->MakePushStreamProgram(FakeIS(), FakeOS(), sql, ETranslationMode::SQL);
        }(), TCompileError, "failed to parse SQL");
    }

    Y_UNIT_TEST(TestUseProcess) {
        auto factory = MakeProgramFactory();

        auto sql = TString(R"(
            $processor = ($row) -> ($row);

            PROCESS Input using $processor(TableRow());
        )");

        UNIT_ASSERT_NO_EXCEPTION([&](){
            factory->MakePullStreamProgram(FakeIS(), FakeOS(), sql, ETranslationMode::SQL);
        }());

        UNIT_ASSERT_NO_EXCEPTION([&](){
            factory->MakePullListProgram(FakeIS(), FakeOS(), sql, ETranslationMode::SQL);
        }());

        UNIT_ASSERT_NO_EXCEPTION([&](){
            factory->MakePushStreamProgram(FakeIS(), FakeOS(), sql, ETranslationMode::SQL);
        }());
    }

    Y_UNIT_TEST(TestUseCodegen) {
        auto factory = MakeProgramFactory();

        auto sql = TString(R"(
            $processor = ($row) -> {
                $lambda = EvaluateCode(LambdaCode(($row) -> ($row)));
                return $lambda($row);
            };

            PROCESS Input using $processor(TableRow());
        )");

        UNIT_ASSERT_NO_EXCEPTION([&](){
            factory->MakePullListProgram(FakeIS(), FakeOS(), sql, ETranslationMode::SQL);
        }());
    }
}