1#include "duckdb/parser/expression/subquery_expression.hpp"
2#include "duckdb/planner/binder.hpp"
3#include "duckdb/planner/expression/bound_cast_expression.hpp"
4#include "duckdb/planner/expression/bound_subquery_expression.hpp"
5#include "duckdb/planner/expression_binder.hpp"
6#include "duckdb/common/string_util.hpp"
7
8namespace duckdb {
9
10class BoundSubqueryNode : public QueryNode {
11public:
12 static constexpr const QueryNodeType TYPE = QueryNodeType::BOUND_SUBQUERY_NODE;
13
14public:
15 BoundSubqueryNode(shared_ptr<Binder> subquery_binder, unique_ptr<BoundQueryNode> bound_node,
16 unique_ptr<SelectStatement> subquery)
17 : QueryNode(QueryNodeType::BOUND_SUBQUERY_NODE), subquery_binder(std::move(subquery_binder)),
18 bound_node(std::move(bound_node)), subquery(std::move(subquery)) {
19 }
20
21 shared_ptr<Binder> subquery_binder;
22 unique_ptr<BoundQueryNode> bound_node;
23 unique_ptr<SelectStatement> subquery;
24
25 const vector<unique_ptr<ParsedExpression>> &GetSelectList() const override {
26 throw InternalException("Cannot get select list of bound subquery node");
27 }
28
29 string ToString() const override {
30 throw InternalException("Cannot ToString bound subquery node");
31 }
32 unique_ptr<QueryNode> Copy() const override {
33 throw InternalException("Cannot copy bound subquery node");
34 }
35 void Serialize(FieldWriter &writer) const override {
36 throw InternalException("Cannot serialize bound subquery node");
37 }
38
39 void FormatSerialize(FormatSerializer &serializer) const override {
40 throw InternalException("Cannot serialize bound subquery node");
41 }
42};
43
44BindResult ExpressionBinder::BindExpression(SubqueryExpression &expr, idx_t depth) {
45 if (expr.subquery->node->type != QueryNodeType::BOUND_SUBQUERY_NODE) {
46 D_ASSERT(depth == 0);
47 // first bind the actual subquery in a new binder
48 auto subquery_binder = Binder::CreateBinder(context, parent: &binder);
49 subquery_binder->can_contain_nulls = true;
50 auto bound_node = subquery_binder->BindNode(node&: *expr.subquery->node);
51 // check the correlated columns of the subquery for correlated columns with depth > 1
52 for (idx_t i = 0; i < subquery_binder->correlated_columns.size(); i++) {
53 CorrelatedColumnInfo corr = subquery_binder->correlated_columns[i];
54 if (corr.depth > 1) {
55 // depth > 1, the column references the query ABOVE the current one
56 // add to the set of correlated columns for THIS query
57 corr.depth -= 1;
58 binder.AddCorrelatedColumn(info: corr);
59 }
60 }
61 if (expr.subquery_type != SubqueryType::EXISTS && bound_node->types.size() > 1) {
62 throw BinderException(binder.FormatError(
63 expr_context&: expr, message: StringUtil::Format(fmt_str: "Subquery returns %zu columns - expected 1", params: bound_node->types.size())));
64 }
65 auto prior_subquery = std::move(expr.subquery);
66 expr.subquery = make_uniq<SelectStatement>();
67 expr.subquery->node =
68 make_uniq<BoundSubqueryNode>(args: std::move(subquery_binder), args: std::move(bound_node), args: std::move(prior_subquery));
69 }
70 // now bind the child node of the subquery
71 if (expr.child) {
72 // first bind the children of the subquery, if any
73 string error = Bind(expr&: expr.child, depth);
74 if (!error.empty()) {
75 return BindResult(error);
76 }
77 }
78 // both binding the child and binding the subquery was successful
79 D_ASSERT(expr.subquery->node->type == QueryNodeType::BOUND_SUBQUERY_NODE);
80 auto &bound_subquery = expr.subquery->node->Cast<BoundSubqueryNode>();
81 auto subquery_binder = std::move(bound_subquery.subquery_binder);
82 auto bound_node = std::move(bound_subquery.bound_node);
83 LogicalType return_type =
84 expr.subquery_type == SubqueryType::SCALAR ? bound_node->types[0] : LogicalType(LogicalTypeId::BOOLEAN);
85 if (return_type.id() == LogicalTypeId::UNKNOWN) {
86 return_type = LogicalType::SQLNULL;
87 }
88
89 auto result = make_uniq<BoundSubqueryExpression>(args&: return_type);
90 if (expr.subquery_type == SubqueryType::ANY) {
91 // ANY comparison
92 // cast child and subquery child to equivalent types
93 D_ASSERT(bound_node->types.size() == 1);
94 auto &child = BoundExpression::GetExpression(expr&: *expr.child);
95 auto compare_type = LogicalType::MaxLogicalType(left: child->return_type, right: bound_node->types[0]);
96 child = BoundCastExpression::AddCastToType(context, expr: std::move(child), target_type: compare_type);
97 result->child_type = bound_node->types[0];
98 result->child_target = compare_type;
99 result->child = std::move(child);
100 }
101 result->binder = std::move(subquery_binder);
102 result->subquery = std::move(bound_node);
103 result->subquery_type = expr.subquery_type;
104 result->comparison_type = expr.comparison_type;
105
106 return BindResult(std::move(result));
107}
108
109} // namespace duckdb
110