Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .unreleased/pr_8545
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Fixes: #8545 Error in LOCF/Interpolate with out-of-order and repeated columns
Thanks: @moodgorning for reporting a bug in queries with LOCF/Interpolate using out-of-order columns
1 change: 1 addition & 0 deletions tsl/src/nodes/gapfill/gapfill_exec.c
Original file line number Diff line number Diff line change
Expand Up @@ -1420,6 +1420,7 @@ gapfill_adjust_varnos(GapFillState *state, Expr *expr)
if (IsA(tle->expr, Var) && castNode(Var, tle->expr)->varattno == var->varattno)
{
var->varattno = tle->resno;
break;
}
}
}
Expand Down
118 changes: 118 additions & 0 deletions tsl/test/shared/expected/gapfill_bug.out
Original file line number Diff line number Diff line change
Expand Up @@ -108,3 +108,121 @@ FROM data GROUP BY 1,2,3 ORDER BY 1,2,3;

drop table tw cascade;
RESET timezone;
-- Fix for 2nd issue reported in #4894: "time_bucket_gapfil" with "locf/interpolate"
-- using out-of-order and repeated columns, resulting in below error:
-- ERROR: attribute 5 of type record has wrong type
-- Should return correct data with no error
WITH el_data(time,position_id,measurement_value,measurement_type,src_mac_id,stream_index,delta) AS(
VALUES
('2025-07-05 23:57:19.000000 +00:00'::timestamptz,'2da82d12-5ceb-4470-b60b-9660b755d052'::uuid,26.5::numeric,'temperature_ambient','FFFF000D6F4DA7B2',3,-900),
('2025-07-06 00:12:19.000000 +00:00'::timestamptz,'2da82d12-5ceb-4470-b60b-9660b755d052'::uuid,26.5::numeric,'temperature_ambient','FFFF000D6F4DA7B2',3,-901),
('2025-07-06 00:27:20.000000 +00:00'::timestamptz,'2da82d12-5ceb-4470-b60b-9660b755d052'::uuid,26.5::numeric,'temperature_ambient','FFFF000D6F4DA7B2',3,-3110560),
('2025-07-06 00:06:12.000000 +00:00'::timestamptz,'9f2e566b-8c26-4ead-ad24-12841c79a932'::uuid,25.5::numeric,'temperature_ambient','FFFF000D6F4DA7B2',3,-900),
('2025-07-06 00:21:12.000000 +00:00'::timestamptz,'9f2e566b-8c26-4ead-ad24-12841c79a932'::uuid,25.5::numeric,'temperature_ambient','FFFF000D6F4DA7B2',3,-3110928),
('2025-07-06 00:02:57.000000 +00:00'::timestamptz,'09ca0d92-1eb5-4d07-adbc-2bb500817cc7'::uuid,26.5::numeric,'temperature_ambient','FFFF000D6F4DA7B2',3,-901),
('2025-07-06 00:17:58.000000 +00:00'::timestamptz,'09ca0d92-1eb5-4d07-adbc-2bb500817cc7'::uuid,26.5::numeric,'temperature_ambient','FFFF000D6F4DA7B2',3,-3111122),
('2025-07-06 00:06:02.000000 +00:00'::timestamptz,'e0145b95-6faa-4eb3-815d-5d0a91ba909c'::uuid,24::numeric,'temperature_ambient','FFFF000D6F4DA7B2',3,-900),
('2025-07-06 00:21:02.000000 +00:00'::timestamptz,'e0145b95-6faa-4eb3-815d-5d0a91ba909c'::uuid,24::numeric,'temperature_ambient','FFFF000D6F4DA7B2',3,-3110938),
('2025-07-06 00:04:48.000000 +00:00'::timestamptz,'d439a8fc-7285-4d32-82bc-2105c9ed2d52'::uuid,24::numeric,'temperature_ambient','FFFF000D6F4DA7B2',3,-900),
('2025-07-06 00:19:48.000000 +00:00'::timestamptz,'d439a8fc-7285-4d32-82bc-2105c9ed2d52'::uuid,24.5::numeric,'temperature_ambient','FFFF000D6F4DA7B2',3,-3111012),
('2025-07-06 00:07:00.000000 +00:00'::timestamptz,'4aa9b368-b9c4-4b15-bfcc-c67b1931dd11'::uuid,25::numeric,'temperature_ambient','FFFF000D6F4DA7B2',3,-900),
('2025-07-06 00:22:00.000000 +00:00'::timestamptz,'4aa9b368-b9c4-4b15-bfcc-c67b1931dd11'::uuid,24.5::numeric,'temperature_ambient','FFFF000D6F4DA7B2',3,-3110880),
('2025-07-06 00:08:03.000000 +00:00'::timestamptz,'64dc15d6-3988-4320-8f18-16ef8512990e'::uuid,30.5::numeric,'temperature_ambient','FFFF000D6F4DA7B2',3,-900),
('2025-07-06 00:23:03.000000 +00:00'::timestamptz,'64dc15d6-3988-4320-8f18-16ef8512990e'::uuid,30.5::numeric,'temperature_ambient','FFFF000D6F4DA7B2',3,-3110817),
('2025-07-05 23:57:13.000000 +00:00'::timestamptz,'bb044d29-c136-412d-a193-354f090dafcb'::uuid,23::numeric,'temperature_ambient','FFFF000D6F4DA7B2',3,-300),
('2025-07-06 00:02:13.000000 +00:00'::timestamptz,'bb044d29-c136-412d-a193-354f090dafcb'::uuid,23::numeric,'temperature_ambient','FFFF000D6F4DA7B2',3,-300),
('2025-07-06 00:07:13.000000 +00:00'::timestamptz,'bb044d29-c136-412d-a193-354f090dafcb'::uuid,23::numeric,'temperature_ambient','FFFF000D6F4DA7B2',3,-300)
)
,
el_nodes_data(time,src_mac_id,stream_index,position_id,measurement_type,measurement_value) as(
VALUES
('2025-07-05 23:42:20.000000 +00:00'::timestamptz,'FFFF000D6F4DA7B2',3,'2da82d12-5ceb-4470-b60b-9660b755d052','temperature_ambient',26::numeric),
('2025-07-06 00:51:09.000000 +00:00'::timestamptz,'FFFF000D6F4DA7B2',3,'2da82d12-5ceb-4470-b60b-9660b755d052','temperature_ambient',26::numeric)
)
SELECT
time_bucket_gapfill('5 minutes', m.time,
start := '2025-07-05 23:55:00+00:00'::timestamptz, finish := '2025-07-06 00:30:00+00:00'::timestamptz) AS intervals,
m.position_id,
m.src_mac_id,
m.stream_index,
m.measurement_type,
m.stream_index,
locf(last(m.measurement_value, m.time)
,
(SELECT d2.measurement_value FROM el_nodes_data d2
WHERE d2.measurement_type = m.measurement_type
AND d2.src_mac_id = m.src_mac_id
AND d2.stream_index = m.stream_index
AND d2.time < '2025-07-05 23:55:00+00:00'
AND d2.time > '2025-07-05 23:55:00+00:00'::timestamptz - INTERVAL '30 minutes'
ORDER BY d2.time DESC LIMIT 1)
) AS locf_measurement_value
FROM el_data m
GROUP BY
time_bucket_gapfill('5 minutes', m.time, start := '2025-07-05 23:55:00+00:00'::timestamptz,
finish := '2025-07-06 00:30:00+00:00'::timestamptz), -- avoid alias here
m.position_id,m.src_mac_id, m.stream_index, m.measurement_type
ORDER BY
time_bucket_gapfill('5 minutes', m.time, start := '2025-07-05 23:55:00+00:00'::timestamptz,
finish := '2025-07-06 00:30:00+00:00'::timestamptz), -- avoid alias here
m.position_id,m.src_mac_id, m.stream_index, m.measurement_type;
intervals | position_id | src_mac_id | stream_index | measurement_type | stream_index | locf_measurement_value
------------------------------+--------------------------------------+------------------+--------------+---------------------+--------------+------------------------
Sat Jul 05 16:55:00 2025 PDT | 09ca0d92-1eb5-4d07-adbc-2bb500817cc7 | FFFF000D6F4DA7B2 | 3 | temperature_ambient | 3 | 26
Sat Jul 05 16:55:00 2025 PDT | 2da82d12-5ceb-4470-b60b-9660b755d052 | FFFF000D6F4DA7B2 | 3 | temperature_ambient | 3 | 26.5
Sat Jul 05 16:55:00 2025 PDT | 4aa9b368-b9c4-4b15-bfcc-c67b1931dd11 | FFFF000D6F4DA7B2 | 3 | temperature_ambient | 3 | 26
Sat Jul 05 16:55:00 2025 PDT | 64dc15d6-3988-4320-8f18-16ef8512990e | FFFF000D6F4DA7B2 | 3 | temperature_ambient | 3 | 26
Sat Jul 05 16:55:00 2025 PDT | 9f2e566b-8c26-4ead-ad24-12841c79a932 | FFFF000D6F4DA7B2 | 3 | temperature_ambient | 3 | 26
Sat Jul 05 16:55:00 2025 PDT | bb044d29-c136-412d-a193-354f090dafcb | FFFF000D6F4DA7B2 | 3 | temperature_ambient | 3 | 23
Sat Jul 05 16:55:00 2025 PDT | d439a8fc-7285-4d32-82bc-2105c9ed2d52 | FFFF000D6F4DA7B2 | 3 | temperature_ambient | 3 | 26
Sat Jul 05 16:55:00 2025 PDT | e0145b95-6faa-4eb3-815d-5d0a91ba909c | FFFF000D6F4DA7B2 | 3 | temperature_ambient | 3 | 26
Sat Jul 05 17:00:00 2025 PDT | 09ca0d92-1eb5-4d07-adbc-2bb500817cc7 | FFFF000D6F4DA7B2 | 3 | temperature_ambient | 3 | 26.5
Sat Jul 05 17:00:00 2025 PDT | 2da82d12-5ceb-4470-b60b-9660b755d052 | FFFF000D6F4DA7B2 | 3 | temperature_ambient | 3 | 26.5
Sat Jul 05 17:00:00 2025 PDT | 4aa9b368-b9c4-4b15-bfcc-c67b1931dd11 | FFFF000D6F4DA7B2 | 3 | temperature_ambient | 3 | 26
Sat Jul 05 17:00:00 2025 PDT | 64dc15d6-3988-4320-8f18-16ef8512990e | FFFF000D6F4DA7B2 | 3 | temperature_ambient | 3 | 26
Sat Jul 05 17:00:00 2025 PDT | 9f2e566b-8c26-4ead-ad24-12841c79a932 | FFFF000D6F4DA7B2 | 3 | temperature_ambient | 3 | 26
Sat Jul 05 17:00:00 2025 PDT | bb044d29-c136-412d-a193-354f090dafcb | FFFF000D6F4DA7B2 | 3 | temperature_ambient | 3 | 23
Sat Jul 05 17:00:00 2025 PDT | d439a8fc-7285-4d32-82bc-2105c9ed2d52 | FFFF000D6F4DA7B2 | 3 | temperature_ambient | 3 | 24
Sat Jul 05 17:00:00 2025 PDT | e0145b95-6faa-4eb3-815d-5d0a91ba909c | FFFF000D6F4DA7B2 | 3 | temperature_ambient | 3 | 26
Sat Jul 05 17:05:00 2025 PDT | 09ca0d92-1eb5-4d07-adbc-2bb500817cc7 | FFFF000D6F4DA7B2 | 3 | temperature_ambient | 3 | 26.5
Sat Jul 05 17:05:00 2025 PDT | 2da82d12-5ceb-4470-b60b-9660b755d052 | FFFF000D6F4DA7B2 | 3 | temperature_ambient | 3 | 26.5
Sat Jul 05 17:05:00 2025 PDT | 4aa9b368-b9c4-4b15-bfcc-c67b1931dd11 | FFFF000D6F4DA7B2 | 3 | temperature_ambient | 3 | 25
Sat Jul 05 17:05:00 2025 PDT | 64dc15d6-3988-4320-8f18-16ef8512990e | FFFF000D6F4DA7B2 | 3 | temperature_ambient | 3 | 30.5
Sat Jul 05 17:05:00 2025 PDT | 9f2e566b-8c26-4ead-ad24-12841c79a932 | FFFF000D6F4DA7B2 | 3 | temperature_ambient | 3 | 25.5
Sat Jul 05 17:05:00 2025 PDT | bb044d29-c136-412d-a193-354f090dafcb | FFFF000D6F4DA7B2 | 3 | temperature_ambient | 3 | 23
Sat Jul 05 17:05:00 2025 PDT | d439a8fc-7285-4d32-82bc-2105c9ed2d52 | FFFF000D6F4DA7B2 | 3 | temperature_ambient | 3 | 24
Sat Jul 05 17:05:00 2025 PDT | e0145b95-6faa-4eb3-815d-5d0a91ba909c | FFFF000D6F4DA7B2 | 3 | temperature_ambient | 3 | 24
Sat Jul 05 17:10:00 2025 PDT | 09ca0d92-1eb5-4d07-adbc-2bb500817cc7 | FFFF000D6F4DA7B2 | 3 | temperature_ambient | 3 | 26.5
Sat Jul 05 17:10:00 2025 PDT | 2da82d12-5ceb-4470-b60b-9660b755d052 | FFFF000D6F4DA7B2 | 3 | temperature_ambient | 3 | 26.5
Sat Jul 05 17:10:00 2025 PDT | 4aa9b368-b9c4-4b15-bfcc-c67b1931dd11 | FFFF000D6F4DA7B2 | 3 | temperature_ambient | 3 | 25
Sat Jul 05 17:10:00 2025 PDT | 64dc15d6-3988-4320-8f18-16ef8512990e | FFFF000D6F4DA7B2 | 3 | temperature_ambient | 3 | 30.5
Sat Jul 05 17:10:00 2025 PDT | 9f2e566b-8c26-4ead-ad24-12841c79a932 | FFFF000D6F4DA7B2 | 3 | temperature_ambient | 3 | 25.5
Sat Jul 05 17:10:00 2025 PDT | bb044d29-c136-412d-a193-354f090dafcb | FFFF000D6F4DA7B2 | 3 | temperature_ambient | 3 | 23
Sat Jul 05 17:10:00 2025 PDT | d439a8fc-7285-4d32-82bc-2105c9ed2d52 | FFFF000D6F4DA7B2 | 3 | temperature_ambient | 3 | 24
Sat Jul 05 17:10:00 2025 PDT | e0145b95-6faa-4eb3-815d-5d0a91ba909c | FFFF000D6F4DA7B2 | 3 | temperature_ambient | 3 | 24
Sat Jul 05 17:15:00 2025 PDT | 09ca0d92-1eb5-4d07-adbc-2bb500817cc7 | FFFF000D6F4DA7B2 | 3 | temperature_ambient | 3 | 26.5
Sat Jul 05 17:15:00 2025 PDT | 2da82d12-5ceb-4470-b60b-9660b755d052 | FFFF000D6F4DA7B2 | 3 | temperature_ambient | 3 | 26.5
Sat Jul 05 17:15:00 2025 PDT | 4aa9b368-b9c4-4b15-bfcc-c67b1931dd11 | FFFF000D6F4DA7B2 | 3 | temperature_ambient | 3 | 25
Sat Jul 05 17:15:00 2025 PDT | 64dc15d6-3988-4320-8f18-16ef8512990e | FFFF000D6F4DA7B2 | 3 | temperature_ambient | 3 | 30.5
Sat Jul 05 17:15:00 2025 PDT | 9f2e566b-8c26-4ead-ad24-12841c79a932 | FFFF000D6F4DA7B2 | 3 | temperature_ambient | 3 | 25.5
Sat Jul 05 17:15:00 2025 PDT | bb044d29-c136-412d-a193-354f090dafcb | FFFF000D6F4DA7B2 | 3 | temperature_ambient | 3 | 23
Sat Jul 05 17:15:00 2025 PDT | d439a8fc-7285-4d32-82bc-2105c9ed2d52 | FFFF000D6F4DA7B2 | 3 | temperature_ambient | 3 | 24.5
Sat Jul 05 17:15:00 2025 PDT | e0145b95-6faa-4eb3-815d-5d0a91ba909c | FFFF000D6F4DA7B2 | 3 | temperature_ambient | 3 | 24
Sat Jul 05 17:20:00 2025 PDT | 09ca0d92-1eb5-4d07-adbc-2bb500817cc7 | FFFF000D6F4DA7B2 | 3 | temperature_ambient | 3 | 26.5
Sat Jul 05 17:20:00 2025 PDT | 2da82d12-5ceb-4470-b60b-9660b755d052 | FFFF000D6F4DA7B2 | 3 | temperature_ambient | 3 | 26.5
Sat Jul 05 17:20:00 2025 PDT | 4aa9b368-b9c4-4b15-bfcc-c67b1931dd11 | FFFF000D6F4DA7B2 | 3 | temperature_ambient | 3 | 24.5
Sat Jul 05 17:20:00 2025 PDT | 64dc15d6-3988-4320-8f18-16ef8512990e | FFFF000D6F4DA7B2 | 3 | temperature_ambient | 3 | 30.5
Sat Jul 05 17:20:00 2025 PDT | 9f2e566b-8c26-4ead-ad24-12841c79a932 | FFFF000D6F4DA7B2 | 3 | temperature_ambient | 3 | 25.5
Sat Jul 05 17:20:00 2025 PDT | bb044d29-c136-412d-a193-354f090dafcb | FFFF000D6F4DA7B2 | 3 | temperature_ambient | 3 | 23
Sat Jul 05 17:20:00 2025 PDT | d439a8fc-7285-4d32-82bc-2105c9ed2d52 | FFFF000D6F4DA7B2 | 3 | temperature_ambient | 3 | 24.5
Sat Jul 05 17:20:00 2025 PDT | e0145b95-6faa-4eb3-815d-5d0a91ba909c | FFFF000D6F4DA7B2 | 3 | temperature_ambient | 3 | 24
Sat Jul 05 17:25:00 2025 PDT | 09ca0d92-1eb5-4d07-adbc-2bb500817cc7 | FFFF000D6F4DA7B2 | 3 | temperature_ambient | 3 | 26.5
Sat Jul 05 17:25:00 2025 PDT | 2da82d12-5ceb-4470-b60b-9660b755d052 | FFFF000D6F4DA7B2 | 3 | temperature_ambient | 3 | 26.5
Sat Jul 05 17:25:00 2025 PDT | 4aa9b368-b9c4-4b15-bfcc-c67b1931dd11 | FFFF000D6F4DA7B2 | 3 | temperature_ambient | 3 | 24.5
Sat Jul 05 17:25:00 2025 PDT | 64dc15d6-3988-4320-8f18-16ef8512990e | FFFF000D6F4DA7B2 | 3 | temperature_ambient | 3 | 30.5
Sat Jul 05 17:25:00 2025 PDT | 9f2e566b-8c26-4ead-ad24-12841c79a932 | FFFF000D6F4DA7B2 | 3 | temperature_ambient | 3 | 25.5
Sat Jul 05 17:25:00 2025 PDT | bb044d29-c136-412d-a193-354f090dafcb | FFFF000D6F4DA7B2 | 3 | temperature_ambient | 3 | 23
Sat Jul 05 17:25:00 2025 PDT | d439a8fc-7285-4d32-82bc-2105c9ed2d52 | FFFF000D6F4DA7B2 | 3 | temperature_ambient | 3 | 24.5
Sat Jul 05 17:25:00 2025 PDT | e0145b95-6faa-4eb3-815d-5d0a91ba909c | FFFF000D6F4DA7B2 | 3 | temperature_ambient | 3 | 24
(56 rows)

60 changes: 60 additions & 0 deletions tsl/test/shared/sql/gapfill_bug.sql
Original file line number Diff line number Diff line change
Expand Up @@ -73,3 +73,63 @@ FROM data GROUP BY 1,2,3 ORDER BY 1,2,3;

drop table tw cascade;
RESET timezone;

-- Fix for 2nd issue reported in #4894: "time_bucket_gapfil" with "locf/interpolate"
-- using out-of-order and repeated columns, resulting in below error:
-- ERROR: attribute 5 of type record has wrong type

-- Should return correct data with no error
WITH el_data(time,position_id,measurement_value,measurement_type,src_mac_id,stream_index,delta) AS(
VALUES
('2025-07-05 23:57:19.000000 +00:00'::timestamptz,'2da82d12-5ceb-4470-b60b-9660b755d052'::uuid,26.5::numeric,'temperature_ambient','FFFF000D6F4DA7B2',3,-900),
('2025-07-06 00:12:19.000000 +00:00'::timestamptz,'2da82d12-5ceb-4470-b60b-9660b755d052'::uuid,26.5::numeric,'temperature_ambient','FFFF000D6F4DA7B2',3,-901),
('2025-07-06 00:27:20.000000 +00:00'::timestamptz,'2da82d12-5ceb-4470-b60b-9660b755d052'::uuid,26.5::numeric,'temperature_ambient','FFFF000D6F4DA7B2',3,-3110560),
('2025-07-06 00:06:12.000000 +00:00'::timestamptz,'9f2e566b-8c26-4ead-ad24-12841c79a932'::uuid,25.5::numeric,'temperature_ambient','FFFF000D6F4DA7B2',3,-900),
('2025-07-06 00:21:12.000000 +00:00'::timestamptz,'9f2e566b-8c26-4ead-ad24-12841c79a932'::uuid,25.5::numeric,'temperature_ambient','FFFF000D6F4DA7B2',3,-3110928),
('2025-07-06 00:02:57.000000 +00:00'::timestamptz,'09ca0d92-1eb5-4d07-adbc-2bb500817cc7'::uuid,26.5::numeric,'temperature_ambient','FFFF000D6F4DA7B2',3,-901),
('2025-07-06 00:17:58.000000 +00:00'::timestamptz,'09ca0d92-1eb5-4d07-adbc-2bb500817cc7'::uuid,26.5::numeric,'temperature_ambient','FFFF000D6F4DA7B2',3,-3111122),
('2025-07-06 00:06:02.000000 +00:00'::timestamptz,'e0145b95-6faa-4eb3-815d-5d0a91ba909c'::uuid,24::numeric,'temperature_ambient','FFFF000D6F4DA7B2',3,-900),
('2025-07-06 00:21:02.000000 +00:00'::timestamptz,'e0145b95-6faa-4eb3-815d-5d0a91ba909c'::uuid,24::numeric,'temperature_ambient','FFFF000D6F4DA7B2',3,-3110938),
('2025-07-06 00:04:48.000000 +00:00'::timestamptz,'d439a8fc-7285-4d32-82bc-2105c9ed2d52'::uuid,24::numeric,'temperature_ambient','FFFF000D6F4DA7B2',3,-900),
('2025-07-06 00:19:48.000000 +00:00'::timestamptz,'d439a8fc-7285-4d32-82bc-2105c9ed2d52'::uuid,24.5::numeric,'temperature_ambient','FFFF000D6F4DA7B2',3,-3111012),
('2025-07-06 00:07:00.000000 +00:00'::timestamptz,'4aa9b368-b9c4-4b15-bfcc-c67b1931dd11'::uuid,25::numeric,'temperature_ambient','FFFF000D6F4DA7B2',3,-900),
('2025-07-06 00:22:00.000000 +00:00'::timestamptz,'4aa9b368-b9c4-4b15-bfcc-c67b1931dd11'::uuid,24.5::numeric,'temperature_ambient','FFFF000D6F4DA7B2',3,-3110880),
('2025-07-06 00:08:03.000000 +00:00'::timestamptz,'64dc15d6-3988-4320-8f18-16ef8512990e'::uuid,30.5::numeric,'temperature_ambient','FFFF000D6F4DA7B2',3,-900),
('2025-07-06 00:23:03.000000 +00:00'::timestamptz,'64dc15d6-3988-4320-8f18-16ef8512990e'::uuid,30.5::numeric,'temperature_ambient','FFFF000D6F4DA7B2',3,-3110817),
('2025-07-05 23:57:13.000000 +00:00'::timestamptz,'bb044d29-c136-412d-a193-354f090dafcb'::uuid,23::numeric,'temperature_ambient','FFFF000D6F4DA7B2',3,-300),
('2025-07-06 00:02:13.000000 +00:00'::timestamptz,'bb044d29-c136-412d-a193-354f090dafcb'::uuid,23::numeric,'temperature_ambient','FFFF000D6F4DA7B2',3,-300),
('2025-07-06 00:07:13.000000 +00:00'::timestamptz,'bb044d29-c136-412d-a193-354f090dafcb'::uuid,23::numeric,'temperature_ambient','FFFF000D6F4DA7B2',3,-300)
)
,
el_nodes_data(time,src_mac_id,stream_index,position_id,measurement_type,measurement_value) as(
VALUES
('2025-07-05 23:42:20.000000 +00:00'::timestamptz,'FFFF000D6F4DA7B2',3,'2da82d12-5ceb-4470-b60b-9660b755d052','temperature_ambient',26::numeric),
('2025-07-06 00:51:09.000000 +00:00'::timestamptz,'FFFF000D6F4DA7B2',3,'2da82d12-5ceb-4470-b60b-9660b755d052','temperature_ambient',26::numeric)
)
SELECT
time_bucket_gapfill('5 minutes', m.time,
start := '2025-07-05 23:55:00+00:00'::timestamptz, finish := '2025-07-06 00:30:00+00:00'::timestamptz) AS intervals,
m.position_id,
m.src_mac_id,
m.stream_index,
m.measurement_type,
m.stream_index,
locf(last(m.measurement_value, m.time)
,
(SELECT d2.measurement_value FROM el_nodes_data d2
WHERE d2.measurement_type = m.measurement_type
AND d2.src_mac_id = m.src_mac_id
AND d2.stream_index = m.stream_index
AND d2.time < '2025-07-05 23:55:00+00:00'
AND d2.time > '2025-07-05 23:55:00+00:00'::timestamptz - INTERVAL '30 minutes'
ORDER BY d2.time DESC LIMIT 1)
) AS locf_measurement_value
FROM el_data m
GROUP BY
time_bucket_gapfill('5 minutes', m.time, start := '2025-07-05 23:55:00+00:00'::timestamptz,
finish := '2025-07-06 00:30:00+00:00'::timestamptz), -- avoid alias here
m.position_id,m.src_mac_id, m.stream_index, m.measurement_type
ORDER BY
time_bucket_gapfill('5 minutes', m.time, start := '2025-07-05 23:55:00+00:00'::timestamptz,
finish := '2025-07-06 00:30:00+00:00'::timestamptz), -- avoid alias here
m.position_id,m.src_mac_id, m.stream_index, m.measurement_type;
Loading