aboutsummaryrefslogtreecommitdiffstats
path: root/yql/essentials/sql/v1/sql_select.h
blob: fd6f0bece52996db24e7d2a9c6c51fa7a7a73c40 (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
#pragma once

#include "sql_translation.h"
#include <yql/essentials/parser/proto_ast/gen/v1_proto_split/SQLv1Parser.pb.main.h>

namespace NSQLTranslationV1 {

using namespace NSQLv1Generated;

class TSqlSelect: public TSqlTranslation {
public:
    TSqlSelect(TContext& ctx, NSQLTranslation::ESqlMode mode)
        : TSqlTranslation(ctx, mode)
    {
    }

    TSourcePtr Build(const TRule_select_stmt& node, TPosition& selectPos);
    TSourcePtr Build(const TRule_select_unparenthesized_stmt& node, TPosition& selectPos);

private:
    bool SelectTerm(TVector<TNodePtr>& terms, const TRule_result_column& node);
    bool ValidateSelectColumns(const TVector<TNodePtr>& terms);
    bool ColumnName(TVector<TNodePtr>& keys, const TRule_column_name& node);
    bool ColumnName(TVector<TNodePtr>& keys, const TRule_without_column_name& node);
    template<typename TRule>
    bool ColumnList(TVector<TNodePtr>& keys, const TRule& node);
    bool NamedColumn(TVector<TNodePtr>& columnList, const TRule_named_column& node);
    TSourcePtr SingleSource(const TRule_single_source& node, const TVector<TString>& derivedColumns, TPosition derivedColumnsPos, bool unorderedSubquery);
    TSourcePtr NamedSingleSource(const TRule_named_single_source& node, bool unorderedSubquery);
    bool FlattenByArg(const TString& sourceLabel, TVector<TNodePtr>& flattenByColumns, TVector<TNodePtr>& flattenByExprs, const TRule_flatten_by_arg& node);
    TSourcePtr FlattenSource(const TRule_flatten_source& node);
    TSourcePtr JoinSource(const TRule_join_source& node);
    bool JoinOp(ISource* join, const TRule_join_source::TBlock3& block, TMaybe<TPosition> anyPos);
    TNodePtr JoinExpr(ISource*, const TRule_join_constraint& node);
    TSourcePtr ProcessCore(const TRule_process_core& node, const TWriteSettings& settings, TPosition& selectPos);
    TSourcePtr ReduceCore(const TRule_reduce_core& node, const TWriteSettings& settings, TPosition& selectPos);

    struct TSelectKindPlacement {
        bool IsFirstInSelectOp = false;
        bool IsLastInSelectOp = false;
    };

    TSourcePtr SelectCore(const TRule_select_core& node, const TWriteSettings& settings, TPosition& selectPos,
        TMaybe<TSelectKindPlacement> placement, TVector<TSortSpecificationPtr>& selectOpOrederBy, bool& selectOpAssumeOrderBy);

    bool WindowDefinition(const TRule_window_definition& node, TWinSpecs& winSpecs);
    bool WindowClause(const TRule_window_clause& node, TWinSpecs& winSpecs);

    struct TSelectKindResult {
        TSourcePtr Source;
        TWriteSettings Settings;

        TVector<TSortSpecificationPtr> SelectOpOrderBy;
        bool SelectOpAssumeOrderBy = false;
        TNodePtr SelectOpSkipTake;

        inline explicit operator bool() const {
            return static_cast<bool>(Source);
        }
    };

    bool ValidateLimitOrderByWithSelectOp(TMaybe<TSelectKindPlacement> placement, TStringBuf what);
    bool NeedPassLimitOrderByToUnderlyingSelect(TMaybe<TSelectKindPlacement> placement);

    template<typename TRule>
    TSourcePtr Build(const TRule& node, TPosition pos, TSelectKindResult&& first);


    TSelectKindResult SelectKind(const TRule_select_kind& node, TPosition& selectPos, TMaybe<TSelectKindPlacement> placement);
    TSelectKindResult SelectKind(const TRule_select_kind_partial& node, TPosition& selectPos, TMaybe<TSelectKindPlacement> placement);
    TSelectKindResult SelectKind(const TRule_select_kind_parenthesis& node, TPosition& selectPos, TMaybe<TSelectKindPlacement> placement);
};

} //namespace NSQLTranslationV1