Skip to content

Commit 6aa11f6

Browse files
Fix compressed sort pushdown to respect collation (#10182)
Fixes #9998
1 parent d4d5766 commit 6aa11f6

4 files changed

Lines changed: 57 additions & 0 deletions

File tree

.unreleased/pr_10182

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fixes: #10182 Fix columnstore sort pushdown to check for different query sortkey collation

tsl/src/nodes/columnar_scan/columnar_scan.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2921,6 +2921,12 @@ match_pathkeys_to_compression_orderby(List *pathkeys, List *chunk_em_exprs,
29212921
{
29222922
return false;
29232923
}
2924+
/* Pathkey collation different from underlying column collation may lead to different sort
2925+
* order */
2926+
if (var->varcollid != pk->pk_eclass->ec_collation)
2927+
{
2928+
return false;
2929+
}
29242930

29252931
char *column_name = get_attname(compression_info->chunk_rte->relid, var->varattno, false);
29262932
int orderby_index = ts_array_position(compression_info->settings->fd.orderby, column_name);

tsl/test/expected/compressed_collation.out

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,3 +119,33 @@ select min(name), max(name) from collation_agg_c;
119119

120120
reset enable_sort;
121121
reset timescaledb.debug_require_vector_agg;
122+
-- Test #9998: should not match query sort key with a different collation to compressed index sort key
123+
create table t9998 (
124+
ts timestamptz not null,
125+
name text collate :"COLLATION"
126+
);
127+
select table_name from create_hypertable('t9998', 'ts');
128+
table_name
129+
------------
130+
t9998
131+
132+
insert into t9998 values
133+
('2024-01-01', 'B'),
134+
('2024-01-01', 'a');
135+
alter table t9998 set (
136+
timescaledb.compress,
137+
timescaledb.compress_orderby = 'name'
138+
);
139+
select count(compress_chunk(ch)) from show_chunks('t9998') ch;
140+
count
141+
-------
142+
1
143+
144+
-- C collation sorts capital before lowercase: expected order is 'B', 'a'.
145+
select name from t9998 order by name collate "C";
146+
name
147+
------
148+
B
149+
a
150+
151+
drop table t9998 cascade;

tsl/test/sql/compressed_collation.sql

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,3 +102,23 @@ select min(name), max(name) from collation_agg_c;
102102

103103
reset enable_sort;
104104
reset timescaledb.debug_require_vector_agg;
105+
106+
-- Test #9998: should not match query sort key with a different collation to compressed index sort key
107+
create table t9998 (
108+
ts timestamptz not null,
109+
name text collate :"COLLATION"
110+
);
111+
select table_name from create_hypertable('t9998', 'ts');
112+
insert into t9998 values
113+
('2024-01-01', 'B'),
114+
('2024-01-01', 'a');
115+
alter table t9998 set (
116+
timescaledb.compress,
117+
timescaledb.compress_orderby = 'name'
118+
);
119+
select count(compress_chunk(ch)) from show_chunks('t9998') ch;
120+
121+
-- C collation sorts capital before lowercase: expected order is 'B', 'a'.
122+
select name from t9998 order by name collate "C";
123+
124+
drop table t9998 cascade;

0 commit comments

Comments
 (0)