11-- This file and its contents are licensed under the Timescale License.
22-- Please see the included NOTICE for copyright information and
33-- LICENSE-TIMESCALE for a copy of the license.
4+ \c :TEST_DBNAME :ROLE_SUPERUSER
45-- Increase the working memory limit slightly, otherwise the batch sorted merge
56-- will be penalized for segmentby cardinalities larger than 100, where it is
67-- still faster than sort.
@@ -1295,8 +1296,8 @@ SELECT 1 as one, 2 as two, 3 as three, x2, x1, c2, time FROM test1 ORDER BY time
12951296 1 | 2 | 3 | 2 | 1 | 43 | Fri Dec 31 16:00:00 1999 PST
12961297 1 | 2 | 3 | 2 | 1 | 43 | Fri Dec 31 16:00:00 1999 PST
12971298
1298- -- Test with null values in x2
1299- set timescaledb.debug_require_batch_sorted_merge to 'forbid ';
1299+ -- Test with null values in x2: batch sorted merge supported with firstlast indexes
1300+ set timescaledb.debug_require_batch_sorted_merge to 'force ';
13001301SELECT time, x2 FROM test_with_defined_null ORDER BY x2 ASC NULLS FIRST;
13011302 time | x2
13021303------------------------------+----
@@ -1329,6 +1330,8 @@ SELECT time, x2 FROM test_with_defined_null ORDER BY x2 DESC NULLS LAST;
13291330 Sat Jan 01 00:00:00 2000 PST |
13301331 Sat Jan 01 00:00:00 2000 PST |
13311332
1333+ -- Should not be optimized (NULL order wrong)
1334+ set timescaledb.debug_require_batch_sorted_merge to 'forbid';
13321335SELECT time, x2 FROM test_with_defined_null ORDER BY x2 ASC NULLS LAST;
13331336 time | x2
13341337------------------------------+----
@@ -1589,7 +1592,7 @@ SELECT * FROM test_segby ORDER BY segby, time;
15891592 -> Seq Scan on _timescaledb_internal.compress_hyper_8_8_chunk (actual rows=2.00 loops=1)
15901593 Output: compress_hyper_8_8_chunk._ts_meta_count, compress_hyper_8_8_chunk.segby, compress_hyper_8_8_chunk._ts_meta_min_1, compress_hyper_8_8_chunk._ts_meta_max_1, compress_hyper_8_8_chunk._ts_meta_v2_first_time, compress_hyper_8_8_chunk._ts_meta_v2_last_time, compress_hyper_8_8_chunk."time", compress_hyper_8_8_chunk.val
15911594
1592- -- Tests for #9445: forbid BatchSortedMerge on nullable orderby columns
1595+ -- Tests for #9445: forbid BatchSortedMerge on nullable orderby columns with no firstlast index
15931596CREATE TABLE t(time int NOT NULL, device int, val int);
15941597SELECT create_hypertable('t', 'time', chunk_time_interval => 10000);
15951598 create_hypertable
@@ -1613,9 +1616,36 @@ SELECT compress_chunk(show_chunks('t'));
16131616SET timescaledb.enable_direct_compress_insert = true;
16141617INSERT INTO t SELECT 1, 1, g FROM generate_series(500, 800) g;
16151618INSERT INTO t SELECT 1, 1, g FROM generate_series(900, 1000) g;
1616- SET timescaledb.debug_require_batch_sorted_merge = 'forbid';
1617- -- In DESC order NULLs come first; a NULL after a non-NULL is wrong.
1619+ -- Batches with NULLs in orderby columns are correctly sorted with firstlast:
1620+ -- Batch Sorted Merge is allowed
1621+ SET timescaledb.debug_require_batch_sorted_merge = 'force';
1622+ -- Should return 0
1623+ SELECT count(*) AS wrong_rows FROM (
1624+ SELECT val, lag(val) OVER (ORDER BY val DESC) AS prev FROM t
1625+ ) t WHERE val IS NULL AND prev IS NOT NULL;
1626+ wrong_rows
1627+ ------------
1628+ 0
1629+
1630+ -- Remove firstlast index from order by columns
1631+ update _timescaledb_catalog.compression_settings
1632+ set index = '[{"type": "minmax", "column": "val", "source": "orderby"}, {"type": "minmax", "column": "time", "source": "orderby"}]'
1633+ where relid = 't'::regclass;
1634+ update _timescaledb_catalog.compression_settings
1635+ set index = '[{"type": "minmax", "column": "val", "source": "orderby"}, {"type": "minmax", "column": "time", "source": "orderby"}]'
1636+ where compress_relid = (select format('%I.%I', schema_name, table_name)::regclass AS chunk_regclass from _timescaledb_catalog.chunk
1637+ where id = (select compressed_chunk_id from _timescaledb_catalog.chunk
1638+ where hypertable_id = (select id from _timescaledb_catalog.hypertable
1639+ where table_name = 't') limit 1));
1640+ -- Use minmax index on (val DESC, time DESC) instead
1641+ select schema_name || '.' || table_name comp_chunk from _timescaledb_catalog.chunk
1642+ where id = (select compressed_chunk_id from _timescaledb_catalog.chunk
1643+ where hypertable_id = (select id from _timescaledb_catalog.hypertable
1644+ where table_name = 't') limit 1)
1645+ \gset
1646+ create index compressed_index_minmax on :comp_chunk (device, _ts_meta_min_1 DESC, _ts_meta_max_1 DESC, _ts_meta_min_2 DESC, _ts_meta_max_2 DESC);
16181647-- Should not use BatchSortedMerge here, should return 0
1648+ SET timescaledb.debug_require_batch_sorted_merge = 'forbid';
16191649SELECT count(*) AS wrong_rows FROM (
16201650 SELECT val, lag(val) OVER (ORDER BY val DESC) AS prev FROM t
16211651) t WHERE val IS NULL AND prev IS NOT NULL;
@@ -1634,6 +1664,7 @@ SELECT val, lag(val) OVER (ORDER BY val DESC NULLS FIRST) AS prev FROM t where v
16341664 997 | 998
16351665 996 | 997
16361666
1667+ drop table t cascade;
16371668-- Tests for BatchSortedMerge over one-segment data
16381669--------------------------------------
16391670-- Should be optimized (all segmentby columns are pinned to a Const, orderby columns match)
@@ -1834,6 +1865,5 @@ drop table test1 cascade;
18341865drop table test2 cascade;
18351866drop table test_segby cascade;
18361867drop table test_nosegby cascade;
1837- drop table t cascade;
18381868RESET timescaledb.enable_direct_compress_insert;
18391869RESET timescaledb.debug_require_batch_sorted_merge;
0 commit comments