Skip to content

Make the standard comparison level the default, with dates by value and SQLite's order across types #3171

Backlog

Description

@mathiasrw

What happens today

After #3163 AlaSQL has one comparator with the old rules. = compiles to ===, so two Date objects with the same time are not equal in WHERE, IN or a join, while <, GROUP BY and DISTINCT treat them as equal. Values of different types compare as JavaScript does, so 2 < 'b' and 2 > 'b' are both false.

const d1 = new Date(2024, 0, 1), d2 = new Date(2024, 0, 1);
alasql('SELECT * FROM ? WHERE d = ?', [[{d: d1}], d2]); // []

What to change

  1. In the comparison module, add three values of the compare rule from Rules that differ between dialects become named rules chosen when a statement is compiled #3170.
    • fast emits the plain operator, as today.
    • standard emits typeof a === 'number' && typeof b === 'number' ? a > b : alasql.cmp(a, b) > 0.
    • strict always calls alasql.cmp.
  2. alasql.cmp(a, b) orders NULL first, then numbers (and bigints), then text, then dates by time value. Two dates are equal when their time values are equal.
  3. The code generator asks the module for the source text of =, <>, <, >, <=, >=, BETWEEN, IN and join keys instead of writing the operator itself.
  4. Set compare: 'standard' as the default. The engine build and alasql/dialect/v4 set compare: 'fast'.

What stays the same

Done when

  • A new test file named after this issue covers =, <>, IN and a join on equal Date objects, a number against a text, and the same queries with alasql/dialect/v4 giving the 4.x results.
  • The dialect test against SQLite gets cases for comparisons across types, and their expected differences are removed.
  • test/performance/ shows the standard level adds no more than the few nanoseconds per comparison measured in One comparison module with levels (dates compared by value, MAX/MIN on strings, one NULL order) #2595.
  • 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 #1204.

Depends on

#3163, #3167, #3170, #2588

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions