1#include "duckdb/planner/subquery/has_correlated_expressions.hpp"
2
3#include "duckdb/planner/expression/bound_columnref_expression.hpp"
4#include "duckdb/planner/expression/bound_subquery_expression.hpp"
5
6using namespace duckdb;
7using namespace std;
8
9HasCorrelatedExpressions::HasCorrelatedExpressions(const vector<CorrelatedColumnInfo> &correlated)
10 : has_correlated_expressions(false), correlated_columns(correlated) {
11}
12
13void HasCorrelatedExpressions::VisitOperator(LogicalOperator &op) {
14 //! The HasCorrelatedExpressions does not recursively visit logical operators, it only visits the current one
15 VisitOperatorExpressions(op);
16}
17
18unique_ptr<Expression> HasCorrelatedExpressions::VisitReplace(BoundColumnRefExpression &expr,
19 unique_ptr<Expression> *expr_ptr) {
20 if (expr.depth == 0) {
21 return nullptr;
22 }
23 // correlated column reference
24 assert(expr.depth == 1);
25 has_correlated_expressions = true;
26 return nullptr;
27}
28
29unique_ptr<Expression> HasCorrelatedExpressions::VisitReplace(BoundSubqueryExpression &expr,
30 unique_ptr<Expression> *expr_ptr) {
31 if (!expr.IsCorrelated()) {
32 return nullptr;
33 }
34 // check if the subquery contains any of the correlated expressions that we are concerned about in this node
35 for (idx_t i = 0; i < correlated_columns.size(); i++) {
36 if (std::find(expr.binder->correlated_columns.begin(), expr.binder->correlated_columns.end(),
37 correlated_columns[i]) != expr.binder->correlated_columns.end()) {
38 has_correlated_expressions = true;
39 break;
40 }
41 }
42 return nullptr;
43}
44