Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions src/424select.js
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,22 @@ yy.Select.prototype.compileSelect1 = function (query, params) {
var tbid = col.tableid;
// console.log(query.sources);
var dbid = col.databaseid || query.sources[0].databaseid || query.database.databaseid;

// Check if tableid is actually a column name (property access pattern like name.length)
// This handles cases where the parser sees "columnname.property" and interprets it as "table.column"
var isPropertyAccess = false;
if (
tbid &&
query.defcols &&
query.defcols[tbid] &&
query.defcols[tbid] !== '-' &&
!query.defcols['.'][tbid]
) {
// tbid is actually a column name (not a table name), so this is property access
isPropertyAccess = true;
var actualTableid = query.defcols[tbid];
}

if (!tbid) tbid = query.defcols[col.columnid];
if (!tbid) tbid = query.defaultTableid;
if (col.columnid !== '_') {
Expand Down Expand Up @@ -260,6 +276,20 @@ yy.Select.prototype.compileSelect1 = function (query, params) {
.join(' ?? ');

ss.push("'" + escapeq(col.as || col.columnid) + "':(" + searchExpr + ')');
} else if (isPropertyAccess) {
// Property access pattern: columnname.property (e.g., name.length)
// Generate code to access the property on the column value
ss.push(
"'" +
escapeq(col.as || col.columnid) +
"':((p['" +
actualTableid +
"']['" +
col.tableid +
"'] || {})['" +
col.columnid +
"'])"
);
} else {
ss.push(
"'" +
Expand Down
17 changes: 17 additions & 0 deletions src/50expression.js
Original file line number Diff line number Diff line change
Expand Up @@ -782,6 +782,23 @@
}

if (this.tableid) {
// Check if tableid is actually a column name (property access pattern like name.length)
// This handles cases where the parser sees "columnname.property" and interprets it as "table.column"
if (
defcols &&
defcols[this.tableid] &&
defcols[this.tableid] !== '-' &&
defcols['.'] &&
!defcols['.'][this.tableid]
) {
// tableid is actually a column name (not a table name), so this is property access
// Generate code to access the property on the column value
const actualTable = defcols[this.tableid];
if (this.columnid !== '_') {
return `((${context}['${actualTable}']['${this.tableid}'] || {})['${this.columnid}'])`;
}
}
// Otherwise, tableid is a table name (normal table.column access)
return this.columnid !== '_'
? `${context}['${this.tableid}']['${this.columnid}']`
: context === 'g'
Expand Down
13 changes: 10 additions & 3 deletions test/test341.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,18 +35,25 @@ describe('Test 341 Intellectual DOT operator', function () {
done();
});

it.skip('4. JavaScript way', function (done) {
it('4. JavaScript way', function (done) {
var res = alasql('SET @a = "who".length');
assert.deepEqual(res, [6, 6, 7]);
assert.deepEqual(res, 1);
assert.deepEqual(alasql.vars.a, 3);
done();
});

it.skip('5. JavaScript way', function (done) {
it('5. JavaScript way', function (done) {
var res = alasql('SELECT COLUMN name.length FROM persons');
assert.deepEqual(res, [6, 6, 7]);
done();
});

it('6. JavaScript way with table.column.length', function (done) {
var res = alasql('SELECT COLUMN persons.name.length FROM persons');
assert.deepEqual(res, [6, 6, 7]);
done();
});

it('5. FOREIGN KEY way', function (done) {
var res = alasql('SELECT VALUE $0; SET $0 = 200; SELECT VALUE $0', [100]);
assert.deepEqual(res, [100, 1, 200]);
Expand Down