-
Notifications
You must be signed in to change notification settings - Fork 690
Fix GROUP BY with aliased expressions (CASE, functions) #2398
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
Draft
Copilot
wants to merge
6
commits into
develop
Choose a base branch
from
copilot/fix-data-extraction-query
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
fe25c6a
Initial plan
Copilot c4e869a
Fix GROUP BY with aliased expressions (CASE, functions, etc.)
Copilot 85895a2
Use cloneDeep for better object cloning in GROUP BY alias resolution
Copilot 3c4f57c
Optimize GROUP BY alias lookup with map to avoid O(n*m) complexity
Copilot 3d4569f
fmt
mathiasrw 428634c
Fix GROUP BY alias resolution to not replace actual column references
Copilot 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,155 @@ | ||
| if (typeof exports === 'object') { | ||
| var assert = require('assert'); | ||
| var alasql = require('..'); | ||
| } | ||
|
|
||
| describe('Test 2361 - GROUP BY with CASE expression alias', function () { | ||
| const test = '2361'; | ||
|
|
||
| before(function () { | ||
| alasql('create database test' + test); | ||
| alasql('use test' + test); | ||
| }); | ||
|
|
||
| after(function () { | ||
| alasql('drop database test' + test); | ||
| }); | ||
|
|
||
| it('A) GROUP BY with CASE WHEN aliased expression', function () { | ||
| // Create test data with ages | ||
| var data = [{age: 25}, {age: 26}, {age: 35}, {age: 36}, {age: 45}, {age: 55}]; | ||
|
|
||
| var result = alasql( | ||
| `SELECT | ||
| CASE | ||
| WHEN age BETWEEN 20 AND 29 THEN '20-29' | ||
| WHEN age BETWEEN 30 AND 39 THEN '30-39' | ||
| WHEN age BETWEEN 40 AND 49 THEN '40-49' | ||
| WHEN age BETWEEN 50 AND 59 THEN '50-59' | ||
| ELSE '60+' | ||
| END AS age_group, | ||
| COUNT(*) AS customer_count | ||
| FROM ? | ||
| GROUP BY age_group | ||
| ORDER BY age_group`, | ||
| [data] | ||
| ); | ||
|
|
||
| var expected = [ | ||
| {age_group: '20-29', customer_count: 2}, | ||
| {age_group: '30-39', customer_count: 2}, | ||
| {age_group: '40-49', customer_count: 1}, | ||
| {age_group: '50-59', customer_count: 1}, | ||
| ]; | ||
|
|
||
| assert.deepEqual(result, expected); | ||
| }); | ||
|
|
||
| it('B) GROUP BY with CASE WHEN and ELSE clause', function () { | ||
| var data = [{age: 10}, {age: 20}, {age: 30}, {age: 100}]; | ||
|
|
||
| var result = alasql( | ||
| `SELECT | ||
| CASE | ||
| WHEN age BETWEEN 0 AND 9 THEN '0-9' | ||
| WHEN age BETWEEN 10 AND 19 THEN '10-19' | ||
| WHEN age BETWEEN 20 AND 29 THEN '20-29' | ||
| ELSE '30+' | ||
| END AS age_group | ||
| FROM ? | ||
| GROUP BY age_group`, | ||
| [data] | ||
| ); | ||
|
|
||
| // Should return three unique groups, not just '30+' | ||
| var expected = [{age_group: '10-19'}, {age_group: '20-29'}, {age_group: '30+'}]; | ||
|
|
||
| assert.deepEqual(result.sort(), expected.sort()); | ||
| }); | ||
|
|
||
| it('C) GROUP BY with function expression alias', function () { | ||
| var data = [{name: 'Alice'}, {name: 'alice'}, {name: 'Bob'}, {name: 'bob'}]; | ||
|
|
||
| var result = alasql( | ||
| `SELECT | ||
| UPPER(name) AS upper_name, | ||
| COUNT(*) AS cnt | ||
| FROM ? | ||
| GROUP BY upper_name | ||
| ORDER BY upper_name`, | ||
| [data] | ||
| ); | ||
|
|
||
| var expected = [ | ||
| {upper_name: 'ALICE', cnt: 2}, | ||
| {upper_name: 'BOB', cnt: 2}, | ||
| ]; | ||
|
|
||
| assert.deepEqual(result, expected); | ||
| }); | ||
|
|
||
| it('D) GROUP BY with multiple CASE expressions', function () { | ||
| var data = [ | ||
| {age: 25, score: 85}, | ||
| {age: 26, score: 90}, | ||
| {age: 35, score: 85}, | ||
| {age: 36, score: 90}, | ||
| ]; | ||
|
|
||
| var result = alasql( | ||
| `SELECT | ||
| CASE | ||
| WHEN age BETWEEN 20 AND 29 THEN '20-29' | ||
| ELSE '30+' | ||
| END AS age_group, | ||
| CASE | ||
| WHEN score >= 90 THEN 'High' | ||
| ELSE 'Low' | ||
| END AS score_group, | ||
| COUNT(*) AS cnt | ||
| FROM ? | ||
| GROUP BY age_group, score_group | ||
| ORDER BY age_group, score_group`, | ||
| [data] | ||
| ); | ||
|
|
||
| var expected = [ | ||
| {age_group: '20-29', score_group: 'High', cnt: 1}, | ||
| {age_group: '20-29', score_group: 'Low', cnt: 1}, | ||
| {age_group: '30+', score_group: 'High', cnt: 1}, | ||
| {age_group: '30+', score_group: 'Low', cnt: 1}, | ||
| ]; | ||
|
|
||
| assert.deepEqual(result, expected); | ||
| }); | ||
|
|
||
| it('E) GROUP BY with WHERE and CASE expression alias', function () { | ||
| var data = [ | ||
| {age: 25, active: true}, | ||
| {age: 26, active: false}, | ||
| {age: 35, active: true}, | ||
| {age: 36, active: true}, | ||
| ]; | ||
|
|
||
| var result = alasql( | ||
| `SELECT | ||
| CASE | ||
| WHEN age BETWEEN 20 AND 29 THEN '20-29' | ||
| ELSE '30+' | ||
| END AS age_group, | ||
| COUNT(*) AS cnt | ||
| FROM ? | ||
| WHERE active = true | ||
| GROUP BY age_group | ||
| ORDER BY age_group`, | ||
| [data] | ||
| ); | ||
|
|
||
| var expected = [ | ||
| {age_group: '20-29', cnt: 1}, | ||
| {age_group: '30+', cnt: 2}, | ||
| ]; | ||
|
|
||
| assert.deepEqual(result, expected); | ||
| }); | ||
| }); |
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.
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.