1#include "duckdb/planner/expression_binder/qualify_binder.hpp"
2
3#include "duckdb/parser/expression/columnref_expression.hpp"
4#include "duckdb/planner/binder.hpp"
5#include "duckdb/planner/expression_binder/aggregate_binder.hpp"
6#include "duckdb/common/string_util.hpp"
7#include "duckdb/planner/query_node/bound_select_node.hpp"
8#include "duckdb/parser/expression/window_expression.hpp"
9
10namespace duckdb {
11
12QualifyBinder::QualifyBinder(Binder &binder, ClientContext &context, BoundSelectNode &node, BoundGroupInformation &info,
13 case_insensitive_map_t<idx_t> &alias_map)
14 : BaseSelectBinder(binder, context, node, info), column_alias_binder(node, alias_map) {
15 target_type = LogicalType(LogicalTypeId::BOOLEAN);
16}
17
18BindResult QualifyBinder::BindColumnRef(unique_ptr<ParsedExpression> &expr_ptr, idx_t depth, bool root_expression) {
19 auto &expr = expr_ptr->Cast<ColumnRefExpression>();
20 auto result = duckdb::BaseSelectBinder::BindExpression(expr_ptr, depth);
21 if (!result.HasError()) {
22 return result;
23 }
24
25 auto alias_result = column_alias_binder.BindAlias(enclosing_binder&: *this, expr, depth, root_expression);
26 if (!alias_result.HasError()) {
27 return alias_result;
28 }
29
30 return BindResult(StringUtil::Format(fmt_str: "Referenced column %s not found in FROM clause and can't find in alias map.",
31 params: expr.ToString()));
32}
33
34BindResult QualifyBinder::BindExpression(unique_ptr<ParsedExpression> &expr_ptr, idx_t depth, bool root_expression) {
35 auto &expr = *expr_ptr;
36 // check if the expression binds to one of the groups
37 auto group_index = TryBindGroup(expr, depth);
38 if (group_index != DConstants::INVALID_INDEX) {
39 return BindGroup(expr, depth, group_index);
40 }
41 switch (expr.expression_class) {
42 case ExpressionClass::WINDOW:
43 return BindWindow(expr&: expr.Cast<WindowExpression>(), depth);
44 case ExpressionClass::COLUMN_REF:
45 return BindColumnRef(expr_ptr, depth, root_expression);
46 default:
47 return duckdb::BaseSelectBinder::BindExpression(expr_ptr, depth);
48 }
49}
50
51} // namespace duckdb
52