Commit e88a4e5
authored
Fix the behavior of
The current implementation has a special handling for `HAVING` clauses
without `GROUP BY` (this is [valid in
MySQL](https://stackoverflow.com/questions/6924896/having-without-group-by)
in some SQL modes). This implementation takes advantage of the
equivalence of ungrouped HAVING with WHERE and rewrites such a `HAVING
<condition>` clause to `AND <condition>`.
However, the current implementation has some issues:
1. The presence of `GROUP BY` in a query is incorrectly detected. The
detection always fails, and thus every HAVING query is considered to be
ungrouped. I added a fix in the first commit.
2. Rewriting `HAVING <condition>` to `AND <condition>` fails with an
error `near "AND": syntax error` when no WHERE is included in the query.
This is fixed in the second commit.
3. When an aggregate function is used in HAVING (e.g, `COUNT(*) > 1`),
the query fails with: `misuse of aggregate function COUNT()`. This is
because aggregate functions can't be used in the WHERE clause. This is
fixed in the second commit.
To fix all of these issues, I implemented a fix for the `GROUP BY`
detection (case 1), and changed the rewriting of `HAVING <condition>`
without `GROUP BY` to `GROUP BY 1 HAVING <condition>` (which fixes both
case 2 and 3).HAVING clause (#154)1 parent a7fd7c3 commit e88a4e5
2 files changed
Lines changed: 90 additions & 8 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
3133 | 3133 | | |
3134 | 3134 | | |
3135 | 3135 | | |
| 3136 | + | |
| 3137 | + | |
| 3138 | + | |
| 3139 | + | |
| 3140 | + | |
| 3141 | + | |
| 3142 | + | |
| 3143 | + | |
| 3144 | + | |
| 3145 | + | |
| 3146 | + | |
| 3147 | + | |
| 3148 | + | |
| 3149 | + | |
| 3150 | + | |
| 3151 | + | |
| 3152 | + | |
| 3153 | + | |
| 3154 | + | |
| 3155 | + | |
| 3156 | + | |
| 3157 | + | |
| 3158 | + | |
| 3159 | + | |
| 3160 | + | |
| 3161 | + | |
| 3162 | + | |
| 3163 | + | |
| 3164 | + | |
| 3165 | + | |
| 3166 | + | |
| 3167 | + | |
| 3168 | + | |
| 3169 | + | |
| 3170 | + | |
| 3171 | + | |
| 3172 | + | |
| 3173 | + | |
| 3174 | + | |
| 3175 | + | |
| 3176 | + | |
| 3177 | + | |
| 3178 | + | |
| 3179 | + | |
| 3180 | + | |
| 3181 | + | |
| 3182 | + | |
| 3183 | + | |
| 3184 | + | |
| 3185 | + | |
| 3186 | + | |
| 3187 | + | |
| 3188 | + | |
| 3189 | + | |
| 3190 | + | |
| 3191 | + | |
| 3192 | + | |
| 3193 | + | |
| 3194 | + | |
| 3195 | + | |
| 3196 | + | |
| 3197 | + | |
| 3198 | + | |
| 3199 | + | |
| 3200 | + | |
| 3201 | + | |
| 3202 | + | |
| 3203 | + | |
| 3204 | + | |
| 3205 | + | |
| 3206 | + | |
| 3207 | + | |
| 3208 | + | |
| 3209 | + | |
| 3210 | + | |
| 3211 | + | |
| 3212 | + | |
| 3213 | + | |
| 3214 | + | |
3136 | 3215 | | |
3137 | 3216 | | |
3138 | 3217 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
2659 | 2659 | | |
2660 | 2660 | | |
2661 | 2661 | | |
2662 | | - | |
| 2662 | + | |
2663 | 2663 | | |
2664 | 2664 | | |
2665 | 2665 | | |
2666 | 2666 | | |
2667 | | - | |
2668 | | - | |
2669 | | - | |
2670 | | - | |
2671 | 2667 | | |
2672 | 2668 | | |
2673 | 2669 | | |
2674 | 2670 | | |
2675 | 2671 | | |
2676 | 2672 | | |
2677 | 2673 | | |
2678 | | - | |
| 2674 | + | |
2679 | 2675 | | |
2680 | 2676 | | |
2681 | 2677 | | |
| |||
2694 | 2690 | | |
2695 | 2691 | | |
2696 | 2692 | | |
2697 | | - | |
2698 | | - | |
| 2693 | + | |
| 2694 | + | |
| 2695 | + | |
| 2696 | + | |
| 2697 | + | |
| 2698 | + | |
| 2699 | + | |
| 2700 | + | |
| 2701 | + | |
2699 | 2702 | | |
2700 | 2703 | | |
2701 | 2704 | | |
| |||
0 commit comments