Skip to content
This repository has been archived by the owner on Dec 11, 2022. It is now read-only.

Support group by scenarios for the auto interval #232

Closed
ptomasroos opened this issue Apr 9, 2020 · 1 comment · Fixed by #237
Closed

Support group by scenarios for the auto interval #232

ptomasroos opened this issue Apr 9, 2020 · 1 comment · Fixed by #237
Assignees
Labels
enhancement New feature or request
Milestone

Comments

@ptomasroos
Copy link
Contributor

ptomasroos commented Apr 9, 2020

Feature Request

Is your feature request related to a problem? Please describe.
The new introduced auto interval works great for plain queries, but in the case of a group by where you return multiple metrics it causes problems since the LIMIT clause won't be per group, rather for the complete set, which is undesired. #220 #225

Describe the solution you'd like

A query google give's me a set of example on how to set limit per group by in BQ. (From stackoverflow)

select t.*
from (select t.*,
             row_number() over (partition by group order by age desc) as seqnum
      from t
     ) t
where seqnum <= 2;

or

#standardSQL
SELECT
  `Group`,
  ARRAY_AGG(Age
            ORDER BY Age DESC LIMIT 2) AS oldest_people
FROM People
GROUP BY `Group`;

At this moment, when using $__timeGroup(ts, auto), the LIMIT would still be appended to the end
LIMIT 1742 for example.

q += " LIMIT " + options.maxDataPoints;

So we either need to find a way to expose $__maxDataPoints to the manual SQL builder?

select t.*
from (select t.*,
             row_number() over (partition by group order by age desc) as seqnum
      from t
     ) t
where seqnum <= $__maxDataPoints;

or we could use a $__limitMarker to insert where the LIMIT should be placed in the above query, aka make sure we can place the appropriate calculated limit into a sub query and not just at the end.

#standardSQL
SELECT
  `Group`,
  ARRAY_AGG(Age
            ORDER BY Age DESC $__limitMarker) AS oldest_people
FROM People
GROUP BY `Group`;

I prefer to use this later method. Using a $__limitMarker, would be very flexible IMHO and would work with arbitrary subqueries

@ghost
Copy link

ghost commented Apr 23, 2020

Essentially, it would be nice if the query parser, could break out each sub query into a "query" and add the limit that way.

--- Query 2
select t.*
from (
     -- Query 1
      select t.*, row_number() over (partition by group order by age desc) as seqnum
      from t
      Limit 2
     ) t 
Limit 1

Is this what you are after?

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
enhancement New feature or request
Projects
None yet
3 participants