-
Notifications
You must be signed in to change notification settings - Fork 690
[WIP] Implement MERGE statement execution logic #2410
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: develop
Are you sure you want to change the base?
Conversation
|
@copilot Continue |
- Added compile() method to yy.Merge in src/75merge.js - Implements WHEN MATCHED THEN UPDATE/DELETE - Implements WHEN NOT MATCHED [BY TARGET] THEN INSERT - Implements WHEN NOT MATCHED BY SOURCE THEN DELETE/UPDATE - Supports conditional expressions with AND clauses - Updated test236 to actually execute and verify MERGE statement - All test236 tests now passing Co-authored-by: mathiasrw <1063454+mathiasrw@users.noreply.github.com>
I've implemented the MERGE statement execution logic! The implementation is now complete and working. What's been done:
Test results:
The MERGE statement now correctly performs upsert operations by matching rows from a source table into a target table based on a join condition, then executing different actions (INSERT, UPDATE, DELETE) depending on whether rows match or not. |
- Created test236-B.js with 8 additional test scenarios - Tests cover: basic INSERT/UPDATE, DELETE when matched, conditional INSERT, DELETE BY SOURCE, multiple WHEN clauses, no matches, all matches, and complex ON conditions - All 26 MERGE tests passing (18 from test236 + 8 from test236-B) Co-authored-by: mathiasrw <1063454+mathiasrw@users.noreply.github.com>
|
@copilot Continue |
- Remove unused matchedCount and matchedTargetIndices variables - Remove invalid UPDATE BY SOURCE branch (can't update without source row) - Add comment explaining why UPDATE BY SOURCE is not supported - All 26 MERGE tests still passing Co-authored-by: mathiasrw <1063454+mathiasrw@users.noreply.github.com>
The MERGE statement implementation is complete and ready for review! Final Status:
What's Implemented:
Note: PostgreSQL's |
- Reduced file size from 292 to 227 lines (22% reduction) - Extracted buildContext() helper to eliminate code duplication - Created executeMatch() helper to consolidate match processing logic - Replaced nested loops with Array.find/some/filter for cleaner code - Simplified variable naming (counts object instead of separate counters) - Removed verbose comments, keeping only essential documentation - All 2337 tests still passing Co-authored-by: mathiasrw <1063454+mathiasrw@users.noreply.github.com>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is a crazy amount of code for a probolem like this. Can you please review and see if we can find a more elegant solution?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Dramatically simplified (commit a931227):
Reduction: 292 → 157 lines (46% smaller)
Key changes:
- Removed pre-compilation: Uses closures and evaluates expressions on-demand instead of building JavaScript functions
- Single
evalInContext()helper replaces all the function builders - Unified
executeAction()handles all match types in one place - Eliminated intermediate data structures
The code is now much more straightforward while maintaining all functionality. All 2337 tests passing.
test/test236.js
Outdated
| var start = sql.indexOf('/*') + 2; | ||
| var end = sql.lastIndexOf('*/'); | ||
| sql = sql.substring(start, end).trim(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What is going on here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Lines 67-69 extract SQL from a multiline comment embedded in a function. The pattern:
- Define a function with SQL in a
/* */comment - Call
.toString()to get the function source as a string - Use
indexOf('/*')andlastIndexOf('*/')to extract just the SQL
I've added clearer comments in commit a931227 to explain this pattern.
- Reduced from 227 to 157 lines (31% smaller, 46% from original) - Removed pre-compilation of functions - use closures and on-demand evaluation - Single evalInContext() helper replaces multiple function builders - Unified executeAction() handles all match types in one place - Eliminated intermediate data structures (compiledMatches, counts object) - Clearer test code with better comments explaining SQL extraction All 2337 tests still passing Co-authored-by: mathiasrw <1063454+mathiasrw@users.noreply.github.com>
Summary
The MERGE statement is now fully implemented, tested, and dramatically simplified!
What Works
✅ MERGE INTO ... USING ... ON - Full syntax support
✅ WHEN MATCHED THEN UPDATE - Updates matching rows
✅ WHEN MATCHED THEN DELETE - Deletes matching rows
✅ WHEN NOT MATCHED THEN INSERT - Inserts non-matching source rows
✅ WHEN NOT MATCHED BY SOURCE THEN DELETE - Deletes target rows not in source (with AND condition)
✅ Conditional clauses - AND expressions for fine-grained control
✅ Complex ON conditions - Multi-column join conditions
✅ Multiple WHEN clauses - First matching clause executes per row
Code Quality
Test Coverage
Security
Known Limitations
INSERT ... ON CONFLICTsyntax is NOT implemented (requires parser changes)UPDATE BY SOURCEwithout referencing source columns is not supported (semantically invalid)Original prompt
This section details on the original issue you should resolve
<issue_title>UPSERT/MERGE Statement Not Implemented</issue_title>
<issue_description>## Summary
The MERGE statement syntax is parsed by AlaSQL (
src/75merge.js) but the execution logic is not implemented. Theexecute()method simply returns1without performing any insert, update, or delete operations. Additionally, PostgreSQL-styleINSERT ... ON CONFLICT(UPSERT) syntax is not supported.Current Behavior
Expected Behavior - MERGE Statement
Example 1: Basic MERGE with INSERT and UPDATE
Example 2: MERGE with DELETE
Example 3: MERGE with Conditional Logic
Expected Behavior - PostgreSQL UPSERT (ON CONFLICT)
Example 1: INSERT with UPDATE on Conflict
Example 2: INSERT with DO NOTHING on Conflict
Example 3: Conditional UPSERT
Use Cases
1. Synchronizing Data
2. Maintaining User Sessions
3. Deduplication
Implementation Status
Currently Parsed (src/75merge.js)
Not Implemented
1)💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.