aboutsummaryrefslogtreecommitdiffstats
path: root/yql/essentials/sql/v1/match_recognize.h
blob: b78c0faf65ec91fcc62855dd44380dcd47c2c9e4 (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
#pragma once
#include "node.h"
#include <yql/essentials/core/sql_types/match_recognize.h>
#include <util/generic/ptr.h>

namespace NSQLTranslationV1 {

struct TNamedFunction {
    TNodePtr callable; //Callable with some free args
    TString name;
};

enum class ERowsPerMatch {
    OneRow,
    AllRows
};

class TMatchRecognizeBuilder: public TSimpleRefCount<TMatchRecognizeBuilder> {
public:
    TMatchRecognizeBuilder(
            TPosition clausePos,
            std::pair<TPosition, TVector<TNamedFunction>>&& partitioners,
            std::pair<TPosition, TVector<TSortSpecificationPtr>>&& sortSpecs,
            std::pair<TPosition, TVector<TNamedFunction>>&& measures,
            std::pair<TPosition, ERowsPerMatch>&& rowsPerMatch,
            std::pair<TPosition, NYql::NMatchRecognize::TAfterMatchSkipTo>&& skipTo,
            std::pair<TPosition, NYql::NMatchRecognize::TRowPattern>&& pattern,
            std::pair<TPosition, TNodePtr>&& subset,
            std::pair<TPosition, TVector<TNamedFunction>>&& definitions
            )
            : Pos(clausePos)
            , Partitioners(std::move(partitioners))
            , SortSpecs(std::move(sortSpecs))
            , Measures(std::move(measures))
            , RowsPerMatch(std::move(rowsPerMatch))
            , SkipTo(std::move(skipTo))
            , Pattern(std::move(pattern))
            , Subset(std::move(subset))
            , Definitions(definitions)

    {}
    TNodePtr Build(TContext& ctx, TString&& inputTable, ISource* source);
private:
    TPosition Pos;
    std::pair<TPosition, TVector<TNamedFunction>> Partitioners;
    std::pair<TPosition, TVector<TSortSpecificationPtr>> SortSpecs;
    std::pair<TPosition, TVector<TNamedFunction>> Measures;
    std::pair<TPosition, ERowsPerMatch> RowsPerMatch;
    std::pair<TPosition, NYql::NMatchRecognize::TAfterMatchSkipTo> SkipTo;
    std::pair<TPosition, NYql::NMatchRecognize::TRowPattern> Pattern;
    std::pair<TPosition, TNodePtr> Subset;
    std::pair<TPosition, TVector<TNamedFunction>> Definitions;
};

using TMatchRecognizeBuilderPtr=TIntrusivePtr<TMatchRecognizeBuilder> ;

class TMatchRecognizeVarAccessNode: public INode {
public:
    TMatchRecognizeVarAccessNode(TPosition pos, const TString& var, const TString& column, bool theSameVar)
        : INode(pos)
        , Var(var)
        , TheSameVar(theSameVar)
        , Column(column)
    {
    }

    TString GetVar() const {
        return Var;
    }

    bool IsTheSameVar() const {
        return TheSameVar;
    }

    TString GetColumn() const {
        return Column;
    }

    bool DoInit(TContext& ctx, ISource* src) override;

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

    TPtr DoClone() const override {
        YQL_ENSURE(!Node, "TMatchRecognizeVarAccessNode::Clone: Node must not be initialized");
        auto copy = new TMatchRecognizeVarAccessNode(Pos, Var, Column, TheSameVar);
        return copy;
    }

protected:
    void DoUpdateState() const override {
        YQL_ENSURE(Node);
    }

    void DoVisitChildren(const TVisitFunc& func, TVisitNodeSet& visited) const final {
        Y_DEBUG_ABORT_UNLESS(Node);
        Node->VisitTree(func, visited);
    }

private:
    TNodePtr Node;
    const TString Var;
    const bool TheSameVar; //reference the same var as being defined by this expression;
    const TString Column;
};

class TMatchRecognizeNavigate: public TAstListNode {
public:
    TMatchRecognizeNavigate(TPosition pos, const TString& name, const TVector<TNodePtr>& args)
        : TAstListNode(pos)
        , Name(name)
        , Args(args)
    {
    }

private:
    TNodePtr DoClone() const override {
        return new TMatchRecognizeNavigate(GetPos(), Name, CloneContainer(Args));
    }

    bool DoInit(TContext& ctx, ISource* src) override;

private:
    const TString Name;
    const TVector<TNodePtr> Args;
};

} // namespace NSQLTranslationV1