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 setUniqueName(const std::string& name) { uniqueName = name; }
58 std::string getUniqueName() const {
59 KU_ASSERT(!uniqueName.empty());
60 return uniqueName;
61 }
62
63 virtual void cast(const common::LogicalType& type);
64 const common::LogicalType& getDataType() const { return dataType; }
65
66 void setAlias(const std::string& newAlias) { alias = newAlias; }
67 bool hasAlias() const { return !alias.empty(); }
68 std::string getAlias() const { return alias; }
69
70 common::idx_t getNumChildren() const { return children.size(); }
71 std::shared_ptr<Expression> getChild(common::idx_t idx) const {
72 KU_ASSERT(idx < children.size());
73 return children[idx];
74 }
76 void setChild(common::idx_t idx, std::shared_ptr<Expression> child) {
77 KU_ASSERT(idx < children.size());
78 children[idx] = std::move(child);
79 }
80
82
83 bool operator==(const Expression& rhs) const { return uniqueName == rhs.uniqueName; }
84
85 std::string toString() const { return hasAlias() ? alias : toStringInternal(); }
86
87 virtual std::unique_ptr<Expression> copy() const {
88 throw common::InternalException("Unimplemented expression copy().");
89 }
90
91 template<class TARGET>
92 TARGET& cast() {
94 }
95 template<class TARGET>
96 TARGET* ptrCast() {
98 }
99 template<class TARGET>
100 const TARGET& constCast() const {
102 }
103 template<class TARGET>
104 const TARGET* constPtrCast() const {
106 }
107
108protected:
109 virtual std::string toStringInternal() const = 0;
110
111public:
114
115protected:
116 // Name that serves as the unique identifier.
117 std::string uniqueName;
118 std::string alias;
120};
121
123 std::size_t operator()(const std::shared_ptr<Expression>& expression) const {
124 return std::hash<std::string>{}(expression->getUniqueName());
125 }
126};
127
129 bool operator()(const std::shared_ptr<Expression>& left,
130 const std::shared_ptr<Expression>& right) const {
131 return left->getUniqueName() == right->getUniqueName();
132 }
133};
134
135} // namespace binder
136} // namespace kuzu
#define KU_ASSERT(condition)
Definition assert.h:19
Definition expression.h:30
const TARGET * constPtrCast() const
Definition expression.h:104
bool operator==(const Expression &rhs) const
Definition expression.h:83
std::string uniqueName
Definition expression.h:117
std::string toString() const
Definition expression.h:85
expression_vector splitOnAND()
const common::LogicalType & getDataType() const
Definition expression.h:64
void setAlias(const std::string &newAlias)
Definition expression.h:66
std::shared_ptr< Expression > getChild(common::idx_t idx) const
Definition expression.h:71
common::ExpressionType expressionType
Definition expression.h:112
const TARGET & constCast() const
Definition expression.h:100
std::string getUniqueName() const
Definition expression.h:58
expression_vector children
Definition expression.h:119
virtual std::unique_ptr< Expression > copy() const
Definition expression.h:87
TARGET * ptrCast()
Definition expression.h:96
friend class ExpressionChildrenCollector
Definition expression.h:31
void setChild(common::idx_t idx, std::shared_ptr< Expression > child)
Definition expression.h:76
virtual ~Expression()=default
bool hasAlias() const
Definition expression.h:67
std::string getAlias() const
Definition expression.h:68
virtual std::string toStringInternal() const =0
common::idx_t getNumChildren() const
Definition expression.h:70
expression_vector getChildren() const
Definition expression.h:75
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:57
virtual void cast(const common::LogicalType &type)
common::LogicalType dataType
Definition expression.h:113
std::string alias
Definition expression.h:118
TARGET & cast()
Definition expression.h:92
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:246
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:41
ExpressionType
Definition expression_type.h:9
TO ku_dynamic_cast(FROM *old)
Definition cast.h:11
Definition array_utils.h:7
Definition expression.h:128
bool operator()(const std::shared_ptr< Expression > &left, const std::shared_ptr< Expression > &right) const
Definition expression.h:129
Definition expression.h:122
std::size_t operator()(const std::shared_ptr< Expression > &expression) const
Definition expression.h:123