An incompatible generate_series change
(1) By Bo Lindbergh (_blgl_) on 2025-03-22 19:56:03 [source]
Before:
SQLite version 3.46.1 2024-08-13 09:16:08 Enter ".help" for usage hints. sqlite> .mode table sqlite> select * from generate_series(1,10) where value<5.5; +-------+ | value | +-------+ | 1 | | 2 | | 3 | | 4 | | 5 | +-------+
After:
SQLite version 3.47.0 2024-10-21 16:30:22 Enter ".help" for usage hints. sqlite> .mode table sqlite> select * from generate_series(1,10) where value<5.5; +-------+ | value | +-------+ | 1 | | 2 | | 3 | | 4 | +-------+
I call the old behaviour more correct.
(2) By Jim G (exumbris) on 2025-03-22 20:21:10 in reply to 1 [link] [source]
Interesting... two possibilities:
- The type conversion rules appear to have changed and the expression now seems to have INTEGER affinity, or
- Type conversion rules haven't changed but the affinity of
generate_series()
asINTEGER
is now being enforced where it wasn't previously
sqlite> select * from generate_series(1,10) where cast(value as real)<5.5;
┌───────┐
│ value │
├───────┤
│ 1 │
│ 2 │
│ 3 │
│ 4 │
│ 5 │
└───────┘
The documentation isn't explicit about the cases of INTEGER op REAL
(and vice versa). It discusses TEXT/BLOB conversion to numeric, but
Otherwise, no affinity is applied and both operands are compared as is.
It's not at all clear what is meant by compared as is.
(3) By Richard Hipp (drh) on 2025-03-22 20:32:56 in reply to 2 [link] [source]
Neither of those. Check-in 2024-08-22T18:12Z attempted to enhance the generate_series virtual table to take advantage of "value<NUM" constraints. But when I implemented that, it never occurred to me that NUM might be a floating point value. After all, generate_series deals with integer. So I used "sqlite3_value_int64()" to extract the value of NUM, which rounds it down from 5.5 to 5, and because the comparison is less-than, not less-than-or-equal-to, it therefore omits the final number.
(5) By Bo Lindbergh (_blgl_) on 2025-03-22 20:59:13 in reply to 3 [link] [source]
It's very easy to hide the constraint from generate_series
.
select * from generate_series(1,10) where not value>=5.5;
or even
select * from generate_series(1,10) where +value<5.5;
(4) By Richard Hipp (drh) on 2025-03-22 20:48:11 in reply to 1 [link] [source]
A related question: What should be the correct output of
SELECT value FROM generate_series(11.5,14.25);
Possible answers:
- 11, 12, 13, 14
- 12, 13, 14
- Raise an error complaining that the arguments must be integers.
The current implementation does option 1. Should I change it?
(6) By Bo Lindbergh (_blgl_) on 2025-03-22 21:31:41 in reply to 4 [link] [source]
Showing the current mismatch between the constraints and the returned values more clearly:
sqlite> SELECT value, start, stop FROM generate_series WHERE start=11.5 AND stop=14.25; +-------+-------+------+ | value | start | stop | +-------+-------+------+ | 11 | 11 | 14 | | 12 | 11 | 14 | | 13 | 11 | 14 | | 14 | 11 | 14 | +-------+-------+------+
My opinion: either option 3, or option 2 but returning the original values instead of the cast-to-integer ones in the start
, stop
, and step
columns.
(7) By Spindrift (spindrift) on 2025-03-22 21:58:19 in reply to 4 [link] [source]
I think 2) is wrong. Worst of both worlds.
I would be keen on 3) actually, as the issue is then explicit and less likely to lead to poor assumptions which underlie irritating bugs in the future.
- at least works consistently with integer parsing.
I believe the author of sqlite has posted his regret about following Postel's law before.
(8) By anonymous on 2025-03-23 17:06:12 in reply to 4 [link] [source]
Consider an optional parameter that selects the behavior.
(9) By SeverKetor on 2025-03-23 17:51:04 in reply to 4 [link] [source]
This could get me stuck in the psych ward for a few months, but what about
SELECT value FROM generate_series(11.5, 14.25);
11.5
12.5
13.5
(10) By Bo Lindbergh (_blgl_) on 2025-03-23 18:12:41 in reply to 9 [link] [source]
That would generate too many bug reports about missing the stop value (because of rounding errors).
(11) By SeverKetor on 2025-03-23 18:17:21 in reply to 10 [link] [source]
Yeah, makes sense. Oh well, worth putting put there I guess