1#include "duckdb/main/relation/order_relation.hpp"
2#include "duckdb/main/client_context.hpp"
3#include "duckdb/parser/query_node.hpp"
4#include "duckdb/parser/query_node/select_node.hpp"
5#include "duckdb/parser/expression/star_expression.hpp"
6
7namespace duckdb {
8
9OrderRelation::OrderRelation(shared_ptr<Relation> child_p, vector<OrderByNode> orders)
10 : Relation(child_p->context, RelationType::ORDER_RELATION), orders(std::move(orders)), child(std::move(child_p)) {
11 D_ASSERT(child.get() != this);
12 // bind the expressions
13 context.GetContext()->TryBindRelation(relation&: *this, result_columns&: this->columns);
14}
15
16unique_ptr<QueryNode> OrderRelation::GetQueryNode() {
17 auto select = make_uniq<SelectNode>();
18 select->from_table = child->GetTableRef();
19 select->select_list.push_back(x: make_uniq<StarExpression>());
20 auto order_node = make_uniq<OrderModifier>();
21 for (idx_t i = 0; i < orders.size(); i++) {
22 order_node->orders.emplace_back(args&: orders[i].type, args&: orders[i].null_order, args: orders[i].expression->Copy());
23 }
24 select->modifiers.push_back(x: std::move(order_node));
25 return std::move(select);
26}
27
28string OrderRelation::GetAlias() {
29 return child->GetAlias();
30}
31
32const vector<ColumnDefinition> &OrderRelation::Columns() {
33 return columns;
34}
35
36string OrderRelation::ToString(idx_t depth) {
37 string str = RenderWhitespace(depth) + "Order [";
38 for (idx_t i = 0; i < orders.size(); i++) {
39 if (i != 0) {
40 str += ", ";
41 }
42 str += orders[i].expression->ToString() + (orders[i].type == OrderType::ASCENDING ? " ASC" : " DESC");
43 }
44 str += "]\n";
45 return str + child->ToString(depth: depth + 1);
46}
47
48} // namespace duckdb
49