383249 {
383250 auto &update = update_op.Cast<LogicalUpdate>();
383251
383252 if (update.update_is_del_and_insert) {
383253 return;
383254 }
383255 auto needs_reinsert = false;
383256 for (auto &col_idx : update.columns) {
383257 auto &column = update.table.GetColumns().GetColumn(col_idx);
383258 if (!column.Type().SupportsRegularUpdate()) {
383259 needs_reinsert = true;
383260 break;
383261 }
383262 }
383263 if (!needs_reinsert) {
383264 return;
383265 }
383266
383267
383268
383269
383270
383271 auto rowid_binding = update_op.children.back()->GetColumnBindings().back();
383272
383273
383274
383275 auto target_binding = rowid_binding;
383276 vector<reference<unique_ptr<LogicalOperator>>> stack;
383277
383278
383279 vector<reference<unique_ptr<LogicalOperator>>> projections;
383280 unique_ptr<LogicalOperator> *get_op = nullptr;
383281
383282 stack.emplace_back(update_op.children.back());
383283
383284 auto is_done = false;
383285
383286 while (!stack.empty() && !is_done) {
383287 auto ¤t = stack.back().get();
383288 stack.pop_back();
383289
383290 switch (current->type) {
383291 case LogicalOperatorType::LOGICAL_PROJECTION: {
383292
383293 auto &proj = current->Cast<LogicalProjection>();
383294 if (target_binding.table_index == proj.table_index) {
383295
383296 projections.emplace_back(current);
383297
383298
383299 target_binding =
383300 proj.expressions[target_binding.column_index]->Cast<BoundColumnRefExpression>().binding;
383301
383302
383303 stack.push_back(proj.children.back());
383304 }
383305 } break;
383306 case LogicalOperatorType::LOGICAL_GET: {
383307
383308
383309 auto &
get = current->Cast<LogicalGet>();
383310 if (target_binding.table_index ==
get.table_index) {
383311
383312 is_done = true;
383313 get_op = ¤t;
383314 break;
383315 }
383316 } break;
383317 default: {
383318
383319 for (auto &child : current->children) {
383320 stack.emplace_back(child);
383321 }
383322 }
383323 }
383324 }
383325
383326
383327 if (!is_done || !get_op) {
383328 throw InternalException("Could not find the expected GET operator in the LogicalUpdate operator children");
383329 }
383330
383331 auto &
get = get_op->get()->Cast<LogicalGet>();
383332
383333
383334 physical_index_set_t all_columns;
383335 for (auto &column : update.table.GetColumns().Physical()) {
383336 all_columns.insert(column.Physical());
383337 }
383338
383339 idx_t found_column_count = 0;
383340 physical_index_set_t found_columns;
383341 for (
idx_t i = 0; i < update.columns.size(); i++) {
383342 if (all_columns.find(update.columns[i]) != all_columns.end()) {
383343
383344 found_column_count++;
383345 found_columns.insert(update.columns[i]);
383346 }
383347 }
383348
383349 if (found_column_count != all_columns.size()) {
383350 for (auto &physical_id : all_columns) {
383351 if (found_columns.find(physical_id) != found_columns.end()) {
383352
383353 continue;
383354 }
383355
383356 auto &column = update.table.GetColumns().GetColumn(physical_id);
383357
383358
383359 get.AddColumnId(column.Logical().index);
383360
383361
383362
383363
383364
383365 auto prev_col_idx =
get.GetColumnIds().size() - 1;
383366 auto prev_tbl_idx =
get.GetTableIndex().back();
383367
383368 for (int64_t i = UnsafeNumericCast<int64_t>(projections.size()) - 1; i >= 0; i--) {
383369 auto &proj = projections[UnsafeNumericCast<idx_t>(i)].get()->Cast<LogicalProjection>();
383370
383371 if (i == 0) {
383372
383373 proj.expressions.insert(
383374 proj.expressions.end() - 1,
383375 make_uniq<BoundColumnRefExpression>(column.Type(), ColumnBinding(prev_tbl_idx, prev_col_idx)));
383376
383377 prev_col_idx = proj.expressions.size() - 2;
383378 prev_tbl_idx = proj.table_index;
383379 } else {
383380 proj.expressions.push_back(
383381 make_uniq<BoundColumnRefExpression>(column.Type(), ColumnBinding(prev_tbl_idx, prev_col_idx)));
383382
383383 prev_col_idx = proj.expressions.size() - 1;
383384 prev_tbl_idx = proj.table_index;
383385 }
383386 }
383387
383388
383389 update.columns.push_back(physical_id);
383390 update.expressions.push_back(
383391 make_uniq<BoundColumnRefExpression>(column.Type(), ColumnBinding(prev_tbl_idx, prev_col_idx)));
383392 }
383393 }
383394
383395 update.update_is_del_and_insert = true;
383396 update.ResolveOperatorTypes();
383397}