Repository: LMFDB/lmfdb

Issue:
Broken statistics
Currently http://beta.lmfdb.org/Genus2Curve/Q/stats gives a server error, and http://www.lmfdb.org/Genus2Curve/Q/stats has duplicated entries.  I believe that this is caused by a [recent change](https://github.com/LMFDB/lmfdb/blob/master/lmfdb/backend/database.py#L4297) to the handling of constraint columns in #2911.  I have a fix forthcoming....


Discussion:

Resolving patch:
diff --git a/lmfdb/backend/database.py b/lmfdb/backend/database.py
--- a/lmfdb/backend/database.py
+++ b/lmfdb/backend/database.py
@@ -3658,13 +3658,9 @@ def _has_stats(self, jcols, ccols, cvals, threshold, split_list=False, threshold
             values = [jcols, "split_total"]
         else:
             values = [jcols, "total"]
-        if ccols is None:
-            ccols = "constraint_cols IS NULL"
-            cvals = "constraint_values IS NULL"
-        else:
-            values.extend([ccols, cvals])
-            ccols = "constraint_cols = %s"
-            cvals = "constraint_values = %s"
+        values.extend([ccols, cvals])
+        ccols = "constraint_cols = %s"
+        cvals = "constraint_values = %s"
         if threshold is None:
             threshold = "threshold IS NULL"
         else:
@@ -3863,12 +3859,8 @@ def _quick_max(self, col, ccols, cvals):
         - ``cvals`` -- constraint values.  The max will be taken over rows where
             the constraint columns take on these values.
         """
-        if ccols is None:
-            constraint = SQL("constraint_cols IS NULL")
-            values = ["max", Json([col])]
-        else:
-            constraint = SQL("constraint_cols = %s AND constraint_values = %s")
-            values = ["max", Json([col]), ccols, cvals]
+        constraint = SQL("constraint_cols = %s AND constraint_values = %s")
+        values = ["max", Json([col]), ccols, cvals]
         selecter = SQL("SELECT value FROM {0} WHERE stat = %s AND cols = %s AND threshold IS NULL AND {1}").format(Identifier(self.stats), constraint)
         cur = self._execute(selecter, values)
         if cur.rowcount:
@@ -4832,13 +4824,9 @@ def _get_total_avg(self, cols, constraint, avg, split_list):
         jcols = Json(cols)
         total_str = "split_total" if split_list else "total"
         totaler = SQL("SELECT value FROM {0} WHERE cols = %s AND stat = %s AND threshold IS NULL").format(Identifier(self.stats))
-        if constraint:
-            ccols, cvals = self._split_dict(constraint)
-            totaler = SQL("{0} AND constraint_cols = %s AND constraint_values = %s").format(totaler)
-            totaler_values = [jcols, total_str, ccols, cvals]
-        else:
-            totaler = SQL("{0} AND constraint_cols IS NULL").format(totaler)
-            totaler_values = [jcols, total_str]
+        ccols, cvals = self._split_dict(constraint)
+        totaler = SQL("{0} AND constraint_cols = %s AND constraint_values = %s").format(totaler)
+        totaler_values = [jcols, total_str, ccols, cvals]
         cur_total = self._execute(totaler, values=totaler_values)
         if cur_total.rowcount == 0:
             raise ValueError("Database does not contain stats for %s"%(cols[0],))


Test patch: