-- This file and its contents are licensed under the Apache License 2.0.
-- Please see the included NOTICE for copyright information and
-- LICENSE-APACHE for a copy of the license.
-- Test hashing Const values. We should expect the same hash value for
-- all integer types when values are compatible
SELECT _timescaledb_functions.get_partition_hash(1::int);
 get_partition_hash 
--------------------
          242423622

SELECT _timescaledb_functions.get_partition_hash(1::bigint);
 get_partition_hash 
--------------------
          242423622

SELECT _timescaledb_functions.get_partition_hash(1::smallint);
 get_partition_hash 
--------------------
          242423622

SELECT _timescaledb_functions.get_partition_hash(true);
 get_partition_hash 
--------------------
          242423622

-- Floating point types should also hash the same for compatible values
SELECT _timescaledb_functions.get_partition_hash(1.0::real);
 get_partition_hash 
--------------------
          376496956

SELECT _timescaledb_functions.get_partition_hash(1.0::double precision);
 get_partition_hash 
--------------------
          376496956

-- Float aliases
SELECT _timescaledb_functions.get_partition_hash(1.0::float);
 get_partition_hash 
--------------------
          376496956

SELECT _timescaledb_functions.get_partition_hash(1.0::float4);
 get_partition_hash 
--------------------
          376496956

SELECT _timescaledb_functions.get_partition_hash(1.0::float8);
 get_partition_hash 
--------------------
          376496956

SELECT _timescaledb_functions.get_partition_hash(1.0::numeric);
 get_partition_hash 
--------------------
         1324868424

-- 'name' and '"char"' are internal PostgreSQL types, which are not
-- intended for use by the general user. They are included here only
-- for completeness
-- https://www.postgresql.org/docs/10/static/datatype-character.html#datatype-character-special-table
SELECT _timescaledb_functions.get_partition_hash('c'::name);
 get_partition_hash 
--------------------
         1903644986

SELECT _timescaledb_functions.get_partition_hash('c'::"char");
 get_partition_hash 
--------------------
          203891234

-- String and character hashes should also have the same output for
-- compatible values
SELECT _timescaledb_functions.get_partition_hash('c'::char);
 get_partition_hash 
--------------------
         1903644986

SELECT _timescaledb_functions.get_partition_hash('c'::varchar(2));
 get_partition_hash 
--------------------
         1903644986

SELECT _timescaledb_functions.get_partition_hash('c'::text);
 get_partition_hash 
--------------------
         1903644986

-- 'c' is 0x63 in ASCII
SELECT _timescaledb_functions.get_partition_hash(E'\\x63'::bytea);
 get_partition_hash 
--------------------
         1903644986

-- Time and date types
SELECT _timescaledb_functions.get_partition_hash(interval '1 day');
 get_partition_hash 
--------------------
           93502988

SELECT _timescaledb_functions.get_partition_hash('2017-03-22T09:18:23'::timestamp);
 get_partition_hash 
--------------------
          307315039

SELECT _timescaledb_functions.get_partition_hash('2017-03-22T09:18:23'::timestamptz);
 get_partition_hash 
--------------------
         1195163597

SELECT _timescaledb_functions.get_partition_hash('2017-03-22'::date);
 get_partition_hash 
--------------------
          693590295

SELECT _timescaledb_functions.get_partition_hash('10:00:00'::time);
 get_partition_hash 
--------------------
         1380652790

SELECT _timescaledb_functions.get_partition_hash('10:00:00-1'::timetz);
 get_partition_hash 
--------------------
          769387140

-- Other types
SELECT _timescaledb_functions.get_partition_hash(ARRAY[1,2,3]);
 get_partition_hash 
--------------------
         1822090118

SELECT _timescaledb_functions.get_partition_hash('08002b:010203'::macaddr);
 get_partition_hash 
--------------------
          294987870

SELECT _timescaledb_functions.get_partition_hash('192.168.100.128/25'::cidr);
 get_partition_hash 
--------------------
         1612896565

SELECT _timescaledb_functions.get_partition_hash('192.168.100.128'::inet);
 get_partition_hash 
--------------------
         1952516432

SELECT _timescaledb_functions.get_partition_hash('2001:4f8:3:ba:2e0:81ff:fe22:d1f1'::inet);
 get_partition_hash 
--------------------
          933321588

SELECT _timescaledb_functions.get_partition_hash('2001:4f8:3:ba:2e0:81ff:fe22:d1f1/128'::cidr);
 get_partition_hash 
--------------------
          933321588

SELECT _timescaledb_functions.get_partition_hash('{ "foo": "bar" }'::jsonb);
 get_partition_hash 
--------------------
          208840587

SELECT _timescaledb_functions.get_partition_hash('4b6a5eec-b344-11e7-abc4-cec278b6b50a'::uuid);
 get_partition_hash 
--------------------
          504202548

SELECT _timescaledb_functions.get_partition_hash(1::regclass);
 get_partition_hash 
--------------------
          242423622

SELECT _timescaledb_functions.get_partition_hash(int4range(10, 20));
 get_partition_hash 
--------------------
         1202375768

SELECT _timescaledb_functions.get_partition_hash(int8range(10, 20));
 get_partition_hash 
--------------------
         1202375768

SELECT _timescaledb_functions.get_partition_hash(numrange(10, 20));
 get_partition_hash 
--------------------
         1083987536

SELECT _timescaledb_functions.get_partition_hash(tsrange('2017-03-22T09:18:23', '2017-03-23T09:18:23'));
 get_partition_hash 
--------------------
         2079608838

SELECT _timescaledb_functions.get_partition_hash(tstzrange('2017-03-22T09:18:23+01', '2017-03-23T09:18:23+00'));
 get_partition_hash 
--------------------
         1255083771

-- Test hashing Var values
CREATE TABLE hash_test(id int, value text);
INSERT INTO hash_test VALUES (1, 'test');
-- Test Vars
SELECT _timescaledb_functions.get_partition_hash(id) FROM hash_test;
 get_partition_hash 
--------------------
          242423622

SELECT _timescaledb_functions.get_partition_hash(value) FROM hash_test;
 get_partition_hash 
--------------------
         1771415073

-- Test coerced value
SELECT _timescaledb_functions.get_partition_hash(id::text) FROM hash_test;
 get_partition_hash 
--------------------
         1516350201

-- Test legacy function that converts values to text first
SELECT _timescaledb_functions.get_partition_for_key('4b6a5eec-b344-11e7-abc4-cec278b6b50a'::text);
 get_partition_for_key 
-----------------------
             934882099

SELECT _timescaledb_functions.get_partition_for_key('4b6a5eec-b344-11e7-abc4-cec278b6b50a'::varchar);
 get_partition_for_key 
-----------------------
             934882099

SELECT _timescaledb_functions.get_partition_for_key(187);
 get_partition_for_key 
-----------------------
            1161071810

SELECT _timescaledb_functions.get_partition_for_key(187::bigint);
 get_partition_for_key 
-----------------------
            1161071810

SELECT _timescaledb_functions.get_partition_for_key(187::numeric);
 get_partition_for_key 
-----------------------
            1161071810

SELECT _timescaledb_functions.get_partition_for_key(187::double precision);
 get_partition_for_key 
-----------------------
            1161071810

SELECT _timescaledb_functions.get_partition_for_key(int4range(10, 20));
 get_partition_for_key 
-----------------------
             505239042

SELECT _timescaledb_functions.get_partition_hash('08002b:010203'::macaddr);
 get_partition_hash 
--------------------
          294987870

-- Test inside IMMUTABLE function (Issue #4575)
CREATE FUNCTION my_get_partition_hash(INTEGER) RETURNS INTEGER
AS 'SELECT _timescaledb_functions.get_partition_hash($1);'
LANGUAGE SQL IMMUTABLE;
CREATE FUNCTION my_get_partition_for_key(INTEGER) RETURNS INTEGER
AS 'SELECT _timescaledb_functions.get_partition_for_key($1);'
LANGUAGE SQL IMMUTABLE;
SELECT my_get_partition_hash(1);
 my_get_partition_hash 
-----------------------
             242423622

SELECT my_get_partition_for_key(1);
 my_get_partition_for_key 
--------------------------
               1516350201

