Skip to content

ORDER BY and dates issue #1513

Closed
Closed

Description

@fabads

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);
}

The result I get:

[ {id: 200}, {id: 102}, {id: 101}, {id: 100} ]

But the result I expect is:

[ {id: 200}, {id: 100}, {id: 101}, {id: 102} ]

Any idea of what's happening ?

Activity

  1. mathiasrw commented on Oct 6, 2022

    @mathiasrw
    Member

    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...

  2. fabads commented on Oct 6, 2022

    @fabads
    Author

    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.

  3. Buroni commented on Oct 7, 2022

    @Buroni
    Contributor

    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 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 work
    SELECT 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!

  4. mathiasrw commented on Oct 8, 2022

    @mathiasrw
    Member

    Nice work @Buroni. Very nice work...

  5. mathiasrw commented on Dec 27, 2022

    @mathiasrw
    Member

    Any luck @Buroni ?

  6. Buroni commented on Mar 7, 2023

    @Buroni
    Contributor

    @mathiasrw oh it's been a while.. Digging into it now

  7. Buroni commented on Mar 8, 2023

    @Buroni
    Contributor

    @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.

  8. mathiasrw commented on Mar 9, 2023

    @mathiasrw
    Member

    Do you know the reason why alasql.options.valueOf = false; by default?

    Probably for backwards compatibility. Happy to change it and bump up to v4

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions