zachasme / h3-pg

PostgreSQL bindings for H3, a hierarchical hexagonal geospatial indexing system
Apache License 2.0
262 stars 34 forks source link

h3_grid_distance causes posgresql SQL to Error out #154

Closed FuryonX closed 3 weeks ago

FuryonX commented 3 weeks ago

I'm trying to find the distance between two h3indexes. For h3indexes that are near each other, the results are correct. For h3indexes that are far enough apart that they aren't comparable, it is supposed to return a negative number. When i test this statement:

select h3_grid_distance('8a2ab4a00a2ffff'::h3index,'8a02cac36cdffff'::h3index) distance

The query fails with this error:

ERROR:  error code: 1
HINT:  https://h3geo.org/docs/library/errors#table-of-error-codes 

SQL state: 38000

I've tested to make sure both h3indexes are valid. This worked properly on h3-pg 3.7.1 using the h3_distance function, however on 4.1.3 its now failing for me.

zachasme commented 3 weeks ago

Hi @FuryonX!

I've been using the error handling introduced upstream in H3 since v4.0. That means whenever I call a C-function from my extension that returns H3Error, that error is thrown as a Postgres error as well.

In case you wish to swallow the above error, and return -1 instead, you can use BEGIN/EXCEPTION like so:

BEGIN
  select h3_grid_distance('8a2ab4a00a2ffff','8a02cac36cdffff');
EXCEPTION WHEN external_routine_exception THEN
  RETURN -1;
END;