summaryrefslogtreecommitdiffstats
path: root/yql/essentials/sql/v1/select_yql_aggregation.cpp
blob: 8e67332f5d7e6b6649c891cca5ecf9e17dfe74af (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
#include "select_yql_aggregation.h"

#include "context.h"
#include "select_yql_window.h"

namespace NSQLTranslationV1 {

class TYqlAggregation final: public IYqlWindowLikeNode, private TYqlAggregationArgs {
public:
    TYqlAggregation(TPosition position, TYqlAggregationArgs&& args)
        : IYqlWindowLikeNode(std::move(position))
        , TYqlAggregationArgs(std::move(args))
        , Aggregation_(BuildAggregationByType(
              Type,
              GetPos(),
              /*realFunctionName=*/FunctionName,
              /*factoryName=*/FactoryName,
              Mode))
    {
    }

    bool DoInit(TContext& ctx, ISource* src) override {
        TAstListNodeImpl dummy(GetPos()); // For a legacy interopability
        if (!NSQLTranslationV1::Init(ctx, src, Args) ||
            !Aggregation_->InitAggr(ctx, /*isFactory=*/false, src, dummy, Args)) {
            return false;
        }

        Apply_ = Y();

        if (IsWindow()) {
            Apply_ = L(std::move(Apply_), "YqlAggWin");
        } else {
            Apply_ = L(std::move(Apply_), "YqlAgg");
        }

        Apply_ = L(std::move(Apply_), Factory());

        if (IsWindow()) {
            Apply_ = L(std::move(Apply_), Q(GetWindowName()));
        }

        Apply_ = L(std::move(Apply_), Options());
        Apply_ = L(std::move(Apply_), TypeStub());
        Apply_ = L(std::move(Apply_), ExtractorBody(ctx));

        return true;
    }

    TAstNode* Translate(TContext& ctx) const override {
        return Apply_->Translate(ctx);
    }

    TNodePtr DoClone() const override {
        return new TYqlAggregation(*this);
    }

private:
    TNodePtr Factory() const {
        TNodePtr factory = Y();

        if (IsWindow()) {
            factory = L(std::move(factory), "YqlWinFactory");
        } else {
            factory = L(std::move(factory), "YqlAggFactory");
        }

        factory = L(std::move(factory), Name());
        for (TNodePtr arg : Args | std::views::drop(1)) {
            factory = L(std::move(factory), std::move(arg));
        }
        return factory;
    }

    TNodePtr Name() const {
        TStringBuf name = FactoryName;
        YQL_ENSURE(name.ChopSuffix("_traits_factory"), "'" << name << "'");
        return Q(TString(name));
    }

    TNodePtr Options() const {
        TNodePtr options = Y();
        if (IsDistinct()) {
            options = L(std::move(options), Q(Y(Q("distinct"))));
        }
        return Q(std::move(options));
    }

    TNodePtr TypeStub() const {
        return Y("Void");
    }

    TNodePtr ExtractorBody(TContext& ctx) const {
        return Aggregation_->GetExtractorBody(/*many=*/false, ctx);
    }

    bool IsDistinct() const {
        return Mode == EAggregateMode::Distinct ||
               Mode == EAggregateMode::OverWindowDistinct;
    }

    bool IsWindow() const {
        return Mode == EAggregateMode::OverWindow ||
               Mode == EAggregateMode::OverWindowDistinct;
    }

    const TAggregationPtr Aggregation_;
    TNodePtr Apply_;
};

bool IsSupported(EAggregationType type) {
    return type == NORMAL ||
           type == COUNT;
}

bool IsSupported(const TVector<TNodePtr>& args) {
    return args.size() == 1;
}

bool IsSupported(TYqlAggregationArgs& args) {
    return IsSupported(args.Type) &&
           IsSupported(args.Args);
}

TNodeResult BuildYqlAggregation(TPosition position, TYqlAggregationArgs&& args) {
    if (!IsSupported(args)) {
        return std::unexpected(ESQLError::UnsupportedYqlSelect);
    }

    return TNonNull(TNodePtr(new TYqlAggregation(std::move(position), std::move(args))));
}

TNodePtr BuildYqlGrouping(TPosition position, TVector<TNodePtr> args) {
    return new TCallNodeImpl(position, "YqlGrouping", std::move(args));
}

} // namespace NSQLTranslationV1