Skip to content

MIN() and MAX() functions not returning values for dates #2453

Closed

Description

@gabrielmongefranco

Hello,

Thank you for making this great library!!

I am testing out alasql for SharePoint lists reports. It looks like MIN() and MAX() are not working for dates (but things like COUNT() and YEAR() work so I know the date format is OK). I'm not 100% sure but I think the issue maybe that alasql is using JavaScript Math functions for aggregates, which do not support dates. I would expect this to work as it is very basic functionality for reports and dashboards.

I made a temporary fix in my project by adding two new functions, like this:

// ==========================================
// ALASQL CUSTOM AGGREGATORS
// ==========================================
alasql.aggr.MAXDATE = function(v, s, stage) {
    if (stage === 1) return v;
    if (stage === 2) {
        if (v == null || v === '') return s; 
        if (s == null || s === '') return v;
        return (v > s) ? v : s;
    }
    if (stage === 3) return s;
};

alasql.aggr.MINDATE = function(v, s, stage) {
    if (stage === 1) return v;
    if (stage === 2) {
        if (v == null || v === '') return s; 
        if (s == null || s === '') return v;
        return (v < s) ? v : s;
    }
    if (stage === 3) return s;
};

I would encourage you to implement some kind of support for dates in the aggregate functions MIN, MAX, FIRST, LAST, whether it's a fix similar to the above or an automatic conversion of date to number+unix epoch or similar.

Gabriel Mongefranco
Mobile Data Architect
Eisenberg Family Depression Center, University of Michigan

Activity

  1. itsLohit commented on Apr 1, 2026

    @itsLohit

    Hi! I'd like to work on a fix for this.

    My plan:

    • Replace Math.min/Math.max in the MIN/MAX aggregators with </>
      comparison operators, which work correctly with Date values
    • Add a test file following the test000.js convention

    Let me know if there's anything specific you'd like me to do differently!

  2. gabrielmongefranco commented on Apr 6, 2026

    @gabrielmongefranco
    Author

    I don't know if the maintainers are around anymore... or maybe they're just busy. I would say, if you know how to fix it, then go for it and send them a pull request.

  3. gabrielmongefranco commented on Apr 19, 2026

    @gabrielmongefranco
    Author

    Hi, I hadn't noticed that version 4.17 has a slightly different implementation. According to Claude Opus, this is the fix needed:

    GREATEST / LEAST (scalar)
    Change needed: Filter nulls; use .getTime() for Date comparison.

    stdlib.GREATEST = stdlib.MAX = function () {
        var args = Array.prototype.slice.call(arguments);
        return '(function(){ ' +
            'var vals = [' + args.join(',') + '].filter(function(x){ return x !== null && typeof x !== "undefined"; }); ' +
            'if(!vals.length) return undefined; ' +
            'return vals.reduce(function(a,b){ ' +
                'var av = a instanceof Date ? a.getTime() : a; ' +
                'var bv = b instanceof Date ? b.getTime() : b; ' +
                'return av > bv ? a : b; }); ' +
        '})()';
    };
    
    stdlib.LEAST = stdlib.MIN = function () {
        var args = Array.prototype.slice.call(arguments);
        return '(function(){ ' +
            'var vals = [' + args.join(',') + '].filter(function(x){ return x !== null && typeof x !== "undefined"; }); ' +
            'if(!vals.length) return undefined; ' +
            'return vals.reduce(function(a,b){ ' +
                'var av = a instanceof Date ? a.getTime() : a; ' +
                'var bv = b instanceof Date ? b.getTime() : b; ' +
                'return av < bv ? a : b; }); ' +
        '})()';
    };
    

    Reasoning: ANSI SQL specifies that GREATEST/LEAST return NULL when all arguments are NULL, and skip NULLs otherwise. JavaScript's > / < against null coerces it to 0, producing wrong results. Dates need .getTime() for correct numeric comparison.
    ANSI SQL: Current non-compliant → after fix compliant.R: pmax/pmin with na.rm=TRUE happen to match, but that is incidental.

  4. gabrielmongefranco commented on Apr 19, 2026

    @gabrielmongefranco
    Author

    Closing here and moving discussion to #2481 , which includes similar issues with other aggregate functions in the same file.

  5. gabrielmongefranco commented on Apr 19, 2026

    @gabrielmongefranco
    Author

    Closing here and moving discussion to #2481 , which includes similar issues with other aggregate functions in the same file.

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

Metadata

Metadata

Assignees

No one assigned

    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