You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
My (quite strong) opinion is to use the same convention as Rust and Python. This is mainly due to argument by Dijkstra.
I know that this notation is not the most average-user-friendly, but after you see it for the first time, it becomes much more intuitive than alternatives.
So for example:
first ten rows: take 10 or take ..10 or take 0..10
next ten rows: take 10..20
I also has the advantage of:
containing end - start elements in the range (-3..18 contains 18+3=21 elements)
having empty ranges denoted as x..x (11..11 is empty)
(PS: I'm sorry for biased examples, I didn't wont to include Matlab)
Which is better, 0-indexing or 1-indexing? (or for jokes 2-based indexing)
What does SQL do now, and how strongly?
Can we / should we deviate?
The first is probably not going to be fully resolved here — though I do think there are real tradeoffs.
I agree with you on the aesthetic elegance of the Dijkstra's argument. But OTOH I think for most normal people, using 1..10 is much more intuitive than 0..10 — I think if you ask the average Excel user, they're going to think 0-indexing is not how they count.
I've heard this described as "Pointer arithmetic vs. Counting", which resonates to me.
As you point out, 1-indexing arguably doesn't compose as well — x..y & y..z aren't mutually exclusive. But arguably data transformation is more frequently counting (LIMIT 10) than composing array indexes.
FWIW re the list above, Julia & Fortran are 1-indexed, though this is more related to how matrixes are traditionally indexed, rather than how people count things.
Specifically with window indexing, inclusive-exclusive indexing — a property of 0-indexing — doesn't reverse well. So rows:0..2 is current & next, but rows:-2:0 is previous & its previous. Or rows:..1 goes to the current row, but rows:-1.. goes from the previous row onwards! This does seem very confusing!
For the second, it seems that most SQL is 1-indexed, but that generally there isn't much indexing.
Substring seems to always use 1-indexing (here's one)
BETWEEN is always inclusive, which is a property of 1-indexing.
0-based index (first row is represented with OFFSET 0 and CURRENT ROW in window functions is 0 FOLLOWING and 0 PRECEDING)
I didn't quite see it this way — particularly in the case of window functions — these seem like deltas / "shifts" rather than 0-indexing. What would be the equivalent for 1-indexing for window functions? Possibly I'm not thinking about it conceptually enough.
For the third, I'm not sure to what extent we can intercept everything and provide an 0-based interface. Would we intercept substr functions?
One fortunate parallel is that take ..10 means the same with a) inclusive-exclusive 0-based indexing and b) inclusive-inclusive 1-based indexing.
Specifically with window indexing, inclusive-exclusive indexing — a property of 0-indexing — doesn't reverse well. So rows:0..2 is current & next, but rows:-2:0 is previous & its previous. Or rows:..1 goes to the current row, but rows:-1.. goes from the previous row onwards! This does seem very confusing!
Coming back to this later in the day — I'm fine with either approach for most things, but for the windows, I think it's quite difficult to understand anything that isn't symmetrical for things before and after the current row.
Is there a way of having 0-indexing while having it symmetrical? Symmetry is going to require inclusive-inclusive or exclusive-exclusive indexes... And range is inclusive-inclusive and isn't possible for us to change.
That's true: for window frames it would be very convenient to have symmetric ranges.
And range is inclusive-inclusive and isn't possible for us to change.
I'm not sure what you mean? If we wanted to, we could define ranges in PRQL to be exclusive-exclusive and when compiling, change that to inclusive-inclusive. It is a bit more complicated but not impossible.
Would we intercept substr functions?
If we wanted to have 0-indexed inclusive-exclusive, we could, yes.
To be honest, I'm a bit undecided on this issue right now. Will revisit in a week.
I'm not sure what you mean? If we wanted to, we could define ranges in PRQL to be exclusive-exclusive and when compiling, change that to inclusive-inclusive. It is a bit more complicated but not impossible.
How would we change it when it's using the underlying values though? e.g. RANGE BETWEEN INTERVAL '3' DAY PRECEDING AND INTERVAL '1' DAY FOLLOWING — I guess we could try and find the largest value that's less than INTERVAL '1' DAY — it would get difficult though, no?
Oh, RANGE accepts expressions other than integers... of course.
Yeah, in this case we really cannot transpile inclusive bound into exclusive, because in SQL, exclusive bound cannot be expressed for reals or dates :D
This means that we cannot have exclusive bounds, which also excludes us from having 0-based index, because 0..9 is strange and also does not have the nice properties I talked about.
So at least until we figure out exclusive bound in SQL, indexes and ranges are 1-based and inclusive-inclusive.
Sorry for digging up an old discussion, but just in case you ever want to revisit the [closed-closed] vs [closed, open) discussion: fwiw, the temporal part of SQL:2011 uses the latter for PERIOD, and it also introduced new syntax FROM <start-inclusive> TO <end-exclusive> (as opposed to BETWEEN <start-inclusive> AND <end-exclusive>).
Activity
aljazerzen commentedon May 15, 2022
My (quite strong) opinion is to use the same convention as Rust and Python. This is mainly due to argument by Dijkstra.
I know that this notation is not the most average-user-friendly, but after you see it for the first time, it becomes much more intuitive than alternatives.
So for example:
take 10ortake ..10ortake 0..10take 10..20I also has the advantage of:
end - startelements in the range (-3..18contains 18+3=21 elements)x..x(11..11is empty)(PS: I'm sorry for biased examples, I didn't wont to include Matlab)
max-sixty commentedon May 16, 2022
I think we have three questions here:
The first is probably not going to be fully resolved here — though I do think there are real tradeoffs.
I agree with you on the aesthetic elegance of the Dijkstra's argument. But OTOH I think for most normal people, using
1..10is much more intuitive than0..10— I think if you ask the average Excel user, they're going to think 0-indexing is not how they count.I've heard this described as "Pointer arithmetic vs. Counting", which resonates to me.
As you point out, 1-indexing arguably doesn't compose as well —
x..y&y..zaren't mutually exclusive. But arguably data transformation is more frequently counting (LIMIT 10) than composing array indexes.FWIW re the list above, Julia & Fortran are 1-indexed, though this is more related to how matrixes are traditionally indexed, rather than how people count things.
Specifically with window indexing, inclusive-exclusive indexing — a property of 0-indexing — doesn't reverse well. So
rows:0..2is current & next, butrows:-2:0is previous & its previous. Orrows:..1goes to the current row, butrows:-1..goes from the previous row onwards! This does seem very confusing!For the second, it seems that most SQL is 1-indexed, but that generally there isn't much indexing.
BETWEENis always inclusive, which is a property of 1-indexing.RANGEis always inclusiveOFFSETandORDINAL.I didn't quite see it this way — particularly in the case of window functions — these seem like deltas / "shifts" rather than 0-indexing. What would be the equivalent for 1-indexing for window functions? Possibly I'm not thinking about it conceptually enough.
For the third, I'm not sure to what extent we can intercept everything and provide an 0-based interface. Would we intercept
substrfunctions?One fortunate parallel is that
take ..10means the same with a) inclusive-exclusive 0-based indexing and b) inclusive-inclusive 1-based indexing.max-sixty commentedon May 16, 2022
Coming back to this later in the day — I'm fine with either approach for most things, but for the windows, I think it's quite difficult to understand anything that isn't symmetrical for things before and after the current row.
Is there a way of having 0-indexing while having it symmetrical? Symmetry is going to require inclusive-inclusive or exclusive-exclusive indexes... And
rangeis inclusive-inclusive and isn't possible for us to change.WDYT?
aljazerzen commentedon May 16, 2022
That's true: for window frames it would be very convenient to have symmetric ranges.
I'm not sure what you mean? If we wanted to, we could define ranges in PRQL to be exclusive-exclusive and when compiling, change that to inclusive-inclusive. It is a bit more complicated but not impossible.
If we wanted to have 0-indexed inclusive-exclusive, we could, yes.
To be honest, I'm a bit undecided on this issue right now. Will revisit in a week.
max-sixty commentedon May 16, 2022
How would we change it when it's using the underlying values though? e.g.
RANGE BETWEEN INTERVAL '3' DAY PRECEDING AND INTERVAL '1' DAY FOLLOWING— I guess we could try and find the largest value that's less thanINTERVAL '1' DAY— it would get difficult though, no?aljazerzen commentedon May 16, 2022
Oh,
RANGEaccepts expressions other than integers... of course.Yeah, in this case we really cannot transpile inclusive bound into exclusive, because in SQL, exclusive bound cannot be expressed for reals or dates :D
This means that we cannot have exclusive bounds, which also excludes us from having 0-based index, because 0..9 is strange and also does not have the nice properties I talked about.
So at least until we figure out exclusive bound in SQL, indexes and ranges are 1-based and inclusive-inclusive.
aljazerzen commentedon May 16, 2022
For future reference:
PostgreSQL has ranges, but not for window functions.
Rust has
start..=endfor inclusive ranges.max-sixty commentedon May 16, 2022
OK, thanks for your openness and compromise!
max-sixty commentedon May 16, 2022
Closed by #501
Felk commentedon Jul 12, 2025
Sorry for digging up an old discussion, but just in case you ever want to revisit the
[closed-closed]vs[closed, open)discussion: fwiw, the temporal part of SQL:2011 uses the latter forPERIOD, and it also introduced new syntaxFROM <start-inclusive> TO <end-exclusive>(as opposed toBETWEEN <start-inclusive> AND <end-exclusive>).