ASTFunction.h 2.6 KB
Newer Older
A
Alexey Milovidov 已提交
1
#pragma once
A
Alexey Milovidov 已提交
2

3 4
#include <Parsers/ASTWithAlias.h>
#include <Parsers/ASTExpressionList.h>
A
Amos Bird 已提交
5
#include <Parsers/ASTSelectWithUnionQuery.h>
A
Alexey Milovidov 已提交
6 7 8 9 10


namespace DB
{

A
Alexander Kuzmenkov 已提交
11 12
class ASTIdentifier;

13
/** AST for function application or operator.
A
Alexey Milovidov 已提交
14
  */
A
Alexey Milovidov 已提交
15
class ASTFunction : public ASTWithAlias
A
Alexey Milovidov 已提交
16 17
{
public:
18 19 20 21 22
    String name;
    ASTPtr arguments;
    /// parameters - for parametric aggregate function. Example: quantile(0.9)(x) - what in first parens are 'parameters'.
    ASTPtr parameters;

A
Alexander Kuzmenkov 已提交
23
    bool is_window_function = false;
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42

    // We have to make these fields ASTPtr because this is what the visitors
    // expect. Some of them take const ASTPtr & (makes no sense), and some
    // take ASTPtr & and modify it. I don't understand how the latter is
    // compatible with also having an owning `children` array -- apparently it
    // leads to some dangling children that are not referenced by the fields of
    // the AST class itself. Some older code hints at the idea of having
    // ownership in `children` only, and making the class fields to be raw
    // pointers of proper type (see e.g. IAST::set), but this is not compatible
    // with the visitor interface.

    // ASTIdentifier
    ASTPtr window_name;

    // ASTExpressionList
    ASTPtr window_partition_by;

    // ASTExpressionList of
    ASTPtr window_order_by;
A
Alexander Kuzmenkov 已提交
43

44 45 46
    /// do not print empty parentheses if there are no args - compatibility with new AST for data types and engine names.
    bool no_empty_args = false;

47
    /** Get text identifying the AST node. */
48
    String getID(char delim) const override;
A
Alexey Milovidov 已提交
49

50
    ASTPtr clone() const override;
51

A
Amos Bird 已提交
52 53
    void updateTreeHashImpl(SipHash & hash_state) const override;

A
Amos Bird 已提交
54 55
    ASTSelectWithUnionQuery * tryGetQueryArgument() const;

56 57
    ASTPtr toLiteral() const;  // Try to convert functions like Array or Tuple to a literal form.

A
Alexander Kuzmenkov 已提交
58 59 60 61 62
    void appendWindowDescription(const FormatSettings & settings,
        FormatState & state, FormatStateStacked frame) const;

    std::string getWindowDescription() const;

63
protected:
64
    void formatImplWithoutAlias(const FormatSettings & settings, FormatState & state, FormatStateStacked frame) const override;
65
    void appendColumnNameImpl(WriteBuffer & ostr) const override;
A
Alexey Milovidov 已提交
66 67
};

68 69

template <typename... Args>
70
std::shared_ptr<ASTFunction> makeASTFunction(const String & name, Args &&... args)
71
{
72
    auto function = std::make_shared<ASTFunction>();
73

74 75 76
    function->name = name;
    function->arguments = std::make_shared<ASTExpressionList>();
    function->children.push_back(function->arguments);
77

78
    function->arguments->children = { std::forward<Args>(args)... };
79

A
Alexey Milovidov 已提交
80
    return function;
81 82
}

A
Alexey Milovidov 已提交
83
}