Repository navigation
Open
Labels
Description
I have 3 questions.
Background:
At the moment MAX (and MIN but to make it easy to read I only say MAX) only works on numbers (its implemented with Math.max)
Been playing a bit with it and expanded stdlib.MAX from 55functions.js only to realise that this only works when you hardcode the values. Ups. The check needs to be in the generated code and not here - so would have to replace the existing
return 'Math.max('+Array.prototype.join.call(arguments, ',')+')'with
return 'someAlasqlUtil.awesomeMaxFunctionThatCanHandleStrings('+Array.prototype.join.call(arguments, ',')+')'where awesomeMaxFunctionThatCanHandleStrings would look something like
// If all arguments are not numbers treat all as string.
for (var i = 0; i < arguments.length; i++) {
if(arguments[i] != +arguments[i]){
return Array.prototype.sort.call(arguments)[arguments.length-1] // I think I read that pop() or switch() on arguments is bad. Todo: theck if .sort() is also bad.
}
};
return Math.max.apply(null, arguments);Questions
- We loose some speed if the function needs to spend time finding out if it will be comparing numbres or strings. Any inputs to weather its best to expand the existing
MAXfunction or to make a new function falled (for example)MAX_STRING? - To try to understand the code: if the
MAXcode was to be expanded where would the best place be to have the code fromsomeAlasqlUtil.awesomeMaxFunctionThatCanHandleStrings? - To try to understand the code: The solution is not hard to implement with a user defined aggregation function - but if the
MAX_STRINGcode was to be implemented in the core - where would the correct place be?
alasql.aggr.MAX_STRING = function(value,accumulator,stage){
if(1===stage){
return value;
}
if(2===stage && accumulator<value){
return value;
}
return accumulator;
};Reacted by TWY
Activity
agershun commentedon Feb 27, 2016
We can change internal aggregator implementation in 423groupby.js`` to:
agershun commentedon Feb 27, 2016
In the above example I changed internal aggregator ``MAX()
. We also can define this aggregator somewhere at55functions.ja`:... thinking...
The important note: we have
MAX()as aggregator andMAX()as function. Now aggregator is hardcoded and the function is a part ofalasql.stdlib(compiled functions).If we want to change the aggregator we need to remove all
MAXlines at423groups.jsand add custom aggregatoralasql.aggr.MAX,To change the function we can remove it from
alasql.stdlib.MAXand simply add the normal function without compilation toalasql.stdfn.MAX. In this case we do not need to dance with compilation, and AlaSQL will use just simple regular JS function.mathiasrw commentedon Feb 27, 2016
Interesting. So
MAX(hight) ... group by xyzis a differentMAXthanMAX(test1, test2, test3)Do you have any inputs regarding peformance regarding to make a new name or expand the existing functionality?
t-wy commentedon Oct 2, 2025
I am not sure about the actual expected behavior of the "aggregator" MAX, and the current code (as of 2025) is as the following: (similarly for MIN) (As in
src/423groupby.js)So currently it seems that it still attempts to cast the value as a number if the value is in
objecttype (not includingstring). I am expecting sometypeof y == 'string'check would work in this case but I'm not that sure.Since a string does not meet all conditions, currently it is never assigned or casted to be null during execution. And this makes AlaSQL the odd one here in handling string (though it may be a compromise given that AlaSQL supports different input FROM sources and maybe if some uses numbers as strings so it may possibly break things).
Bumping this for more attention regarding the consensus on the correct handling this basic feature when handling strings.
mathiasrw commentedon Nov 4, 2025
@t-wy so what behaviour do you propose?
Is it to return NULL or is to return the string latest in the alphabet? I guess NULL?
Maybe the larges string (ascii sort) but then if there is only one (actual) number in the mix it will be the number?
t-wy commentedon Nov 4, 2025
So I think the problem is that currently AlaSQL treating a numeric string as a number may lead to ambiguous behavior when the MAX aggregator follows typically SQL behaviors that support sorting by lexicographical order, and also need to keep numeric order when it only encounters number?
Currently I have not seen any other SQL language has any function name for MAX that works for some explicit type, but I am also aware that modifying the current MAX behavior may be a breaking change.