Skip to content

Commit

Permalink
Fix bool expression pushdown for queries on compressed chunks
Browse files Browse the repository at this point in the history
`NOT column` or `column = false` result in different expressions than
other boolean expressions which were not handled in the qual pushdown
code. This patch enables pushdown for these expressions and also enables
pushdown for OR expressions on compressed chunks.
  • Loading branch information
svenklemm committed Apr 29, 2024
1 parent 396dd0c commit cf88858
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 4 deletions.
1 change: 1 addition & 0 deletions tsl/src/nodes/decompress_chunk/qual_pushdown.c
Original file line number Diff line number Diff line change
Expand Up @@ -328,6 +328,7 @@ modify_expression(Node *node, QualPushdownContext *context)
/* opexpr will still be checked for segment by columns */
break;
}
case T_BoolExpr:
case T_CoerceViaIO:
case T_RelabelType:
case T_ScalarArrayOpExpr:
Expand Down
46 changes: 44 additions & 2 deletions tsl/test/expected/compression_qualpushdown.out
Original file line number Diff line number Diff line change
Expand Up @@ -351,15 +351,15 @@ EXPLAIN (costs off) SELECT '1' FROM deleteme_with_bytea WHERE bdata::text = '123
DROP table deleteme;
DROP table deleteme_with_bytea;
-- test sqlvaluefunction pushdown
CREATE TABLE svf_pushdown(time timestamptz, c_date date, c_time time, c_timetz timetz, c_timestamp timestamptz, c_name text);
CREATE TABLE svf_pushdown(time timestamptz, c_date date, c_time time, c_timetz timetz, c_timestamp timestamptz, c_name text, c_bool bool);
SELECT table_name FROM create_hypertable('svf_pushdown', 'time');
NOTICE: adding not-null constraint to column "time"
table_name
--------------
svf_pushdown
(1 row)

ALTER TABLE svf_pushdown SET (timescaledb.compress,timescaledb.compress_segmentby='c_date,c_time, c_timetz,c_timestamp,c_name');
ALTER TABLE svf_pushdown SET (timescaledb.compress,timescaledb.compress_segmentby='c_date,c_time, c_timetz,c_timestamp,c_name,c_bool');
NOTICE: default order by for hypertable "svf_pushdown" is set to ""time" DESC"
INSERT INTO svf_pushdown SELECT '2020-01-01';
SELECT compress_chunk(show_chunks('svf_pushdown'));
Expand Down Expand Up @@ -489,6 +489,16 @@ EXPLAIN (costs off) SELECT * FROM svf_pushdown WHERE c_name = SESSION_USER;
Filter: (c_name = SESSION_USER)
(5 rows)

EXPLAIN (costs off) SELECT * FROM svf_pushdown WHERE c_name = CURRENT_USER OR c_name = SESSION_USER;
QUERY PLAN
----------------------------------------------------------------------------
Custom Scan (ChunkAppend) on svf_pushdown
Chunks excluded during startup: 0
-> Custom Scan (DecompressChunk) on _hyper_11_12_chunk
-> Seq Scan on compress_hyper_12_13_chunk
Filter: ((c_name = CURRENT_USER) OR (c_name = SESSION_USER))
(5 rows)

EXPLAIN (costs off) SELECT * FROM svf_pushdown WHERE c_name = CURRENT_CATALOG;
QUERY PLAN
-----------------------------------------------------------
Expand All @@ -509,6 +519,38 @@ EXPLAIN (costs off) SELECT * FROM svf_pushdown WHERE c_name = CURRENT_SCHEMA;
Filter: (c_name = CURRENT_SCHEMA)
(5 rows)

EXPLAIN (costs off) SELECT * FROM svf_pushdown WHERE c_bool;
QUERY PLAN
----------------------------------------------------------------------------------------------------------------------
Custom Scan (DecompressChunk) on _hyper_11_12_chunk
-> Index Scan using compress_hyper_12_13_chunk_c_date_c_time_c_timetz_c_timesta_idx on compress_hyper_12_13_chunk
Index Cond: (c_bool = true)
(3 rows)

EXPLAIN (costs off) SELECT * FROM svf_pushdown WHERE c_bool = true;
QUERY PLAN
----------------------------------------------------------------------------------------------------------------------
Custom Scan (DecompressChunk) on _hyper_11_12_chunk
-> Index Scan using compress_hyper_12_13_chunk_c_date_c_time_c_timetz_c_timesta_idx on compress_hyper_12_13_chunk
Index Cond: (c_bool = true)
(3 rows)

EXPLAIN (costs off) SELECT * FROM svf_pushdown WHERE c_bool = false;
QUERY PLAN
----------------------------------------------------------------------------------------------------------------------
Custom Scan (DecompressChunk) on _hyper_11_12_chunk
-> Index Scan using compress_hyper_12_13_chunk_c_date_c_time_c_timetz_c_timesta_idx on compress_hyper_12_13_chunk
Index Cond: (c_bool = false)
(3 rows)

EXPLAIN (costs off) SELECT * FROM svf_pushdown WHERE NOT c_bool;
QUERY PLAN
----------------------------------------------------------------------------------------------------------------------
Custom Scan (DecompressChunk) on _hyper_11_12_chunk
-> Index Scan using compress_hyper_12_13_chunk_c_date_c_time_c_timetz_c_timesta_idx on compress_hyper_12_13_chunk
Index Cond: (c_bool = false)
(3 rows)

-- current_query() is not a sqlvaluefunction and volatile so should not be pushed down
EXPLAIN (costs off) SELECT * FROM svf_pushdown WHERE c_name = current_query();
QUERY PLAN
Expand Down
9 changes: 7 additions & 2 deletions tsl/test/sql/compression_qualpushdown.sql
Original file line number Diff line number Diff line change
Expand Up @@ -155,9 +155,9 @@ DROP table deleteme;
DROP table deleteme_with_bytea;

-- test sqlvaluefunction pushdown
CREATE TABLE svf_pushdown(time timestamptz, c_date date, c_time time, c_timetz timetz, c_timestamp timestamptz, c_name text);
CREATE TABLE svf_pushdown(time timestamptz, c_date date, c_time time, c_timetz timetz, c_timestamp timestamptz, c_name text, c_bool bool);
SELECT table_name FROM create_hypertable('svf_pushdown', 'time');
ALTER TABLE svf_pushdown SET (timescaledb.compress,timescaledb.compress_segmentby='c_date,c_time, c_timetz,c_timestamp,c_name');
ALTER TABLE svf_pushdown SET (timescaledb.compress,timescaledb.compress_segmentby='c_date,c_time, c_timetz,c_timestamp,c_name,c_bool');

INSERT INTO svf_pushdown SELECT '2020-01-01';
SELECT compress_chunk(show_chunks('svf_pushdown'));
Expand All @@ -175,8 +175,13 @@ EXPLAIN (costs off) SELECT * FROM svf_pushdown WHERE c_timestamp = LOCALTIMESTAM
EXPLAIN (costs off) SELECT * FROM svf_pushdown WHERE c_name = USER;
EXPLAIN (costs off) SELECT * FROM svf_pushdown WHERE c_name = CURRENT_USER;
EXPLAIN (costs off) SELECT * FROM svf_pushdown WHERE c_name = SESSION_USER;
EXPLAIN (costs off) SELECT * FROM svf_pushdown WHERE c_name = CURRENT_USER OR c_name = SESSION_USER;
EXPLAIN (costs off) SELECT * FROM svf_pushdown WHERE c_name = CURRENT_CATALOG;
EXPLAIN (costs off) SELECT * FROM svf_pushdown WHERE c_name = CURRENT_SCHEMA;
EXPLAIN (costs off) SELECT * FROM svf_pushdown WHERE c_bool;
EXPLAIN (costs off) SELECT * FROM svf_pushdown WHERE c_bool = true;
EXPLAIN (costs off) SELECT * FROM svf_pushdown WHERE c_bool = false;
EXPLAIN (costs off) SELECT * FROM svf_pushdown WHERE NOT c_bool;

-- current_query() is not a sqlvaluefunction and volatile so should not be pushed down
EXPLAIN (costs off) SELECT * FROM svf_pushdown WHERE c_name = current_query();
Expand Down

0 comments on commit cf88858

Please sign in to comment.