From 083b695b8db1b264a98e5ab7e1a907305393d257 Mon Sep 17 00:00:00 2001 From: AKash Date: Mon, 1 Dec 2025 02:08:51 +1100 Subject: [PATCH 1/3] issue fixed #1740 --- src/alasqlparser.jison | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/alasqlparser.jison b/src/alasqlparser.jison index 2e910b001c..d75f66cfec 100755 --- a/src/alasqlparser.jison +++ b/src/alasqlparser.jison @@ -356,7 +356,7 @@ Literal else $$ = $1.toLowerCase(); } | BRALITERAL - { $$ = {val:doubleq($1.substr(1,$1.length-2)), wrap:$1.substr(0,1)}; } + { $$ = {val:doubleq($1.substr(1,$1.length-2)), wrap:$1.substr(0,1)}; } | error NonReserved { $$ = $2.toLowerCase() } ; From c3e609cca01315ffdfc5421459ee84cbfcfdf15b Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Sun, 30 Nov 2025 15:16:14 +0000 Subject: [PATCH 2/3] Add test1740.js and fix Column.toString() to preserve square brackets Co-authored-by: mathiasrw <1063454+mathiasrw@users.noreply.github.com> --- src/50expression.js | 15 ++++++++++++--- test/test1740.js | 38 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 50 insertions(+), 3 deletions(-) create mode 100644 test/test1740.js diff --git a/src/50expression.js b/src/50expression.js index a340d3c995..a164298221 100755 --- a/src/50expression.js +++ b/src/50expression.js @@ -689,14 +689,23 @@ } toString() { - let s = this.columnid; + let s; - if (this.columnid == +this.columnid) { + // Handle columnid that is an object with val and wrap properties (e.g., from bracket notation) + if (typeof this.columnid === 'object' && this.columnid !== null && this.columnid.val !== undefined) { + const wrap = this.columnid.wrap; + const closeWrap = wrap === '[' ? ']' : wrap === '`' ? '`' : ''; + s = wrap + this.columnid.val + closeWrap; + } else if (this.columnid == +this.columnid) { s = '[' + this.columnid + ']'; + } else { + s = this.columnid; } if (this.tableid) { - s = this.tableid + (this.columnid === +this.columnid ? '' : '.') + s; + const isNumericColumnId = this.columnid === +this.columnid; + const isBracketedColumnId = typeof this.columnid === 'object' && this.columnid !== null && this.columnid.val !== undefined; + s = this.tableid + (isNumericColumnId || isBracketedColumnId ? '' : '.') + s; if (this.databaseid) { s = this.databaseid + '.' + s; diff --git a/test/test1740.js b/test/test1740.js new file mode 100644 index 0000000000..667214baf7 --- /dev/null +++ b/test/test1740.js @@ -0,0 +1,38 @@ +if (typeof exports === 'object') { + var assert = require('assert'); + var alasql = require('..'); +} + +describe('Test 1740 - Parse() then AST.toString() should restore square brackets on column names', function () { + const test = '1740'; + + it('A) Column name with space should preserve square brackets', function () { + var ast = alasql.parse('SELECT [Foo Bar] FROM tbl'); + var result = ast.toString(); + assert.strictEqual(result, 'SELECT [Foo Bar] FROM tbl'); + }); + + it('B) Column name with period should preserve square brackets', function () { + var ast = alasql.parse('SELECT [Foo.Bar] FROM tbl'); + var result = ast.toString(); + assert.strictEqual(result, 'SELECT [Foo.Bar] FROM tbl'); + }); + + it('C) Regular column name should not get square brackets', function () { + var ast = alasql.parse('SELECT FooBar FROM tbl'); + var result = ast.toString(); + assert.strictEqual(result, 'SELECT FooBar FROM tbl'); + }); + + it('D) Multiple bracketed columns should all preserve square brackets', function () { + var ast = alasql.parse('SELECT [First Name], [Last Name], Age FROM users'); + var result = ast.toString(); + assert.strictEqual(result, 'SELECT [First Name], [Last Name], Age FROM users'); + }); + + it('E) Bracketed column with special characters should preserve square brackets', function () { + var ast = alasql.parse('SELECT [Column!@#] FROM tbl'); + var result = ast.toString(); + assert.strictEqual(result, 'SELECT [Column!@#] FROM tbl'); + }); +}); From 81d771227b24be7781e624766a604945afb8abb7 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Sun, 30 Nov 2025 15:18:20 +0000 Subject: [PATCH 3/3] Refactor Column.toString() and add backtick test case Co-authored-by: mathiasrw <1063454+mathiasrw@users.noreply.github.com> --- src/50expression.js | 12 ++++++++---- test/test1740.js | 6 ++++++ 2 files changed, 14 insertions(+), 4 deletions(-) diff --git a/src/50expression.js b/src/50expression.js index a164298221..ba98b231cc 100755 --- a/src/50expression.js +++ b/src/50expression.js @@ -691,10 +691,15 @@ toString() { let s; - // Handle columnid that is an object with val and wrap properties (e.g., from bracket notation) - if (typeof this.columnid === 'object' && this.columnid !== null && this.columnid.val !== undefined) { + // Check if columnid is an object with val and wrap properties (e.g., from bracket notation) + const isBracketedColumnId = + typeof this.columnid === 'object' && + this.columnid !== null && + this.columnid.val !== undefined; + + if (isBracketedColumnId) { const wrap = this.columnid.wrap; - const closeWrap = wrap === '[' ? ']' : wrap === '`' ? '`' : ''; + const closeWrap = wrap === '[' ? ']' : '`'; s = wrap + this.columnid.val + closeWrap; } else if (this.columnid == +this.columnid) { s = '[' + this.columnid + ']'; @@ -704,7 +709,6 @@ if (this.tableid) { const isNumericColumnId = this.columnid === +this.columnid; - const isBracketedColumnId = typeof this.columnid === 'object' && this.columnid !== null && this.columnid.val !== undefined; s = this.tableid + (isNumericColumnId || isBracketedColumnId ? '' : '.') + s; if (this.databaseid) { diff --git a/test/test1740.js b/test/test1740.js index 667214baf7..4b4c39c436 100644 --- a/test/test1740.js +++ b/test/test1740.js @@ -35,4 +35,10 @@ describe('Test 1740 - Parse() then AST.toString() should restore square brackets var result = ast.toString(); assert.strictEqual(result, 'SELECT [Column!@#] FROM tbl'); }); + + it('F) Column name with backticks should preserve backticks', function () { + var ast = alasql.parse('SELECT `Foo Bar` FROM tbl'); + var result = ast.toString(); + assert.strictEqual(result, 'SELECT `Foo Bar` FROM tbl'); + }); });