AlaSQL / alasql

AlaSQL.js - JavaScript SQL database for browser and Node.js. Handles both traditional relational tables and nested JSON data (NoSQL). Export, store, and import data from localStorage, IndexedDB, or Excel.
http://alasql.org
MIT License
7.02k stars 655 forks source link

SELECT query (a AS b, b AS c), change in behavior between 4.1.9 and 4.1.10 #1820

Closed citruspress closed 11 months ago

citruspress commented 12 months ago

With a SELECT query like the following:

let item1 = { a: 1, b: "hello" };
let item2 = { a: 2, b: "" };

var res = alasql('SELECT a as b, b as c FROM ? GROUP BY a,b', [[item1, item2]]);
console.log(res);

I get the following results using 4.1.9 (https://jsfiddle.net/02c9esvw/37/):

[{
  b: 1,
  c: "hello"
}, {
  b: 2,
  c: ""
}]

And the following with 4.1.10 (https://jsfiddle.net/kqjtoapz/99/):

[{
  c: "hello"
}, {
  c: ""
}]

The same goes when selecting MATRIX like this: [0] AS [1], [1] AS [2] which is what we're actually using in some cases.

I would expect the 4.1.9 result is correct. If it's not is this an intentional change that we should adapt to?

paulrutter commented 11 months ago

I shortly looked into this issue, and the reason that this fails is due the these lines in 38query (which were added in https://github.com/AlaSQL/alasql/issues/1797):

                for (const key in query.groupColumns) {
                    if (query.groupColumns[key] !== key && d[query.groupColumns[key]])
                        delete d[query.groupColumns[key]];
                }

Although i don't have a fix, the workaround would be to not use aliases in queries that map to columns used in the same SELECT.

paulrutter commented 11 months ago

I have added a fix here: https://github.com/AlaSQL/alasql/pull/1835

mathiasrw commented 11 months ago

Released as part of v4.2.2