Skip to content

Commit 291a762

Browse files
Copilotmathiasrw
andauthored
Support BETWEEN operator in CASE statement to fix #735 (#2195)
Co-authored-by: copilot-swe-agent[bot] <[email protected]> Co-authored-by: mathiasrw <[email protected]> Co-authored-by: Mathias Wulff <[email protected]>
1 parent c554575 commit 291a762

File tree

2 files changed

+75
-0
lines changed

2 files changed

+75
-0
lines changed

src/50expression.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -249,6 +249,15 @@
249249
if (this.left && this.left.findAggregator) {
250250
this.left.findAggregator(query);
251251
}
252+
// Handle BETWEEN operators which have right1 and right2
253+
if (this.op === 'BETWEEN' || this.op === 'NOT BETWEEN') {
254+
if (this.right1 && this.right1.findAggregator) {
255+
this.right1.findAggregator(query);
256+
}
257+
if (this.right2 && this.right2.findAggregator) {
258+
this.right2.findAggregator(query);
259+
}
260+
}
252261
// Do not go in > ALL
253262
if (this.right && this.right.findAggregator && !this.allsome) {
254263
this.right.findAggregator(query);

test/test735.js

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
if (typeof exports === 'object') {
2+
var assert = require('assert');
3+
var alasql = require('..');
4+
}
5+
6+
describe('Test 735: BETWEEN in CASE statement', function () {
7+
it('1. CASE WHEN with > and < operators should work', function (done) {
8+
var cities = [
9+
{City: 'Moscow', Population: 1},
10+
{City: 'London', Population: 10},
11+
{City: 'Paris', Population: 100},
12+
];
13+
14+
var res = alasql(
15+
'SELECT City, CASE WHEN Population > 1 AND Population < 100 THEN "yes" ELSE "no" END AS isBetween1and100 FROM ?',
16+
[cities]
17+
);
18+
19+
assert.deepEqual(res, [
20+
{City: 'Moscow', isBetween1and100: 'no'},
21+
{City: 'London', isBetween1and100: 'yes'},
22+
{City: 'Paris', isBetween1and100: 'no'},
23+
]);
24+
done();
25+
});
26+
27+
it('2. CASE WHEN with BETWEEN operator should work', function (done) {
28+
var cities = [
29+
{City: 'Moscow', Population: 1},
30+
{City: 'London', Population: 10},
31+
{City: 'Paris', Population: 100},
32+
];
33+
34+
var res = alasql(
35+
'SELECT City, CASE WHEN Population BETWEEN 1 AND 100 THEN "yes" ELSE "no" END AS isBetween1and100 FROM ?',
36+
[cities]
37+
);
38+
39+
assert.deepEqual(res, [
40+
{City: 'Moscow', isBetween1and100: 'yes'},
41+
{City: 'London', isBetween1and100: 'yes'},
42+
{City: 'Paris', isBetween1and100: 'yes'},
43+
]);
44+
done();
45+
});
46+
47+
it('3. CASE WHEN with BETWEEN operator (exclusive) should work', function (done) {
48+
var cities = [
49+
{City: 'Moscow', Population: 1},
50+
{City: 'London', Population: 10},
51+
{City: 'Paris', Population: 100},
52+
];
53+
54+
var res = alasql(
55+
'SELECT City, CASE WHEN Population BETWEEN 2 AND 99 THEN "yes" ELSE "no" END AS isBetween2and99 FROM ?',
56+
[cities]
57+
);
58+
59+
assert.deepEqual(res, [
60+
{City: 'Moscow', isBetween2and99: 'no'},
61+
{City: 'London', isBetween2and99: 'yes'},
62+
{City: 'Paris', isBetween2and99: 'no'},
63+
]);
64+
done();
65+
});
66+
});

0 commit comments

Comments
 (0)