1#include "duckdb/parser/tableref/expressionlistref.hpp"
2
3#include "duckdb/common/field_writer.hpp"
4#include "duckdb/common/serializer/format_serializer.hpp"
5#include "duckdb/common/serializer/format_deserializer.hpp"
6
7namespace duckdb {
8
9string ExpressionListRef::ToString() const {
10 D_ASSERT(!values.empty());
11 string result = "(VALUES ";
12 for (idx_t row_idx = 0; row_idx < values.size(); row_idx++) {
13 if (row_idx > 0) {
14 result += ", ";
15 }
16 auto &row = values[row_idx];
17 result += "(";
18 for (idx_t col_idx = 0; col_idx < row.size(); col_idx++) {
19 if (col_idx > 0) {
20 result += ", ";
21 }
22 result += row[col_idx]->ToString();
23 }
24 result += ")";
25 }
26 result += ")";
27 return BaseToString(result, column_name_alias: expected_names);
28}
29
30bool ExpressionListRef::Equals(const TableRef &other_p) const {
31 if (!TableRef::Equals(other: other_p)) {
32 return false;
33 }
34 auto &other = other_p.Cast<ExpressionListRef>();
35 if (values.size() != other.values.size()) {
36 return false;
37 }
38 for (idx_t i = 0; i < values.size(); i++) {
39 if (values[i].size() != other.values[i].size()) {
40 return false;
41 }
42 for (idx_t j = 0; j < values[i].size(); j++) {
43 if (!values[i][j]->Equals(other: *other.values[i][j])) {
44 return false;
45 }
46 }
47 }
48 return true;
49}
50
51unique_ptr<TableRef> ExpressionListRef::Copy() {
52 // value list
53 auto result = make_uniq<ExpressionListRef>();
54 for (auto &val_list : values) {
55 vector<unique_ptr<ParsedExpression>> new_val_list;
56 new_val_list.reserve(n: val_list.size());
57 for (auto &val : val_list) {
58 new_val_list.push_back(x: val->Copy());
59 }
60 result->values.push_back(x: std::move(new_val_list));
61 }
62 result->expected_names = expected_names;
63 result->expected_types = expected_types;
64 CopyProperties(target&: *result);
65 return std::move(result);
66}
67
68void ExpressionListRef::Serialize(FieldWriter &writer) const {
69 writer.WriteList<string>(elements: expected_names);
70 writer.WriteRegularSerializableList<LogicalType>(elements: expected_types);
71 auto &serializer = writer.GetSerializer();
72 writer.WriteField<uint32_t>(element: values.size());
73 for (idx_t i = 0; i < values.size(); i++) {
74 serializer.WriteList(list: values[i]);
75 }
76}
77
78void ExpressionListRef::FormatSerialize(FormatSerializer &serializer) const {
79 TableRef::FormatSerialize(serializer);
80 serializer.WriteProperty(tag: "expected_names", value: expected_names);
81 serializer.WriteProperty(tag: "expected_types", value: expected_types);
82 serializer.WriteProperty(tag: "values", value: values);
83}
84
85unique_ptr<TableRef> ExpressionListRef::FormatDeserialize(FormatDeserializer &source) {
86 auto result = make_uniq<ExpressionListRef>();
87 source.ReadProperty(tag: "expected_names", ret&: result->expected_names);
88 source.ReadProperty(tag: "expected_types", ret&: result->expected_types);
89 source.ReadProperty(tag: "values", ret&: result->values);
90 return std::move(result);
91}
92
93unique_ptr<TableRef> ExpressionListRef::Deserialize(FieldReader &reader) {
94 auto result = make_uniq<ExpressionListRef>();
95 // value list
96 result->expected_names = reader.ReadRequiredList<string>();
97 result->expected_types = reader.ReadRequiredSerializableList<LogicalType, LogicalType>();
98 idx_t value_list_size = reader.ReadRequired<uint32_t>();
99 auto &source = reader.GetSource();
100 for (idx_t i = 0; i < value_list_size; i++) {
101 vector<unique_ptr<ParsedExpression>> value_list;
102 source.ReadList<ParsedExpression>(list&: value_list);
103 result->values.push_back(x: std::move(value_list));
104 }
105 return std::move(result);
106}
107
108} // namespace duckdb
109