Skip to content

Commit b82ebb7

Browse files
Copilotmathiasrw
andauthored
Fix CI/CD (#2291)
Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: mathiasrw <1063454+mathiasrw@users.noreply.github.com>
1 parent 0e44f9c commit b82ebb7

File tree

4 files changed

+78
-8
lines changed

4 files changed

+78
-8
lines changed

src/50expression.js

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,8 @@
189189

190190
const toTypeNumberOps = new Set(['-', '*', '/', '%', '^']);
191191
const toTypeStringOps = new Set(['||']);
192+
// Regex to detect identifiers that need bracket wrapping (spaces, dots, hyphens, square brackets)
193+
const re_needsBrackets = /[\s.\-\[\]]/;
192194
const toTypeBoolOps = new Set([
193195
'AND',
194196
'OR',
@@ -688,18 +690,34 @@
688690
assign(this, params);
689691
}
690692

691-
toString() {
692-
let s = this.columnid;
693+
// Check if identifier needs to be wrapped in brackets
694+
// (numeric values, or contains spaces, dots, or other special characters)
695+
static needsBrackets(id) {
696+
if (id == null) return false;
697+
// Numeric indices need brackets
698+
if (id == +id) return true;
699+
// Check for special characters that require brackets
700+
return re_needsBrackets.test(id);
701+
}
693702

694-
if (this.columnid == +this.columnid) {
695-
s = '[' + this.columnid + ']';
703+
static wrapId(id) {
704+
if (Column.needsBrackets(id)) {
705+
return '[' + id + ']';
696706
}
707+
return id;
708+
}
709+
710+
toString() {
711+
const colNeedsBrackets = Column.needsBrackets(this.columnid);
712+
let s = colNeedsBrackets ? '[' + this.columnid + ']' : this.columnid;
697713

698714
if (this.tableid) {
699-
s = this.tableid + (this.columnid === +this.columnid ? '' : '.') + s;
715+
// Omit dot separator when columnid is wrapped in brackets (e.g., table[1] not table.[1])
716+
const separator = colNeedsBrackets ? '' : '.';
717+
s = Column.wrapId(this.tableid) + separator + s;
700718

701719
if (this.databaseid) {
702-
s = this.databaseid + '.' + s;
720+
s = Column.wrapId(this.databaseid) + '.' + s;
703721
}
704722
}
705723

src/alasqlparser.jison

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -356,7 +356,7 @@ Literal
356356
else $$ = $1.toLowerCase();
357357
}
358358
| BRALITERAL
359-
{ $$ = {val:doubleq($1.substr(1,$1.length-2)), wrap:$1.substr(0,1)}; }
359+
{ $$ = doubleq($1.substr(1,$1.length-2)); }
360360
| error NonReserved
361361
{ $$ = $2.toLowerCase() }
362362
;

src/alasqlparser.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

test/test1740.js

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
if (typeof exports === 'object') {
2+
var assert = require('assert');
3+
var alasql = require('..');
4+
}
5+
6+
/*
7+
Test for issue #1740 - Parse() then AST.toString() doesn't restore square brackets on column name
8+
https://github.com/AlaSQL/alasql/issues/1740
9+
10+
If a column name requires square brackets (e.g. due to a space or a period in the column name),
11+
toString() on the AST should preserve the brackets.
12+
*/
13+
14+
describe('Test 1740 - AST.toString() preserves square brackets on column names', function () {
15+
it('A) Column with space in name', function () {
16+
var sql = 'SELECT [Foo Bar] FROM tbl';
17+
var ast = alasql.parse(sql);
18+
assert.equal(ast.toString(), sql);
19+
});
20+
21+
it('B) Column with dot in name', function () {
22+
var sql = 'SELECT [Foo.Bar] FROM tbl';
23+
var ast = alasql.parse(sql);
24+
assert.equal(ast.toString(), sql);
25+
});
26+
27+
it('C) Column with hyphen in name', function () {
28+
var sql = 'SELECT [Foo-Bar] FROM tbl';
29+
var ast = alasql.parse(sql);
30+
assert.equal(ast.toString(), sql);
31+
});
32+
33+
it('D) Numeric column index preserves brackets', function () {
34+
var sql = 'SELECT [1] FROM tbl';
35+
var ast = alasql.parse(sql);
36+
assert.equal(ast.toString(), sql);
37+
});
38+
39+
it('E) Table.column with bracket notation (dot omitted for numeric index)', function () {
40+
// When using table alias with numeric column index, the dot is omitted in toString()
41+
// Input: d.[1] -> Output: d[1]
42+
var sql = 'SELECT d.[1] FROM tbl AS d';
43+
var ast = alasql.parse(sql);
44+
assert.equal(ast.toString(), 'SELECT d[1] FROM tbl AS d');
45+
});
46+
47+
it('F) Normal column names without special characters', function () {
48+
var sql = 'SELECT normalColumn FROM tbl';
49+
var ast = alasql.parse(sql);
50+
assert.equal(ast.toString(), sql);
51+
});
52+
});

0 commit comments

Comments
 (0)