Skip to content

Commit

Permalink
First attempt to fix
Browse files Browse the repository at this point in the history
Updated tests
  • Loading branch information
konskov committed Jan 3, 2024
1 parent c8b0f83 commit ee71889
Show file tree
Hide file tree
Showing 6 changed files with 314 additions and 8 deletions.
10 changes: 2 additions & 8 deletions src/planner/constify_now.c
Original file line number Diff line number Diff line change
Expand Up @@ -249,17 +249,11 @@ ts_constify_now(PlannerInfo *root, List *rtable, Node *node)

foreach (lc, be->args)
{
if (IsA(lfirst(lc), OpExpr) && is_valid_now_expr(lfirst_node(OpExpr, lc), rtable))
{
OpExpr *op = lfirst_node(OpExpr, lc);
additions = lappend(additions, constify_now_expr(root, op));
}
additions = lappend(additions, ts_constify_now(root, rtable, (Node *) lfirst(lc)));
}

if (additions)
{
be->args = list_concat(be->args, additions);
}
be->args = additions; // list_concat(NIL, additions);

break;
}
Expand Down
67 changes: 67 additions & 0 deletions tsl/test/shared/expected/constify_now-13.out
Original file line number Diff line number Diff line change
Expand Up @@ -606,3 +606,70 @@ WHERE subq_1.time > m1.time;
------+-----------+----+----+----+----+------+-----------+----+----+----+----+------
(0 rows)

CREATE TABLE logged_data (
rawtag_id INTEGER,
timestamp TIMESTAMP WITH TIME ZONE NOT NULL,
value REAL,
site_id INTEGER
);
SELECT table_name FROM create_hypertable('logged_data', 'timestamp');
table_name
logged_data
(1 row)

INSERT INTO logged_data (rawtag_id, timestamp, value, site_id)
VALUES
(1, '2023-01-01'::timestamptz, 13, 1),
(1, '2023-01-07'::timestamptz, 13, 1),
(1, '2023-01-10'::timestamptz, 13, 1),
(1, '2023-01-15'::timestamptz, 13, 1),
(1, '2023-01-20'::timestamptz, 15, 1),
(2, '2023-01-01'::timestamptz, 90, 1),
(3, '2023-01-07'::timestamptz, 2, 1),
(4, '2023-01-10'::timestamptz, 13, 3),
(2, '2023-01-15'::timestamptz, 13, 4),
(5, '2023-01-20'::timestamptz, 13, 1);
-- four chunks, all of them should be excluded at plantime
SELECT COUNT(*) FROM show_chunks('logged_data');
count
4
(1 row)

SET timescaledb.current_timestamp_mock TO '2024-01-01 0:30:00+00';
SET timescaledb.enable_chunk_append TO true;
SET timescaledb.enable_constraint_aware_append TO true;
-- no startup exclusion should be happening for any of the queries below
EXPLAIN (ANALYZE, SUMMARY OFF, TIMING OFF) SELECT * FROM logged_data WHERE
timestamp BETWEEN now() AND now() - interval '1 day'
AND rawtag_id = 1 ORDER BY "timestamp" ASC;
QUERY PLAN
Sort (cost=0.01..0.02 rows=0 width=20) (actual rows=0 loops=1)
Sort Key: "timestamp"
Sort Method: quicksort
-> Result (cost=0.00..0.00 rows=0 width=0) (actual rows=0 loops=1)
One-Time Filter: false
(5 rows)

EXPLAIN (ANALYZE, SUMMARY OFF, TIMING OFF) SELECT * FROM logged_data WHERE
timestamp <= now() AND timestamp >= now() - interval '1 day'
AND rawtag_id = 1 ORDER BY "timestamp" ASC;
QUERY PLAN
Sort (cost=0.01..0.02 rows=0 width=20) (actual rows=0 loops=1)
Sort Key: "timestamp"
Sort Method: quicksort
-> Result (cost=0.00..0.00 rows=0 width=0) (actual rows=0 loops=1)
One-Time Filter: false
(5 rows)

EXPLAIN (ANALYZE, SUMMARY OFF, TIMING OFF) SELECT * FROM logged_data WHERE
timestamp <= now() AND timestamp >= now() - interval '1 day'
ORDER BY "timestamp" ASC;
QUERY PLAN
Sort (cost=0.01..0.02 rows=0 width=20) (actual rows=0 loops=1)
Sort Key: "timestamp"
Sort Method: quicksort
-> Result (cost=0.00..0.00 rows=0 width=0) (actual rows=0 loops=1)
One-Time Filter: false
(5 rows)

DROP TABLE logged_data;
67 changes: 67 additions & 0 deletions tsl/test/shared/expected/constify_now-14.out
Original file line number Diff line number Diff line change
Expand Up @@ -603,3 +603,70 @@ WHERE subq_1.time > m1.time;
------+-----------+----+----+----+----+------+-----------+----+----+----+----+------
(0 rows)

CREATE TABLE logged_data (
rawtag_id INTEGER,
timestamp TIMESTAMP WITH TIME ZONE NOT NULL,
value REAL,
site_id INTEGER
);
SELECT table_name FROM create_hypertable('logged_data', 'timestamp');
table_name
logged_data
(1 row)

INSERT INTO logged_data (rawtag_id, timestamp, value, site_id)
VALUES
(1, '2023-01-01'::timestamptz, 13, 1),
(1, '2023-01-07'::timestamptz, 13, 1),
(1, '2023-01-10'::timestamptz, 13, 1),
(1, '2023-01-15'::timestamptz, 13, 1),
(1, '2023-01-20'::timestamptz, 15, 1),
(2, '2023-01-01'::timestamptz, 90, 1),
(3, '2023-01-07'::timestamptz, 2, 1),
(4, '2023-01-10'::timestamptz, 13, 3),
(2, '2023-01-15'::timestamptz, 13, 4),
(5, '2023-01-20'::timestamptz, 13, 1);
-- four chunks, all of them should be excluded at plantime
SELECT COUNT(*) FROM show_chunks('logged_data');
count
4
(1 row)

SET timescaledb.current_timestamp_mock TO '2024-01-01 0:30:00+00';
SET timescaledb.enable_chunk_append TO true;
SET timescaledb.enable_constraint_aware_append TO true;
-- no startup exclusion should be happening for any of the queries below
EXPLAIN (ANALYZE, SUMMARY OFF, TIMING OFF) SELECT * FROM logged_data WHERE
timestamp BETWEEN now() AND now() - interval '1 day'
AND rawtag_id = 1 ORDER BY "timestamp" ASC;
QUERY PLAN
Sort (cost=0.01..0.02 rows=0 width=20) (actual rows=0 loops=1)
Sort Key: "timestamp"
Sort Method: quicksort
-> Result (cost=0.00..0.00 rows=0 width=0) (actual rows=0 loops=1)
One-Time Filter: false
(5 rows)

EXPLAIN (ANALYZE, SUMMARY OFF, TIMING OFF) SELECT * FROM logged_data WHERE
timestamp <= now() AND timestamp >= now() - interval '1 day'
AND rawtag_id = 1 ORDER BY "timestamp" ASC;
QUERY PLAN
Sort (cost=0.01..0.02 rows=0 width=20) (actual rows=0 loops=1)
Sort Key: "timestamp"
Sort Method: quicksort
-> Result (cost=0.00..0.00 rows=0 width=0) (actual rows=0 loops=1)
One-Time Filter: false
(5 rows)

EXPLAIN (ANALYZE, SUMMARY OFF, TIMING OFF) SELECT * FROM logged_data WHERE
timestamp <= now() AND timestamp >= now() - interval '1 day'
ORDER BY "timestamp" ASC;
QUERY PLAN
Sort (cost=0.01..0.02 rows=0 width=20) (actual rows=0 loops=1)
Sort Key: "timestamp"
Sort Method: quicksort
-> Result (cost=0.00..0.00 rows=0 width=0) (actual rows=0 loops=1)
One-Time Filter: false
(5 rows)

DROP TABLE logged_data;
67 changes: 67 additions & 0 deletions tsl/test/shared/expected/constify_now-15.out
Original file line number Diff line number Diff line change
Expand Up @@ -603,3 +603,70 @@ WHERE subq_1.time > m1.time;
------+-----------+----+----+----+----+------+-----------+----+----+----+----+------
(0 rows)

CREATE TABLE logged_data (
rawtag_id INTEGER,
timestamp TIMESTAMP WITH TIME ZONE NOT NULL,
value REAL,
site_id INTEGER
);
SELECT table_name FROM create_hypertable('logged_data', 'timestamp');
table_name
logged_data
(1 row)

INSERT INTO logged_data (rawtag_id, timestamp, value, site_id)
VALUES
(1, '2023-01-01'::timestamptz, 13, 1),
(1, '2023-01-07'::timestamptz, 13, 1),
(1, '2023-01-10'::timestamptz, 13, 1),
(1, '2023-01-15'::timestamptz, 13, 1),
(1, '2023-01-20'::timestamptz, 15, 1),
(2, '2023-01-01'::timestamptz, 90, 1),
(3, '2023-01-07'::timestamptz, 2, 1),
(4, '2023-01-10'::timestamptz, 13, 3),
(2, '2023-01-15'::timestamptz, 13, 4),
(5, '2023-01-20'::timestamptz, 13, 1);
-- four chunks, all of them should be excluded at plantime
SELECT COUNT(*) FROM show_chunks('logged_data');
count
4
(1 row)

SET timescaledb.current_timestamp_mock TO '2024-01-01 0:30:00+00';
SET timescaledb.enable_chunk_append TO true;
SET timescaledb.enable_constraint_aware_append TO true;
-- no startup exclusion should be happening for any of the queries below
EXPLAIN (ANALYZE, SUMMARY OFF, TIMING OFF) SELECT * FROM logged_data WHERE
timestamp BETWEEN now() AND now() - interval '1 day'
AND rawtag_id = 1 ORDER BY "timestamp" ASC;
QUERY PLAN
Sort (cost=0.01..0.02 rows=0 width=20) (actual rows=0 loops=1)
Sort Key: "timestamp"
Sort Method: quicksort
-> Result (cost=0.00..0.00 rows=0 width=0) (actual rows=0 loops=1)
One-Time Filter: false
(5 rows)

EXPLAIN (ANALYZE, SUMMARY OFF, TIMING OFF) SELECT * FROM logged_data WHERE
timestamp <= now() AND timestamp >= now() - interval '1 day'
AND rawtag_id = 1 ORDER BY "timestamp" ASC;
QUERY PLAN
Sort (cost=0.01..0.02 rows=0 width=20) (actual rows=0 loops=1)
Sort Key: "timestamp"
Sort Method: quicksort
-> Result (cost=0.00..0.00 rows=0 width=0) (actual rows=0 loops=1)
One-Time Filter: false
(5 rows)

EXPLAIN (ANALYZE, SUMMARY OFF, TIMING OFF) SELECT * FROM logged_data WHERE
timestamp <= now() AND timestamp >= now() - interval '1 day'
ORDER BY "timestamp" ASC;
QUERY PLAN
Sort (cost=0.01..0.02 rows=0 width=20) (actual rows=0 loops=1)
Sort Key: "timestamp"
Sort Method: quicksort
-> Result (cost=0.00..0.00 rows=0 width=0) (actual rows=0 loops=1)
One-Time Filter: false
(5 rows)

DROP TABLE logged_data;
67 changes: 67 additions & 0 deletions tsl/test/shared/expected/constify_now-16.out
Original file line number Diff line number Diff line change
Expand Up @@ -603,3 +603,70 @@ WHERE subq_1.time > m1.time;
------+-----------+----+----+----+----+------+-----------+----+----+----+----+------
(0 rows)

CREATE TABLE logged_data (
rawtag_id INTEGER,
timestamp TIMESTAMP WITH TIME ZONE NOT NULL,
value REAL,
site_id INTEGER
);
SELECT table_name FROM create_hypertable('logged_data', 'timestamp');
table_name
logged_data
(1 row)

INSERT INTO logged_data (rawtag_id, timestamp, value, site_id)
VALUES
(1, '2023-01-01'::timestamptz, 13, 1),
(1, '2023-01-07'::timestamptz, 13, 1),
(1, '2023-01-10'::timestamptz, 13, 1),
(1, '2023-01-15'::timestamptz, 13, 1),
(1, '2023-01-20'::timestamptz, 15, 1),
(2, '2023-01-01'::timestamptz, 90, 1),
(3, '2023-01-07'::timestamptz, 2, 1),
(4, '2023-01-10'::timestamptz, 13, 3),
(2, '2023-01-15'::timestamptz, 13, 4),
(5, '2023-01-20'::timestamptz, 13, 1);
-- four chunks, all of them should be excluded at plantime
SELECT COUNT(*) FROM show_chunks('logged_data');
count
4
(1 row)

SET timescaledb.current_timestamp_mock TO '2024-01-01 0:30:00+00';
SET timescaledb.enable_chunk_append TO true;
SET timescaledb.enable_constraint_aware_append TO true;
-- no startup exclusion should be happening for any of the queries below
EXPLAIN (ANALYZE, SUMMARY OFF, TIMING OFF) SELECT * FROM logged_data WHERE
timestamp BETWEEN now() AND now() - interval '1 day'
AND rawtag_id = 1 ORDER BY "timestamp" ASC;
QUERY PLAN
Sort (cost=0.01..0.02 rows=0 width=20) (actual rows=0 loops=1)
Sort Key: "timestamp"
Sort Method: quicksort
-> Result (cost=0.00..0.00 rows=0 width=0) (actual rows=0 loops=1)
One-Time Filter: false
(5 rows)

EXPLAIN (ANALYZE, SUMMARY OFF, TIMING OFF) SELECT * FROM logged_data WHERE
timestamp <= now() AND timestamp >= now() - interval '1 day'
AND rawtag_id = 1 ORDER BY "timestamp" ASC;
QUERY PLAN
Sort (cost=0.01..0.02 rows=0 width=20) (actual rows=0 loops=1)
Sort Key: "timestamp"
Sort Method: quicksort
-> Result (cost=0.00..0.00 rows=0 width=0) (actual rows=0 loops=1)
One-Time Filter: false
(5 rows)

EXPLAIN (ANALYZE, SUMMARY OFF, TIMING OFF) SELECT * FROM logged_data WHERE
timestamp <= now() AND timestamp >= now() - interval '1 day'
ORDER BY "timestamp" ASC;
QUERY PLAN
Sort (cost=0.01..0.02 rows=0 width=20) (actual rows=0 loops=1)
Sort Key: "timestamp"
Sort Method: quicksort
-> Result (cost=0.00..0.00 rows=0 width=0) (actual rows=0 loops=1)
One-Time Filter: false
(5 rows)

DROP TABLE logged_data;
44 changes: 44 additions & 0 deletions tsl/test/shared/sql/constify_now.sql.in
Original file line number Diff line number Diff line change
Expand Up @@ -189,3 +189,47 @@ SELECT * FROM
LATERAL (SELECT m2.time FROM devices LIMIT 1) as subq_1
WHERE subq_1.time > m1.time;

CREATE TABLE logged_data (
rawtag_id INTEGER,
timestamp TIMESTAMP WITH TIME ZONE NOT NULL,
value REAL,
site_id INTEGER
);

SELECT table_name FROM create_hypertable('logged_data', 'timestamp');

INSERT INTO logged_data (rawtag_id, timestamp, value, site_id)
VALUES
(1, '2023-01-01'::timestamptz, 13, 1),
(1, '2023-01-07'::timestamptz, 13, 1),
(1, '2023-01-10'::timestamptz, 13, 1),
(1, '2023-01-15'::timestamptz, 13, 1),
(1, '2023-01-20'::timestamptz, 15, 1),
(2, '2023-01-01'::timestamptz, 90, 1),
(3, '2023-01-07'::timestamptz, 2, 1),
(4, '2023-01-10'::timestamptz, 13, 3),
(2, '2023-01-15'::timestamptz, 13, 4),
(5, '2023-01-20'::timestamptz, 13, 1);

-- four chunks, all of them should be excluded at plantime
SELECT COUNT(*) FROM show_chunks('logged_data');

SET timescaledb.current_timestamp_mock TO '2024-01-01 0:30:00+00';

SET timescaledb.enable_chunk_append TO true;
SET timescaledb.enable_constraint_aware_append TO true;

-- no startup exclusion should be happening for any of the queries below
EXPLAIN (ANALYZE, SUMMARY OFF, TIMING OFF) SELECT * FROM logged_data WHERE
timestamp BETWEEN now() AND now() - interval '1 day'
AND rawtag_id = 1 ORDER BY "timestamp" ASC;

EXPLAIN (ANALYZE, SUMMARY OFF, TIMING OFF) SELECT * FROM logged_data WHERE
timestamp <= now() AND timestamp >= now() - interval '1 day'
AND rawtag_id = 1 ORDER BY "timestamp" ASC;

EXPLAIN (ANALYZE, SUMMARY OFF, TIMING OFF) SELECT * FROM logged_data WHERE
timestamp <= now() AND timestamp >= now() - interval '1 day'
ORDER BY "timestamp" ASC;

DROP TABLE logged_data;

0 comments on commit ee71889

Please sign in to comment.