Skip to content

Make GREATEST and LEAST return NULL consistently to close #2583 - #2735

Open
vaibh14yadav-tech wants to merge 1 commit into
AlaSQL:developfrom
vaibh14yadav-tech:fix-2583-greatest-least-null
Open

vaibh14yadav-tech wants to merge 1 commit into
AlaSQL:developfrom
vaibh14yadav-tech:fix-2583-greatest-least-null

Conversation

@vaibh14yadav-tech

Copy link
Copy Markdown

Closes #2583

Problem

GREATEST/LEAST (and the scalar MAX(a, b)/MIN(a, b)) used a plain reduce with a > b ? a : b. Comparisons with undefined (SQL NULL) are always false and null is coerced to 0, so results depended on argument order and on whether the NULL was a SQL NULL or a JS null parameter, e.g. GREATEST(1, NULL) → undefined but GREATEST(NULL, 1) → 1.

Fix

In src/55functions.js, check for NULL (== null, covering both null and undefined) before comparing. If any argument is NULL the result is NULL; otherwise the existing >/< comparison is used unchanged, so numbers, strings and dates behave as before (avoiding the regression noted for #2479/#2480).

NULL rule

I implemented the SQL-standard rule (NULL if any argument is NULL, as in SQL:2023, MySQL, Oracle and SQLite). I know the default is meant to be decided for v5 — happy to switch to the skip-NULLs rule (PostgreSQL / SQL Server / DuckDB) or put one behind an option if you prefer. Only the filter in the reduce and test D would change.

Tests

New test/test2583.js:

  • A: argument order doesn't change the result (SQL NULL)
  • B: a JS null parameter is treated as NULL, not as 0
  • C: normal numbers and strings still work
  • D: any NULL argument gives NULL, including scalar MAX/MIN

yarn test-only: 2446 pass, 0 fail.

@vaibh14yadav-tech

Copy link
Copy Markdown
Author

@mathiasrw,
Thanks for triaging this! Is the plan to merge it into develop now, or hold it until v5 work begins? Happy to put the new behavior behind an option if that would let it land sooner.

@mathiasrw

Copy link
Copy Markdown
Member

@vaibh14yadav-tech

We are currently preparing towards the v5.0.0 introducing a bunch of breaking changes - a lot of them about NULL - and a lot of them about the dialects of different engines. I hope it happens during next week.

I will take your PR and get into the mix. The code structure will update and I am OK to to do the work adapting your PR.

Thank you for putting time into AlaSQL.

@vaibh14yadav-tech

Copy link
Copy Markdown
Author

Thanks a lot, that's great to hear! Happy for you to adapt it however fits the v5 structure.

One small request: if possible, could the change land through this PR (or keep my commit), or credit me as a co-author if it ends up in a different commit? It's part of a course contribution, so it would really help me show it. Thanks again, and good luck with the v5 release!

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

GREATEST / LEAST (scalar MAX / MIN) give order-dependent results when an argument is NULL

3 participants