Kùzu C++ API
Loading...
Searching...
No Matches
expression.h
Go to the documentation of this file.
1#pragma once
2
3#include <functional>
4#include <memory>
5#include <unordered_map>
6#include <unordered_set>
7
8#include "assert.h"
9#include "cast.h"
10#include "copy_constructors.h"
11#include "expression_type.h"
12#include "internal.h"
13#include "types.h"
14
15namespace kuzu {
16namespace binder {
17
18class Expression;
19using expression_vector = std::vector<std::shared_ptr<Expression>>;
20using expression_pair = std::pair<std::shared_ptr<Expression>, std::shared_ptr<Expression>>;
21
22struct ExpressionHasher;
25 std::unordered_set<std::shared_ptr<Expression>, ExpressionHasher, ExpressionEquality>;
26template<typename T>
28 std::unordered_map<std::shared_ptr<Expression>, T, ExpressionHasher, ExpressionEquality>;
29
30class Expression : public std::enable_shared_from_this<Expression> {
32
33public:
38 // Create binary expression.
40 const std::shared_ptr<Expression>& left, const std::shared_ptr<Expression>& right,
41 std::string uniqueName)
42 : Expression{expressionType, std::move(dataType), expression_vector{left, right},
43 std::move(uniqueName)} {}
44 // Create unary expression.
46 const std::shared_ptr<Expression>& child, std::string uniqueName)
48 std::move(uniqueName)} {}
49 // Create leaf expression
55 virtual ~Expression() = default;
56
57 void setAlias(const std::string& name) { alias = name; }
58
59 void setUniqueName(const std::string& name) { uniqueName = name; }
60 std::string getUniqueName() const {
61 KU_ASSERT(!uniqueName.empty());
62 return uniqueName;
63 }
64
65 virtual void cast(const common::LogicalType& type);
66 // NOTE: Avoid using the following unsafe getter. It is meant for resolving ANY data type only.
68 const common::LogicalType& getDataType() const { return dataType; }
69
70 bool hasAlias() const { return !alias.empty(); }
71 std::string getAlias() const { return alias; }
72
73 uint32_t getNumChildren() const { return children.size(); }
74 std::shared_ptr<Expression> getChild(common::idx_t idx) const {
75 KU_ASSERT(idx < children.size());
76 return children[idx];
77 }
79 void setChild(common::idx_t idx, std::shared_ptr<Expression> child) {
80 KU_ASSERT(idx < children.size());
81 children[idx] = std::move(child);
82 }
83
85
86 bool operator==(const Expression& rhs) const { return uniqueName == rhs.uniqueName; }
87
88 std::string toString() const { return hasAlias() ? alias : toStringInternal(); }
89
90 virtual std::unique_ptr<Expression> copy() const {
91 throw common::InternalException("Unimplemented expression copy().");
92 }
93
94 template<class TARGET>
95 TARGET& cast() {
97 }
98 template<class TARGET>
99 const TARGET& constCast() const {
101 }
102 template<class TARGET>
103 const TARGET* constPtrCast() const {
105 }
106
107protected:
108 virtual std::string toStringInternal() const = 0;
109
110public:
113
114protected:
115 // Name that serves as the unique identifier.
116 std::string uniqueName;
117 std::string alias;
119};
120
122 std::size_t operator()(const std::shared_ptr<Expression>& expression) const {
123 return std::hash<std::string>{}(expression->getUniqueName());
124 }
125};
126
128 bool operator()(const std::shared_ptr<Expression>& left,
129 const std::shared_ptr<Expression>& right) const {
130 return left->getUniqueName() == right->getUniqueName();
131 }
132};
133
134} // namespace binder
135} // namespace kuzu
#define KU_ASSERT(condition)
Definition assert.h:19
Definition expression.h:30
const TARGET * constPtrCast() const
Definition expression.h:103
bool operator==(const Expression &rhs) const
Definition expression.h:86
std::string uniqueName
Definition expression.h:116
std::string toString() const
Definition expression.h:88
expression_vector splitOnAND()
const common::LogicalType & getDataType() const
Definition expression.h:68
std::shared_ptr< Expression > getChild(common::idx_t idx) const
Definition expression.h:74
common::ExpressionType expressionType
Definition expression.h:111
const TARGET & constCast() const
Definition expression.h:99
uint32_t getNumChildren() const
Definition expression.h:73
void setAlias(const std::string &name)
Definition expression.h:57
std::string getUniqueName() const
Definition expression.h:60
expression_vector children
Definition expression.h:118
common::LogicalType & getDataTypeUnsafe()
Definition expression.h:67
virtual std::unique_ptr< Expression > copy() const
Definition expression.h:90
friend class ExpressionChildrenCollector
Definition expression.h:31
void setChild(common::idx_t idx, std::shared_ptr< Expression > child)
Definition expression.h:79
virtual ~Expression()=default
bool hasAlias() const
Definition expression.h:70
std::string getAlias() const
Definition expression.h:71
virtual std::string toStringInternal() const =0
expression_vector getChildren() const
Definition expression.h:78
DELETE_COPY_DEFAULT_MOVE(Expression)
Expression(common::ExpressionType expressionType, common::LogicalType dataType, expression_vector children, std::string uniqueName)
Definition expression.h:34
void setUniqueName(const std::string &name)
Definition expression.h:59
virtual void cast(const common::LogicalType &type)
common::LogicalType dataType
Definition expression.h:112
std::string alias
Definition expression.h:117
TARGET & cast()
Definition expression.h:95
Expression(common::ExpressionType expressionType, common::LogicalType dataType, const std::shared_ptr< Expression > &left, const std::shared_ptr< Expression > &right, std::string uniqueName)
Definition expression.h:39
Expression(common::ExpressionType expressionType, common::LogicalType dataType, std::string uniqueName)
Definition expression.h:50
Expression(common::ExpressionType expressionType, common::LogicalType dataType, const std::shared_ptr< Expression > &child, std::string uniqueName)
Definition expression.h:45
Definition internal.h:9
Definition types.h:201
std::unordered_map< std::shared_ptr< Expression >, T, ExpressionHasher, ExpressionEquality > expression_map
Definition expression.h:27
std::unordered_set< std::shared_ptr< Expression >, ExpressionHasher, ExpressionEquality > expression_set
Definition expression.h:24
std::pair< std::shared_ptr< Expression >, std::shared_ptr< Expression > > expression_pair
Definition expression.h:20
std::vector< std::shared_ptr< Expression > > expression_vector
Definition expression.h:19
uint32_t idx_t
Definition types.h:39
TO ku_dynamic_cast(FROM old)
Definition cast.h:11
ExpressionType
Definition expression_type.h:9
Definition alter_type.h:5
Definition expression.h:127
bool operator()(const std::shared_ptr< Expression > &left, const std::shared_ptr< Expression > &right) const
Definition expression.h:128
Definition expression.h:121
std::size_t operator()(const std::shared_ptr< Expression > &expression) const
Definition expression.h:122