will / crystal-pg

a postgres driver for crystal
BSD 3-Clause "New" or "Revised" License
462 stars 77 forks source link

add support for ranges #213

Open pascalbetz opened 3 years ago

pascalbetz commented 3 years ago

Implements Ranges (https://github.com/will/crystal-pg/issues/164)

This is as far as i can get with my limited Crystal knowhow...so i'll need some help to finish it.

will commented 3 years ago

Thanks for putting this together. I'm not available for a couple of days, but I'll take a look at this next week and give feedback then :)

pascalbetz commented 3 years ago

Begin/Endless Ranges

Postgres supports "infinite" values for lower/upper boundary. This corresponds to begin/endless Ranges in Crystal. E.g.: [, 50) => nil..50 This makes the Range that gets created Range(T | Nil, T | Nil) Is this OK?

Another possibility would be to use the Datatypes MIN/MAX value (e.g. Int64::MIN) and make sure the boundary is inclusive: E.g.: [, 50) => Int32::MIN..50 so we would not create begin/endless Ranges. (This would not map what is in Postgres though, so I don't like it)

Empty Ranges

Postgres empty ranges are currently implemented as exclusive Ranges with the same value for lower and upper boundary. E.g. 0...0

The values I picked to build these empty ranges are arbitrary though and this does not map exactly what is stored in Postgres.

There is no way to differentiate between

(,)::int4range and [0,0]::int4range because both values end up in Range.new(0, 0).

What do you think?

pascalbetz commented 3 years ago

@will I plan on fixing the build and adding multiranges (Postgres 14) support if this can get merged. Let me know what you think.