summaryrefslogtreecommitdiffstats
path: root/yql/essentials/core/yql_sqlselect.cpp
blob: 8456f94f4e9e9cde6c8e73d6b39661a61a1c2843 (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
#include "yql_sqlselect.h"

#include <yql/essentials/core/yql_expr_optimize.h>
#include <yql/essentials/core/yql_module_helpers.h>

namespace NYql {

TExprNode::TPtr ExpandYqlTraitsFactory(
    const TExprNode::TPtr& factory,
    TExprNode::TPtr listType,
    TExprNode::TPtr extractor,
    TExprContext& ctxExpr,
    TTypeAnnotationContext& ctxTypes)
{
    TString name(factory->Child(0)->Content());

    TString module;
    if (factory->IsCallable("YqlAggFactory")) {
        module = "aggregate";
    } else if (factory->IsCallable("YqlWinFactory")) {
        module = "window";
    } else {
        YQL_ENSURE(false, "Unexpected " << factory->Content());
    }

    TString path = "/lib/yql/" + module + ".yqls";
    TString binding = name + "_traits_factory";

    TExprNode::TPtr traitsFactory = ImportDeeplyCopied(
        factory->Child(0)->Pos(ctxExpr),
        path, binding, ctxExpr, ctxTypes);
    YQL_ENSURE(traitsFactory);

    // clang-format off
    TExprNode::TPtr traits = ctxExpr.Builder(factory->Pos())
        .Apply(std::move(traitsFactory))
            .With(0, std::move(listType))
            .With(1, std::move(extractor))
        .Seal()
        .Build();
    // clang-format on

    ctxExpr.Step.Repeat(TExprStep::ExpandApplyForLambdas);
    auto status = ExpandApplyNoRepeat(traits, traits, ctxExpr);
    YQL_ENSURE(status == IGraphTransformer::TStatus::Ok);

    return traits;
}

size_t DefValIndex(const TExprNode::TPtr& traits) {
    if (traits->IsCallable("AggregationTraits")) {
        return 7;
    }

    if (traits->IsCallable("WindowTraits")) {
        return 5;
    }

    YQL_ENSURE(false, "unexpected " << traits->Content());
}

TExprNode::TPtr ExpandResultType(
    const TExprNode::TPtr& traits,
    const TExprNode::TPtr& body,
    TExprContext& ctxExpr)
{
    const size_t defValIndex = DefValIndex(traits);

    // clang-format off
    TExprNode::TPtr init = ctxExpr.Builder(traits->Pos())
        .Apply(traits->Child(1))
            .With(0, std::move(body))
        .Seal()
        .Build();
    // clang-format on

    TExprNode::TPtr defVal = traits->ChildPtr(defValIndex);
    const bool isDefault = !defVal->IsCallable("Null");

    TExprNode::TPtr finish;
    if (!isDefault) {
        // clang-format off
        finish = ctxExpr.Builder(traits->Pos())
            .Apply(traits->Child(defValIndex - 1))
                .With(0, std::move(init))
            .Seal()
            .Build();
        // clang-format on
    } else if (defVal->IsLambda()) {
        ctxExpr.AddError(TIssue(
            traits->Pos(ctxExpr),
            TStringBuilder()
                << "An aggregation with a lambda DefVal "
                << "is not yet supported"));
        return nullptr;
    } else {
        finish = std::move(defVal);
    }

    // clang-format off
    return ctxExpr.Builder(traits->Pos())
        .Callable("TypeOf")
            .Add(0, std::move(finish))
        .Seal()
        .Build();
    // clang-format on
}

TExprNode::TPtr ExpandSqlWindowCall(
    const TExprNode::TPtr& call,
    TExprNode::TPtr listType,
    TExprNode::TPtr keyExtractor,
    TAggsRewriter rewrite,
    TExprContext& ctxExpr,
    TTypeAnnotationContext& ctxTypes)
{
    YQL_ENSURE(call->IsCallable({"PgWindowCall", "YqlWin"}));
    const bool isYql = call->IsCallable("YqlWin");

    TString name(call->Child(0)->Content());
    SubstGlobal(name, "_", "");

    const size_t argsOffset = isYql ? 4 : 3;
    YQL_ENSURE(argsOffset <= call->ChildrenSize());
    const size_t argsCount = call->ChildrenSize() - argsOffset;
    const auto argAt = [&](size_t index) -> TExprNode::TPtr {
        return call->Child(argsOffset + index);
    };

    if (name == "rownumber" && argsCount == 0) {
        // clang-format off
        return ctxExpr.Builder(call->Pos())
            .Callable("RowNumber")
                .Add(0, std::move(listType))
            .Seal()
            .Build();
        // clang-format on
    }

    if (name == "cumedist" && argsCount == 0) {
        // clang-format off
        return ctxExpr.Builder(call->Pos())
            .Callable("CumeDist")
                .Add(0, std::move(listType))
                .List(1)
                    .List(0)
                        .Atom(0, "ansi")
                    .Seal()
                .Seal()
            .Seal()
            .Build();
        // clang-format on
    }

    if (name == "ntile" && argsCount == 1) {
        TExprNode::TPtr arg = argAt(0);

        if (!isYql) {
            // clang-format off
            arg = ctxExpr.Builder(arg->Pos())
                .Callable("Unwrap")
                    .Callable(0, "FromPg")
                        .Add(0, std::move(arg))
                    .Seal()
                .Seal()
                .Build();
            // clang-format on
        }

        // clang-format off
        return ctxExpr.Builder(call->Pos())
            .Callable("NTile")
                .Add(0, std::move(listType))
                .Add(1, std::move(arg))
            .Seal()
            .Build();
        // clang-format on
    }

    if ((name == "rank" || name == "denserank" || name == "percentrank") &&
        ((isYql && argsCount <= 1) || (!isYql && argsCount == 0)))
    {
        TStringBuf callable;
        if (name == "rank") {
            callable = "Rank";
        } else if (name == "denserank") {
            callable = "DenseRank";
        } else if (name == "percentrank") {
            callable = "PercentRank";
        } else {
            YQL_ENSURE(false, "unexpected " << name);
        }

        // clang-format off
        return ctxExpr.Builder(call->Pos())
            .Callable(callable)
                .Add(0, std::move(listType))
                .Add(1, keyExtractor)
                .List(2)
                    .List(0)
                        .Atom(0, "ansi")
                    .Seal()
                .Seal()
            .Seal()
            .Build();
        // clang-format on
    }

    if ((name == "lead" || name == "lag") &&
        ((isYql && argsCount <= 2) || (!isYql && argsCount == 1)))
    {
        TExprNode::TPtr row = ctxExpr.NewArgument(call->Pos(), "row");

        TExprNode::TPtr arg = argAt(0);
        arg = rewrite(arg, row);

        TExprNode::TPtr offset = (argsCount == 2) ? argAt(1) : nullptr;

        TExprNode::TPtr extractor = ctxExpr.NewLambda(
            arg->Pos(), ctxExpr.NewArguments(call->Pos(), {row}), std::move(arg));

        if (offset) {
            // clang-format off
            return ctxExpr.Builder(call->Pos())
                .Callable(name == "lead" ? "Lead" : "Lag")
                    .Add(0, std::move(listType))
                    .Add(1, std::move(extractor))
                    .Add(2, std::move(offset))
                .Seal()
                .Build();
            // clang-format on
        }

        // clang-format off
        return ctxExpr.Builder(call->Pos())
            .Callable(name == "lead" ? "Lead" : "Lag")
                .Add(0, std::move(listType))
                .Add(1, std::move(extractor))
            .Seal()
            .Build();
        // clang-format on
    }

    if (((name == "firstvalue" || name == "lastvalue") && argsCount == 1) ||
        (name == "nthvalue" && argsCount == 2))
    {
        TString traitName;
        if (name == "firstvalue") {
            traitName = "first_value";
        } else if (name == "lastvalue") {
            traitName = "last_value";
        } else if (name == "nthvalue") {
            traitName = "nth_value";
        } else {
            YQL_ENSURE(false, "unexpected " << name);
        }

        TString path = "/lib/yql/window.yqls";
        TString binding = traitName + "_traits_factory";

        TExprNode::TPtr factory = ImportDeeplyCopied(
            call->Pos(ctxExpr),
            path, binding, ctxExpr, ctxTypes);
        YQL_ENSURE(factory);

        TExprNode::TPtr row = ctxExpr.NewArgument(call->Pos(), "row");

        TExprNode::TPtr arg = argAt(0);
        arg = rewrite(arg, row);

        TExprNode::TPtr extractor = ctxExpr.NewLambda(
            arg->Pos(), ctxExpr.NewArguments(call->Pos(), {row}), std::move(arg));

        TExprNode::TPtr traits;
        if (argsCount == 2) {
            TExprNode::TPtr offset = argAt(1);

            if (!isYql) {
                // clang-format off
                offset = ctxExpr.Builder(offset->Pos())
                    .Callable("FromPg")
                        .Add(0, std::move(offset))
                    .Seal()
                    .Build();
                // clang-format on
            }

            // clang-format off
            traits = ctxExpr.Builder(factory->Pos())
                .Apply(std::move(factory))
                    .With(0, std::move(listType))
                    .With(1, std::move(extractor))
                    .With(2, std::move(offset))
                .Seal()
                .Build();
            // clang-format on
        } else {
            // clang-format off
            traits = ctxExpr.Builder(factory->Pos())
                .Apply(std::move(factory))
                    .With(0, std::move(listType))
                    .With(1, std::move(extractor))
                .Seal()
                .Build();
            // clang-format on
        }

        ctxExpr.Step.Repeat(TExprStep::ExpandApplyForLambdas);
        auto status = ExpandApplyNoRepeat(traits, traits, ctxExpr);
        YQL_ENSURE(status == IGraphTransformer::TStatus::Ok);

        return traits;
    }

    ctxExpr.AddError(TIssue(
        call->Pos(ctxExpr),
        TStringBuilder()
            << "A window function " << name
            << "with " << argsCount << " arguments "
            << "is not yet supported"));
    return nullptr;
}

} // namespace NYql