vitaly-m / postgis-diesel

Postgis types extension for Diesel framework
MIT License
37 stars 15 forks source link

Using st_d_within with Nullable<Geography> #21

Closed GioF closed 11 months ago

GioF commented 11 months ago
error[E0277]: the trait bound `diesel::sql_types::Nullable<postgis_diesel::sql_types::Geography>: GeoType` is not satisfied
   --> src/models/hardware.rs:113:42
    |
113 |                     query = query.filter(st_d_within(location, second_point, radius));
    |                                          ^^^^^^^^^^^ the trait `GeoType` is not implemented for `diesel::sql_types::Nullable<postgis_diesel::sql_types::Geography>`
    |
    = help: the following other types implement trait `GeoType`:
              Geometry
              postgis_diesel::sql_types::Geography
note: required by a bound in `st_d_within`
   --> /home/gio/.cargo/registry/src/github.com-1ecc6299db9ec823/postgis_diesel-2.2.1/src/functions.rs:99:23
    |
99  |     fn st_d_within<G: GeoType>(left: G, right: G, distance: Double) -> Bool;
    |                       ^^^^^^^ required by this bound in `st_d_within`

The error above appears when attempting to call st_d_within with a column of type Nullable.

vitaly-m commented 11 months ago

Hello @GioF, my understanding is that functions like that are working properly only in case parameters are not null, otherwise, in case any geometry is NULL it will return NULL, which is kind of unexpected anyway and comparison with FALSE anyway will be failed.

Here is an example of requests and results:

select ST_DWithin(track, track, 0) from laps;

result:

st_dwithin|
----------+
false     |
true      |
select ST_DWithin(track, null, 0) from laps;

result with nulls:

st_dwithin|
----------+
          |
          |

And another example with result = 0:

select count(*) from laps where ST_DWithin(track, null, 0);

Maybe it is possible to check the null value on code level and pass only not null geometries? But also maybe it should be supported like it is supported on PG side, with nullable parameters and nullable result, I will think about how to do that, or you can create a PR for that.

vitaly-m commented 11 months ago

Functions which support nullable parameters are implemented in separate module functions_nullable, this is done because return type should have also being changed and it could lead to backward incompatible changes. Example of usage in d_within_test

Implemented in v2.3.0.

GioF commented 11 months ago

Sadly i couldn't take a look on how to implement this because i was busy with personal matters, but i will look into the code to learn a bit. Either way, thanks!

rj76 commented 11 months ago

This is great! Also a big thank you from me.