Skip to content

Commit 7ce536b

Browse files
Copilotmathiasrw
andauthored
Use DISTINCT with object values in SELECT and SEARCH to fix #167 (#2192)
Co-authored-by: copilot-swe-agent[bot] <[email protected]> Co-authored-by: mathiasrw <[email protected]> Co-authored-by: Mathias Wulff <[email protected]>
1 parent 4eddc73 commit 7ce536b

File tree

3 files changed

+91
-1
lines changed

3 files changed

+91
-1
lines changed

src/38query.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -376,7 +376,9 @@ function doDistinct(query) {
376376
for (var i = 0, ilen = query.data.length; i < ilen; i++) {
377377
var uix = keys
378378
.map(function (k) {
379-
return query.data[i][k];
379+
var val = query.data[i][k];
380+
// Properly serialize objects for comparison
381+
return typeof val === 'object' ? JSON.stringify(val) : val;
380382
})
381383
.join('`');
382384
uniq[uix] = query.data[i];

src/40select.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -469,6 +469,11 @@ function modify(query, res) {
469469
ar.push(res[i][key]);
470470
}
471471

472+
// Apply DISTINCT if specified
473+
if (query.distinct) {
474+
ar = alasql.utils.distinctArray(ar);
475+
}
476+
472477
return ar;
473478

474479
case 'MATRIX':

test/test167B.js

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
if (typeof exports === 'object') {
2+
var assert = require('assert');
3+
var alasql = require('..');
4+
}
5+
6+
describe('Test 167B - SEARCH DISTINCT and SELECT DISTINCT functions', function () {
7+
it('1. Basic DISTINCT comparison', () => {
8+
var data = [{a: 1}, {a: 2}, {a: 1}];
9+
10+
var res1 = alasql('SELECT * FROM ?', [data]);
11+
var res2 = alasql('SELECT DISTINCT * FROM ?', [data]);
12+
var res3 = alasql('SELECT COLUMN DISTINCT _ FROM ?', [data]);
13+
var res4 = alasql('SEARCH DISTINCT(/) FROM ?', [data]);
14+
15+
console.log('res1 (SELECT *):', JSON.stringify(res1));
16+
console.log('res2 (SELECT DISTINCT *):', JSON.stringify(res2));
17+
console.log('res3 (SELECT COLUMN DISTINCT _):', JSON.stringify(res3));
18+
console.log('res4 (SEARCH DISTINCT(/)):', JSON.stringify(res4));
19+
20+
// res1 should have all 3 items
21+
assert.equal(res1.length, 3);
22+
23+
// res2, res3, res4 should all have 2 distinct items
24+
assert.equal(res2.length, 2, 'SELECT DISTINCT * should return 2 items');
25+
assert.equal(res3.length, 2, 'SELECT COLUMN DISTINCT _ should return 2 items');
26+
assert.equal(res4.length, 2, 'SEARCH DISTINCT(/) should return 2 items');
27+
});
28+
29+
it('2. DISTINCT with different middle value (issue b)', () => {
30+
var data = [{a: 1}, {b: 2}, {a: 1}];
31+
32+
var res3 = alasql('SELECT COLUMN DISTINCT _ FROM ?', [data]);
33+
var res4 = alasql('SEARCH DISTINCT(/) FROM ?', [data]);
34+
35+
console.log('res3 (SELECT COLUMN DISTINCT _) with mixed data:', JSON.stringify(res3));
36+
console.log('res4 (SEARCH DISTINCT(/)) with mixed data:', JSON.stringify(res4));
37+
38+
// Should return 2 distinct objects: {a:1} and {b:2}
39+
assert.equal(res3.length, 2, 'SELECT COLUMN DISTINCT _ should return 2 items');
40+
assert.equal(res4.length, 2, 'SEARCH DISTINCT(/) should return 2 items');
41+
42+
// Check that we have both distinct objects
43+
var hasA1 = res3.some(function (item) {
44+
return item.a === 1 && !item.b;
45+
});
46+
var hasB2 = res3.some(function (item) {
47+
return item.b === 2 && !item.a;
48+
});
49+
50+
assert(hasA1, 'Result should contain {a:1}');
51+
assert(hasB2, 'Result should contain {b:2}');
52+
});
53+
54+
it('3. SEARCH DISTINCT vs SELECT DISTINCT consistency', () => {
55+
var data = [{a: 1}, {b: 2}, {a: 1}];
56+
57+
var res2 = alasql('SELECT DISTINCT * FROM ?', [data]);
58+
var res4 = alasql('SEARCH DISTINCT(/) FROM ?', [data]);
59+
60+
console.log('res2 (SELECT DISTINCT *):', JSON.stringify(res2));
61+
console.log('res4 (SEARCH DISTINCT(/)):', JSON.stringify(res4));
62+
63+
// Results should be the same
64+
assert.deepEqual(
65+
res2,
66+
res4,
67+
'SELECT DISTINCT * and SEARCH DISTINCT(/) should produce same results'
68+
);
69+
});
70+
71+
it('4. SELECT COLUMN DISTINCT _ with arrays', () => {
72+
var data = [1, 2, 1, 3, 2];
73+
74+
var res = alasql('SELECT COLUMN DISTINCT _ FROM ?', [data]);
75+
console.log('SELECT COLUMN DISTINCT _ from array:', JSON.stringify(res));
76+
77+
// Should return distinct values: [1, 2, 3]
78+
assert.equal(res.length, 3, 'Should return 3 distinct values');
79+
assert(res.includes(1), 'Should include 1');
80+
assert(res.includes(2), 'Should include 2');
81+
assert(res.includes(3), 'Should include 3');
82+
});
83+
});

0 commit comments

Comments
 (0)