Skip to content

Commit d90acc2

Browse files
committed
Support Qualified Table Names in ON Clauses to fix #2186
1 parent d2efda5 commit d90acc2

File tree

5 files changed

+91
-19
lines changed

5 files changed

+91
-19
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
"build": "bun run format && ./build.sh",
3131
"install-g": "bun run build && npm uninstall alasql -g && npm install -g ./",
3232
"release": "yarn version",
33-
"jison": "bun run jison-only && bun run test",
33+
"jison": "bun run jison-only && bun run build",
3434
"jison-only": "bun --bun ./node_modules/.bin/jison ./src/alasqlparser.jison -o ./src/alasqlparser.js",
3535
"push": "git push --force-with-lease && git push --no-verify --tags #",
3636
"repush": "yarn rebase && yarn push",

src/alasqlparser.jison

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -317,6 +317,7 @@ SETS return 'SET'
317317
. return 'INVALID'
318318

319319
/lex
320+
%nonassoc LOW_PREC
320321
%left COMMA
321322
%left DOUBLECOLON
322323
%left OR
@@ -1176,7 +1177,7 @@ Star
11761177
Column
11771178
: Literal DOT Literal DOT Literal
11781179
{ $$ = new yy.Column({columnid: $5, tableid: $3, databaseid:$1});}
1179-
| Literal DOT Literal
1180+
| Literal DOT Literal %prec LOW_PREC
11801181
{ $$ = new yy.Column({columnid: $3, tableid: $1});}
11811182
| Literal DOT VALUE
11821183
{ $$ = new yy.Column({columnid: $3, tableid: $1});}

src/alasqlparser.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

test/test2186.test.js

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
// @ts-ignore
2+
import {describe, expect, test, beforeAll, afterAll} from 'bun:test';
3+
import alasql from '..';
4+
5+
describe('Test 2186 - Qualified Names in ON Clause', () => {
6+
beforeAll(() => {
7+
alasql('CREATE DATABASE test2186');
8+
alasql('CREATE TABLE test2186.A_2186 (id INT, val STRING)');
9+
alasql('CREATE TABLE test2186.B_2186 (id INT, val STRING)');
10+
alasql('INSERT INTO test2186.A_2186 VALUES (1, "A"), (2, "B")');
11+
alasql('INSERT INTO test2186.B_2186 VALUES (1, "X"), (2, "Y")');
12+
});
13+
14+
afterAll(() => {
15+
alasql('DROP DATABASE test2186');
16+
});
17+
18+
test('1. Qualified names WITHOUT USE', () => {
19+
// Ensure we are not in test2186 DB
20+
alasql('USE alasql');
21+
const res = alasql(`
22+
SELECT test2186.A_2186.val AS v1, test2186.B_2186.val AS v2
23+
FROM test2186.A_2186
24+
JOIN test2186.B_2186
25+
ON test2186.A_2186.id = test2186.B_2186.id
26+
`);
27+
expect(res).toEqual([
28+
{v1: 'A', v2: 'X'},
29+
{v1: 'B', v2: 'Y'},
30+
]);
31+
});
32+
33+
test('2. Unqualified names WITH USE', () => {
34+
alasql('USE test2186');
35+
const res = alasql(`
36+
SELECT A_2186.val AS v1, B_2186.val AS v2
37+
FROM A_2186
38+
JOIN B_2186
39+
ON A_2186.id = B_2186.id
40+
`);
41+
expect(res).toEqual([
42+
{v1: 'A', v2: 'X'},
43+
{v1: 'B', v2: 'Y'},
44+
]);
45+
});
46+
47+
test('3. Aliased tables with qualified FROM', () => {
48+
alasql('USE alasql');
49+
const res = alasql(`
50+
SELECT t1.val AS v1, t2.val AS v2
51+
FROM test2186.A_2186 AS t1
52+
JOIN test2186.B_2186 AS t2
53+
ON t1.id = t2.id
54+
`);
55+
expect(res).toEqual([
56+
{v1: 'A', v2: 'X'},
57+
{v1: 'B', v2: 'Y'},
58+
]);
59+
});
60+
61+
test('4. Qualified names in ON clause (Explicit check)', () => {
62+
alasql('USE alasql');
63+
const res = alasql(`
64+
SELECT test2186.A_2186.val AS v1
65+
FROM test2186.A_2186
66+
JOIN test2186.B_2186
67+
ON test2186.A_2186.id = test2186.B_2186.id
68+
`);
69+
expect(res).toEqual([{v1: 'A'}, {v1: 'B'}]);
70+
});
71+
});

test/test407.test.js

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import alasql from '..';
99
1010
*/
1111

12-
describe.skip('Test 407 - TWO JOINS', () => {
12+
describe('Test 407 - TWO JOINS', () => {
1313
test('0.1. CREATE DATABASE', done => {
1414
alasql('CREATE DATABASE test407');
1515
alasql.options.modifier = 'MATRIX';
@@ -39,7 +39,7 @@ describe.skip('Test 407 - TWO JOINS', () => {
3939

4040
test('1.2. INNER AND LEFT', done => {
4141
var res = alasql(
42-
'SELECT one.id AS a, two.id AS b, three.id AS c FROM one INNER JOIN two ON one.id = two.id LEFT JOIN three ON two.id = three.id'
42+
'SELECT test407.one.id AS a, test407.two.id AS b, test407.three.id AS c FROM test407.one INNER JOIN test407.two ON test407.one.id = test407.two.id LEFT JOIN test407.three ON test407.two.id = test407.three.id'
4343
);
4444
expect(res).toEqual([
4545
['AB', 'AB', undefined],
@@ -50,7 +50,7 @@ describe.skip('Test 407 - TWO JOINS', () => {
5050

5151
test.skip('1.3. INNER AND RIGHT', done => {
5252
var res = alasql(
53-
'SELECT one.id AS a, two.id AS b, three.id AS c FROM one INNER JOIN two ON one.id = two.id RIGHT JOIN three ON two.id = three.id'
53+
'SELECT test407.one.id AS a, test407.two.id AS b, test407.three.id AS c FROM test407.one INNER JOIN test407.two ON test407.one.id = test407.two.id RIGHT JOIN test407.three ON test407.two.id = test407.three.id'
5454
);
5555
expect(res).toEqual([
5656
[undefined, undefined, 'C'],
@@ -63,7 +63,7 @@ describe.skip('Test 407 - TWO JOINS', () => {
6363

6464
test.skip('1.4. INNER AND OUTER', done => {
6565
var res = alasql(
66-
'SELECT one.id AS a, two.id AS b, three.id AS c FROM one INNER JOIN two ON one.id = two.id OUTER JOIN three ON two.id = three.id'
66+
'SELECT test407.one.id AS a, test407.two.id AS b, test407.three.id AS c FROM test407.one INNER JOIN test407.two ON test407.one.id = test407.two.id OUTER JOIN test407.three ON test407.two.id = test407.three.id'
6767
);
6868
expect(res).toEqual([
6969
['AB', 'AB', undefined],
@@ -76,15 +76,15 @@ describe.skip('Test 407 - TWO JOINS', () => {
7676

7777
test('2.1. LEFT AND INNER', done => {
7878
var res = alasql(
79-
'SELECT one.id AS a, two.id AS b, three.id AS c FROM one LEFT JOIN two ON one.id = two.id INNER JOIN three ON two.id = three.id'
79+
'SELECT test407.one.id AS a, test407.two.id AS b, test407.three.id AS c FROM test407.one LEFT JOIN test407.two ON test407.one.id = test407.two.id INNER JOIN test407.three ON test407.two.id = test407.three.id'
8080
);
8181
expect(res).toEqual([['ABC', 'ABC', 'ABC']]);
8282
done();
8383
});
8484

8585
test('2.2. LEFT AND LEFT', done => {
8686
var res = alasql(
87-
'SELECT one.id AS a, two.id AS b, three.id AS c FROM one LEFT JOIN two ON one.id = two.id LEFT JOIN three ON two.id = three.id'
87+
'SELECT test407.one.id AS a, test407.two.id AS b, test407.three.id AS c FROM test407.one LEFT JOIN test407.two ON test407.one.id = test407.two.id LEFT JOIN test407.three ON test407.two.id = test407.three.id'
8888
);
8989
expect(res).toEqual([
9090
['A', undefined, undefined],
@@ -97,7 +97,7 @@ describe.skip('Test 407 - TWO JOINS', () => {
9797

9898
test.skip('2.3. LEFT AND RIGHT', done => {
9999
var res = alasql(
100-
'SELECT one.id AS a, two.id AS b, three.id AS c FROM one LEFT JOIN two ON one.id = two.id RIGHT JOIN three ON two.id = three.id'
100+
'SELECT test407.one.id AS a, test407.two.id AS b, test407.three.id AS c FROM test407.one LEFT JOIN test407.two ON test407.one.id = test407.two.id RIGHT JOIN test407.three ON test407.two.id = test407.three.id'
101101
);
102102
console.log(res);
103103
expect(res).toEqual([
@@ -111,7 +111,7 @@ describe.skip('Test 407 - TWO JOINS', () => {
111111

112112
test.skip('2.4. LEFT AND OUTER', done => {
113113
var res = alasql(
114-
'SELECT one.id AS a, two.id AS b, three.id AS c FROM one LEFT JOIN two ON one.id = two.id OUTER JOIN three ON two.id = three.id'
114+
'SELECT test407.one.id AS a, test407.two.id AS b, test407.three.id AS c FROM test407.one LEFT JOIN test407.two ON test407.one.id = test407.two.id OUTER JOIN test407.three ON test407.two.id = test407.three.id'
115115
);
116116
console.log(res);
117117
expect(res).toEqual([
@@ -128,7 +128,7 @@ describe.skip('Test 407 - TWO JOINS', () => {
128128

129129
test('3.1. RIGHT AND INNER', done => {
130130
var res = alasql(
131-
'SELECT one.id AS a, two.id AS b, three.id AS c FROM one RIGHT JOIN two ON one.id = two.id INNER JOIN three ON two.id = three.id'
131+
'SELECT test407.one.id AS a, test407.two.id AS b, test407.three.id AS c FROM test407.one RIGHT JOIN test407.two ON test407.one.id = test407.two.id INNER JOIN test407.three ON test407.two.id = test407.three.id'
132132
);
133133
expect(res).toEqual([
134134
['ABC', 'ABC', 'ABC'],
@@ -139,7 +139,7 @@ describe.skip('Test 407 - TWO JOINS', () => {
139139

140140
test('3.2. RIGHT AND LEFT', done => {
141141
var res = alasql(
142-
'SELECT one.id AS a, two.id AS b, three.id AS c FROM one RIGHT JOIN two ON one.id = two.id LEFT JOIN three ON two.id = three.id'
142+
'SELECT test407.one.id AS a, test407.two.id AS b, test407.three.id AS c FROM test407.one RIGHT JOIN test407.two ON test407.one.id = test407.two.id LEFT JOIN test407.three ON test407.two.id = test407.three.id'
143143
);
144144
expect(res).toEqual([
145145
['AB', 'AB', undefined],
@@ -152,7 +152,7 @@ describe.skip('Test 407 - TWO JOINS', () => {
152152

153153
test.skip('3.3. RIGHT AND RIGHT', done => {
154154
var res = alasql(
155-
'SELECT one.id AS a, two.id AS b, three.id AS c FROM one RIGHT JOIN two ON one.id = two.id RIGHT JOIN three ON two.id = three.id'
155+
'SELECT test407.one.id AS a, test407.two.id AS b, test407.three.id AS c FROM test407.one RIGHT JOIN test407.two ON test407.one.id = test407.two.id RIGHT JOIN test407.three ON test407.two.id = test407.three.id'
156156
);
157157
expect(res).toEqual([
158158
[undefined, undefined, 'C'],
@@ -165,7 +165,7 @@ describe.skip('Test 407 - TWO JOINS', () => {
165165

166166
test.skip('3.4. RIGHT AND OUTER', done => {
167167
var res = alasql(
168-
'SELECT one.id AS a, two.id AS b, three.id AS c FROM one RIGHT JOIN two ON one.id = two.id OUTER JOIN three ON two.id = three.id'
168+
'SELECT test407.one.id AS a, test407.two.id AS b, test407.three.id AS c FROM test407.one RIGHT JOIN test407.two ON test407.one.id = test407.two.id OUTER JOIN test407.three ON test407.two.id = test407.three.id'
169169
);
170170
console.log(res);
171171
expect(res).toEqual([
@@ -181,7 +181,7 @@ describe.skip('Test 407 - TWO JOINS', () => {
181181

182182
test('4.1. OUTER AND INNER', done => {
183183
var res = alasql(
184-
'SELECT one.id AS a, two.id AS b, three.id AS c FROM one OUTER JOIN two ON one.id = two.id INNER JOIN three ON two.id = three.id'
184+
'SELECT test407.one.id AS a, test407.two.id AS b, test407.three.id AS c FROM test407.one OUTER JOIN test407.two ON test407.one.id = test407.two.id INNER JOIN test407.three ON test407.two.id = test407.three.id'
185185
);
186186
expect(res).toEqual([
187187
['ABC', 'ABC', 'ABC'],
@@ -192,7 +192,7 @@ describe.skip('Test 407 - TWO JOINS', () => {
192192

193193
test('4.2. OUTER AND LEFT', done => {
194194
var res = alasql(
195-
'SELECT one.id AS a, two.id AS b, three.id AS c FROM one OUTER JOIN two ON one.id = two.id LEFT JOIN three ON two.id = three.id'
195+
'SELECT test407.one.id AS a, test407.two.id AS b, test407.three.id AS c FROM test407.one OUTER JOIN test407.two ON test407.one.id = test407.two.id LEFT JOIN test407.three ON test407.two.id = test407.three.id'
196196
);
197197
expect(res).toEqual([
198198
['A', undefined, undefined],
@@ -207,7 +207,7 @@ describe.skip('Test 407 - TWO JOINS', () => {
207207

208208
test.skip('4.3. OUTER AND RIGHT', done => {
209209
var res = alasql(
210-
'SELECT one.id AS a, two.id AS b, three.id AS c FROM one OUTER JOIN two ON one.id = two.id RIGHT JOIN three ON two.id = three.id'
210+
'SELECT test407.one.id AS a, test407.two.id AS b, test407.three.id AS c FROM test407.one OUTER JOIN test407.two ON test407.one.id = test407.two.id RIGHT JOIN test407.three ON test407.two.id = test407.three.id'
211211
);
212212
expect(res).toEqual([
213213
[undefined, undefined, 'C'],
@@ -220,7 +220,7 @@ describe.skip('Test 407 - TWO JOINS', () => {
220220

221221
test.skip('4.4. OUTER AND OUTER', done => {
222222
var res = alasql(
223-
'SELECT one.id AS a, two.id AS b, three.id AS c FROM one OUTER JOIN two ON one.id = two.id OUTER JOIN three ON two.id = three.id'
223+
'SELECT test407.one.id AS a, test407.two.id AS b, test407.three.id AS c FROM test407.one OUTER JOIN test407.two ON test407.one.id = test407.two.id OUTER JOIN test407.three ON test407.two.id = test407.three.id'
224224
);
225225
expect(res).toEqual([
226226
['A', undefined, undefined],

0 commit comments

Comments
 (0)