superset: Time comparison does not always capture the correct data when the result set is limited
Bug description
When fetching a set of data over a period of time and adding a time comparison, if the result set is truncated, some of the comparison information will also be missing.
Here is an example with missing data when a small row limit is applied:
In the above example, I am trying to fetch the top 10 years from the years 1989 - 2004 where the name Alex was the most popular and compare that count to the period 10 years prior. As you can see, the values for 1981, 1982, 1983, and 1984 are missing, although I would expect to get those values, because as you can see, if I fetch more data by removing the row limit, those values exist.
Here is an example with all the data when a larger row limit is applied
I believe the reason why this bug exists is because we are making two separate db requests, one for each time period when the name is “Alex”, and limiting each request by 10 and sorting on the count. Because the count for each response is going to be different, when we make the comparison, the values for the dates in the original time series may not exist.
Here are the current db requests when a row limit is applied:
I believe what needs to happen instead is that the first query with timerange A needs to join the data from timerange B on a left outer join, and then apply the limit, and that would give the correct results.
This is an example of a query that would fetch all the data required:
SELECT
query1.ds AS ds1,
query1.count AS count1,
query2.ds AS ds2,
query2.count AS count2
FROM
(
SELECT
DATE_TRUNC('year', ds) AS ds,
COUNT(*) AS count
FROM
public.birth_names
WHERE
ds >= TO_TIMESTAMP('1989-03-13 00:00:00.000000', 'YYYY-MM-DD HH24:MI:SS.US') AND
ds < TO_TIMESTAMP('2004-03-13 00:00:00.000000', 'YYYY-MM-DD HH24:MI:SS.US') AND
name IN ('Alex')
GROUP BY
DATE_TRUNC('year', ds)
ORDER BY
ds DESC
) AS query1
LEFT JOIN
(
SELECT
DATE_TRUNC('year', ds) AS ds,
COUNT(*) AS count
FROM
public.birth_names
WHERE
ds >= TO_TIMESTAMP('1979-03-13 00:00:00.000000', 'YYYY-MM-DD HH24:MI:SS.US') AND
ds < TO_TIMESTAMP('1994-03-13 00:00:00.000000', 'YYYY-MM-DD HH24:MI:SS.US') AND
name IN ('Alex')
GROUP BY
DATE_TRUNC('year', ds)
ORDER BY
ds DESC
) AS query2
ON
query1.ds = query2.ds + INTERVAL '10 years'
ORDER BY
count1 DESC
LIMIT 10;
How to reproduce the bug
- create a line chart with a date range
- create a time comparison to the chart
- truncate the values by limiting the top rows
Screenshots/recordings
See above
Superset version
master / latest-dev
Python version
3.10
Node version
18 or greater
Browser
Chrome
Additional context
I am using the examples db on Postgres with the birth_names dataset. This issue will also exist on some chart types that also have server pagination as it will apply the same row limit.
Checklist
- I have searched Superset docs and Slack and didn’t find a solution to my problem.
- I have searched the GitHub issue tracker and didn’t find a similar bug report.
- I have checked Superset’s logs for errors and if I found a relevant Python stacktrace, I included it here as text in the “additional context” section.
About this issue
- Original URL
- State: closed
- Created 3 years ago
- Comments: 15 (15 by maintainers)
@eschutho BTW, I’ll open the SIP as soon as possible in the week.