Skip to content

Make one date conversion produce every date value, so NOW, DATE, CAST and DATE columns agree #3173

Backlog

Description

@mathiasrw

What happens today

Each date producer has its own form (src/functions/date.js, src/expression/convert.js). NOW and CURRENT_DATE follow alasql.options.dateAsString, DATE always returns a Date, CAST formats text through structuredDate with a two-digit year, and a DATE column stores whatever it is given.

alasql('SELECT VALUE NOW()');                      // '2026-11-26 10:38:16.449'
alasql('SELECT VALUE DATE("2024-01-02")');         // a Date object
alasql('SELECT VALUE CAST("2020-01-02" AS DATE)'); // '20.01.02'
alasql('SELECT VALUE NOW() > DATE("2020-01-01")'); // false

What to change

  1. Add toDate(value, type) to the conversion module of Convert values for a typed column in one function, used by every write into a table #2659. It reads a Date, ISO text (YYYY-MM-DD, YYYY-MM-DD HH:MM:SS[.SSS], with T or a space) and a number in the form YYYYMMDD or YYYYMMDDhhmmss. An impossible date such as 1994-02-30 is an error that names the value. NULL gives NULL.
  2. toDate returns one form per type, chosen by alasql.options.dateAsString. With true (the default) a DATE is YYYY-MM-DD and a DATETIME or TIMESTAMP is YYYY-MM-DD HH:MM:SS.SSS, in local time as NOW gives it today. With false every one is a Date object.
  3. NOW, GETDATE, CURRENT_TIMESTAMP, CURRENT_DATE, DATE, CAST and CONVERT to a date type, and the write into a DATE or DATETIME column, all call toDate. The CONVERT styles (CONVERT(STRING, d, 102) and the others) keep formatting text as today.
  4. Date functions (DATEDIFF, DATEADD, YEAR and the others) accept both forms, as they do now through newDate.
  5. Add the rule dateValues to Rules that differ between dialects become named rules chosen when a statement is compiled #3170 with the values iso (the new default) and v4. With v4 each producer keeps its 4.x form. Add it to alasql/dialect/v4.

What stays the same

  • NOW() and CURRENT_DATE() give the same text as before.
  • With dateAsString: false they give Date objects as before.
  • With alasql/dialect/v4 every date producer gives its 4.x value.
  • In the default, DATE(...) returns text, CAST to a date type returns ISO text or an error, and a DATE column stores ISO text.

Done when

  • A new test file named after this issue checks every producer above with dateAsString true and false, that NOW() > DATE('2020-01-01') is true in both, that CAST('1994-02-30' AS DATE) throws, that CAST(20200102 AS DATE) is 2020-01-02, and the 4.x values with alasql/dialect/v4.
  • The dialect test against SQLite gets date cases (DATE('2020-01-02'), comparisons and ORDER BY on ISO text, MAX over a date column).
  • The full test suite and the CI checks (dialect test, parity check, run-state check, binding check) pass, and the pull request lists every parity difference.

Fixes

Fixes #673, #2804, #1627, #2853.

Depends on

#3170, #3171, #2659

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions