Skip to content

Ranges and indexing #488

Closed
Closed

Description

@aljazerzen

#471
#420 (comment)

We need to decide on:

  • 0-based vs 1-based index
  • inclusive/exclusive range bounds

SQL uses (very implicitly):

  • 0-based index (first row is represented with OFFSET 0 and CURRENT ROW in window functions is 0 FOLLOWING and 0 PRECEDING)
  • start <= i <= end ranges (OVER (BETWEEN 2 FOLLOWING AND 4 FOLLOWING))

Rust uses:

  • 0-based
  • start <= i < end
for i in 2..5 {
    print!("{i} ");
}
2 3 4

Python uses:

  • 0-based
  • start <= i < end
>>> [i for i in range(2, 5)]
[2, 3, 4]
>>> list(range(100))[2:5]
[2, 3, 4]

Activity

  1. aljazerzen commented on May 15, 2022

    @aljazerzen
    MemberAuthor

    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)

  2. max-sixty commented on May 16, 2022

    @max-sixty
    Member

    I think we have three questions here:

    1. Which is better, 0-indexing or 1-indexing? (or for jokes 2-based indexing)
    2. What does SQL do now, and how strongly?
    3. 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.

    • 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.

  3. added
    language-design
    on May 16, 2022
  4. max-sixty commented on May 16, 2022

    @max-sixty
    Member

    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.

    WDYT?

  5. aljazerzen commented on May 16, 2022

    @aljazerzen
    MemberAuthor

    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.

  6. max-sixty commented on May 16, 2022

    @max-sixty
    Member

    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?

  7. aljazerzen commented on May 16, 2022

    @aljazerzen
    MemberAuthor

    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.

  8. aljazerzen commented on May 16, 2022

    @aljazerzen
    MemberAuthor

    For future reference:
    PostgreSQL has ranges, but not for window functions.

    Rust has start..=end for inclusive ranges.

  9. max-sixty commented on May 16, 2022

    @max-sixty
    Member

    OK, thanks for your openness and compromise!

  10. max-sixty commented on May 16, 2022

    @max-sixty
    Member

    Closed by #501

  11. Felk commented on Jul 12, 2025

    @Felk

    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>).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    language-design

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions