Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

$__timeGroup with fill parameter panics with "integer divide by zero" for interval below 1s #43579

Open
Tracked by #51476
mrittler opened this issue Dec 29, 2021 · 3 comments
Assignees

Comments

@mrittler
Copy link

mrittler commented Dec 29, 2021

What happened:
When using $__timeGroup with a fill parameter (null, previous or 0) and an interval below 1s the query execution panics with the following error:
lvl=eror msg="executeQuery panic" logger=tsdb.postgres error="runtime error: integer divide by zero" stack="[sql_engine.go:215 panic.go:1052 panic.go:200 resample.go:93 sql_engine.go:357]"

The division by zero is happening in the following line:

startUnixTime := qm.TimeRange.From.Unix() / int64(qm.Interval.Seconds()) * int64(qm.Interval.Seconds())

What you expected to happen:
I would expect that the macro also works for intervals below 1s because the $__interval variable delivers values in the millisecond range if zoomed in far enough (see screen recording below) and the minimum time interval that can be specified via data source settings can also be in millisecond range (see: https://grafana.com/docs/grafana/latest/datasources/postgres/#min-time-interval).

This happens when using query builder:
Peek 2021-12-29 15-31
...and also when using $__timeGroup/$__timeGroupAlias in the text editor directly:
Peek 2021-12-29 15-51

How to reproduce it (as minimally and precisely as possible):

  • use a SQL data source (I use TimescaleDB via PostgreSQL data source, but I guess all SQL data sources are affected by that issue)
  • go to data source settings and change the minimum time interval to 1ms (default is 10s)
    image
  • group by time, change fill parameter to NULL, previous or 0 and zoom in far enough or set interval parameter to a value below 1s (see screen recording)
    image

Environment:

  • Grafana version: v8.3.2 (afb9e8e)
  • Data source type & version: timescaledb:2.5.1-pg14 (via PostgreSQL plugin)
  • OS Grafana is installed on: Docker grafana/grafana-oss:8.3.2
  • User OS & Browser: Mozilla/5.0 (X11; Linux x86_64; rv:95.0) Gecko/20100101 Firefox/95.0
  • Grafana plugins: PostgreSQL
@codeincarnate
Copy link
Collaborator

We're in the midst of converting SQL datasources away from Angular and this seems like a good follow up to that effort.

@gabor
Copy link
Contributor

gabor commented Oct 16, 2023

i was able to reproduce it on grafana 10.1.5, with the following sql:
(let's assume the table is named tbl, the time column is named t, and the value-column is named v)

make sure you use format=time series

run this query:

SELECT
  $__timeGroup(t,$__interval,NULL),
  avg(v)
FROM tbl
WHERE
  $__timeFilter(t)
GROUP BY 1
ORDER BY 1

set query options / min interval to 1ms

and keep zooming in until the query options / interval value shows less than 1s (it will probably show 500ms.

@gabor
Copy link
Contributor

gabor commented Oct 17, 2023

originally i had a comment here that has proven incorrect later.
the misunderstanding was the result-type of extract(epoch from column).. i assumed it's an integer, but in fact it's a numeric, so it can have fractional parts, in other words, extract(epoch from t) - floor(extract(epoch from t)) is not always zero.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Status: In Progress
Development

No branches or pull requests

4 participants