1 | #include "duckdb/parser/expression/columnref_expression.hpp" |
2 | #include "duckdb/parser/expression/constant_expression.hpp" |
3 | #include "duckdb/parser/query_node/select_node.hpp" |
4 | #include "duckdb/parser/tableref/joinref.hpp" |
5 | #include "duckdb/planner/binder.hpp" |
6 | #include "duckdb/execution/expression_executor.hpp" |
7 | #include "duckdb/planner/expression_binder/constant_binder.hpp" |
8 | #include "duckdb/planner/expression_binder/group_binder.hpp" |
9 | #include "duckdb/planner/expression_binder/having_binder.hpp" |
10 | #include "duckdb/planner/expression_binder/order_binder.hpp" |
11 | #include "duckdb/planner/expression_binder/select_binder.hpp" |
12 | #include "duckdb/planner/expression_binder/where_binder.hpp" |
13 | #include "duckdb/planner/query_node/bound_select_node.hpp" |
14 | #include "duckdb/parser/expression/table_star_expression.hpp" |
15 | |
16 | using namespace std; |
17 | |
18 | namespace duckdb { |
19 | |
20 | static int64_t BindConstant(Binder &binder, ClientContext &context, string clause, unique_ptr<ParsedExpression> &expr) { |
21 | ConstantBinder constant_binder(binder, context, clause); |
22 | auto bound_expr = constant_binder.Bind(expr); |
23 | Value value = ExpressionExecutor::EvaluateScalar(*bound_expr); |
24 | if (!TypeIsNumeric(value.type)) { |
25 | throw BinderException("LIMIT clause can only contain numeric constants!" ); |
26 | } |
27 | int64_t limit_value = value.GetValue<int64_t>(); |
28 | if (limit_value < 0) { |
29 | throw BinderException("LIMIT must not be negative" ); |
30 | } |
31 | return limit_value; |
32 | } |
33 | |
34 | unique_ptr<Expression> Binder::BindFilter(unique_ptr<ParsedExpression> condition) { |
35 | WhereBinder where_binder(*this, context); |
36 | return where_binder.Bind(condition); |
37 | } |
38 | |
39 | unique_ptr<Expression> Binder::BindOrderExpression(OrderBinder &order_binder, unique_ptr<ParsedExpression> expr) { |
40 | // we treat the Distinct list as a order by |
41 | auto bound_expr = order_binder.Bind(move(expr)); |
42 | if (!bound_expr) { |
43 | // DISTINCT ON non-integer constant |
44 | // remove the expression from the DISTINCT ON list |
45 | return nullptr; |
46 | } |
47 | assert(bound_expr->type == ExpressionType::BOUND_COLUMN_REF); |
48 | return bound_expr; |
49 | } |
50 | |
51 | unique_ptr<BoundResultModifier> Binder::BindLimit(LimitModifier &limit_mod) { |
52 | auto result = make_unique<BoundLimitModifier>(); |
53 | if (limit_mod.limit) { |
54 | result->limit = BindConstant(*this, context, "LIMIT clause" , limit_mod.limit); |
55 | result->offset = 0; |
56 | } |
57 | if (limit_mod.offset) { |
58 | result->offset = BindConstant(*this, context, "OFFSET clause" , limit_mod.offset); |
59 | if (!limit_mod.limit) { |
60 | result->limit = std::numeric_limits<int64_t>::max(); |
61 | } |
62 | } |
63 | return move(result); |
64 | } |
65 | |
66 | void Binder::BindModifiers(OrderBinder &order_binder, QueryNode &statement, BoundQueryNode &result) { |
67 | for (auto &mod : statement.modifiers) { |
68 | unique_ptr<BoundResultModifier> bound_modifier; |
69 | switch (mod->type) { |
70 | case ResultModifierType::DISTINCT_MODIFIER: { |
71 | auto &distinct = (DistinctModifier &)*mod; |
72 | auto bound_distinct = make_unique<BoundDistinctModifier>(); |
73 | for (idx_t i = 0; i < distinct.distinct_on_targets.size(); i++) { |
74 | auto expr = BindOrderExpression(order_binder, move(distinct.distinct_on_targets[i])); |
75 | if (!expr) { |
76 | continue; |
77 | } |
78 | bound_distinct->target_distincts.push_back(move(expr)); |
79 | } |
80 | bound_modifier = move(bound_distinct); |
81 | break; |
82 | } |
83 | case ResultModifierType::ORDER_MODIFIER: { |
84 | auto &order = (OrderModifier &)*mod; |
85 | auto bound_order = make_unique<BoundOrderModifier>(); |
86 | for (idx_t i = 0; i < order.orders.size(); i++) { |
87 | BoundOrderByNode node; |
88 | node.type = order.orders[i].type; |
89 | node.expression = BindOrderExpression(order_binder, move(order.orders[i].expression)); |
90 | if (!node.expression) { |
91 | continue; |
92 | } |
93 | bound_order->orders.push_back(move(node)); |
94 | } |
95 | if (bound_order->orders.size() > 0) { |
96 | bound_modifier = move(bound_order); |
97 | } |
98 | break; |
99 | } |
100 | case ResultModifierType::LIMIT_MODIFIER: |
101 | bound_modifier = BindLimit((LimitModifier &)*mod); |
102 | break; |
103 | default: |
104 | throw Exception("Unsupported result modifier" ); |
105 | } |
106 | if (bound_modifier) { |
107 | result.modifiers.push_back(move(bound_modifier)); |
108 | } |
109 | } |
110 | } |
111 | |
112 | void Binder::BindModifierTypes(BoundQueryNode &result, const vector<SQLType> &sql_types, idx_t projection_index) { |
113 | for (auto &bound_mod : result.modifiers) { |
114 | switch (bound_mod->type) { |
115 | case ResultModifierType::DISTINCT_MODIFIER: { |
116 | auto &distinct = (BoundDistinctModifier &)*bound_mod; |
117 | if (distinct.target_distincts.size() == 0) { |
118 | // DISTINCT without a target: push references to the standard select list |
119 | for (idx_t i = 0; i < sql_types.size(); i++) { |
120 | distinct.target_distincts.push_back( |
121 | make_unique<BoundColumnRefExpression>(GetInternalType(sql_types[i]), ColumnBinding(projection_index, i))); |
122 | } |
123 | } else { |
124 | // DISTINCT with target list: set types |
125 | for (idx_t i = 0; i < distinct.target_distincts.size(); i++) { |
126 | auto &expr = distinct.target_distincts[i]; |
127 | assert(expr->type == ExpressionType::BOUND_COLUMN_REF); |
128 | auto &bound_colref = (BoundColumnRefExpression &)*expr; |
129 | if (bound_colref.binding.column_index == INVALID_INDEX) { |
130 | throw BinderException("Ambiguous name in DISTINCT ON!" ); |
131 | } |
132 | assert(bound_colref.binding.column_index < sql_types.size()); |
133 | bound_colref.return_type = GetInternalType(sql_types[bound_colref.binding.column_index]); |
134 | } |
135 | } |
136 | for(idx_t i = 0; i < distinct.target_distincts.size(); i++) { |
137 | auto &bound_colref = (BoundColumnRefExpression &)*distinct.target_distincts[i]; |
138 | auto sql_type = sql_types[bound_colref.binding.column_index]; |
139 | if (sql_type.id == SQLTypeId::VARCHAR) { |
140 | distinct.target_distincts[i] = ExpressionBinder::PushCollation(context, move(distinct.target_distincts[i]), sql_type.collation, true); |
141 | } |
142 | } |
143 | break; |
144 | } |
145 | case ResultModifierType::ORDER_MODIFIER: { |
146 | auto &order = (BoundOrderModifier &)*bound_mod; |
147 | for (idx_t i = 0; i < order.orders.size(); i++) { |
148 | auto &expr = order.orders[i].expression; |
149 | assert(expr->type == ExpressionType::BOUND_COLUMN_REF); |
150 | auto &bound_colref = (BoundColumnRefExpression &)*expr; |
151 | if (bound_colref.binding.column_index == INVALID_INDEX) { |
152 | throw BinderException("Ambiguous name in ORDER BY!" ); |
153 | } |
154 | assert(bound_colref.binding.column_index < sql_types.size()); |
155 | auto sql_type = sql_types[bound_colref.binding.column_index]; |
156 | bound_colref.return_type = GetInternalType(sql_types[bound_colref.binding.column_index]); |
157 | if (sql_type.id == SQLTypeId::VARCHAR) { |
158 | order.orders[i].expression = ExpressionBinder::PushCollation(context, move(order.orders[i].expression), sql_type.collation); |
159 | } |
160 | } |
161 | break; |
162 | } |
163 | default: |
164 | break; |
165 | } |
166 | } |
167 | } |
168 | |
169 | unique_ptr<BoundQueryNode> Binder::BindNode(SelectNode &statement) { |
170 | auto result = make_unique<BoundSelectNode>(); |
171 | result->projection_index = GenerateTableIndex(); |
172 | result->group_index = GenerateTableIndex(); |
173 | result->aggregate_index = GenerateTableIndex(); |
174 | result->window_index = GenerateTableIndex(); |
175 | result->unnest_index = GenerateTableIndex(); |
176 | result->prune_index = GenerateTableIndex(); |
177 | |
178 | // first bind the FROM table statement |
179 | result->from_table = Bind(*statement.from_table); |
180 | |
181 | // visit the select list and expand any "*" statements |
182 | vector<unique_ptr<ParsedExpression>> new_select_list; |
183 | for (auto &select_element : statement.select_list) { |
184 | if (select_element->GetExpressionType() == ExpressionType::STAR) { |
185 | // * statement, expand to all columns from the FROM clause |
186 | bind_context.GenerateAllColumnExpressions(new_select_list); |
187 | } else if (select_element->GetExpressionType() == ExpressionType::TABLE_STAR) { |
188 | auto table_star = |
189 | (TableStarExpression *)select_element.get(); // TODO this cast to explicit class is a bit dirty? |
190 | bind_context.GenerateAllColumnExpressions(new_select_list, table_star->relation_name); |
191 | } else { |
192 | // regular statement, add it to the list |
193 | new_select_list.push_back(move(select_element)); |
194 | } |
195 | } |
196 | statement.select_list = move(new_select_list); |
197 | |
198 | // create a mapping of (alias -> index) and a mapping of (Expression -> index) for the SELECT list |
199 | unordered_map<string, idx_t> alias_map; |
200 | expression_map_t<idx_t> projection_map; |
201 | for (idx_t i = 0; i < statement.select_list.size(); i++) { |
202 | auto &expr = statement.select_list[i]; |
203 | result->names.push_back(expr->GetName()); |
204 | if (!expr->alias.empty()) { |
205 | alias_map[expr->alias] = i; |
206 | } |
207 | ExpressionBinder::BindTableNames(*this, *expr); |
208 | projection_map[expr.get()] = i; |
209 | result->original_expressions.push_back(expr->Copy()); |
210 | } |
211 | result->column_count = statement.select_list.size(); |
212 | |
213 | // first visit the WHERE clause |
214 | // the WHERE clause happens before the GROUP BY, PROJECTION or HAVING clauses |
215 | if (statement.where_clause) { |
216 | result->where_clause = BindFilter(move(statement.where_clause)); |
217 | } |
218 | |
219 | // now bind all the result modifiers; including DISTINCT and ORDER BY targets |
220 | OrderBinder order_binder({this}, result->projection_index, statement, alias_map, projection_map); |
221 | BindModifiers(order_binder, statement, *result); |
222 | |
223 | vector<unique_ptr<ParsedExpression>> unbound_groups; |
224 | BoundGroupInformation info; |
225 | if (statement.groups.size() > 0) { |
226 | // the statement has a GROUP BY clause, bind it |
227 | unbound_groups.resize(statement.groups.size()); |
228 | GroupBinder group_binder(*this, context, statement, result->group_index, alias_map, info.alias_map); |
229 | for (idx_t i = 0; i < statement.groups.size(); i++) { |
230 | |
231 | // we keep a copy of the unbound expression; |
232 | // we keep the unbound copy around to check for group references in the SELECT and HAVING clause |
233 | // the reason we want the unbound copy is because we want to figure out whether an expression |
234 | // is a group reference BEFORE binding in the SELECT/HAVING binder |
235 | group_binder.unbound_expression = statement.groups[i]->Copy(); |
236 | group_binder.bind_index = i; |
237 | |
238 | // bind the groups |
239 | SQLType group_type; |
240 | auto bound_expr = group_binder.Bind(statement.groups[i], &group_type); |
241 | assert(bound_expr->return_type != TypeId::INVALID); |
242 | info.group_types.push_back(group_type); |
243 | // push a potential collation, if necessary |
244 | bound_expr = ExpressionBinder::PushCollation(context, move(bound_expr), group_type.collation, true); |
245 | result->groups.push_back(move(bound_expr)); |
246 | |
247 | // in the unbound expression we DO bind the table names of any ColumnRefs |
248 | // we do this to make sure that "table.a" and "a" are treated the same |
249 | // if we wouldn't do this then (SELECT test.a FROM test GROUP BY a) would not work because "test.a" <> "a" |
250 | // hence we convert "a" -> "test.a" in the unbound expression |
251 | unbound_groups[i] = move(group_binder.unbound_expression); |
252 | ExpressionBinder::BindTableNames(*this, *unbound_groups[i]); |
253 | info.map[unbound_groups[i].get()] = i; |
254 | } |
255 | } |
256 | |
257 | // bind the HAVING clause, if any |
258 | if (statement.having) { |
259 | HavingBinder having_binder(*this, context, *result, info); |
260 | ExpressionBinder::BindTableNames(*this, *statement.having); |
261 | result->having = having_binder.Bind(statement.having); |
262 | } |
263 | |
264 | // after that, we bind to the SELECT list |
265 | SelectBinder select_binder(*this, context, *result, info); |
266 | vector<SQLType> internal_sql_types; |
267 | for (idx_t i = 0; i < statement.select_list.size(); i++) { |
268 | SQLType result_type; |
269 | auto expr = select_binder.Bind(statement.select_list[i], &result_type); |
270 | if (statement.aggregate_handling == AggregateHandling::FORCE_AGGREGATES && select_binder.BoundColumns()) { |
271 | if (select_binder.BoundAggregates()) { |
272 | throw BinderException("Cannot mix aggregates with non-aggregated columns!" ); |
273 | } |
274 | // we are forcing aggregates, and the node has columns bound |
275 | // this entry becomes a group |
276 | auto group_type = expr->return_type; |
277 | auto group_ref = make_unique<BoundColumnRefExpression>( |
278 | group_type, ColumnBinding(result->group_index, result->groups.size())); |
279 | result->groups.push_back(move(expr)); |
280 | expr = move(group_ref); |
281 | } |
282 | result->select_list.push_back(move(expr)); |
283 | if (i < result->column_count) { |
284 | result->types.push_back(result_type); |
285 | } |
286 | internal_sql_types.push_back(result_type); |
287 | if (statement.aggregate_handling == AggregateHandling::FORCE_AGGREGATES) { |
288 | select_binder.ResetBindings(); |
289 | } |
290 | } |
291 | result->need_prune = result->select_list.size() > result->column_count; |
292 | |
293 | // in the normal select binder, we bind columns as if there is no aggregation |
294 | // i.e. in the query [SELECT i, SUM(i) FROM integers;] the "i" will be bound as a normal column |
295 | // since we have an aggregation, we need to either (1) throw an error, or (2) wrap the column in a FIRST() aggregate |
296 | // we choose the former one [CONTROVERSIAL: this is the PostgreSQL behavior] |
297 | if (result->groups.size() > 0 || result->aggregates.size() > 0 || statement.having) { |
298 | if (statement.aggregate_handling == AggregateHandling::NO_AGGREGATES_ALLOWED) { |
299 | throw BinderException("Aggregates cannot be present in a Project relation!" ); |
300 | } else if (statement.aggregate_handling == AggregateHandling::STANDARD_HANDLING) { |
301 | if (select_binder.BoundColumns()) { |
302 | throw BinderException("column must appear in the GROUP BY clause or be used in an aggregate function" ); |
303 | } |
304 | } |
305 | } |
306 | |
307 | // now that the SELECT list is bound, we set the types of DISTINCT/ORDER BY expressions |
308 | BindModifierTypes(*result, internal_sql_types, result->projection_index); |
309 | return move(result); |
310 | } |
311 | |
312 | } // namespace duckdb |
313 | |