Skip to content

Order by date not working #5

Closed
Closed

Description

@billymoon

I was trying to run the sample: https://github.com/agershun/alasql/blob/master/examples/sandbox.html

It is not behaving as I would expect.

Order by hired_on is not working when running...

SELECT name,hired_on FROM employees ORDER BY hired_on

http://jsfiddle.net/czqfyhat/

Ordering by all other columns seems to be ok.

Activity

  1. agershun commented on Nov 8, 2014

    @agershun
    Member

    Thank you for the issue! I will fix this problem with dates in the next version. The reason of problem is that Dates in JS are actually not Dates, but objects, and we can not compare them directly with "<" or ">", but need to convert them before to milliseconds or write special compare function.

  2. billymoon commented on Nov 9, 2014

    @billymoon
    Author

    @agershun thanks for feedback. I don't think there is a problem with comparing javascript date objects with < and >. See example code here...

    var past = (new Date("2000-01-01"));
    var now = (new Date());
    var future = (new Date("2020-01-01"));
    console.log(JSON.stringify({
        past: past,
        now: now,
        future: future,
        past_gt_future: (past > future),
        past_lt_future: (past < future),
        past_gt_now: (past > now),
        past_lt_now: (past < now),
        future_gt_now: (future > now),
        future_lt_now: (future < now)
    }, null, 2));
    

    ... which outputs ...

    {
      "past": "2000-01-01T00:00:00.000Z",
      "now": "2014-11-09T21:05:13.685Z",
      "future": "2020-01-01T00:00:00.000Z",
      "past_gt_future": false,
      "past_lt_future": true,
      "past_gt_now": false,
      "past_lt_now": true,
      "future_gt_now": true,
      "future_lt_now": false
    } 
    

    If you point me in the right direction, I wouldn't mind having a go at making the fix myself for you to pull.

  3. agershun commented on Nov 10, 2014

    @agershun
    Member

    Hi. Thank you for you post. I will check it during next two days.

    Now I use the following approach:

    1. In case if exists column definitions (see alasql.databases..tables..columns and I know that this column has DATE or DATETIME type:

    for compare function - conversion to integer with "+" like

    var dt = new Date();
    console.log(+dt);

    I already uses this approach in compileOrder() function (https://github.com/agershun/alasql/blob/master/src/42compile.js).

    I think, I will use function like from the posts at stackoverflow about dates in JS

    1. In case when there is no column definition, I will check Date-type on the fly and compile the different function code, like:
      if(dt instanceof Date) {
      ... generate right comparisin
      }
    2. I think this Date issue can be fixed in the following functions:
    3. yy.Select.prototype.compileOrder() - https://github.com/agershun/alasql/blob/master/src/42compile.js
    4. yy.Op.prototype.toJavaScript() - file https://github.com/agershun/alasql/blob/master/src/50expression.js
    5. This issue will require to require generation of two different types of code: when you predefine columns (SQL style) and when - not (JavaSqript style). The first approach is much faster at execution time, because second will require check datatypes on the fly.
  4. added this to the milestone on Nov 10, 2014
  5. self-assigned this
    on Nov 10, 2014
  6. agershun commented on Nov 10, 2014

    @agershun
    Member

    I think I will use .getTime() instead "+", or better .valueof() method to compare any objects except numbers, booleans and strings.

    Here are performance tests for dates and for primitive types

  7. agershun commented on Nov 10, 2014

    @agershun
    Member

    Another idea is to keep all SQL dates in string format, like: "YYYY-MM-DD", but to have separate Date type for JavaScript dates, like JSDATE. It much faster and better from localization point of view, e.g., I am tired to fight with dates like: "Mon Oct 20 2014 03:00:00 GMT+0300 (MSK)" in JavaScript instead simple "2014-10-20"

  8. agershun commented on Nov 10, 2014

    @agershun
    Member

    I fixed a little bit, and now the example works fine for ORDER BY clause. But, unfortunately, the problem is more wider...

  9. agershun commented on Nov 22, 2014

    @agershun
    Member

    I want tol use two different approaches:

    • Use SQL ISO Date (which easily can be emulated with simple strings)
    • Use JavaScript Date objects

    We added special option: alasql.jsdate to choose proper approach.

  10. billymoon commented on Nov 22, 2014

    @billymoon
    Author

    @agershun does the SQL ISO Date include timezone offset? What about summer/winter time? Or is it just date - not time? Does it account for validating leap years?

  11. agershun commented on Nov 22, 2014

    @agershun
    Member

    I am thinking about just simple date without time zones. For exmple, for our main project (planning and budgeting software - timezones are not important. Even more: I always have to fix dates like this:

    Sat Nov 22 2014 23:37:45 GMT+0300 (MSK)

    Because, sometimes JS adds 3 hours of difference between Greenwich and local time (in also depends on the computer setup), because "Midnight in Moscow is lunchtime in L.A.", and we have to check all this issues.
    So, I think we will simply as an option use simple string in "Japanese" format: YYYY-MM-DD. It easy to sort and compare strings rather than JS Date objects. I think, it is not so hard to rewrite all necessary DATE functions to operate with strings (like DAY(), MONTH(), YEAR()).
    Of course, for complex software with time functionality, we will keep "classic" JS Date objects.


    I think this is acceptable, because SQL.js does exactly the same thing:
    http://kripken.github.io/sql.js/GUI/

  12. billymoon commented on Nov 22, 2014

    @billymoon
    Author

    @agershun I see what you are saying about SQLite and strings as date type. I was not thinking clearly - but you are completely right - it is a very reasonable approach to store dates as string, and leave it up to the user to manage the data type in what format they like, and use date manipulation functions. It is also easy for functions to be added by the user, so it is easy for dates to be handled in any which way the user pleases. Having seen the light - I would say that converting to JS dates should not be the default at all, but it is a nice feature if enabled.

    I had a look at the latest library, and was trying to disable the jsdate (which I expect is alasql.options.jsdate - not alasql.jsdate as you mentioned in this thread) option, but unfortunately can't get it to work...

    http://jsfiddle.net/Lvemnqb3/

    Seems that the dates are somehow converted to js date format even when I set the option to false, and more strangely, seem to pick up random timezone offsets, and display the wrong date/time.

  13. agershun commented on Nov 23, 2014

    @agershun
    Member

    Yes, sorry, it was not implemented fully yet. I think in couple of days I finish with this issue.

    What do you think, which approach is the better?
    a) to have two separate datatypes (e.g. ISODATE and JSDATE)
    b) or to use general option (like alasql.options.jsdate) to switch between these approaches by default (but with one datatype DATE)

  14. agershun commented on Nov 24, 2014

    @agershun
    Member

    @billymoon HI,

    I have made a test in version 0.0.25 to try the approach with alasql.options.datetimeformat:
    https://github.com/agershun/alasql/blob/version-0.0.25/test/test131.js

    All other datetime functionality will be chnged in the similar way.

  15. agershun commented on Nov 26, 2014

    @agershun
    Member

    Hi, I finished with it! I simply implement Date (and other JavaScript objects) into SQL.

        alasql.fn.Date = Date;
        alasql('CREATE TABLE orders (orderdate Date)');
        alasql('INSERT INTO orders VALUES ("2014-01-01")');
        var res = alasql('SELECT * FROM orders");
        // It give a JavaScript dates
        var res = alasql('SELECT orderdare->getFullYear() FROM orders');
        // This is gives an array of years
        // Here - getFullYear() - is a function of object Date.prototype

    Check for more examples:
    https://github.com/agershun/alasql/blob/version-0.0.26/test/test140.js

    If you want ot use good old SQL date just use DATE, instead Date:

        alasql('CREATE TABLE sample (sqldate DATE, jsdate Date)');
  16. agershun commented on Nov 27, 2014

    @agershun
    Member

    I updated the first sample in Fiddle - now it works!

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

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions