Skip to content

Commit 8aa97be

Browse files
Copilotmathiasrw
andauthored
Fix NULL values for GROUP_CONCAT to close #1259 (#2400)
Co-authored-by: copilot-swe-agent[bot] <[email protected]> Co-authored-by: mathiasrw <[email protected]>
1 parent 7dd85c3 commit 8aa97be

File tree

2 files changed

+73
-0
lines changed

2 files changed

+73
-0
lines changed

src/55functions.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -283,8 +283,20 @@ stdfn.CONCAT_WS = function () {
283283
// Aggregator for joining strings
284284
alasql.aggr.group_concat = alasql.aggr.GROUP_CONCAT = function (v, s, stage) {
285285
if (stage === 1) {
286+
// Initialize: skip null values
287+
if (v === null || v === undefined) {
288+
return null;
289+
}
286290
return '' + v;
287291
} else if (stage === 2) {
292+
// Accumulate: skip null values
293+
if (v === null || v === undefined) {
294+
return s;
295+
}
296+
// If accumulator is null/undefined, start with current value
297+
if (s === null || s === undefined) {
298+
return '' + v;
299+
}
288300
s += ',' + v;
289301
return s;
290302
}

test/test942.js

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
if (typeof exports === 'object') {
2+
var assert = require('assert');
3+
var alasql = require('..');
4+
}
5+
6+
describe('Test 942 - GROUP_CONCAT null handling', function () {
7+
it('A) GROUP_CONCAT should skip null values', function () {
8+
var data = [
9+
{a: 1, b: 'x'},
10+
{a: 1, b: null},
11+
{a: 1, b: 'z'},
12+
];
13+
var res = alasql('SELECT a, GROUP_CONCAT(b) AS b FROM ? GROUP BY a', [data]);
14+
assert.equal(res[0].b, 'x,z');
15+
});
16+
17+
it('B) GROUP_CONCAT with all nulls should return null', function () {
18+
var data = [
19+
{a: 1, b: null},
20+
{a: 1, b: null},
21+
];
22+
var res = alasql('SELECT a, GROUP_CONCAT(b) AS b FROM ? GROUP BY a', [data]);
23+
assert.strictEqual(res[0].b, null);
24+
});
25+
26+
it('C) GROUP_CONCAT DISTINCT with nulls should skip nulls', function () {
27+
var data = [
28+
{a: 1, b: 'x'},
29+
{a: 1, b: null},
30+
{a: 1, b: 'x'},
31+
{a: 1, b: null},
32+
];
33+
var res = alasql('SELECT a, GROUP_CONCAT(DISTINCT b) AS b FROM ? GROUP BY a', [data]);
34+
assert.equal(res[0].b, 'x');
35+
});
36+
37+
it('D) GROUP_CONCAT with mixed values and nulls', function () {
38+
var data = [
39+
{a: 1, b: 10},
40+
{a: 1, b: null},
41+
{a: 1, b: 20},
42+
{a: 2, b: null},
43+
{a: 2, b: 30},
44+
];
45+
var res = alasql('SELECT a, GROUP_CONCAT(b) AS b FROM ? GROUP BY a ORDER BY a', [data]);
46+
assert.equal(res[0].b, '10,20');
47+
assert.equal(res[1].b, '30');
48+
});
49+
50+
it('E) GROUP_CONCAT without GROUP BY with nulls', function () {
51+
var data = [{b: 'a'}, {b: null}, {b: 'b'}, {b: null}, {b: 'c'}];
52+
var res = alasql('SELECT GROUP_CONCAT(b) AS b FROM ?', [data]);
53+
assert.equal(res[0].b, 'a,b,c');
54+
});
55+
56+
it('F) GROUP_CONCAT with only null values (no GROUP BY)', function () {
57+
var data = [{b: null}, {b: null}, {b: null}];
58+
var res = alasql('SELECT GROUP_CONCAT(b) AS b FROM ?', [data]);
59+
assert.strictEqual(res[0].b, null);
60+
});
61+
});

0 commit comments

Comments
 (0)