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
7using namespace duckdb;
8using namespace std;
9
10class BoundSubqueryNode : public QueryNode {
11public:
12 BoundSubqueryNode(unique_ptr<Binder> subquery_binder, unique_ptr<BoundQueryNode> bound_node,
13 unique_ptr<QueryNode> subquery)
14 : QueryNode(QueryNodeType::BOUND_SUBQUERY_NODE), subquery_binder(move(subquery_binder)),
15 bound_node(move(bound_node)), subquery(move(subquery)) {
16 }
17
18 unique_ptr<Binder> subquery_binder;
19 unique_ptr<BoundQueryNode> bound_node;
20 unique_ptr<QueryNode> subquery;
21
22 const vector<unique_ptr<ParsedExpression>> &GetSelectList() const {
23 throw Exception("Cannot get select list of bound subquery node");
24 }
25
26 unique_ptr<QueryNode> Copy() {
27 throw Exception("Cannot copy bound subquery node");
28 }
29};
30
31BindResult ExpressionBinder::BindExpression(SubqueryExpression &expr, idx_t depth) {
32 if (expr.subquery->type != QueryNodeType::BOUND_SUBQUERY_NODE) {
33 assert(depth == 0);
34 // first bind the actual subquery in a new binder
35 auto subquery_binder = make_unique<Binder>(context, &binder);
36 // the subquery may refer to CTEs from the parent query
37 subquery_binder->CTE_bindings = binder.CTE_bindings;
38 auto bound_node = subquery_binder->BindNode(*expr.subquery);
39 // check the correlated columns of the subquery for correlated columns with depth > 1
40 for (idx_t i = 0; i < subquery_binder->correlated_columns.size(); i++) {
41 CorrelatedColumnInfo corr = subquery_binder->correlated_columns[i];
42 if (corr.depth > 1) {
43 // depth > 1, the column references the query ABOVE the current one
44 // add to the set of correlated columns for THIS query
45 corr.depth -= 1;
46 binder.AddCorrelatedColumn(corr);
47 }
48 }
49 if (expr.subquery_type != SubqueryType::EXISTS && bound_node->types.size() > 1) {
50 throw BinderException("Subquery returns %zu columns - expected 1", bound_node->types.size());
51 }
52 expr.subquery = make_unique<BoundSubqueryNode>(move(subquery_binder), move(bound_node), move(expr.subquery));
53 }
54 // now bind the child node of the subquery
55 if (expr.child) {
56 // first bind the children of the subquery, if any
57 string error = Bind(&expr.child, depth);
58 if (!error.empty()) {
59 return BindResult(error);
60 }
61 }
62 // both binding the child and binding the subquery was successful
63 assert(expr.subquery->type == QueryNodeType::BOUND_SUBQUERY_NODE);
64 auto bound_subquery = (BoundSubqueryNode *)expr.subquery.get();
65 auto child = (BoundExpression *)expr.child.get();
66 auto subquery_binder = move(bound_subquery->subquery_binder);
67 auto bound_node = move(bound_subquery->bound_node);
68 SQLType return_type =
69 expr.subquery_type == SubqueryType::SCALAR ? bound_node->types[0] : SQLType(SQLTypeId::BOOLEAN);
70 if (return_type.id == SQLTypeId::UNKNOWN) {
71 throw BinderException("Could not determine type of parameters: try adding explicit type casts");
72 }
73
74 auto result = make_unique<BoundSubqueryExpression>(GetInternalType(return_type));
75 if (expr.subquery_type == SubqueryType::ANY) {
76 // ANY comparison
77 // cast child and subquery child to equivalent types
78 assert(bound_node->types.size() == 1);
79 auto compare_type = MaxSQLType(child->sql_type, bound_node->types[0]);
80 child->expr = BoundCastExpression::AddCastToType(move(child->expr), child->sql_type, compare_type);
81 result->child_type = bound_node->types[0];
82 result->child_target = compare_type;
83 }
84 result->binder = move(subquery_binder);
85 result->subquery = move(bound_node);
86 result->subquery_type = expr.subquery_type;
87 result->child = child ? move(child->expr) : nullptr;
88 result->comparison_type = expr.comparison_type;
89
90 return BindResult(move(result), return_type);
91}
92