Skip to content

Commit ebb0f8b

Browse files
Copilotmathiasrw
andcommitted
Use optional chaining and add edge case test for same-name scenarios
- Apply optional chaining to remaining query.defcols accesses in src/424select.js - Add comprehensive edge case test for table and column with same name - Test covers: table.column, table.column.property, and column.property patterns - Demonstrates that when table name = column name, explicit qualification is needed - All 2287 tests passing (1 new edge case test added) Co-authored-by: mathiasrw <1063454+mathiasrw@users.noreply.github.com>
1 parent fa413a7 commit ebb0f8b

File tree

2 files changed

+26
-2
lines changed

2 files changed

+26
-2
lines changed

src/424select.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -212,10 +212,10 @@ yy.Select.prototype.compileSelect1 = function (query, params) {
212212
}
213213
}
214214

215-
if (!tbid) tbid = query.defcols[col.columnid];
215+
if (!tbid) tbid = query.defcols?.[col.columnid];
216216
if (!tbid) tbid = query.defaultTableid;
217217
if (col.columnid !== '_') {
218-
if (false && tbid && !query.defcols['.'][col.tableid] && !query.defcols[col.columnid]) {
218+
if (false && tbid && !query.defcols?.['.']?.[col.tableid] && !query.defcols?.[col.columnid]) {
219219
ss.push(
220220
"'" +
221221
escapeq(col.as || col.columnid) +

test/test341.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,30 @@ describe('Test 341 Intellectual DOT operator', function () {
6262
done();
6363
});
6464

65+
it('7. Edge case: table and column with same name', function (done) {
66+
// Create a table named "item" with a column named "item"
67+
alasql('CREATE TABLE item (id INT, item STRING)');
68+
alasql('INSERT INTO item VALUES (1, "Alpha"), (2, "Beta")');
69+
70+
// Test 1: table.column access (item.item should get the column value)
71+
var res1 = alasql('SELECT item.item FROM item');
72+
assert.deepEqual(res1, [{item: 'Alpha'}, {item: 'Beta'}]);
73+
74+
// Test 2: table.column.property access (item.item.length should get the length)
75+
var res2 = alasql('SELECT COLUMN item.item.length FROM item');
76+
assert.deepEqual(res2, [5, 4]);
77+
78+
// Test 3: When only one column exists with unique name, property access works
79+
alasql('CREATE TABLE products (title STRING)');
80+
alasql('INSERT INTO products VALUES ("Product"), ("Item")');
81+
var res3 = alasql('SELECT COLUMN title.length FROM products');
82+
assert.deepEqual(res3, [7, 4]);
83+
84+
alasql('DROP TABLE item');
85+
alasql('DROP TABLE products');
86+
done();
87+
});
88+
6589
it('5. FOREIGN KEY way', function (done) {
6690
var res = alasql('SELECT VALUE $0; SET $0 = 200; SELECT VALUE $0', [100]);
6791
assert.deepEqual(res, [100, 1, 200]);

0 commit comments

Comments
 (0)