|
| 1 | +CREATE TABLE regular_table(name text, junk text); |
| 2 | +CREATE TABLE ht(time timestamptz NOT NULL, location text); |
| 3 | +SELECT create_hypertable('ht', 'time'); |
| 4 | + create_hypertable |
| 5 | +------------------- |
| 6 | + |
| 7 | +(1 row) |
| 8 | + |
| 9 | +INSERT INTO ht(time) select timestamp 'epoch' + (i * interval '1 second') from generate_series(1, 100) as T(i); |
| 10 | +INSERT INTO regular_table values('name', 'junk'); |
| 11 | +SELECT * FROM regular_table ik LEFT JOIN LATERAL (select max(time::timestamptz) from ht s where ik.name='name' and s.time < now()) s on true; |
| 12 | + name | junk | max |
| 13 | +------+------+------------------------------ |
| 14 | + name | junk | Thu Jan 01 00:01:40 1970 PST |
| 15 | +(1 row) |
| 16 | + |
| 17 | +select * from regular_table ik LEFT JOIN LATERAL (select max(time::timestamptz) from ht s where ik.name='name' and s.time > now()) s on true; |
| 18 | + name | junk | max |
| 19 | +------+------+----- |
| 20 | + name | junk | |
| 21 | +(1 row) |
| 22 | + |
| 23 | +DROP TABLE regular_table; |
| 24 | +DROP TABLE ht; |
| 25 | +CREATE TABLE orders(id int, user_id int, time TIMESTAMPTZ NOT NULL); |
| 26 | +SELECT create_hypertable('orders', 'time'); |
| 27 | + create_hypertable |
| 28 | +------------------- |
| 29 | + |
| 30 | +(1 row) |
| 31 | + |
| 32 | +INSERT INTO orders values(1,1,timestamp 'epoch' + '1 second'); |
| 33 | +INSERT INTO orders values(2,1,timestamp 'epoch' + '2 second'); |
| 34 | +INSERT INTO orders values(3,1,timestamp 'epoch' + '3 second'); |
| 35 | +INSERT INTO orders values(4,2,timestamp 'epoch' + '4 second'); |
| 36 | +INSERT INTO orders values(5,1,timestamp 'epoch' + '5 second'); |
| 37 | +INSERT INTO orders values(6,3,timestamp 'epoch' + '6 second'); |
| 38 | +INSERT INTO orders values(7,1,timestamp 'epoch' + '7 second'); |
| 39 | +INSERT INTO orders values(8,4,timestamp 'epoch' + '8 second'); |
| 40 | +INSERT INTO orders values(9,2,timestamp 'epoch' + '9 second'); |
| 41 | +-- Need a LATERAL query with a reference to the upper-level table and |
| 42 | +-- with a restriction on time |
| 43 | +-- Upper-level table constraint should be a constant in order to trigger |
| 44 | +-- creation of a one-time filter in the planner |
| 45 | +SELECT user_id, first_order_time, max_time FROM |
| 46 | +(SELECT user_id, min(time) AS first_order_time FROM orders GROUP BY user_id) o1 |
| 47 | +LEFT JOIN LATERAL |
| 48 | +(SELECT max(time) AS max_time FROM orders WHERE o1.user_id = '2' AND time > now()) o2 ON true |
| 49 | +ORDER BY user_id, first_order_time, max_time; |
| 50 | + user_id | first_order_time | max_time |
| 51 | +---------+------------------------------+---------- |
| 52 | + 1 | Thu Jan 01 00:00:01 1970 PST | |
| 53 | + 2 | Thu Jan 01 00:00:04 1970 PST | |
| 54 | + 3 | Thu Jan 01 00:00:06 1970 PST | |
| 55 | + 4 | Thu Jan 01 00:00:08 1970 PST | |
| 56 | +(4 rows) |
| 57 | + |
| 58 | +SELECT user_id, first_order_time, max_time FROM |
| 59 | +(SELECT user_id, min(time) AS first_order_time FROM orders GROUP BY user_id) o1 |
| 60 | +LEFT JOIN LATERAL |
| 61 | +(SELECT max(time) AS max_time FROM orders WHERE o1.user_id = '2' AND time < now()) o2 ON true |
| 62 | +ORDER BY user_id, first_order_time, max_time; |
| 63 | + user_id | first_order_time | max_time |
| 64 | +---------+------------------------------+------------------------------ |
| 65 | + 1 | Thu Jan 01 00:00:01 1970 PST | |
| 66 | + 2 | Thu Jan 01 00:00:04 1970 PST | Thu Jan 01 00:00:09 1970 PST |
| 67 | + 3 | Thu Jan 01 00:00:06 1970 PST | |
| 68 | + 4 | Thu Jan 01 00:00:08 1970 PST | |
| 69 | +(4 rows) |
| 70 | + |
| 71 | +-- Nested LATERALs |
| 72 | +SELECT user_id, first_order_time, time1, min_time FROM |
| 73 | +(SELECT user_id, min(time) AS first_order_time FROM orders GROUP BY user_id) o1 |
| 74 | +LEFT JOIN LATERAL |
| 75 | +(SELECT user_id as o2user_id, time AS time1 FROM orders WHERE o1.user_id = '2' AND time < now()) o2 ON true |
| 76 | +LEFT JOIN LATERAL |
| 77 | +(SELECT min(time) as min_time FROM orders WHERE o2.o2user_id = '1' AND time < now()) o3 ON true |
| 78 | +ORDER BY user_id, first_order_time, time1, min_time; |
| 79 | + user_id | first_order_time | time1 | min_time |
| 80 | +---------+------------------------------+------------------------------+------------------------------ |
| 81 | + 1 | Thu Jan 01 00:00:01 1970 PST | | |
| 82 | + 2 | Thu Jan 01 00:00:04 1970 PST | Thu Jan 01 00:00:01 1970 PST | Thu Jan 01 00:00:01 1970 PST |
| 83 | + 2 | Thu Jan 01 00:00:04 1970 PST | Thu Jan 01 00:00:02 1970 PST | Thu Jan 01 00:00:01 1970 PST |
| 84 | + 2 | Thu Jan 01 00:00:04 1970 PST | Thu Jan 01 00:00:03 1970 PST | Thu Jan 01 00:00:01 1970 PST |
| 85 | + 2 | Thu Jan 01 00:00:04 1970 PST | Thu Jan 01 00:00:04 1970 PST | |
| 86 | + 2 | Thu Jan 01 00:00:04 1970 PST | Thu Jan 01 00:00:05 1970 PST | Thu Jan 01 00:00:01 1970 PST |
| 87 | + 2 | Thu Jan 01 00:00:04 1970 PST | Thu Jan 01 00:00:06 1970 PST | |
| 88 | + 2 | Thu Jan 01 00:00:04 1970 PST | Thu Jan 01 00:00:07 1970 PST | Thu Jan 01 00:00:01 1970 PST |
| 89 | + 2 | Thu Jan 01 00:00:04 1970 PST | Thu Jan 01 00:00:08 1970 PST | |
| 90 | + 2 | Thu Jan 01 00:00:04 1970 PST | Thu Jan 01 00:00:09 1970 PST | |
| 91 | + 3 | Thu Jan 01 00:00:06 1970 PST | | |
| 92 | + 4 | Thu Jan 01 00:00:08 1970 PST | | |
| 93 | +(12 rows) |
| 94 | + |
| 95 | +-- Cleanup |
| 96 | +DROP TABLE orders; |
0 commit comments