1#include "duckdb/planner/operator/logical_pivot.hpp"
2
3#include "duckdb/main/config.hpp"
4
5namespace duckdb {
6
7LogicalPivot::LogicalPivot(idx_t pivot_idx, unique_ptr<LogicalOperator> plan, BoundPivotInfo info_p)
8 : LogicalOperator(LogicalOperatorType::LOGICAL_PIVOT), pivot_index(pivot_idx), bound_pivot(std::move(info_p)) {
9 D_ASSERT(plan);
10 children.push_back(x: std::move(plan));
11}
12
13vector<ColumnBinding> LogicalPivot::GetColumnBindings() {
14 vector<ColumnBinding> result;
15 for (idx_t i = 0; i < bound_pivot.types.size(); i++) {
16 result.emplace_back(args&: pivot_index, args&: i);
17 }
18 return result;
19}
20
21void LogicalPivot::Serialize(FieldWriter &writer) const {
22 writer.WriteField(element: pivot_index);
23 writer.WriteOptional<LogicalOperator>(element: children.back());
24 writer.WriteField(element: bound_pivot.group_count);
25 writer.WriteRegularSerializableList<LogicalType>(elements: bound_pivot.types);
26 writer.WriteList<string>(elements: bound_pivot.pivot_values);
27 writer.WriteSerializableList<Expression>(elements: bound_pivot.aggregates);
28}
29
30unique_ptr<LogicalOperator> LogicalPivot::Deserialize(LogicalDeserializationState &state, FieldReader &reader) {
31 auto pivot_index = reader.ReadRequired<idx_t>();
32 auto plan = reader.ReadOptional<LogicalOperator>(default_value: nullptr, args&: state.gstate);
33 BoundPivotInfo info;
34 info.group_count = reader.ReadRequired<idx_t>();
35 info.types = reader.ReadRequiredSerializableList<LogicalType, LogicalType>();
36 info.pivot_values = reader.ReadRequiredList<string>();
37 info.aggregates = reader.ReadRequiredSerializableList<Expression>(args&: state.gstate);
38 return make_uniq<LogicalPivot>(args&: pivot_index, args: std::move(plan), args: std::move(info));
39}
40
41vector<idx_t> LogicalPivot::GetTableIndex() const {
42 return vector<idx_t> {pivot_index};
43}
44
45void LogicalPivot::ResolveTypes() {
46 this->types = bound_pivot.types;
47}
48
49string LogicalPivot::GetName() const {
50#ifdef DEBUG
51 if (DBConfigOptions::debug_print_bindings) {
52 return LogicalOperator::GetName() + StringUtil::Format(" #%llu", pivot_index);
53 }
54#endif
55 return LogicalOperator::GetName();
56}
57
58} // namespace duckdb
59