Skip to content

Commit c4b46f3

Browse files
Copilotmathiasrw
andauthored
Improve UNION to support parenthesized SELECT and ORDER BY to close (#2417)
Implemented SQL-99 compliant solution with parenthesized SELECT statements: `(SELECT ... ORDER BY ...) UNION ...` and `(SELECT ... ORDER BY ...) UNION (SELECT ... ORDER BY ...)`. The grammar now properly requires parentheses for ORDER BY before UNION, while still supporting standard `SELECT ... UNION ... ORDER BY ...` syntax. --------- Co-authored-by: copilot-swe-agent[bot] <[email protected]> Co-authored-by: mathiasrw <[email protected]> Co-authored-by: Mathias Wulff <[email protected]>
1 parent 0fec8c5 commit c4b46f3

File tree

4 files changed

+308
-43
lines changed

4 files changed

+308
-43
lines changed

.gitattributes

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,6 @@ yarn.lock binary
1212
# paths that don't start with / are relative to the .gitattributes folder
1313
#relative/path/*.txt text eol=lf
1414

15-
src/alasqlparser.js merge=union
15+
src/alasqlparser.js binary
1616

1717

.github/copilot-instructions.md

Lines changed: 8 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -7,54 +7,33 @@ AlaSQL is an open source SQL database for JavaScript with a focus on query speed
77
## When Implementing Features
88

99
1. **Understand the issue thoroughly** - Read related test cases and existing code
10-
2. **Write a test first** - Create `test/test###.js` for the issue where `###` is the id of the issue we are actually fixing.
10+
2. **Write a test first** - Copy test/test000.js into a new file called `test/test###.js` where where `###` is the id of the issue we are trying to solve
1111
3. **Verify test fails** - Run `yarn test` to confirm the test catches the issue
1212
4. **Implement the fix** - Modify appropriate file(s) in `src/`
13-
5. **Format code** - Run `yarn format` before committing
14-
6. **Verify test passes** - Run `yarn test` again
13+
- If you modify the grammar in `src/alasqlgrammar.jison`, run `yarn jison && yarn test` to regenerate the parser and verify
14+
5. **Reconsider elegance** - Make sure to assess the solution and reconsider if this can be more elegant or efficient
15+
6. **Format code** - Run `yarn format` before committing
1516

1617

17-
## How to to test files
18-
1. Make a test file
19-
- Name new test files as `test/test###.js` where `###` is the GitHub issue number of the issue we are actually fixing.
20-
- If the file already exists we name the file test/test###-B.js
21-
1. Copy the structure in `test/test000.js` as a template
22-
3. Tests should be self-contained and clear about what they're testing
23-
4. Use the Mocha test framework with standard assertions
24-
25-
## SQL Parser Modifications
26-
27-
If a problem demands modifying the lexical parser then seek to do chances as small as possible to `src/alasqlparser.jison`. Afterwards run `yarn jison && yarn test` to confirm the result.
28-
2918
## Commands
3019

3120
```bash
3221
# Install dependencies
3322
yarn
3423

24+
# Generate grammar (if needed)
25+
yarn jison
26+
3527
# Run tests
3628
yarn test
3729

3830
# Format code
3931
yarn format
40-
41-
# Build project
42-
yarn build
32+
```
4333

4434

4535
## Files to Avoid Modifying
4636
- `dist/` - Generated files, will be overwritten on build
4737
- `src/alasqlparser.js` - Generated from Jison grammar (modify the `.jison` file instead)
4838
- `.min.js` files - Generated during build
4939

50-
51-
now commit the code.
52-
53-
## When Reviewing Code
54-
55-
- Verify tests exist for any new functionality and any regression the code changes could have affected.
56-
57-
## Resources
58-
59-
- [AlaSQL Documentation](https://github.com/alasql/alasql/wiki)
60-
- [Issue Tracker](https://github.com/AlaSQL/alasql/issues)

test/test000.js

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,24 +3,22 @@ if (typeof exports === 'object') {
33
var alasql = require('..');
44
}
55

6-
describe('Test 000 - multiple statements', function () {
7-
const test = '000'; // insert test file number
6+
let testId = '000'; // Use the ID of the issue being fixed by this PR
87

8+
describe(`Test ${testId} - multiple statements`, function () {
99
before(function () {
10-
alasql('create database test' + test);
11-
alasql('use test' + test);
10+
alasql('create database test' + testId);
11+
alasql('use test' + testId);
1212
});
1313

1414
after(function () {
15-
alasql('drop database test' + test);
15+
alasql('drop database test' + testId);
1616
});
1717

18-
// NOTE: Tests should use assert.deepEqual to verify the complete expected output
19-
// against the actual result object. This ensures comprehensive validation and
20-
// makes test failures more informative by showing the full diff.
18+
// NOTE: Always assert.deepEqual the final and complete output of the last call to alasql
2119

2220
it('A) From single lines', function () {
23-
var res = [];
21+
let res = [];
2422
res.push(alasql('create table one (a int)'));
2523
res.push(alasql('insert into one values (1),(2),(3),(4),(5)'));
2624
res.push(alasql('select * from one'));
@@ -29,16 +27,16 @@ describe('Test 000 - multiple statements', function () {
2927

3028
it('B) Multiple statements in one string', function () {
3129
//
32-
var sql = 'create table two (a int);';
30+
let sql = 'create table two (a int);';
3331
sql += 'insert into two values (1),(2),(3),(4),(5);';
3432
sql += 'select * from two;';
35-
var res = alasql(sql);
33+
let res = alasql(sql);
3634
assert.deepStrictEqual(res, [1, 5, [{a: 1}, {a: 2}, {a: 3}, {a: 4}, {a: 5}]]);
3735
});
3836

3937
it('C) Multiple statements in one string with callback', function (done) {
40-
// Please note that first parameter (here `done`) must be called if defined - and is needed when testing async code
41-
var sql = 'create table three (a int);';
38+
// use first param (here `done`) when operating with async function or async code
39+
let sql = 'create table three (a int);';
4240
sql += 'insert into three values (1),(2),(3),(4),(5);';
4341
sql += 'select * from three;';
4442
alasql(sql, function (res) {

0 commit comments

Comments
 (0)