310406 {
310407 if (op.type == LogicalOperatorType::LOGICAL_COMPARISON_JOIN) {
310408 if (!parent) {
310409 return;
310410 }
310411 D_ASSERT(!pipe_info.join_parent);
310412 pipe_info.join_parent = parent;
310413 pipe_info.join_index = idx;
310414 left_child = CreateChildren();
310415 right_child = CreateChildren();
310416 left_child->OptimizeInternal(std::move(op.children[0]));
310417 right_child->OptimizeInternal(std::move(op.children[1]));
310418 return;
310419 }
310420
310422
310423 switch (op.type) {
310424 case LogicalOperatorType::LOGICAL_DISTINCT: {
310425 auto &distinct = op.Cast<LogicalDistinct>();
310426 if (distinct.distinct_type != DistinctType::DISTINCT) {
310427 break;
310428 }
310429 column_binding_set_t distinct_group;
310430 if (distinct.distinct_targets[0]->type != ExpressionType::BOUND_COLUMN_REF) {
310431 break;
310432 }
310433 idx_t table_idx = distinct.distinct_targets[0]->Cast<BoundColumnRefExpression>().binding.table_index;
310434 bool can_add = true;
310435 for (auto &target : distinct.distinct_targets) {
310436 if (target->type != ExpressionType::BOUND_COLUMN_REF) {
310437 can_add = false;
310438 break;
310439 }
310440 auto &col_ref = target->Cast<BoundColumnRefExpression>();
310441 distinct_group.insert(col_ref.binding);
310442 D_ASSERT(table_idx == col_ref.binding.table_index);
310443 }
310444 if (can_add) {
310445 pipe_info.distinct_groups[table_idx] = std::move(distinct_group);
310446 }
310447 break;
310448 }
310449 case LogicalOperatorType::LOGICAL_AGGREGATE_AND_GROUP_BY: {
310450 auto &aggr = op.Cast<LogicalAggregate>();
310451 if (aggr.grouping_sets.size() > 1) {
310452 break;
310453 }
310454
310455 column_binding_set_t distinct_group;
310456 idx_t table_idx = aggr.group_index;
310457 for (
idx_t i = 0; i < aggr.groups.size(); i++) {
310458 distinct_group.insert(ColumnBinding(aggr.group_index, i));
310459 }
310460 if (!distinct_group.empty()) {
310461 pipe_info.distinct_groups[table_idx] = std::move(distinct_group);
310462 }
310463 break;
310464 }
310465 case LogicalOperatorType::LOGICAL_PROJECTION: {
310466 auto &projection = op.Cast<LogicalProjection>();
310467 unordered_map<idx_t, vector<idx_t>> reference_records;
310468
310469
310470
310471
310472 auto it = pipe_info.distinct_groups.find(projection.table_index);
310473 if (it != pipe_info.distinct_groups.end()) {
310474 column_binding_set_t new_distinct_group;
310475 auto &expression = projection.expressions.get(it->second.begin()->column_index);
310476 if (expression->GetExpressionType() != ExpressionType::BOUND_COLUMN_REF) {
310477
310478 break;
310479 }
310480 bool could_add = true;
310481 idx_t ref_id = expression->Cast<BoundColumnRefExpression>().binding.table_index;
310482 for (auto &col : it->second) {
310483 auto &expression = projection.expressions.get(col.column_index);
310484 if (expression->GetExpressionType() != ExpressionType::BOUND_COLUMN_REF) {
310485
310486 could_add = false;
310487 break;
310488 }
310489 auto &col_ref = expression->Cast<BoundColumnRefExpression>();
310490 if (ref_id != col_ref.binding.table_index) {
310491 could_add = false;
310492 break;
310493 }
310494 new_distinct_group.insert(col_ref.binding);
310495 }
310496 if (could_add) {
310497 pipe_info.distinct_groups[ref_id] = std::move(new_distinct_group);
310498 }
310499 }
310500 break;
310501 }
310502 case LogicalOperatorType::LOGICAL_GET: {
310503 auto &
get = op.Cast<LogicalGet>();
310504 if (!
get.table_filters.filters.empty()) {
310505 pipe_info.has_filter = true;
310506 }
310507 break;
310508 }
310509 default:
310510 break;
310511 }
310512
310513 if (op.children.size() == 1) {
310514 OptimizeChildren(*op.children[0], op, idx);
310515 } else {
310516 children_root = op;
310517 for (auto &child : op.children) {
310518 auto child_optimizer = CreateChildren();
310519 child_optimizer->OptimizeInternal(std::move(child));
310520 children.emplace_back(std::move(child_optimizer));
310521 }
310522 return;
310523 }
310524
310525 switch (op.type) {
310526 case LogicalOperatorType::LOGICAL_PROJECTION: {
310527 auto &projection = op.Cast<LogicalProjection>();
310528
310529 unordered_map<idx_t, DistinctGroupRef> ref_table_columns;
310530 for (
idx_t idx = 0; idx < projection.expressions.size(); idx++) {
310531 auto &expression = projection.expressions.get(idx);
310532 if (expression->GetExpressionType() == ExpressionType::BOUND_COLUMN_REF) {
310533 auto &col_ref = expression->Cast<BoundColumnRefExpression>();
310534 auto distinct_group_it = pipe_info.distinct_groups.find(col_ref.binding.table_index);
310535 if (distinct_group_it == pipe_info.distinct_groups.end()) {
310536 continue;
310537 }
310538 if (ref_table_columns.find(col_ref.binding.table_index) == ref_table_columns.end()) {
310539 auto ref = DistinctGroupRef();
310540 for (auto &col : distinct_group_it->second) {
310541 ref.ref_column_ids.insert(col.column_index);
310542 }
310543 ref_table_columns[col_ref.binding.table_index] = ref;
310544 }
310545 ref_table_columns[col_ref.binding.table_index].distinct_group.insert(
310546 ColumnBinding(projection.table_index, idx));
310547 ref_table_columns[col_ref.binding.table_index].ref_column_ids.erase(col_ref.binding.column_index);
310548 }
310549 }
310550 for (auto &refs : ref_table_columns) {
310551 if (refs.second.ref_column_ids.empty()) {
310552 pipe_info.distinct_groups[projection.table_index] = std::move(refs.second.distinct_group);
310553 }
310554 }
310555 break;
310556 }
310557 default:
310558 D_ASSERT(op.type != LogicalOperatorType::LOGICAL_COMPARISON_JOIN);
310559 break;
310560 }
310561}
void VisitOperatorExpressions(LogicalOperator &op)
Definition duckdb.cpp:381188
__host__ cudaTextureObject_t get() const noexcept
uint64_t idx_t
a saner size_t for loop indices etc
Definition duckdb.hpp:237