timescaledb icon indicating copy to clipboard operation
timescaledb copied to clipboard

`time_bucket_gapfill` is not working when trying to offset the timestamp

Open cloud-rocket opened this issue 4 years ago • 9 comments

Relevant system information:

  • OS: Kubernetes
  • PostgreSQL version (output of postgres --version): 10
  • TimescaleDB version (output of \dx in psql): 1.4.1
  • Installation method: using Docker

Describe the bug time_bucket_gapfill is not working when trying to offset the timestamp.

To Reproduce

SELECT time_bucket_gapfill('5 minutes', time) + '2.5 minutes'
  AS five_min, last(cpu, time)
FROM metrics
GROUP BY five_min
ORDER BY five_min DESC LIMIT 10;

Expected behavior The same behavior as with time_bucket (except the gap filling)

Actual behavior

 Could not execute the SQL command.
 Message returned: `ERROR:  no top level time_bucket_gapfill in group by clause

cloud-rocket avatar Sep 10 '19 21:09 cloud-rocket

I have the same issue. Query in below

SELECT table.id, to_char(time_bucket_gapfill('15 hours', datetime) at time zone 'utc', 'YYYY-MM-DD"T"HH24:MI:SS"Z"') as time,
avg(value) as value, data.description from table JOIN data ON table.id = data.id where table.id in(300,301,302,303,304)
AND datetime BETWEEN '2019-05-01' AND '2019-09-30' group by time, table.id, table.description order by time DESC;

sukkamehu avatar Oct 11 '19 06:10 sukkamehu

It's not a compact workaround and ideally I would want gapfill to take an offset as well, but what I've had to resort to is running gapfill first (and depending on your requirements, using smaller buckets - say 1 hour), then wrap that in a CTE and run a query on the CTE where you do a regular time_bucket of 15 hours offset by your desired amount. Here's an example:

with gapfilled_data as ( 
  select  
    time_bucket_gapfill('1 hour', time, '2019-05-01', '2019-09-30') as hours 
    , count(*) as total 
  from history 
  where time between '2019-05-01' and '2019-09-30' 
) 
select  
  time_bucket('15 hour', hours, '6 hour') as shift 
  , sum(total) as total 
from gapfilled_data  
group by shift 
order by shift

Krummelz avatar Oct 11 '19 10:10 Krummelz

having the same issue when trying to add + INTERVAL '1 hour' AS hour for example.

pantchox avatar Dec 30 '19 20:12 pantchox

Any updates on this?

Nizarius avatar Dec 21 '20 13:12 Nizarius

Experiencing this same issue also

tiemma avatar Apr 25 '21 12:04 tiemma

experiencing the same SQL Error [22023]: ERROR: no top level time_bucket_gapfill in group by clause error when using time_bucket_gapfill function. I wonder if any update. thanks.

Haybu avatar Sep 20 '21 05:09 Haybu

I can't seem to use time_bucket_gapfill at all (timescale 2.4.1), I always get this error

no top level time_bucket_gapfill in group by clause

jflambert avatar Oct 13 '21 17:10 jflambert

Same here, time_bucket_gapfill also return this error if you want to format the time column in another time format than default. For example, formatting in Epoch :

SELECT EXTRACT
	( EPOCH FROM time_bucket_gapfill ( '5 minutes' :: INTERVAL, datetime )) AS time
FROM
	"v1" 
WHERE
	"datetime" > '2022-06-15T21:59:00' 
	AND "datetime" < '2022-06-16T12:00:00' 
GROUP BY
	"time"
ORDER BY
	"time" ASC

ERROR: no top level time_bucket_gapfill in group by clause I don't have this issue with time_bucket

Raphyyy avatar Jun 16 '22 14:06 Raphyyy

@Raphyyy the documentation states that you can't embed time_bucket_gapfill in another function. Though not ideal you can get around it using a CTE, first getting the gapfill values then performing additional transformations afterwards.

genedavis avatar Jul 24 '22 15:07 genedavis

Is there a workaround for getting the time buckets to start at the "origin", like with time_bucket()? The examples in @Krummelz's workaround are good for offsetting, but I can't see how to adapt them to align my first time bucket to start at (e.g.) start_time..

ghost avatar Dec 13 '22 09:12 ghost

+1

melicheradam avatar Aug 21 '23 10:08 melicheradam