-
Notifications
You must be signed in to change notification settings - Fork 690
Implement SQL-99 ROW_NUMBER() OVER (PARTITION BY) syntax for per-group row numbering #2208
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
7 commits
Select commit
Hold shift + click to select a range
9a3d16b
Initial plan
Copilot 1294d0a
Initial implementation of GROUP_ROW_NUMBER - work in progress
Copilot 74e0d43
Implement GROUP_ROW_NUMBER() function for per-group row numbering
Copilot 4079787
Implement SQL-99 ROW_NUMBER() OVER (PARTITION BY) syntax
Copilot 0d76e04
Merge branch 'develop' into copilot/implement-group-row-numbers
mathiasrw f774410
Clean up test structure and remove backup file
Copilot ec6270e
Merge branch 'develop' into copilot/implement-group-row-numbers
mathiasrw 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
Some comments aren't visible on the classic Files Changed page.
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
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,98 @@ | ||
| if (typeof exports === 'object') { | ||
| var assert = require('assert'); | ||
| var alasql = require('..'); | ||
| } | ||
|
|
||
| describe('Test 1126 GROUP_ROW_NUMBER()', function () { | ||
| it('1. CREATE DATABASE', function (done) { | ||
| alasql('CREATE DATABASE test1126;USE test1126'); | ||
| done(); | ||
| }); | ||
|
|
||
| it('2. Basic GROUP_ROW_NUMBER() - per group row numbering with first column partitioning', function (done) { | ||
| var data = [ | ||
| {category: 'A', amount: 10}, | ||
| {category: 'A', amount: 20}, | ||
| {category: 'A', amount: 30}, | ||
| {category: 'B', amount: 40}, | ||
| {category: 'B', amount: 50}, | ||
| {category: 'C', amount: 60}, | ||
| ]; | ||
| // GROUP_ROW_NUMBER() numbers rows, restarting at 1 when the first selected column changes | ||
| // Data should be pre-sorted by the grouping column(s) | ||
| var res = alasql( | ||
| 'SELECT category, amount, GROUP_ROW_NUMBER() AS rn FROM ? ORDER BY category, amount', | ||
| [data] | ||
| ); | ||
| assert.deepEqual(res, [ | ||
| {category: 'A', amount: 10, rn: 1}, | ||
| {category: 'A', amount: 20, rn: 2}, | ||
| {category: 'A', amount: 30, rn: 3}, | ||
| {category: 'B', amount: 40, rn: 1}, | ||
| {category: 'B', amount: 50, rn: 2}, | ||
| {category: 'C', amount: 60, rn: 1}, | ||
| ]); | ||
| done(); | ||
| }); | ||
|
|
||
| it('3. Use GROUP_ROW_NUMBER() to get first N rows per group', function (done) { | ||
| alasql('CREATE TABLE test_data (category STRING, amount INT)'); | ||
| alasql('INSERT INTO test_data VALUES ("X", 1), ("X", 2), ("X", 3), ("Y", 10), ("Y", 20)'); | ||
|
|
||
| var res = alasql( | ||
| 'SELECT * FROM (SELECT category, amount, GROUP_ROW_NUMBER() AS rn FROM test_data ORDER BY category, amount) WHERE rn <= 2' | ||
mathiasrw marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| ); | ||
| assert.deepEqual(res, [ | ||
| {category: 'X', amount: 1, rn: 1}, | ||
| {category: 'X', amount: 2, rn: 2}, | ||
| {category: 'Y', amount: 10, rn: 1}, | ||
| {category: 'Y', amount: 20, rn: 2}, | ||
| ]); | ||
|
|
||
| alasql('DROP TABLE test_data'); | ||
| done(); | ||
| }); | ||
|
|
||
| it('4. ROW_NUMBER() should still work for entire result set', function (done) { | ||
| var data = [ | ||
| {category: 'A', amount: 10}, | ||
| {category: 'A', amount: 20}, | ||
| {category: 'B', amount: 30}, | ||
| ]; | ||
| var res = alasql('SELECT category, amount, ROW_NUMBER() AS rn FROM ?', [data]); | ||
| assert.deepEqual(res, [ | ||
| {category: 'A', amount: 10, rn: 1}, | ||
| {category: 'A', amount: 20, rn: 2}, | ||
| {category: 'B', amount: 30, rn: 3}, | ||
| ]); | ||
| done(); | ||
| }); | ||
|
|
||
| it('5. GROUP_ROW_NUMBER() with multi-column grouping', function (done) { | ||
| var data = [ | ||
| {dept: 'IT', team: 'A', name: 'Alice'}, | ||
| {dept: 'IT', team: 'A', name: 'Charlie'}, | ||
| {dept: 'IT', team: 'B', name: 'Dave'}, | ||
| {dept: 'Sales', team: 'A', name: 'Jane'}, | ||
| {dept: 'Sales', team: 'A', name: 'John'}, | ||
| ]; | ||
| // When there are multiple columns, GROUP_ROW_NUMBER() partitions by the first column | ||
| var res = alasql( | ||
| 'SELECT dept, team, name, GROUP_ROW_NUMBER() AS rn FROM ? ORDER BY dept, team, name', | ||
| [data] | ||
| ); | ||
| assert.deepEqual(res, [ | ||
| {dept: 'IT', team: 'A', name: 'Alice', rn: 1}, | ||
| {dept: 'IT', team: 'A', name: 'Charlie', rn: 2}, | ||
| {dept: 'IT', team: 'B', name: 'Dave', rn: 3}, | ||
| {dept: 'Sales', team: 'A', name: 'Jane', rn: 1}, | ||
| {dept: 'Sales', team: 'A', name: 'John', rn: 2}, | ||
| ]); | ||
| done(); | ||
| }); | ||
|
|
||
| it('6. DROP DATABASE', function (done) { | ||
| alasql('DROP DATABASE test1126'); | ||
| done(); | ||
| }); | ||
| }); | ||
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.