Browse Source

Fixed error where aggregates were returning NULL

Kenric Nugteren 5 days ago
parent
commit
1eca9d862c
1 changed files with 6 additions and 4 deletions
  1. 6 4
      inabox.database.sqlite/SQLiteProvider.cs

+ 6 - 4
inabox.database.sqlite/SQLiteProvider.cs

@@ -95,7 +95,7 @@ public class SQLiteRegExp : SQLiteFunction
 [SQLiteFunction(Name = "DECIMAL_SUM", Arguments = 1, FuncType = FunctionType.Aggregate)]
 public class SQLiteDecimalSum : SQLiteFunction
 {
-    public override void Step(object[] args, int stepNumber, ref object contextData)
+    public override void Step(object[] args, int stepNumber, ref object? contextData)
     {
         if (args.Length < 1 || args[0] == DBNull.Value)
             return;
@@ -104,9 +104,9 @@ public class SQLiteDecimalSum : SQLiteFunction
         contextData = d;
     }
 
-    public override object Final(object contextData)
+    public override object? Final(object? contextData)
     {
-        return contextData;
+        return contextData ?? 0M;
     }
 }
 
@@ -2458,7 +2458,9 @@ public class SQLiteProvider : IProvider
                     tables.Add(tuple);
                 }
 
-                return string.Format("{0}.[{1}]", tuple.Item2, aggCol);
+                // If there were no rows to aggregate over, then the LEFT OUTER JOIN will fill the row with NULL,
+                // and so we need to coerce into a reasonable value.
+                return $"IFNULL({tuple.Item2}.[{aggCol}],{EscapeValue(GetColumnDefaultValue(agg.TResult))})";
             case IComplexFormulaConstantNode constantNode:
                 var constant = constantNode.GetConstant();
                 if (constant is FilterConstant filterConstant)