1#include "duckdb/planner/expression/bound_reference_expression.hpp"
2
3#include "duckdb/common/field_writer.hpp"
4#include "duckdb/common/serializer.hpp"
5#include "duckdb/common/to_string.hpp"
6#include "duckdb/common/types/hash.hpp"
7#include "duckdb/main/config.hpp"
8
9namespace duckdb {
10
11BoundReferenceExpression::BoundReferenceExpression(string alias, LogicalType type, idx_t index)
12 : Expression(ExpressionType::BOUND_REF, ExpressionClass::BOUND_REF, std::move(type)), index(index) {
13 this->alias = std::move(alias);
14}
15BoundReferenceExpression::BoundReferenceExpression(LogicalType type, idx_t index)
16 : BoundReferenceExpression(string(), std::move(type), index) {
17}
18
19string BoundReferenceExpression::ToString() const {
20#ifdef DEBUG
21 if (DBConfigOptions::debug_print_bindings) {
22 return "#" + to_string(index);
23 }
24#endif
25 if (!alias.empty()) {
26 return alias;
27 }
28 return "#" + to_string(val: index);
29}
30
31bool BoundReferenceExpression::Equals(const BaseExpression &other_p) const {
32 if (!Expression::Equals(other: other_p)) {
33 return false;
34 }
35 auto &other = other_p.Cast<BoundReferenceExpression>();
36 return other.index == index;
37}
38
39hash_t BoundReferenceExpression::Hash() const {
40 return CombineHash(left: Expression::Hash(), right: duckdb::Hash<idx_t>(val: index));
41}
42
43unique_ptr<Expression> BoundReferenceExpression::Copy() {
44 return make_uniq<BoundReferenceExpression>(args&: alias, args&: return_type, args&: index);
45}
46
47void BoundReferenceExpression::Serialize(FieldWriter &writer) const {
48 writer.WriteString(val: alias);
49 writer.WriteSerializable(element: return_type);
50 writer.WriteField(element: index);
51}
52
53unique_ptr<Expression> BoundReferenceExpression::Deserialize(ExpressionDeserializationState &state,
54 FieldReader &reader) {
55 auto alias = reader.ReadRequired<string>();
56 auto return_type = reader.ReadRequiredSerializable<LogicalType, LogicalType>();
57 auto index = reader.ReadRequired<idx_t>();
58 return make_uniq<BoundReferenceExpression>(args&: alias, args&: return_type, args&: index);
59}
60
61} // namespace duckdb
62