1#include "duckdb/main/relation/limit_relation.hpp"
2#include "duckdb/parser/query_node/select_node.hpp"
3#include "duckdb/parser/query_node.hpp"
4#include "duckdb/parser/expression/constant_expression.hpp"
5#include "duckdb/common/to_string.hpp"
6
7namespace duckdb {
8
9LimitRelation::LimitRelation(shared_ptr<Relation> child_p, int64_t limit, int64_t offset)
10 : Relation(child_p->context, RelationType::PROJECTION_RELATION), limit(limit), offset(offset),
11 child(std::move(child_p)) {
12 D_ASSERT(child.get() != this);
13}
14
15unique_ptr<QueryNode> LimitRelation::GetQueryNode() {
16 auto child_node = child->GetQueryNode();
17 auto limit_node = make_uniq<LimitModifier>();
18 if (limit >= 0) {
19 limit_node->limit = make_uniq<ConstantExpression>(args: Value::BIGINT(value: limit));
20 }
21 if (offset > 0) {
22 limit_node->offset = make_uniq<ConstantExpression>(args: Value::BIGINT(value: offset));
23 }
24
25 child_node->modifiers.push_back(x: std::move(limit_node));
26 return child_node;
27}
28
29string LimitRelation::GetAlias() {
30 return child->GetAlias();
31}
32
33const vector<ColumnDefinition> &LimitRelation::Columns() {
34 return child->Columns();
35}
36
37string LimitRelation::ToString(idx_t depth) {
38 string str = RenderWhitespace(depth) + "Limit " + to_string(val: limit);
39 if (offset > 0) {
40 str += " Offset " + to_string(val: offset);
41 }
42 str += "\n";
43 return str + child->ToString(depth: depth + 1);
44}
45
46} // namespace duckdb
47