Join GitHub today
GitHub is home to over 50 million developers working together to host and review code, manage projects, and build software together.
Sign upSupport GRANULARITY in context variables #457
Comments
@diogo-andrade Hey Diogo! Thanks for posting this one! Could you please elaborate a little bit on your business use case? From the first sight your case is a good candidate for https://cube.dev/docs/subquery#top. |
Consider the following Calls table,
and Success Ratio = (total users with at least 2 calls performed and average points >= 50 / total users with at least 2 calls performed). What is expected is that be possible to calculate the Success Ratio for a selected period and granularity, and present the result in a bar chart. Therefore, if last week of January is selected with day granularity, following the Success Ratio formula, we expect the following result per day: Let me know if this is enough. |
@diogo-andrade Makes sense. Do you want to control for which period of time you expect to see at least 2 calls? Or you want it to be always aligned with selected interval of time? |
I want it to be always aligned with the selected date range. |
@diogo-andrade There's cube(`Expert`, {
// ...
dimensions: {
chatsCount: {
sql: `${Chats.count}`,
type: `number`,
subQuery: true,
propagateFiltersToSubQuery: true
}
}
}) |
Currently, roughly speaking we can define schemas that accesses the filter date range, during the SQL generation time, through
FILTER_PARAMS.<CUBE_NAME>.<TIME_TYPE_FILTER_NAME>.filter(expression)
. This allows us to define subqueries filtered by the date range coming from a request. As in the following example:When granularity is present in a coming request the final
SELECT
it's grouped by that granularity, as expected. However, if we want to use that grouping in a subquery, like the one in the example above, is not possible.To see what this limitation affects, lets consider the query A, and the following cube:
Query A:
Give me the experts Id with 5 or more chats made from Partner X for this year
Chats Stats Expert Id by Chats Stats Five Chats Expert and Partners External Id = X and Chats Stats Created At this Year
Will generate,
which will return the correct output. But, if we add 'month' granularity as example, will generate the following SQL:
which will not answer correctly the query "Give me the experts Id with 5 or more chats made from Partner X for this year and grouped by Month".
In order to answer the aforementioned query correctly, it should be possible to write fiveChatsExpert segment in the following way:
so that the following SQL be generated,
In this way, my suggestion is to have the granularity access through GRANULARITY context variables, or through FILTER_PARAMS.<CUBE_NAME>.<TIME_TYPE_FILTER_NAME>.granularity.
I believe that there are many other scenarios where access to granularity would be helpful.