Skip to content

Conversation

@krleonid
Copy link
Owner

This PR optimizes MERGE INTO DELETE RETURNING by passing physical columns through from the scan instead of fetching them by row ID in the fallback path.

Changes

  • Add delete_return_columns vector to LogicalMergeInto to map physical columns to their positions in the projection
  • Populate delete_return_columns in bind_merge_into.cpp when RETURNING is used with a DELETE action
  • Add serialization for the new field
  • Add tests for MERGE INTO DELETE RETURNING with generated columns

Context

This optimization mirrors the approach used in regular DELETE RETURNING (PR duckdb#20485) but extends it to MERGE INTO statements. Previously, MERGE INTO DELETE RETURNING would fall back to fetching columns by row ID, which is slower than passing them through from the scan.

Testing

  • Added tests for MERGE INTO DELETE RETURNING with generated columns
  • Existing tests pass

…scan

Instead of fetching columns by row ID in the fallback path, pass physical
columns through from the scan for MERGE INTO DELETE operations with RETURNING.

This optimization mirrors the approach used in regular DELETE RETURNING
(PR duckdb#20485) but extends it to MERGE INTO statements.

Changes:
- Add delete_return_columns vector to LogicalMergeInto to map physical columns
  to their positions in the projection
- Populate delete_return_columns in bind_merge_into.cpp when RETURNING is used
  with a DELETE action
- Add serialization for the new field
- Add tests for MERGE INTO DELETE RETURNING with generated columns
- Move BindDeleteReturningColumns helper methods from bind_update.cpp to binder.cpp
- Place them next to BindReturning for logical grouping of RETURNING-related helpers
- Add overloaded version that handles projection mapping for MERGE INTO
- Simplify bind_delete.cpp and bind_merge_into.cpp by using the shared helpers
- Add test for DELETE RETURNING with rowid (not supported)
- Add test for MERGE INTO DELETE RETURNING with rowid (not supported)
@krleonid krleonid merged commit 47c227d into main Jan 27, 2026
55 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants