iCyberon / pg_hashids

Short unique id generator for PostgreSQL, using hashids
MIT License
287 stars 25 forks source link

Wrong result when encoding bigger numbers #14

Closed gpip closed 6 years ago

gpip commented 6 years ago

Hey, I was just trying this extension with Postgres 10 and found something odd:

select id_encode(1981515714898, 'salt1', 6, 'ABCDEFGHJKLMNPRSTVXYZ');
 id_encode  
------------
 XDMNVNJPBP
(1 row)

select id_decode('XDMNVNJPBP', 'salt1', 6, 'ABCDEFGHJKLMNPRSTVXYZ');
  id_decode   
--------------
 {1535791442}
(1 row)

The python hashid module returns 'XXAJGNGVJEBX' (and not 'XDMNVNJPBP'), and if I pass that value to id_decode I get back the expected id 1981515714898

gpip commented 6 years ago

I see the code is using PG_GETARG_INT32 for the id, shouldn't that be at least INT64 instead?

iCyberon commented 6 years ago

@gpip Does this work for you?

select id_encode(ARRAY[1981515714898], 'salt1', 6, 'ABCDEFGHJKLMNPRSTVXYZ');
select id_decode('XXAJGNGVJEBX', 'salt1', 6, 'ABCDEFGHJKLMNPRSTVXYZ');

It should work, I'll fix the issue.

iCyberon commented 6 years ago

@gpip Just pushed v1.2.1, should work with BIGINT. Let me know if you encounter other issues.

gpip commented 6 years ago

Thank you for the fix! Earlier I recompiled it with INT64, haven't tried using ARRAY but looks fine to me