Skip to content

Commit

Permalink
Fix compression_hypertable ordering reliance
Browse files Browse the repository at this point in the history
The hypertable_compression test had on implicit reliance on the
ordering of tuples when querying the materialized results.
This patch makes the ordering explicit in this test.
  • Loading branch information
svenklemm committed Feb 7, 2023
1 parent 4cb76bc commit e47d6cb
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 42 deletions.
32 changes: 16 additions & 16 deletions tsl/test/expected/compression_hypertable.out
Original file line number Diff line number Diff line change
Expand Up @@ -71,11 +71,9 @@ NOTICE: adding not-null constraint to column "Time"

INSERT INTO test1 SELECT t, gen_rand_minstd(), gen_rand_minstd(), gen_rand_minstd()::text FROM generate_series('2018-03-02 1:00'::TIMESTAMPTZ, '2018-03-28 1:00', '1 hour') t;
ALTER TABLE test1 set (timescaledb.compress, timescaledb.compress_segmentby = '', timescaledb.compress_orderby = '"Time" DESC');
SELECT
$$
SELECT * FROM test1 ORDER BY "Time"
$$ AS "QUERY" \gset
SELECT 'test1' AS "HYPERTABLE_NAME" \gset
\set QUERY 'SELECT * FROM test1'
\set QUERY_ORDER 'ORDER BY "Time"'
\set HYPERTABLE_NAME 'test1'
\ir include/compression_test_hypertable.sql
-- This file and its contents are licensed under the Timescale License.
-- Please see the included NOTICE for copyright information and
Expand Down Expand Up @@ -206,11 +204,9 @@ ALTER TABLE test2 ADD COLUMN d INT;
--write to both old chunks and new chunks with different column #s
INSERT INTO test2 SELECT t, gen_rand_minstd(), gen_rand_minstd()::text, gen_rand_minstd(), gen_rand_minstd() FROM generate_series('2018-03-02 1:00'::TIMESTAMPTZ, '2018-03-06 1:00', '1 hour') t;
ALTER TABLE test2 set (timescaledb.compress, timescaledb.compress_segmentby = '', timescaledb.compress_orderby = 'c, "Time" DESC');
SELECT
$$
SELECT * FROM test2 ORDER BY c, "Time"
$$ AS "QUERY" \gset
SELECT 'test2' AS "HYPERTABLE_NAME" \gset
\set QUERY 'SELECT * FROM test2'
\set QUERY_ORDER 'ORDER BY c,"Time"'
\set HYPERTABLE_NAME 'test2'
\ir include/compression_test_hypertable.sql
-- This file and its contents are licensed under the Timescale License.
-- Please see the included NOTICE for copyright information and
Expand Down Expand Up @@ -321,8 +317,9 @@ group by location ORDER BY location;
POR | 18721
(2 rows)

SELECT $$ SELECT * FROM test4 ORDER BY timec $$ AS "QUERY" \gset
SELECT 'test4' AS "HYPERTABLE_NAME" \gset
\set QUERY 'SELECT * FROM test4'
\set QUERY_ORDER 'ORDER BY timec'
\set HYPERTABLE_NAME 'test4'
\ir include/compression_test_hypertable.sql
-- This file and its contents are licensed under the Timescale License.
-- Please see the included NOTICE for copyright information and
Expand Down Expand Up @@ -434,8 +431,9 @@ insert into test5
select generate_series('2018-01-01 00:00'::timestamp, '2018-01-10 00:00'::timestamp, '2 hour'), 'device_2', gen_rand_minstd();
insert into test5
select generate_series('2018-01-01 00:00'::timestamp, '2018-01-10 00:00'::timestamp, '2 hour'), NULL, gen_rand_minstd();
SELECT $$ SELECT * FROM test5 ORDER BY device_id, time $$ AS "QUERY" \gset
SELECT 'test5' AS "HYPERTABLE_NAME" \gset
\set QUERY 'SELECT * FROM test5'
\set QUERY_ORDER 'ORDER BY device_id, time'
\set HYPERTABLE_NAME 'test5'
\ir include/compression_test_hypertable.sql
-- This file and its contents are licensed under the Timescale License.
-- Please see the included NOTICE for copyright information and
Expand Down Expand Up @@ -514,7 +512,8 @@ INSERT INTO test6 SELECT t, d, customtype_in((t + d)::TEXT::cstring)
FROM generate_series(1, 200) t, generate_series(1, 3) d;
INSERT INTO test6 SELECT t, NULL, customtype_in(t::TEXT::cstring)
FROM generate_series(1, 200) t;
\set QUERY 'SELECT * FROM test6 ORDER BY device_id, time'
\set QUERY 'SELECT * FROM test6'
\set QUERY_ORDER 'ORDER BY device_id, time'
\set HYPERTABLE_NAME 'test6'
\ir include/compression_test_hypertable.sql
-- This file and its contents are licensed under the Timescale License.
Expand Down Expand Up @@ -591,7 +590,8 @@ INSERT INTO test7
SELECT t, d, '2019/07/07 01:30', gen_rand_minstd(), NULL
FROM generate_series(10, 20) t,
generate_series('2019/03/01'::DATE, '2019/03/10', '1d') d;
\set QUERY 'SELECT * FROM test7 ORDER BY time, c1'
\set QUERY 'SELECT * FROM test7'
\set QUERY_ORDER 'ORDER BY time, c1'
\set HYPERTABLE_NAME 'test7'
\ir include/compression_test_hypertable.sql
-- This file and its contents are licensed under the Timescale License.
Expand Down
38 changes: 16 additions & 22 deletions tsl/test/sql/compression_hypertable.sql
Original file line number Diff line number Diff line change
Expand Up @@ -50,12 +50,9 @@ INSERT INTO test1 SELECT t, gen_rand_minstd(), gen_rand_minstd(), gen_rand_mins

ALTER TABLE test1 set (timescaledb.compress, timescaledb.compress_segmentby = '', timescaledb.compress_orderby = '"Time" DESC');

SELECT
$$
SELECT * FROM test1 ORDER BY "Time"
$$ AS "QUERY" \gset

SELECT 'test1' AS "HYPERTABLE_NAME" \gset
\set QUERY 'SELECT * FROM test1'
\set QUERY_ORDER 'ORDER BY "Time"'
\set HYPERTABLE_NAME 'test1'

\ir include/compression_test_hypertable.sql
\set TYPE timestamptz
Expand Down Expand Up @@ -103,12 +100,10 @@ INSERT INTO test2 SELECT t, gen_rand_minstd(), gen_rand_minstd()::text, gen_rand

ALTER TABLE test2 set (timescaledb.compress, timescaledb.compress_segmentby = '', timescaledb.compress_orderby = 'c, "Time" DESC');

SELECT
$$
SELECT * FROM test2 ORDER BY c, "Time"
$$ AS "QUERY" \gset
\set QUERY 'SELECT * FROM test2'
\set QUERY_ORDER 'ORDER BY c,"Time"'
\set HYPERTABLE_NAME 'test2'

SELECT 'test2' AS "HYPERTABLE_NAME" \gset
\ir include/compression_test_hypertable.sql

\set TYPE int
Expand Down Expand Up @@ -146,9 +141,9 @@ select location, count(*)
from test4
group by location ORDER BY location;

SELECT $$ SELECT * FROM test4 ORDER BY timec $$ AS "QUERY" \gset

SELECT 'test4' AS "HYPERTABLE_NAME" \gset
\set QUERY 'SELECT * FROM test4'
\set QUERY_ORDER 'ORDER BY timec'
\set HYPERTABLE_NAME 'test4'

\ir include/compression_test_hypertable.sql
\set TYPE TIMESTAMPTZ
Expand Down Expand Up @@ -188,10 +183,9 @@ select generate_series('2018-01-01 00:00'::timestamp, '2018-01-10 00:00'::timest
insert into test5
select generate_series('2018-01-01 00:00'::timestamp, '2018-01-10 00:00'::timestamp, '2 hour'), NULL, gen_rand_minstd();


SELECT $$ SELECT * FROM test5 ORDER BY device_id, time $$ AS "QUERY" \gset

SELECT 'test5' AS "HYPERTABLE_NAME" \gset
\set QUERY 'SELECT * FROM test5'
\set QUERY_ORDER 'ORDER BY device_id, time'
\set HYPERTABLE_NAME 'test5'

\ir include/compression_test_hypertable.sql
\set TYPE TEXT
Expand Down Expand Up @@ -220,8 +214,8 @@ INSERT INTO test6 SELECT t, d, customtype_in((t + d)::TEXT::cstring)
INSERT INTO test6 SELECT t, NULL, customtype_in(t::TEXT::cstring)
FROM generate_series(1, 200) t;

\set QUERY 'SELECT * FROM test6 ORDER BY device_id, time'

\set QUERY 'SELECT * FROM test6'
\set QUERY_ORDER 'ORDER BY device_id, time'
\set HYPERTABLE_NAME 'test6'

\ir include/compression_test_hypertable.sql
Expand All @@ -248,8 +242,8 @@ INSERT INTO test7
FROM generate_series(10, 20) t,
generate_series('2019/03/01'::DATE, '2019/03/10', '1d') d;

\set QUERY 'SELECT * FROM test7 ORDER BY time, c1'

\set QUERY 'SELECT * FROM test7'
\set QUERY_ORDER 'ORDER BY time, c1'
\set HYPERTABLE_NAME 'test7'

\ir include/compression_test_hypertable.sql
Expand Down
8 changes: 4 additions & 4 deletions tsl/test/sql/include/compression_test_hypertable.sql
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,10 @@ SELECT timescaledb_post_restore();
\c :TEST_DBNAME :ROLE_DEFAULT_PERM_USER

with original AS (
SELECT row_number() OVER() row_number, * FROM original_result
SELECT row_number() OVER() row_number, * FROM (SELECT * FROM original_result :QUERY_ORDER) qo
),
decompressed AS (
SELECT row_number() OVER() row_number, * FROM (:QUERY) as q
SELECT row_number() OVER() row_number, * FROM (:QUERY :QUERY_ORDER) as q
)
SELECT 'Number of rows different between original and query on compressed data (expect 0)', count(*)
FROM original
Expand All @@ -49,10 +49,10 @@ WHERE hypertable.table_name like :'HYPERTABLE_NAME' and chunk.compressed_chunk_i

--run data on data that's been compressed and decompressed, make sure it's the same.
with original AS (
SELECT row_number() OVER() row_number, * FROM original_result
SELECT row_number() OVER() row_number, * FROM (SELECT * FROM original_result :QUERY_ORDER) qo
),
uncompressed AS (
SELECT row_number() OVER() row_number, * FROM (:QUERY) as q
SELECT row_number() OVER() row_number, * FROM (:QUERY :QUERY_ORDER) as q
)
SELECT 'Number of rows different between original and data that has been compressed and then decompressed (expect 0)', count(*)
FROM original
Expand Down

0 comments on commit e47d6cb

Please sign in to comment.