CREATE TABLE test (a int);

-- smooth with ~10000 values
TRUNCATE test;
INSERT INTO test SELECT random() * 10000 FROM generate_series(1,10000000);
ANALYZE test;
SELECT COUNT(DISTINCT a) FROM test;

TRUNCATE test;
INSERT INTO test SELECT pow(random(),2) * 10000 FROM generate_series(1,10000000);
ANALYZE test;
SELECT COUNT(DISTINCT a) FROM test;

TRUNCATE test;
INSERT INTO test SELECT pow(random(),3) * 10000 FROM generate_series(1,10000000);
ANALYZE test;
SELECT COUNT(DISTINCT a) FROM test;

TRUNCATE test;
INSERT INTO test SELECT pow(random(),4) * 10000 FROM generate_series(1,10000000);
ANALYZE test;
SELECT COUNT(DISTINCT a) FROM test;

TRUNCATE test;
INSERT INTO test SELECT pow(random(),5) * 10000 FROM generate_series(1,10000000);
ANALYZE test;
SELECT COUNT(DISTINCT a) FROM test;

TRUNCATE test;
INSERT INTO test SELECT pow(random(),6) * 10000 FROM generate_series(1,10000000);
ANALYZE test;
SELECT COUNT(DISTINCT a) FROM test;

TRUNCATE test;
INSERT INTO test SELECT pow(random(),7) * 10000 FROM generate_series(1,10000000);
ANALYZE test;
SELECT COUNT(DISTINCT a) FROM test;

TRUNCATE test;
INSERT INTO test SELECT pow(random(),8) * 10000 FROM generate_series(1,10000000);
ANALYZE test;
SELECT COUNT(DISTINCT a) FROM test;

TRUNCATE test;
INSERT INTO test SELECT pow(random(),9) * 10000 FROM generate_series(1,10000000);
ANALYZE test;
SELECT COUNT(DISTINCT a) FROM test;

-- smooth with ~100 values
TRUNCATE test;
INSERT INTO test SELECT random() * 100 FROM generate_series(1,10000000);
ANALYZE test;
SELECT COUNT(DISTINCT a) FROM test;

TRUNCATE test;
INSERT INTO test SELECT pow(random(),2) * 100 FROM generate_series(1,10000000);
ANALYZE test;
SELECT COUNT(DISTINCT a) FROM test;

TRUNCATE test;
INSERT INTO test SELECT pow(random(),3) * 100 FROM generate_series(1,10000000);
ANALYZE test;
SELECT COUNT(DISTINCT a) FROM test;

TRUNCATE test;
INSERT INTO test SELECT pow(random(),4) * 100 FROM generate_series(1,10000000);
ANALYZE test;
SELECT COUNT(DISTINCT a) FROM test;

TRUNCATE test;
INSERT INTO test SELECT pow(random(),5) * 100 FROM generate_series(1,10000000);
ANALYZE test;
SELECT COUNT(DISTINCT a) FROM test;

TRUNCATE test;
INSERT INTO test SELECT pow(random(),6) * 100 FROM generate_series(1,10000000);
ANALYZE test;
SELECT COUNT(DISTINCT a) FROM test;

TRUNCATE test;
INSERT INTO test SELECT pow(random(),7) * 100 FROM generate_series(1,10000000);
ANALYZE test;
SELECT COUNT(DISTINCT a) FROM test;

TRUNCATE test;
INSERT INTO test SELECT pow(random(),8) * 100 FROM generate_series(1,10000000);
ANALYZE test;
SELECT COUNT(DISTINCT a) FROM test;

TRUNCATE test;
INSERT INTO test SELECT pow(random(),9) * 100 FROM generate_series(1,10000000);
ANALYZE test;
SELECT COUNT(DISTINCT a) FROM test;

-- step with 5 frequent values, ~100 less frequent values
TRUNCATE test;
INSERT INTO test SELECT (CASE WHEN (v < 900) THEN MOD(v,5) ELSE v END) FROM (SELECT (random()*1000)::int AS v FROM generate_series(1,10000000)) foo;
ANALYZE test;
SELECT COUNT(DISTINCT a) FROM test;

-- step with 5 frequent values, ~100 rare values
TRUNCATE test;
INSERT INTO test SELECT (CASE WHEN (v < 99900) THEN MOD(v,5) ELSE v END) FROM (SELECT (random()*100000)::int AS v FROM generate_series(1,10000000)) foo;
ANALYZE test;
SELECT COUNT(DISTINCT a) FROM test;

-- step with 5 frequent values, ~1000 rare values
TRUNCATE test;
INSERT INTO test SELECT (CASE WHEN (v < 99000) THEN MOD(v,5) ELSE v END) FROM (SELECT (random()*100000)::int AS v FROM generate_series(1,10000000)) foo;
ANALYZE test;
SELECT COUNT(DISTINCT a) FROM test;

-- step with 5 frequent values, ~10000 rare values
TRUNCATE test;
INSERT INTO test SELECT (CASE WHEN (v < 90000) THEN MOD(v,5) ELSE v END) FROM (SELECT (random()*100000)::int AS v FROM generate_series(1,10000000)) foo;
ANALYZE test;
SELECT COUNT(DISTINCT a) FROM test;

