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
Add PGRange class. #15098
Add PGRange class. #15098
Conversation
@jefflai2 did you prototype the integration into AR? Would be nice to see the required changes to get it working full-stack. |
Do we want to make the use of PGRange transparent, i.e. do we want this to be completely backward compatible or do we want to make the use of PGRange explicit. OID::Range.new('Integer').type_cast('[1,5]') or OID::Range.new('Integer').type_cast('[1,5]').to_range I've sophisticated the
|
@jefflai2 We either break backwards compatibility directly or introduce a switch combined with a deprecation warning. The idea is that your models hold a |
Updated, modifying the range_test.rb. Passes |
assert_equal 1...11, @first_range.int4_range | ||
assert_equal 1...10, @second_range.int4_range | ||
assert_equal 1...Float::INFINITY, @third_range.int4_range | ||
assert_equal(-Float::INFINITY...Float::INFINITY, @fourth_range.int4_range) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In addition, can we keep the old assertions and verify them agains a converted PGRange -> Range
? This should help us to see where we changed behavior.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would apply for the following tests as well.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do you mean this would be preferrable? assert_equal 1...11, @first_range.int4_range.to_range
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@jefflai2 at least for the cases where it's different.
Updated, and put PGRange into |
def infinity?(value) | ||
value.respond_to?(:infinite?) && value.infinite? | ||
end | ||
|
||
def type_cast_single(value) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This method isn't used outside of this class. Do you think we can just remove it instead?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Removed.
@jefflai2 Can you push the updated code so I can re-review? |
end | ||
end | ||
|
||
delegate :each, to: :to_range |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we move these to the top of the class below the attr readers, and merge them? delegate :bsearch, :each, to: :to_range
Just pushed, sorry I was just commenting to make sure I hit everything. I'm thinking about the change to |
end | ||
|
||
# Different from first; we can't depend on #succ to hack around an unbounded start or end. | ||
delegate :last, to: :to_range |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Merge above.
A few more minor comments, but this looks good to me. Would like to see it merged so I can do a few refactorings based off of it. |
# def test_update_int8range | ||
# assert_equal_round_trip(@first_range, :int8_range, pgrange(60000, 10000000, :integer, true)) | ||
# assert_nil_round_trip(@first_range, :int8_range, pgrange(39999, 39999, :integer, true)) | ||
# end |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
is everything commented out by intention?
Unlike Ruby Ranges, the new `PGRange` can support exclusive beginnings and unbounded beginnings/ends. `PGRange` supports the most common operations and provides a method to attempt to convert to a Ruby Range. There are issues with some methods because it does not make sense to enumerate unbounded ranges.
Updated, but I still have a question/concern: It appears that inclusive ranges, e.g. It also appears that this was expected/accepted behavior before: I don't know if/why this should happen and I can't find out where this change is happening. @senny do you know? |
In an integer range, I haven't read through this closely enough to count as a review, but I will mention: please avoid 'rng' -- that has a well-defined meaning, which is not "range". |
To be clear, the canonicalization is happening inside the database, and isn't something we could have any control over even if we wanted to:
(see http://www.postgresql.org/docs/9.2/static/rangetypes.html#RANGETYPES-DISCRETE) |
It is important that we maintain |
Does anyone have additional feedback on this? Would like to get it merged so I can work on some of the refactorings that this enables. |
what about the comparisons of the transformations that pg performs?— On Mon, Jun 2, 2014 at 1:36 AM, Sean Griffin notifications@github.com
|
Sorry, had work and was working on resolving issue on another PR. :( Made changes to allow comparisons on transformed ranges. But there are a couple major caveats:
In light of all this, I think it's best to not try to be robust to these transformations, but rather, just document this issue and perhaps provide a warning to the user. As it is now, |
Any comments on this? |
@jefflai2 The code this affects has changed drastically. Can you rebase onto master, so we can review the code in the context of the more recent structure? |
I may have overstated how drastic it might be, but could you rebase regardless? |
Hmm, after revisiting this, it looks like there have been several changes to the range class that address several issues originally mentioned in since the last update to this PR. If the approach originally proposed by @senny in #14010 is still something we want to move forward with (i.e. introduce a PGRange class), I'd like to close this and reopen a new PR based off the new master. |
Since there's support for PostgreSQL ranges (since 2014) this PR can be closed @rafaelfranca |
@yawboakye From what I can tell it looks like that support doesn't include a |
@axelson I'll take a look today |
This pull request has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. |
Addresses #14010.
Adding a new class
PGRange
that PostgreSQL Ranges will map to instead of Ruby Ranges. The new PGRange class addresses the shortcomings when attempting to directly translate the Postgres Range to Ruby Range. Namely, Ruby ranges do not support unbound beginnings and ends, nor do they support exclusive beginnings.