Find delim joins that contain an UNNEST.
327118 {
327119
327120 for (auto &child : op->children) {
327122 }
327123
327124
327125 if (op->children.size() != 1) {
327126 return;
327127 }
327128 if (op->children[0]->type != LogicalOperatorType::LOGICAL_DELIM_JOIN) {
327129 return;
327130 }
327131
327132
327133 auto &delim_join = op->children[0]->Cast<LogicalComparisonJoin>();
327134
327135 if (delim_join.join_type != JoinType::INNER) {
327136 return;
327137 }
327138
327139 if (delim_join.conditions.size() != 1) {
327140 return;
327141 }
327142
327143
327144 idx_t delim_idx = delim_join.delim_flipped ? 1 : 0;
327145 idx_t other_idx = 1 - delim_idx;
327146 if (delim_join.children[delim_idx]->type != LogicalOperatorType::LOGICAL_WINDOW) {
327147 return;
327148 }
327149
327150
327151 auto curr_op = &delim_join.children[other_idx];
327152 while (curr_op->get()->type == LogicalOperatorType::LOGICAL_PROJECTION) {
327153 if (curr_op->get()->children.size() != 1) {
327154 break;
327155 }
327156 curr_op = &curr_op->get()->children[0];
327157 }
327158
327159
327160 if (curr_op->get()->type == LogicalOperatorType::LOGICAL_UNNEST &&
327161 curr_op->get()->children[0]->type == LogicalOperatorType::LOGICAL_DELIM_GET) {
327162 candidates.push_back(op);
327163 return;
327164 }
327165
327166 curr_op = &delim_join.children[other_idx];
327167 if (curr_op->get()->type == LogicalOperatorType::LOGICAL_GET) {
327168 auto &
get = curr_op->get()->Cast<LogicalGet>();
327170 return;
327171 }
327172
327173 auto &unnest_get_ref = curr_op->get()->Cast<LogicalGet>();
327174 if (unnest_get_ref.ordinality_idx.IsValid()) {
327175
327176 return;
327177 }
327178 curr_op = &curr_op->get()->children[0];
327179
327180
327181 if (curr_op->get()->type == LogicalOperatorType::LOGICAL_PROJECTION &&
327182 curr_op->get()->children[0]->type == LogicalOperatorType::LOGICAL_DELIM_GET) {
327183
327184
327185
327186
327187 {
327188 auto &pre_get = *delim_join.children[other_idx];
327189 pre_get.ResolveOperatorTypes();
327190 auto pre_columns = pre_get.GetColumnBindings();
327191 auto pre_tbl_idx = pre_get.GetTableIndex()[0];
327192 auto &pre_proj = curr_op->get()->Cast<LogicalProjection>();
327193 for (
idx_t i = 0; i < pre_columns.size(); i++) {
327194 auto &col_bind = pre_columns[i];
327195 auto is_unnest_column = col_bind.table_index == pre_tbl_idx;
327196 auto is_column_ref = pre_proj.expressions[col_bind.column_index]->GetExpressionClass() ==
327197 ExpressionClass::BOUND_COLUMN_REF;
327198 if (is_unnest_column && !is_column_ref) {
327199 return;
327200 }
327201 }
327202 }
327203 auto unnest_get = std::move(delim_join.children[other_idx]);
327204
327205 ColumnBindingReplacer replacer;
327206 auto unnest_get_column = unnest_get->GetColumnBindings();
327207 auto &proj = curr_op->get()->Cast<LogicalProjection>();
327208 auto delim_get = std::move(proj.children[0]);
327209 auto unnest = make_uniq<LogicalUnnest>(unnest_get->GetTableIndex()[0]);
327210 unnest->children.push_back(std::move(delim_get));
327211 delim_join.children[other_idx] = std::move(*curr_op);
327212 for (
idx_t i = 0; i < unnest_get_column.size(); i++) {
327213 auto &col_bind = unnest_get_column[i];
327214 D_ASSERT(col_bind.table_index == unnest_get->GetTableIndex()[0] ||
327215 col_bind.table_index == proj.table_index);
327216 if (col_bind.table_index == unnest_get->GetTableIndex()[0]) {
327217
327218 D_ASSERT(proj.expressions[col_bind.column_index]->GetExpressionClass() ==
327219 ExpressionClass::BOUND_COLUMN_REF);
327220 auto &bind_col = proj.expressions[col_bind.column_index]->Cast<BoundColumnRefExpression>();
327221 auto unnest_expr = make_uniq<BoundUnnestExpression>(unnest_get->types[i]);
327222 unnest_expr->child = proj.expressions[col_bind.column_index]->Copy();
327223 bind_col.binding = ColumnBinding(unnest->GetTableIndex()[0], bind_col.binding.column_index);
327224 unnest->expressions.push_back(std::move(unnest_expr));
327225 auto new_column_ref = ColumnBinding(bind_col.binding.table_index, unnest->expressions.size() - 1);
327226 auto unnest_ref = make_uniq<BoundColumnRefExpression>(bind_col.alias, unnest_get->types[i],
327227 new_column_ref, bind_col.depth);
327228 proj.expressions[col_bind.column_index] = std::move(unnest_ref);
327229 proj.types[col_bind.column_index] = unnest_get->types[i];
327230 replacer.replacement_bindings.push_back(ReplacementBinding(
327231 col_bind, ColumnBinding(proj.table_index, col_bind.column_index), unnest_get->types[i]));
327232 }
327233 }
327234 proj.children[0] = std::move(unnest);
327235 replacer.stop_operator = proj;
327236 replacer.VisitOperator(*root);
327237 candidates.push_back(op);
327238 }
327239 }
327240}
static bool IsUnnestFunction(const string &function_name)
Returns true if the function name is an alias for the UNNEST function.
Definition duckdb.cpp:379192
__host__ cudaTextureObject_t get() const noexcept
uint64_t idx_t
a saner size_t for loop indices etc
Definition duckdb.hpp:237