set enable_compress_hll = on;

create schema hll_func;
set current_schema = hll_func;

create table test(id int);
create table test1(id int);
insert into test values (generate_series(1,5));
insert into test1 values (generate_series(1,500));

--------------CONTENTS--------------------
-- hyperloglog compare function test cases
------------------------------------------
--1. hll_eq hll_ne
--2. smallint integer bigint
--3. bytea text
--4. hll_hash_any
------------------------------------------

------------------------------------------
-- 1. compare function
------------------------------------------
select hll_eq((hll_empty() || hll_hash_integer(1)), (hll_empty() || hll_hash_integer(1) || hll_hash_integer(2)));
select hll_ne((hll_empty() || hll_hash_integer(1)), (hll_empty() || hll_hash_integer(1) || hll_hash_integer(2)));

-- test hll_eq with four parameters, default paramters are (11, 5, -1, 1)
select hll_eq((hll_empty() || hll_hash_integer(1)), (hll_empty(10,5,-1,1) || hll_hash_integer(1)));
select hll_eq((hll_empty() || hll_hash_integer(1)), (hll_empty(11,5,-1,0) || hll_hash_integer(1)));
select hll_eq((hll_empty() || hll_hash_integer(1)), (hll_empty(11,4,-1,1) || hll_hash_integer(1)));
select hll_eq((hll_empty() || hll_hash_integer(1)), (hll_empty(11,5,0,1) || hll_hash_integer(1)));

-- test hll_ne with four parameter, default paramters are (11, 5, -1, 1)
select hll_ne((hll_empty() || hll_hash_integer(1)), (hll_empty(10,5,-1,1) || hll_hash_integer(1)));
select hll_ne((hll_empty() || hll_hash_integer(1)), (hll_empty(11,5,-1,0) || hll_hash_integer(1)));
select hll_ne((hll_empty() || hll_hash_integer(1)), (hll_empty(11,4,-1,1) || hll_hash_integer(1)));
select hll_ne((hll_empty() || hll_hash_integer(1)), (hll_empty(11,5,0,1) || hll_hash_integer(1)));

select hll_hashval_eq(hll_hash_integer(1), hll_hash_integer(1));
select hll_hashval_ne(hll_hash_integer(1), hll_hash_integer(1));

-- test hll_hashval_eq and hll_hashval_ne with hash seed
select hll_hashval_eq(hll_hash_integer(1), hll_hash_integer(1,123));
select hll_hashval_ne(hll_hash_integer(1), hll_hash_integer(1,123));
select hll_hashval_ne(hll_hash_integer(1), hll_hash_integer(1,0));

------------------------------------------
-- 2. debug function
------------------------------------------
select hll_print(hll_empty());
select hll_print(hll_empty(10,5,-1,1));
select hll_print(hll_empty() || E'\\x108b7f');

select hll_print(hll_union_agg(hll_add_value))
    from (
        select hll_add_agg(hll_hash_integer(id)) hll_add_value from test
        );

select hll_print(hll_union_agg(hll_add_value))
	from (
		select hll_add_agg(hll_hash_integer(id)) hll_add_value from test1
		);


select hll_schema_version(hll_empty());
select hll_regwidth(hll_empty());
select hll_regwidth(hll_empty(11,5));
select hll_expthresh(hll_empty());
select hll_expthresh(hll_empty(11,5,0));
select hll_sparseon(hll_empty());
select hll_sparseon(hll_empty(11,5,-1,0));
select hll_log2m(hll_empty());
select hll_log2m(hll_empty(10));
select hll_type(hll_empty());

------------------------------------------
-- 3. operator function
------------------------------------------

select (hll_empty() || hll_hash_integer(1)) = (hll_empty() || hll_hash_integer(1));
select (hll_empty() || hll_hash_integer(1)) <> (hll_empty() || hll_hash_integer(2));

select (hll_hash_integer(1) || hll_empty()) = (hll_hash_integer(1) || hll_empty());
select hll_cardinality((hll_empty() || hll_hash_integer(1)) || (hll_empty() || hll_hash_integer(2)));
select #((hll_empty() || hll_hash_integer(1)) || (hll_empty() || hll_hash_integer(2)));

select hll_hash_integer(1) = hll_hash_integer(1);
select hll_hash_integer(1) <> hll_hash_integer(1);

-- test with hll parameters, default paramters are (11, 5, -1, 1)
select (hll_empty() || hll_hash_integer(1)) = (hll_empty(10,5,-1,1) || hll_hash_integer(1));
select (hll_empty() || hll_hash_integer(1)) <> (hll_empty(10,5,-1,1) || hll_hash_integer(1));

select hll_empty(11,5,3,1) = hll_empty(11,5,3,1);
select hll_empty(11,5,3,1) = hll_empty(11,5,3,0);
select hll_empty(11,5,3,1) = hll_empty(11,5,7,1);
select hll_empty(11,5,3,1) = hll_empty(11,4,3,1);
select hll_empty(11,5,3,1) = hll_empty(10,5,3,1);

-- ERROR:	hll with different parameters cannot union
select hll_cardinality((hll_empty() || hll_hash_integer(1)) || (hll_empty(10,5,-1,1) || hll_hash_integer(2)));

select hll_hash_integer(1) = hll_hash_integer(1,123);
select hll_hash_integer(1) <> hll_hash_integer(1,123);

------------------------------------------
-- 4. cast function
------------------------------------------

-- Empty Casts ----

SELECT E'\\x118b7f'::hll;

SELECT E'\\x118b7f'::hll(11,5,-1,1);

-- ERROR:  register count does not match: source uses 2048 and dest uses 1024
SELECT E'\\x118b7f'::hll(10,5,-1,1);

-- ERROR:  register width does not match: source uses 5 and dest uses 4
SELECT E'\\x118b7f'::hll(11,4,-1,1);

-- ERROR:  explicit threshold does not match: source uses -1 and dest uses 64
SELECT E'\\x118b7f'::hll(11,5,6,1);

-- ERROR:  sparse enable does not match: source uses 1 and dest uses 0
SELECT E'\\x118b7f'::hll(11,5,-1,0);

-- Explicit Vector Casts ----

SELECT E'\\x128b7f1111111111111111'::hll;

SELECT E'\\x128b7f1111111111111111'::hll(11,5,-1,1);

-- ERROR:  register count does not match: source uses 2048 and dest uses 1024
SELECT E'\\x128b7f1111111111111111'::hll(10,5,-1,1);

-- ERROR:  register width does not match: source uses 5 and dest uses 4
SELECT E'\\x128b7f1111111111111111'::hll(11,4,-1,1);

-- ERROR:  explicit threshold does not match: source uses -1 and dest uses 64
SELECT E'\\x128b7f1111111111111111'::hll(11,5,6,1);

-- ERROR:  sparse enable does not match: source uses 1 and dest uses 0
SELECT E'\\x128b7f1111111111111111'::hll(11,5,-1,0);

-- Sparse Vector Casts ----

SELECT E'\\x138b7f0001'::hll;

SELECT E'\\x138b7f0001'::hll(11,5,-1,1);

-- ERROR:  register count does not match: source uses 2048 and dest uses 1024
SELECT E'\\x138b7f0001'::hll(10,5,-1,1);

-- ERROR:  register width does not match: source uses 5 and dest uses 4
SELECT E'\\x138b7f0001'::hll(11,4,-1,1);

-- ERROR:  explicit threshold does not match: source uses -1 and dest uses 64
SELECT E'\\x138b7f0001'::hll(11,5,6,1);

-- ERROR:  sparse enable does not match: source uses 1 and dest uses 0
SELECT E'\\x138b7f0001'::hll(11,5,-1,0);

-- Compressed Vector Casts ----
SELECT E'\\x148b7f0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000300000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000800000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000010000000000000000000'::hll;

SELECT E'\\x148b7f0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000300000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000800000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000010000000000000000000'::hll(11,5,-1,1);

-- ERROR:  register count does not match: source uses 2048 and dest uses 1024
SELECT E'\\x148b7f0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000300000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000800000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000010000000000000000000'::hll(10,5,-1,1);

-- ERROR:  register width does not match: source uses 5 and dest uses 4
SELECT E'\\x148b7f0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000300000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000800000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000010000000000000000000'::hll(11,4,-1,1);

-- ERROR:  explicit threshold does not match: source uses -1 and dest uses 64
SELECT E'\\x148b7f0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000300000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000800000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000010000000000000000000'::hll(11,5,6,1);

-- ERROR:  sparse enable does not match: source uses 1 and dest uses 0
SELECT E'\\x148b7f0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000300000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000800000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000010000000000000000000'::hll(11,5,-1,0);

-- cleaning up
drop table test;
drop table test1;

--final cleaning up
drop schema hll_func cascade;
reset current_schema;