forked from ClickHouse/ClickHouse
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConstantNode.h
More file actions
130 lines (99 loc) · 3.71 KB
/
ConstantNode.h
File metadata and controls
130 lines (99 loc) · 3.71 KB
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 <Core/Field.h>
#include <Analyzer/ConstantValue.h>
#include <Analyzer/IQueryTreeNode.h>
#include <Columns/IColumn_fwd.h>
namespace DB
{
/** Constant node represents constant value in query tree.
* Constant value must be representable by Field.
* Examples: 1, 'constant_string', [1,2,3].
*
* Constant node can optionally keep pointer to its source expression.
*/
class ConstantNode;
using ConstantNodePtr = std::shared_ptr<ConstantNode>;
class ConstantNode final : public IQueryTreeNode
{
public:
/// Construct constant query tree node from constant value and source expression
explicit ConstantNode(ConstantValue constant_value_, QueryTreeNodePtr source_expression);
/// Construct constant query tree node from constant value
explicit ConstantNode(ConstantValue constant_value_);
/** Construct constant query tree node from column and data type.
*
* Throws exception if value cannot be converted to value data type.
*/
explicit ConstantNode(ColumnPtr constant_column_, DataTypePtr value_data_type_);
/// Construct constant query tree node from column, data type will be derived from field value
explicit ConstantNode(ColumnPtr constant_column_);
/** Construct constant query tree node from field and data type.
*
* Throws exception if value cannot be converted to value data type.
*/
explicit ConstantNode(Field value_, DataTypePtr value_data_type_);
/// Construct constant query tree node from field, data type will be derived from field value
explicit ConstantNode(Field value_);
/// Get constant value
const ColumnPtr & getColumn() const
{
return constant_value.getColumn();
}
/// Get constant value
Field getValue() const
{
Field out;
constant_value.getColumn()->get(0, out);
return out;
}
/// Get constant value string representation
String getValueStringRepresentation() const;
/// Returns true if constant node has source expression, false otherwise
bool hasSourceExpression() const
{
return source_expression != nullptr;
}
/// Get source expression
const QueryTreeNodePtr & getSourceExpression() const
{
return source_expression;
}
/// Get source expression
QueryTreeNodePtr & getSourceExpression()
{
return source_expression;
}
QueryTreeNodeType getNodeType() const override
{
return QueryTreeNodeType::CONSTANT;
}
DataTypePtr getResultType() const override
{
return constant_value.getType();
}
/// Check if conversion to AST requires wrapping with _CAST function.
static bool requiresCastCall(const DataTypePtr & field_type, const DataTypePtr & data_type);
/// Check if constant is a result of _CAST function constant folding.
bool receivedFromInitiatorServer() const;
void setMaskId(size_t id = std::numeric_limits<decltype(mask_id)>::max())
{
mask_id = id;
}
void convertToNullable() override;
void dumpTreeImpl(WriteBuffer & buffer, FormatState & format_state, size_t indent) const override;
std::pair<String, DataTypePtr> getValueNameAndType() const
{
return constant_value.getValueNameAndType();
}
protected:
bool isEqualImpl(const IQueryTreeNode & rhs, CompareOptions compare_options) const override;
void updateTreeHashImpl(HashState & hash_state, CompareOptions compare_options) const override;
QueryTreeNodePtr cloneImpl() const override;
ASTPtr toASTImpl(const ConvertToASTOptions & options) const override;
private:
ConstantValue constant_value;
QueryTreeNodePtr source_expression;
size_t mask_id = 0;
static constexpr size_t children_size = 0;
};
}