forked from jackskj/carta
-
Notifications
You must be signed in to change notification settings - Fork 0
adjustments made to getting unique row id #1
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
Merged
Merged
Changes from 3 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
8b4f5c3
adjustments made to getting unique row id
tmaiaroto 0553cf8
optimizations and more test coverage as per coderabbits good suggestions
tmaiaroto c5e661c
📝 Add docstrings to `fix-duplicate-rows`
coderabbitai[bot] 5306111
update readme to note the change in de-duplication behavior
tmaiaroto d20e33d
Merge pull request #2 from hackafterdark/coderabbitai/docstrings/0553cf8
tmaiaroto 62bb4de
account for the issue coderabbit found with exactly one column errors…
tmaiaroto e8a2522
Merge branch 'fix-duplicate-rows' of github.com:hackafterdark/carta i…
tmaiaroto 2293378
suggseted rabbit changes and enhancment for trimming tag whitespace
tmaiaroto File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,37 @@ | ||
| # Problem: Incorrect De-duplication When Mapping to Basic Slices | ||
|
|
||
| ## Summary | ||
| The `carta` library is designed to de-duplicate entities when mapping SQL rows to slices of structs (e.g., `[]User`). This is achieved by generating a unique ID for each entity based on the content of its primary key columns. This behavior is correct for handling `JOIN`s where a single entity might appear across multiple rows. | ||
|
|
||
| However, this same logic is incorrectly applied when the mapping destination is a slice of a basic type (e.g., `[]string`, `[]int`). In this scenario, rows with duplicate values are treated as the same entity and are de-duplicated, which is incorrect. The desired behavior is to preserve every row from the result set, including duplicates. | ||
|
|
||
| This issue is the root cause for the following problems: | ||
| 1. The `if m.IsBasic` code path in `load.go` lacks test coverage because no tests exist for mapping to basic slices. | ||
| 2. Attempts to write such tests lead to infinite loops and incorrect behavior because the column allocation and unique ID generation logic are not designed to handle this case. | ||
|
|
||
| ## Proposed Solution | ||
| The solution is to create a distinct execution path for "basic mappers" (`m.IsBasic == true`) that ensures every row is treated as a unique element. | ||
|
|
||
| This will be accomplished in two main steps: | ||
|
|
||
| ### 1. Fix Column Allocation (`allocateColumns`) | ||
| The logic will be modified to enforce a clear rule for basic slices: the source SQL query must return **exactly one column**. | ||
|
|
||
| - If `m.IsBasic` is true, the function will bypass the existing name-matching logic. | ||
| - It will validate that only one column is present in the query result. | ||
| - This single column will be assigned as the `PresentColumn` for the mapper. | ||
| - If more than one column is found, the function will return an error to prevent ambiguity. | ||
|
|
||
| ### 2. Fix Unique ID Generation (`loadRow`) | ||
| The logic will be modified to generate a unique ID based on the row's position rather than its content. | ||
|
|
||
| - If `m.IsBasic` is true, the call to `getUniqueId(row, m)` will be bypassed. | ||
| - A new, position-based unique ID will be generated for each row (e.g., using a simple counter that increments with each row processed). | ||
| - This ensures that every row, regardless of its content, is treated as a distinct element to be added to the destination slice. | ||
|
|
||
| This approach preserves the existing, correct behavior for struct mapping while introducing a new, robust path for handling basic slices correctly. | ||
|
|
||
| ## Plan | ||
| 1. **Modify `column.go`**: Update the `allocateColumns` function to implement the single-column rule for basic mappers. | ||
| 2. **Modify `load.go`**: Update the `loadRow` function to use a position-based counter for unique ID generation when `m.IsBasic` is true. | ||
| 3. **Add Tests**: Create a new test case in `mapper_test.go` that maps a query result to a slice of a basic type (e.g., `[]string`) to validate the fix and provide coverage for the `m.IsBasic` code path. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.