|
| 1 | +if (typeof exports === 'object') { |
| 2 | + var assert = require('assert'); |
| 3 | + var alasql = require('..'); |
| 4 | +} |
| 5 | + |
| 6 | +describe('Test 635-B - INTERSECT/EXCEPT with ORDER BY', function () { |
| 7 | + const test = '635B'; |
| 8 | + |
| 9 | + before(function () { |
| 10 | + alasql('create database test' + test); |
| 11 | + alasql('use test' + test); |
| 12 | + }); |
| 13 | + |
| 14 | + after(function () { |
| 15 | + alasql('drop database test' + test); |
| 16 | + }); |
| 17 | + |
| 18 | + it('A) INTERSECT with ORDER BY on column', function () { |
| 19 | + var data = [{eventStart: 1}, {eventStart: 100}, {eventStart: 10}]; |
| 20 | + var res = alasql( |
| 21 | + 'SELECT eventStart FROM $0 INTERSECT SELECT eventStart FROM $0 ORDER BY eventStart', |
| 22 | + [data] |
| 23 | + ); |
| 24 | + assert.deepEqual(res, [{eventStart: 1}, {eventStart: 10}, {eventStart: 100}]); |
| 25 | + }); |
| 26 | + |
| 27 | + it('B) EXCEPT with ORDER BY on column', function () { |
| 28 | + var data = [{eventStart: 1}, {eventStart: 100}, {eventStart: 10}, {eventStart: 60}]; |
| 29 | + var res = alasql( |
| 30 | + 'SELECT eventStart FROM $0 WHERE eventStart < 100 EXCEPT SELECT eventStart FROM $0 WHERE eventStart > 50 ORDER BY eventStart', |
| 31 | + [data] |
| 32 | + ); |
| 33 | + assert.deepEqual(res, [{eventStart: 1}, {eventStart: 10}]); |
| 34 | + }); |
| 35 | + |
| 36 | + it('C) INTERSECT with SELECT * and ORDER BY', function () { |
| 37 | + alasql('CREATE TABLE Persons (eventStart INT, eventEnd INT)'); |
| 38 | + alasql('INSERT INTO Persons VALUES (1000000, 1000100), (1000050, 1000150), (1000200, 1000300)'); |
| 39 | + var res = alasql( |
| 40 | + 'SELECT * FROM Persons INTERSECT SELECT * FROM Persons WHERE eventStart BETWEEN 1000000 AND 1000100 OR eventEnd BETWEEN 1000000 AND 1000100 ORDER BY eventStart' |
| 41 | + ); |
| 42 | + assert.deepEqual(res, [ |
| 43 | + {eventStart: 1000000, eventEnd: 1000100}, |
| 44 | + {eventStart: 1000050, eventEnd: 1000150}, |
| 45 | + ]); |
| 46 | + alasql('DROP TABLE Persons'); |
| 47 | + }); |
| 48 | + |
| 49 | + it('D) EXCEPT with SELECT * and ORDER BY', function () { |
| 50 | + alasql('CREATE TABLE Persons (eventStart INT, eventEnd INT)'); |
| 51 | + alasql('INSERT INTO Persons VALUES (1000000, 1000100), (1000050, 1000150), (1000200, 1000300)'); |
| 52 | + var res = alasql( |
| 53 | + 'SELECT * FROM Persons EXCEPT SELECT * FROM Persons WHERE eventStart > 1000100 ORDER BY eventStart' |
| 54 | + ); |
| 55 | + assert.deepEqual(res, [ |
| 56 | + {eventStart: 1000000, eventEnd: 1000100}, |
| 57 | + {eventStart: 1000050, eventEnd: 1000150}, |
| 58 | + ]); |
| 59 | + alasql('DROP TABLE Persons'); |
| 60 | + }); |
| 61 | + |
| 62 | + it('E) INTERSECT with multiple WHERE clauses and ORDER BY (from issue)', function () { |
| 63 | + // This test validates the fix for issue #635 where ORDER BY with INTERSECT caused |
| 64 | + // "Unable to get property 'modifier' of undefined" error |
| 65 | + alasql('CREATE TABLE Persons (eventStart INT, eventEnd INT)'); |
| 66 | + alasql('INSERT INTO Persons VALUES (1000000, 1000100), (1000050, 1000150), (1000200, 1000300)'); |
| 67 | + // Testing the exact scenario from the issue: INTERSECT with WHERE and ORDER BY |
| 68 | + var res = alasql( |
| 69 | + 'SELECT * FROM Persons INTERSECT SELECT * FROM Persons WHERE eventStart BETWEEN 1000000 AND 1000100 OR eventEnd BETWEEN 1000000 AND 1000100 ORDER BY eventStart' |
| 70 | + ); |
| 71 | + // Should return rows where BOTH conditions are true (INTERSECT) and ordered by eventStart |
| 72 | + // The INTERSECT of all rows with filtered rows should give us the filtered rows |
| 73 | + assert.deepEqual(res, [ |
| 74 | + {eventStart: 1000000, eventEnd: 1000100}, |
| 75 | + {eventStart: 1000050, eventEnd: 1000150}, |
| 76 | + ]); |
| 77 | + alasql('DROP TABLE Persons'); |
| 78 | + }); |
| 79 | + |
| 80 | + it('F) INTERSECT with ORDER BY DESC', function () { |
| 81 | + var data = [{eventStart: 1}, {eventStart: 100}, {eventStart: 10}]; |
| 82 | + var res = alasql( |
| 83 | + 'SELECT eventStart FROM $0 INTERSECT SELECT eventStart FROM $0 ORDER BY eventStart DESC', |
| 84 | + [data] |
| 85 | + ); |
| 86 | + assert.deepEqual(res, [{eventStart: 100}, {eventStart: 10}, {eventStart: 1}]); |
| 87 | + }); |
| 88 | +}); |
0 commit comments