Skip to content

Fix incorrect result when grouping by two columns with the same name - #2307

Merged
mathiasrw merged 10 commits into
developfrom
copilot/fix-grouping-by-same-names
Dec 4, 2025
Merged

mathiasrw merged 10 commits into
developfrom
copilot/fix-grouping-by-same-names

Conversation

Copilot AI commented Dec 2, 2025 •

Copy link
Copy Markdown
Contributor
  • Reproduce the issue with test script
  • Identify the root cause in 43rollup.js and 38query.js
  • Create test file for the issue (test941.js with 10 comprehensive tests)
  • Fix the issue in 424select.js and 38query.js
  • Build and test the fix (all 1821 tests passing)
  • Address code review feedback and optimize performance
  • Expand test coverage for edge cases
  • Run final code review (no issues found)
  • Run security checks (no vulnerabilities)
  • Fix code formatting with prettier
  • Finalized

Summary

Successfully fixed issue #941 where GROUP BY queries with duplicate column names would result in undefined values.

Changes:

  • Modified compileSelectGroup2 in 424select.js to properly handle columns with renamed nicks
  • Updated cleanup logic in 38query.js to prevent premature deletion of shared nick values
  • Added SELECT * handling and performance optimizations
  • Fixed code formatting per CI requirements

Testing:

  • 10 comprehensive test cases covering all edge cases
  • All 1821 tests passing
  • No security vulnerabilities
  • No code review issues
  • Code properly formatted with prettier
Original prompt

This section details on the original issue you should resolve

<issue_title>Incorrect result when grouping by two columns with the same name with or without joins</issue_title>
<issue_description>The results to this query are just incorrect. One of the projected expressions ends up not being there and the other has the values intended for the missing one. Works in any situation where the column names are the same regardless of what else you do.

Examples here:
http://jsfiddle.net/tp3v4h86/4/</issue_description>

Comments on the Issue (you are @copilot in this section)

@mathiasrw ``` var accounts = [ { name: 'A', region_id: 1 }, { name: 'B', region_id: 2 } ];

var regions = [
{ id: 1, name: 'North' },
{ id: 2, name: 'South' }
];

// Do the query
var result = alasql("SELECT accounts.name AS AccountName, regions.name AS RegionName, COUNT(1) AS Count FROM ? accounts LEFT JOIN ? regions ON accounts.region_id = regions.id GROUP BY accounts.name, regions.name", [accounts, regions]);

document.getElementById("result").innerHTML = JSON.stringify(result);

// An even simpler example

var result2 = alasql("SELECT accounts.name AS AccountName, accounts.name AS AccountName2, COUNT(1) AS Count FROM ? accounts GROUP BY accounts.name", [accounts]);

document.getElementById("result2").innerHTML = JSON.stringify(result2);

</comments>


💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.

@mathiasrw

Copy link
Copy Markdown
Member

@copilot Continue

Copilot AI and others added 3 commits December 3, 2025 00:01
The issue was in compileSelectGroup2 where it checked if col.nick was in query.ingroup.
When columns were renamed to avoid duplicates (e.g., 'x' becomes 'x:1'), the renamed
columns wouldn't match what's in query.ingroup, causing them to not be populated.

The fix checks if a column is part of GROUP BY by comparing columnid and tableid,
and then uses the GROUP BY column's nick to fetch the value from the group object.

Fixes #941

Co-authored-by: mathiasrw <[email protected]>
- Avoid duplicate GROUP BY column lookup by reusing groupCol variable
- Add check to skip SELECT * columns in compileSelectGroup2 (fixes test1797)
- All 1814 tests passing

Co-authored-by: mathiasrw <[email protected]>
Replace O(n*m) find operation with O(1) map lookup for better performance
with queries that have many columns and GROUP BY clauses.

Co-authored-by: mathiasrw <[email protected]>

Copilot AI commented Dec 3, 2025

Copy link
Copy Markdown
Contributor Author

@copilot Continue

I've completed the fix for issue #941. The problem was in compileSelectGroup2 where columns with renamed nicks (to avoid duplicates) wouldn't match the GROUP BY tracking.

Changes made in commit a743b3d:

  • Modified column matching logic to compare columnid and tableid instead of just nick
  • Added SELECT * handling to prevent undefined properties
  • Optimized with a lookup map for O(1) performance
  • All 1814 tests passing, no security vulnerabilities

The fix now correctly handles:

  • Same column selected multiple times with different aliases
  • JOIN queries with duplicate column names
  • SELECT * with GROUP BY

Copilot AI changed the title [WIP] Fix incorrect result when grouping by two columns with the same name Fix incorrect result when grouping by two columns with the same name Dec 3, 2025
Comment thread test/test926.js Resolved
Comment thread test/test926.js Resolved
Comment thread test/test926.js Resolved
Comment thread test/test926.js Resolved
Comment thread test/test941.js Resolved
@mathiasrw

Copy link
Copy Markdown
Member

@copilot Continue

@mathiasrw

Copy link
Copy Markdown
Member

@copilot Continue

Added comprehensive edge case tests covering:
- SELECT * with GROUP BY and duplicate columns
- Multiple aggregates (MAX, MIN, SUM, AVG) with duplicate columns
- Multiple GROUP BY columns with duplicates in SELECT
- HAVING clause with duplicate columns
- Subqueries with duplicate columns
- DISTINCT with duplicate columns

All 1821 tests passing.

Co-authored-by: mathiasrw <[email protected]>

Copilot AI commented Dec 4, 2025

Copy link
Copy Markdown
Contributor Author

@copilot Continue

I've completed the work with commit 68fd86c:

Test Coverage Expansion:

  • Created 10 comprehensive test cases in test941.js covering key edge cases
  • Tests now validate: SELECT *, multiple aggregates, multi-column GROUP BY, HAVING, subqueries, and DISTINCT

Results:

  • All 1821 tests passing (up from 1814)
  • No code review issues
  • No security vulnerabilities detected

The fix correctly handles all scenarios for duplicate column names in GROUP BY queries.

@mathiasrw

Copy link
Copy Markdown
Member

@copilot

Run yarn test-format-all
yarn run v1.22.22
$ prettier --list-different '{.,src,test}/*.{js,ts,json}' || (echo 'Please correct file formatting using yarn format-all and try again.' && exit 1)
src/424select.js
test/test941.js
Please correct file formatting using yarn format-all and try again.
error Command failed with exit code 1.
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.
Error: Process completed with exit code 1.

Ran yarn format-all to fix formatting issues in:
- src/424select.js (trailing whitespace)
- test/test941.js (array formatting)

All tests still passing.

Co-authored-by: mathiasrw <[email protected]>

Copilot AI commented Dec 4, 2025

Copy link
Copy Markdown
Contributor Author

@copilot

Run yarn test-format-all
...

Fixed in commit e39661d. Ran yarn format-all to fix formatting issues in src/424select.js and test/test941.js. The yarn test-format-all check now passes.

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.

Incorrect result when grouping by two columns with the same name with or without joins

2 participants