1//===----------------------------------------------------------------------===//
2// DuckDB
3//
4// duckdb/parser/base_expression.hpp
5//
6//
7//===----------------------------------------------------------------------===//
8
9#pragma once
10
11#include "duckdb/common/common.hpp"
12#include "duckdb/common/enums/expression_type.hpp"
13#include "duckdb/common/exception.hpp"
14
15namespace duckdb {
16
17//! The BaseExpression class is a base class that can represent any expression
18//! part of a SQL statement.
19class BaseExpression {
20public:
21 //! Create an Expression
22 BaseExpression(ExpressionType type, ExpressionClass expression_class)
23 : type(type), expression_class(expression_class) {
24 }
25 virtual ~BaseExpression() {
26 }
27
28 //! Returns the type of the expression
29 ExpressionType GetExpressionType() const {
30 return type;
31 }
32 //! Returns the class of the expression
33 ExpressionClass GetExpressionClass() const {
34 return expression_class;
35 }
36
37 //! Type of the expression
38 ExpressionType type;
39 //! The expression class of the node
40 ExpressionClass expression_class;
41 //! The alias of the expression,
42 string alias;
43
44public:
45 //! Returns true if this expression is an aggregate or not.
46 /*!
47 Examples:
48
49 (1) SUM(a) + 1 -- True
50
51 (2) a + 1 -- False
52 */
53 virtual bool IsAggregate() const = 0;
54 //! Returns true if the expression has a window function or not
55 virtual bool IsWindow() const = 0;
56 //! Returns true if the query contains a subquery
57 virtual bool HasSubquery() const = 0;
58 //! Returns true if expression does not contain a group ref or col ref or parameter
59 virtual bool IsScalar() const = 0;
60 //! Returns true if the expression has a parameter
61 virtual bool HasParameter() const = 0;
62
63 //! Get the name of the expression
64 virtual string GetName() const;
65 //! Convert the Expression to a String
66 virtual string ToString() const = 0;
67 //! Print the expression to stdout
68 void Print() const;
69
70 //! Creates a hash value of this expression. It is important that if two expressions are identical (i.e.
71 //! Expression::Equals() returns true), that their hash value is identical as well.
72 virtual hash_t Hash() const = 0;
73 //! Returns true if this expression is equal to another expression
74 virtual bool Equals(const BaseExpression &other) const;
75
76 static bool Equals(const BaseExpression &left, const BaseExpression &right) {
77 return left.Equals(other: right);
78 }
79 bool operator==(const BaseExpression &rhs) {
80 return Equals(other: rhs);
81 }
82
83 virtual void Verify() const;
84
85public:
86 template <class TARGET>
87 TARGET &Cast() {
88 if (expression_class != TARGET::TYPE) {
89 throw InternalException("Failed to cast expression to type - expression type mismatch");
90 }
91 return reinterpret_cast<TARGET &>(*this);
92 }
93
94 template <class TARGET>
95 const TARGET &Cast() const {
96 if (expression_class != TARGET::TYPE) {
97 throw InternalException("Failed to cast expression to type - expression type mismatch");
98 }
99 return reinterpret_cast<const TARGET &>(*this);
100 }
101};
102
103} // namespace duckdb
104