Repository navigation
Commit 7ea372a
committed
fix(query): count and restrict expressions with an empty primary key
An expression with an empty primary key, such as `dj.U() & T` or
`dj.U().aggr(T, n="count(*)")`, holds at most one row. Two operations on
such expressions failed:
- `len()` took the single-column branch and generated
`count(DISTINCT )`, which is invalid SQL. It now returns 0 or 1 by
checking whether the row exists.
- Restricting a `dj.U()` aggregation hit a bare assert in
`Aggregation.make_sql`. Removing the assert alone would not have been
enough: the MySQL branch emitted HAVING only together with GROUP BY,
so the restriction would have been dropped without notice. MySQL now
emits HAVING without GROUP BY, and PostgreSQL takes the subquery path
whenever the aggregation is restricted, with or without grouping
attributes.
Restriction conditions on an aggregation are now joined with str(), as
the base where_clause already does, so restricting any aggregation by
False no longer raises TypeError.
Closes #1581
Closes #15801 parent a00b89f commit 7ea372a
2 files changed
Lines changed: 136 additions & 17 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1156 | 1156 | | |
1157 | 1157 | | |
1158 | 1158 | | |
| 1159 | + | |
| 1160 | + | |
| 1161 | + | |
| 1162 | + | |
1159 | 1163 | | |
1160 | 1164 | | |
1161 | 1165 | | |
| |||
1355 | 1359 | | |
1356 | 1360 | | |
1357 | 1361 | | |
1358 | | - | |
1359 | 1362 | | |
| 1363 | + | |
| 1364 | + | |
| 1365 | + | |
| 1366 | + | |
| 1367 | + | |
| 1368 | + | |
| 1369 | + | |
1360 | 1370 | | |
1361 | 1371 | | |
1362 | 1372 | | |
1363 | | - | |
1364 | | - | |
1365 | | - | |
| 1373 | + | |
1366 | 1374 | | |
1367 | 1375 | | |
1368 | 1376 | | |
1369 | 1377 | | |
1370 | 1378 | | |
1371 | 1379 | | |
1372 | | - | |
| 1380 | + | |
1373 | 1381 | | |
1374 | 1382 | | |
1375 | 1383 | | |
1376 | | - | |
1377 | | - | |
| 1384 | + | |
1378 | 1385 | | |
1379 | | - | |
1380 | | - | |
| 1386 | + | |
| 1387 | + | |
1381 | 1388 | | |
1382 | 1389 | | |
1383 | 1390 | | |
1384 | 1391 | | |
1385 | | - | |
1386 | | - | |
1387 | | - | |
1388 | | - | |
1389 | | - | |
1390 | | - | |
1391 | | - | |
1392 | | - | |
| 1392 | + | |
| 1393 | + | |
1393 | 1394 | | |
1394 | 1395 | | |
1395 | 1396 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
| 103 | + | |
| 104 | + | |
| 105 | + | |
| 106 | + | |
| 107 | + | |
| 108 | + | |
| 109 | + | |
| 110 | + | |
| 111 | + | |
| 112 | + | |
| 113 | + | |
| 114 | + | |
| 115 | + | |
| 116 | + | |
| 117 | + | |
| 118 | + | |
0 commit comments