|
| 1 | +if (typeof exports === 'object') { |
| 2 | + var assert = require('assert'); |
| 3 | + var alasql = require('..'); |
| 4 | +} |
| 5 | + |
| 6 | +describe('Test 128-B CROSS JOIN with parentheses (sqllogictest)', function () { |
| 7 | + const test = '128B'; |
| 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) Setup test tables', function () { |
| 19 | + alasql('CREATE TABLE tab1 (id INT, val VARCHAR(10))'); |
| 20 | + alasql('CREATE TABLE tab2 (id INT, name VARCHAR(10))'); |
| 21 | + alasql("INSERT INTO tab1 VALUES (1, 'A'), (2, 'B')"); |
| 22 | + alasql("INSERT INTO tab2 VALUES (3, 'X'), (4, 'Y')"); |
| 23 | + }); |
| 24 | + |
| 25 | + it('B) CROSS JOIN without parentheses (baseline)', function () { |
| 26 | + var res = alasql('SELECT -92 AS col1 FROM tab1 AS cor0 CROSS JOIN tab2 AS cor1'); |
| 27 | + assert.deepEqual(res, [{col1: -92}, {col1: -92}, {col1: -92}, {col1: -92}]); |
| 28 | + }); |
| 29 | + |
| 30 | + it('C) CROSS JOIN with parentheses around FROM clause', function () { |
| 31 | + var res = alasql('SELECT -92 AS col1 FROM ( tab1 AS cor0 CROSS JOIN tab2 AS cor1 )'); |
| 32 | + assert.deepEqual(res, [{col1: -92}, {col1: -92}, {col1: -92}, {col1: -92}]); |
| 33 | + }); |
| 34 | + |
| 35 | + it('D) Verify CROSS JOIN produces correct results', function () { |
| 36 | + var res = alasql( |
| 37 | + 'SELECT cor0.id as id1, cor1.id as id2 FROM ( tab1 AS cor0 CROSS JOIN tab2 AS cor1 )' |
| 38 | + ); |
| 39 | + assert.deepEqual(res, [ |
| 40 | + {id1: 1, id2: 3}, |
| 41 | + {id1: 1, id2: 4}, |
| 42 | + {id1: 2, id2: 3}, |
| 43 | + {id1: 2, id2: 4}, |
| 44 | + ]); |
| 45 | + }); |
| 46 | + |
| 47 | + it('E) CROSS JOIN with WHERE clause', function () { |
| 48 | + var res = alasql( |
| 49 | + 'SELECT cor0.id FROM ( tab1 AS cor0 CROSS JOIN tab2 AS cor1 ) WHERE cor0.id = 1' |
| 50 | + ); |
| 51 | + assert.deepEqual(res, [{id: 1}, {id: 1}]); |
| 52 | + }); |
| 53 | + |
| 54 | + it('F) CROSS JOIN with ORDER BY', function () { |
| 55 | + var res = alasql( |
| 56 | + 'SELECT cor0.id as id1, cor1.id as id2 FROM ( tab1 AS cor0 CROSS JOIN tab2 AS cor1 ) ORDER BY cor0.id DESC, cor1.id ASC' |
| 57 | + ); |
| 58 | + assert.deepEqual(res, [ |
| 59 | + {id1: 2, id2: 3}, |
| 60 | + {id1: 2, id2: 4}, |
| 61 | + {id1: 1, id2: 3}, |
| 62 | + {id1: 1, id2: 4}, |
| 63 | + ]); |
| 64 | + }); |
| 65 | + |
| 66 | + it('G) Multiple CROSS JOINs with parentheses', function () { |
| 67 | + alasql('CREATE TABLE tab3 (id INT)'); |
| 68 | + alasql('INSERT INTO tab3 VALUES (5), (6)'); |
| 69 | + var res = alasql( |
| 70 | + 'SELECT -92 AS col1 FROM ( tab1 AS cor0 CROSS JOIN tab2 AS cor1 CROSS JOIN tab3 AS cor2 )' |
| 71 | + ); |
| 72 | + assert.deepEqual(res, [ |
| 73 | + {col1: -92}, |
| 74 | + {col1: -92}, |
| 75 | + {col1: -92}, |
| 76 | + {col1: -92}, |
| 77 | + {col1: -92}, |
| 78 | + {col1: -92}, |
| 79 | + {col1: -92}, |
| 80 | + {col1: -92}, |
| 81 | + ]); |
| 82 | + alasql('DROP TABLE tab3'); |
| 83 | + }); |
| 84 | + |
| 85 | + it('H) Nested subquery with CROSS JOIN in parentheses', function () { |
| 86 | + var res = alasql( |
| 87 | + 'SELECT * FROM (SELECT cor0.id FROM ( tab1 AS cor0 CROSS JOIN tab2 AS cor1 )) AS sub' |
| 88 | + ); |
| 89 | + assert.deepEqual(res, [{id: 1}, {id: 1}, {id: 2}, {id: 2}]); |
| 90 | + }); |
| 91 | + |
| 92 | + it('I) CROSS JOIN in parentheses with complex expression', function () { |
| 93 | + var res = alasql( |
| 94 | + 'SELECT cor0.val, cor1.name, cor0.id * 10 + cor1.id AS computed FROM ( tab1 AS cor0 CROSS JOIN tab2 AS cor1 ) WHERE cor0.id > 0' |
| 95 | + ); |
| 96 | + assert.deepEqual(res, [ |
| 97 | + {val: 'A', name: 'X', computed: 13}, |
| 98 | + {val: 'A', name: 'Y', computed: 14}, |
| 99 | + {val: 'B', name: 'X', computed: 23}, |
| 100 | + {val: 'B', name: 'Y', computed: 24}, |
| 101 | + ]); |
| 102 | + }); |
| 103 | +}); |
0 commit comments