Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support unbounded ranges #184

Merged
merged 10 commits into from
Jun 24, 2020
Merged

Support unbounded ranges #184

merged 10 commits into from
Jun 24, 2020

Conversation

nicoleepp
Copy link
Collaborator

WIP because this is waiting on invenia/Intervals.jl#104

src/parsing.jl Outdated Show resolved Hide resolved
src/results.jl Outdated Show resolved Hide resolved
src/results.jl Outdated Show resolved Hide resolved
src/results.jl Outdated Show resolved Hide resolved
src/parsing.jl Outdated Show resolved Hide resolved
src/parsing.jl Outdated Show resolved Hide resolved
src/parsing.jl Outdated Show resolved Hide resolved
src/results.jl Outdated Show resolved Hide resolved
test/runtests.jl Outdated Show resolved Hide resolved
test/runtests.jl Show resolved Hide resolved
test/runtests.jl Outdated Show resolved Hide resolved
test/runtests.jl Outdated
("'[3,]'::int4range", Interval(Int32(3), nothing)),
("'(3,)'::int4range", Interval{Closed, Unbounded}(Int32(4), nothing)),
("'(,3)'::int4range", Interval{Unbounded, Open}(nothing, Int32(3))),
("'(,3]'::int4range", Interval{Unbounded, Open}(nothing, Int32(4))),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Another invalid test:

Suggested change
("'(,3]'::int4range", Interval{Unbounded, Open}(nothing, Int32(4))),
("'(,3]'::int4range", Interval{Unbounded, Closed}(nothing, 3)),

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is how postgres stores that in the db. Changing it to your suggestion doesn't work:

(test_str, data) = ("'(,3]'::int4range", Interval{Int32,Unbounded,Closed}(nothing, 3)): Test Failed at /Users/nicoleepp/.julia/dev/LibPQ/test/runtests.jl:1191
  Expression: isequal(parsed, data)
   Evaluated: isequal(Interval{Int32,Unbounded,Open}(nothing, 4), Interval{Int32,Unbounded,Closed}(nothing, 3))

Copy link
Contributor

@omus omus Jun 23, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Very interesting. This seems to only apply to the upper endpoint.

omus=# SELECT '(,3]'::int4range;
 int4range
-----------
 (,4)
(1 row)

I would suggest at least adding a comment about this as it is rather strange. I'll see if I can find out more about this.

Suggested change
("'(,3]'::int4range", Interval{Unbounded, Open}(nothing, Int32(4))),
("'(,3]'::int4range", Interval{Unbounded, Open}(nothing, Int32(4))), # Postgres normalizes the range `(,3]` to `(,4)`

Copy link
Contributor

@omus omus Jun 23, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I didn't find anything official but I believe what is going on here is that PostgreSQL is normalizing the range (see the last example in https://www.postgresql.org/docs/9.3/rangetypes.html#RANGETYPES-IO).

omus=# SELECT '[4,4)'::numrange;  -- includes no points (and will be normalized to 'empty')
 numrange
----------
 empty

Since we're using int4range here PostgreSQL can change the interval as it knows the possible values that are included in the interval. We can see this in action by making an interval set that just contains 4:

omus=# SELECT '[4,4]'::int4range;
 int4range
-----------
 [4,5)
(1 row)

omus=# SELECT '(3,4]'::int4range;
 int4range
-----------
 [4,5)
(1 row)

omus=# SELECT '[4,5)'::int4range;
 int4range
-----------
 [4,5)
(1 row)

omus=# SELECT '(3,5)'::int4range;
 int4range
-----------
 [4,5)
(1 row)

Finally, for ranges which the sets are not so clearly defined no normalization should occur which is the case:

omus=# SELECT '[4.0,4.0]'::numrange;
 numrange
-----------
 [4.0,4.0]
(1 row)

test/runtests.jl Outdated Show resolved Hide resolved
test/runtests.jl Outdated Show resolved Hide resolved
src/parsing.jl Outdated Show resolved Hide resolved
test/runtests.jl Outdated Show resolved Hide resolved
test/runtests.jl Outdated Show resolved Hide resolved
@nicoleepp nicoleepp changed the title WIP: Support unbounded ranges Support unbounded ranges Jun 24, 2020
@nicoleepp
Copy link
Collaborator Author

Documentation job is running into JuliaPackaging/Yggdrasil#1203

@nicoleepp nicoleepp closed this Jun 24, 2020
@nicoleepp nicoleepp reopened this Jun 24, 2020
@omus omus merged commit f609823 into master Jun 24, 2020
@omus omus deleted the ne/unbounded-ranges branch June 24, 2020 20:26
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants