Skip to content
Closed
Show file tree
Hide file tree
Changes from 3 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
24 changes: 24 additions & 0 deletions src/424select.js
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,16 @@ 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] !== '-') {
// tbid is actually a column 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 +270,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
11 changes: 11 additions & 0 deletions src/50expression.js
Original file line number Diff line number Diff line change
Expand Up @@ -782,6 +782,17 @@
}

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] !== '-') {
// tableid is actually a column 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
7 changes: 4 additions & 3 deletions test/test341.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,14 @@ 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();
Expand Down
Loading