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