I am trying to sum an INT column as was getting Arithmetic overflow errors.
This thread http://www.llblgen.com/tinyforum/Messages.aspx?ThreadID=23137&HighLight=1 has some code that solves that, which works fine on SQL Server. I am also running against MySQL (actually MariaDB 10.3.11). The problem is the code fails on MySQL. This is using LLBLGen Pro v5.5.1 with the build from 27-Nov-2018.
private static long GetSum(IDataAccessAdapter dataAccessAdapter, IEntityField2 field, IPredicate filter = null)
{
var qf = new MyData.FactoryClasses.QueryFactory();
var sq = qf.Create().Select(ConvertFunctions.ToInt64(field).Sum());
if (filter != null)
{
sq.Where(filter);
}
return dataAccessAdapter.FetchScalar<long?>(sq) ?? 0;
}
The offending SQL is:
SELECT SUM(CAST(`table`.`field` AS BIGINT)) AS `ExF`
FROM `table`
WHERE (((`table`.`field` IS NOT NULL)))
LIMIT 1
Per
https://stackoverflow.com/questions/4660383/how-do-i-cast-a-type-to-a-bigint-in-mysql
it is not legal to CAST as BIGINT. However, it does work to CAST as SIGNED (or UNSIGNED).
I think the problem is ConvertFunctions.ToInt64 does a CAST as BIGINT.
I worked around this for now by changing the code to use ConvertFunctions.ToDouble and FetchScalar<double?>. It loses some precision but generates valid MySQL queries. If you agree with my analysis, please consider fixing this in a future release.
Thank you!