1 | //===----------------------------------------------------------------------===// |
---|---|
2 | // DuckDB |
3 | // |
4 | // duckdb/parser/tableref/pivotref.hpp |
5 | // |
6 | // |
7 | //===----------------------------------------------------------------------===// |
8 | |
9 | #pragma once |
10 | |
11 | #include "duckdb/parser/tableref.hpp" |
12 | #include "duckdb/parser/query_node/select_node.hpp" |
13 | |
14 | namespace duckdb { |
15 | |
16 | struct PivotColumnEntry { |
17 | //! The set of values to match on |
18 | vector<Value> values; |
19 | //! The star expression (UNPIVOT only) |
20 | unique_ptr<ParsedExpression> star_expr; |
21 | //! The alias of the pivot column entry |
22 | string alias; |
23 | |
24 | bool Equals(const PivotColumnEntry &other) const; |
25 | void Serialize(Serializer &serializer) const; |
26 | PivotColumnEntry Copy() const; |
27 | static PivotColumnEntry Deserialize(Deserializer &source); |
28 | |
29 | void FormatSerialize(FormatSerializer &serializer) const; |
30 | static PivotColumnEntry FormatDeserialize(FormatDeserializer &source); |
31 | }; |
32 | |
33 | struct PivotValueElement { |
34 | vector<Value> values; |
35 | string name; |
36 | }; |
37 | |
38 | struct PivotColumn { |
39 | //! The set of expressions to pivot on |
40 | vector<unique_ptr<ParsedExpression>> pivot_expressions; |
41 | //! The set of unpivot names |
42 | vector<string> unpivot_names; |
43 | //! The set of values to pivot on |
44 | vector<PivotColumnEntry> entries; |
45 | //! The enum to read pivot values from (if any) |
46 | string pivot_enum; |
47 | //! Subquery (if any) - used during transform only |
48 | unique_ptr<QueryNode> subquery; |
49 | |
50 | string ToString() const; |
51 | bool Equals(const PivotColumn &other) const; |
52 | void Serialize(Serializer &serializer) const; |
53 | PivotColumn Copy() const; |
54 | static PivotColumn Deserialize(Deserializer &source); |
55 | |
56 | void FormatSerialize(FormatSerializer &serializer) const; |
57 | static PivotColumn FormatDeserialize(FormatDeserializer &source); |
58 | }; |
59 | |
60 | //! Represents a PIVOT or UNPIVOT expression |
61 | class PivotRef : public TableRef { |
62 | public: |
63 | static constexpr const TableReferenceType TYPE = TableReferenceType::PIVOT; |
64 | |
65 | public: |
66 | explicit PivotRef() : TableRef(TableReferenceType::PIVOT), include_nulls(false) { |
67 | } |
68 | |
69 | //! The source table of the pivot |
70 | unique_ptr<TableRef> source; |
71 | //! The aggregates to compute over the pivot (PIVOT only) |
72 | vector<unique_ptr<ParsedExpression>> aggregates; |
73 | //! The names of the unpivot expressions (UNPIVOT only) |
74 | vector<string> unpivot_names; |
75 | //! The set of pivots |
76 | vector<PivotColumn> pivots; |
77 | //! The groups to pivot over. If none are specified all columns not included in the pivots/aggregate are chosen. |
78 | vector<string> groups; |
79 | //! Aliases for the column names |
80 | vector<string> column_name_alias; |
81 | //! Whether or not to include nulls in the result (UNPIVOT only) |
82 | bool include_nulls; |
83 | //! The set of values to pivot on (bound pivot only) |
84 | vector<PivotValueElement> bound_pivot_values; |
85 | //! The set of bound group names (bound pivot only) |
86 | vector<string> bound_group_names; |
87 | //! The set of bound aggregate names (bound pivot only) |
88 | vector<string> bound_aggregate_names; |
89 | |
90 | public: |
91 | string ToString() const override; |
92 | bool Equals(const TableRef &other_p) const override; |
93 | |
94 | unique_ptr<TableRef> Copy() override; |
95 | |
96 | //! Serializes a blob into a PivotRef |
97 | void Serialize(FieldWriter &serializer) const override; |
98 | //! Deserializes a blob back into a PivotRef |
99 | static unique_ptr<TableRef> Deserialize(FieldReader &source); |
100 | |
101 | void FormatSerialize(FormatSerializer &serializer) const override; |
102 | static unique_ptr<TableRef> FormatDeserialize(FormatDeserializer &source); |
103 | }; |
104 | } // namespace duckdb |
105 |