1#include "duckdb/main/relation/query_relation.hpp"
2#include "duckdb/main/client_context.hpp"
3#include "duckdb/parser/statement/select_statement.hpp"
4#include "duckdb/parser/tableref/subqueryref.hpp"
5#include "duckdb/parser/parser.hpp"
6
7namespace duckdb {
8
9QueryRelation::QueryRelation(const std::shared_ptr<ClientContext> &context, unique_ptr<SelectStatement> select_stmt_p,
10 string alias_p)
11 : Relation(context, RelationType::QUERY_RELATION), select_stmt(std::move(select_stmt_p)),
12 alias(std::move(alias_p)) {
13 context->TryBindRelation(relation&: *this, result_columns&: this->columns);
14}
15
16QueryRelation::~QueryRelation() {
17}
18
19unique_ptr<SelectStatement> QueryRelation::ParseStatement(ClientContext &context, const string &query,
20 const string &error) {
21 Parser parser(context.GetParserOptions());
22 parser.ParseQuery(query);
23 if (parser.statements.size() != 1) {
24 throw ParserException(error);
25 }
26 if (parser.statements[0]->type != StatementType::SELECT_STATEMENT) {
27 throw ParserException(error);
28 }
29 return unique_ptr_cast<SQLStatement, SelectStatement>(src: std::move(parser.statements[0]));
30}
31
32unique_ptr<SelectStatement> QueryRelation::GetSelectStatement() {
33 return unique_ptr_cast<SQLStatement, SelectStatement>(src: select_stmt->Copy());
34}
35
36unique_ptr<QueryNode> QueryRelation::GetQueryNode() {
37 auto select = GetSelectStatement();
38 return std::move(select->node);
39}
40
41unique_ptr<TableRef> QueryRelation::GetTableRef() {
42 auto subquery_ref = make_uniq<SubqueryRef>(args: GetSelectStatement(), args: GetAlias());
43 return std::move(subquery_ref);
44}
45
46string QueryRelation::GetAlias() {
47 return alias;
48}
49
50const vector<ColumnDefinition> &QueryRelation::Columns() {
51 return columns;
52}
53
54string QueryRelation::ToString(idx_t depth) {
55 return RenderWhitespace(depth) + "Subquery";
56}
57
58} // namespace duckdb
59