| 419 | } |
| 420 | |
| 421 | BoundStatement Binder::BindSelectNode(SelectNode &statement, BoundStatement from_table) { |
| 422 | D_ASSERT(from_table.plan); |
| 423 | D_ASSERT(!statement.from_table); |
| 424 | auto result_ptr = make_uniq<BoundSelectNode>(); |
| 425 | auto &result = *result_ptr; |
| 426 | result.projection_index = GenerateTableIndex(); |
| 427 | result.group_index = GenerateTableIndex(); |
| 428 | result.aggregate_index = GenerateTableIndex(); |
| 429 | result.groupings_index = GenerateTableIndex(); |
| 430 | result.window_index = GenerateTableIndex(); |
| 431 | result.prune_index = GenerateTableIndex(); |
| 432 | |
| 433 | result.from_table = std::move(from_table); |
| 434 | // bind the sample clause |
| 435 | if (statement.sample) { |
| 436 | result.sample_options = std::move(statement.sample); |
| 437 | } |
| 438 | |
| 439 | // visit the select list and expand any "*" statements |
| 440 | vector<unique_ptr<ParsedExpression>> new_select_list; |
| 441 | ExpandStarExpressions(statement.select_list, new_select_list); |
| 442 | |
| 443 | if (new_select_list.empty()) { |
| 444 | throw BinderException("SELECT list is empty after resolving * expressions!"); |
| 445 | } |
| 446 | statement.select_list = std::move(new_select_list); |
| 447 | |
| 448 | auto &bind_state = result.bind_state; |
| 449 | for (idx_t i = 0; i < statement.select_list.size(); i++) { |
| 450 | auto &expr = statement.select_list[i]; |
| 451 | result.names.push_back(expr->GetName()); |
| 452 | ExpressionBinder::QualifyColumnNames(*this, expr); |
| 453 | if (!expr->GetAlias().empty()) { |
| 454 | bind_state.alias_map[expr->GetAlias()] = i; |
| 455 | result.names[i] = expr->GetAlias(); |
| 456 | } |
| 457 | bind_state.projection_map[*expr] = i; |
| 458 | bind_state.original_expressions.push_back(expr->Copy()); |
| 459 | } |
| 460 | result.column_count = statement.select_list.size(); |
| 461 | |
| 462 | // first visit the WHERE clause |
| 463 | // the WHERE clause happens before the GROUP BY, PROJECTION or HAVING clauses |
| 464 | if (statement.where_clause) { |
| 465 | // bind any star expressions in the WHERE clause |
| 466 | BindWhereStarExpression(statement.where_clause); |
| 467 | |
| 468 | ColumnAliasBinder alias_binder(bind_state); |
| 469 | WhereBinder where_binder(*this, context, &alias_binder); |
| 470 | unique_ptr<ParsedExpression> condition = std::move(statement.where_clause); |
| 471 | result.where_clause = where_binder.Bind(condition); |
| 472 | } |
| 473 | |
| 474 | // now bind all the result modifiers; including DISTINCT and ORDER BY targets |
| 475 | OrderBinder order_binder({*this}, statement, bind_state); |
| 476 | PrepareModifiers(order_binder, statement, result); |
| 477 | |
| 478 | vector<unique_ptr<ParsedExpression>> unbound_groups; |
no test coverage detected