You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Hello,
I encounter an issue trying to ordering my data containing date columns.
Here an example of code (Apps script):
function myTest() {
var data =[{id: 200, fromDate: new Date("2022-11-29"), toDate: new Date("2022-11-29")},
{id: 100, fromDate: new Date("2022-11-30"), toDate: new Date("2022-11-30")},
{id: 101, fromDate: new Date("2022-11-30"), toDate: new Date("2022-11-30")},
{id: 102, fromDate: new Date("2022-11-30"), toDate: new Date("2022-11-30")},
];
var db = new alasql.Database("MyDB");
db.exec('CREATE TABLE mytable (id INT, fromDate Date, toDate Date)');
db.tables.mytable.data = data;
console.log(db.tables.mytable.data);
var res = db.exec(`SELECT id FROM mytable ORDER BY fromDate, id ASC`,);
console.log(res);
}
Yes you are right, deeply linked to the Javascript date format. I tried to use string date (YYYYMMDD) and the double ordering is working fine. In my case, I do not have the choice, my data source contains javascript dates already.
This seems to be partially fixed on the develop branch for 2.1.3. If I recreate @fabads' example in a local test on develop, it reproduces the error. However if I add fromDate to the select query columns then it works as we'd expect.
# This doesn't work on `develop`SELECT id FROM mytable ORDER BY fromDate, id ASC# This does workSELECT id, fromDate FROM mytable ORDER BY fromDate, id ASC# Note that neither work on <2.1.3
The issue here is that the date needs to be converted to a Unix epoch via date.valueOf(). This only happens when the column type explicitly is DATE, DATETIME, STRING or NUMBER: see 426orderby.js#L60. However, the fromDate column as well as all associated type information is dropped out of the query object when it's not explicitly SELECTed.
This is also what causes the issue in @mathiasrw's example: since it's an anonymous query, there's no type information for date so the .valueOf() prefix is never added to the date.
I'm looking into how to get round this, but someone more familiar with the codebase may be able to make a quicker fix with this information!
@mathiasrw Do you know the reason why alasql.options.valueOf = false; by default? Is there some non-primitive type whose ordering would be broken by getting its valueOf? A quick fix would be to set this default to true. Within the current structure I can't see another good way to solve the problem for anonymous queries, as determining whether to get a date's valueOf depends on type information which anonymous queries don't have.
Activity
fabads commentedon Oct 5, 2022
With the JSFiddle:
http://jsfiddle.net/crf3nvj6/
mathiasrw commentedon Oct 6, 2022
I see. Hmm.
First I thought that it was a bug that we just fixed in a recent release with #1268 - but turns out the bug is still there.
I made an example that is a bit more simple: https://jsfiddle.net/3fL6y8on/2/
But when I change the value from a date object to an int it works as expected: https://jsfiddle.net/3fL6y8on/3/
So its something about the way we comparing dates...
fabads commentedon Oct 6, 2022
Yes you are right, deeply linked to the Javascript date format. I tried to use string date (YYYYMMDD) and the double ordering is working fine. In my case, I do not have the choice, my data source contains javascript dates already.
Buroni commentedon Oct 7, 2022
I've spent some time looking into this issue. It seems like there's several factors involved.
Short answer: You can get round this by setting
alasql.options.valueof = true;. For example see this fiddle http://jsfiddle.net/3rbL75hc/Long answer:
This seems to be partially fixed on the
developbranch for2.1.3. If I recreate @fabads' example in a local test ondevelop, it reproduces the error. However if I addfromDateto the select query columns then it works as we'd expect.The issue here is that the date needs to be converted to a Unix epoch via
date.valueOf(). This only happens when the column type explicitly isDATE,DATETIME,STRINGorNUMBER: see 426orderby.js#L60. However, thefromDatecolumn as well as all associated type information is dropped out of the query object when it's not explicitlySELECTed.This is also what causes the issue in @mathiasrw's example: since it's an anonymous query, there's no type information for
dateso the.valueOf()prefix is never added to the date.I'm looking into how to get round this, but someone more familiar with the codebase may be able to make a quicker fix with this information!
mathiasrw commentedon Oct 8, 2022
Nice work @Buroni. Very nice work...
mathiasrw commentedon Dec 27, 2022
Any luck @Buroni ?
Buroni commentedon Mar 7, 2023
@mathiasrw oh it's been a while.. Digging into it now
Buroni commentedon Mar 8, 2023
@mathiasrw Do you know the reason why
alasql.options.valueOf = false;by default? Is there some non-primitive type whose ordering would be broken by getting itsvalueOf? A quick fix would be to set this default totrue. Within the current structure I can't see another good way to solve the problem for anonymous queries, as determining whether to get a date'svalueOfdepends on type information which anonymous queries don't have.mathiasrw commentedon Mar 9, 2023
Probably for backwards compatibility. Happy to change it and bump up to v4