| 1 | #include "duckdb/parser/expression/columnref_expression.hpp" |
| 2 | |
| 3 | #include "duckdb/common/exception.hpp" |
| 4 | #include "duckdb/common/serializer.hpp" |
| 5 | #include "duckdb/common/types/hash.hpp" |
| 6 | |
| 7 | using namespace duckdb; |
| 8 | using namespace std; |
| 9 | |
| 10 | //! Specify both the column and table name |
| 11 | ColumnRefExpression::ColumnRefExpression(string column_name, string table_name) |
| 12 | : ParsedExpression(ExpressionType::COLUMN_REF, ExpressionClass::COLUMN_REF), column_name(column_name), |
| 13 | table_name(table_name) { |
| 14 | } |
| 15 | |
| 16 | ColumnRefExpression::ColumnRefExpression(string column_name) : ColumnRefExpression(column_name, string()) { |
| 17 | } |
| 18 | |
| 19 | string ColumnRefExpression::GetName() const { |
| 20 | return !alias.empty() ? alias : column_name; |
| 21 | } |
| 22 | |
| 23 | string ColumnRefExpression::ToString() const { |
| 24 | if (table_name.empty()) { |
| 25 | return column_name; |
| 26 | } else { |
| 27 | return table_name + "." + column_name; |
| 28 | } |
| 29 | } |
| 30 | |
| 31 | bool ColumnRefExpression::Equals(const ColumnRefExpression *a, const ColumnRefExpression *b) { |
| 32 | return a->column_name == b->column_name && a->table_name == b->table_name; |
| 33 | } |
| 34 | |
| 35 | hash_t ColumnRefExpression::Hash() const { |
| 36 | hash_t result = ParsedExpression::Hash(); |
| 37 | result = CombineHash(result, duckdb::Hash<const char *>(column_name.c_str())); |
| 38 | return result; |
| 39 | } |
| 40 | |
| 41 | unique_ptr<ParsedExpression> ColumnRefExpression::Copy() const { |
| 42 | auto copy = make_unique<ColumnRefExpression>(column_name, table_name); |
| 43 | copy->CopyProperties(*this); |
| 44 | return move(copy); |
| 45 | } |
| 46 | |
| 47 | void ColumnRefExpression::Serialize(Serializer &serializer) { |
| 48 | ParsedExpression::Serialize(serializer); |
| 49 | serializer.WriteString(table_name); |
| 50 | serializer.WriteString(column_name); |
| 51 | } |
| 52 | |
| 53 | unique_ptr<ParsedExpression> ColumnRefExpression::Deserialize(ExpressionType type, Deserializer &source) { |
| 54 | auto table_name = source.Read<string>(); |
| 55 | auto column_name = source.Read<string>(); |
| 56 | auto expression = make_unique<ColumnRefExpression>(column_name, table_name); |
| 57 | return move(expression); |
| 58 | } |
| 59 | |