Skip to content

feat(schema-compiler): Allow to specify td with granularity in REST API query order section #9630

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

Open
wants to merge 13 commits into
base: master
Choose a base branch
from

Conversation

KSDaemon
Copy link
Member

@KSDaemon KSDaemon commented May 29, 2025

This PR introduces the ability to specify granularity in the order section of the query and fixes the ordering when same time dimension is requested multiple times (with different granularities).

Imaging you have a query like this:

{
        measures: [
          'BigECommerce.rollingCountYTD',
        ],
        timeDimensions: [{
          dimension: 'BigECommerce.orderDate',
          granularity: 'month',
          dateRange: ['2020-01-01', '2020-12-31'],
        }, {
          dimension: 'BigECommerce.orderDate',
          granularity: 'week',
          dateRange: ['2020-01-01', '2020-12-31'],
        }],
        order: [
          ['BigECommerce.orderDate', 'asc'],
        ],
}

Note the order part.

Before now - the sorting will be done by 1st td with month granularity. But resulting rows still could jump around because week timestamps inside one month are not guaranteed to be ordered. Now column with the lowest granularity (week in the example) will be used for ordering.

Btw, this also works with custom granularities! ;)

To have full control, now you can specify the granularity in the order section like this:

{
        measures: [
          'visitors.count'
        ],
        timeDimensions: [{
          dimension: 'visitors.createdAt',
          granularity: 'week',
          dateRange: ['2020-01-01', '2020-12-31'],
        }, {
          dimension: 'visitors.createdAt',
          granularity: 'month',
          dateRange: ['2020-01-01', '2020-12-31'],
        }, {
          dimension: 'visitors.createdAt',
          dateRange: ['2020-01-01', '2020-12-31'],
        }],
        order: [{ id: 'visitors.createdAt.month', desc: false }],
        timezone: 'America/Los_Angeles'
}

or like this:

{
        measures: [
          'visitors.count'
        ],
        timeDimensions: [{
          dimension: 'visitors.createdAt',
          granularity: 'week',
          dateRange: ['2020-01-01', '2020-12-31'],
        }, {
          dimension: 'visitors.createdAt',
          granularity: 'month',
          dateRange: ['2020-01-01', '2020-12-31'],
        }, {
          dimension: 'visitors.createdAt',
          granularity: 'fiscal_quarter',
          dateRange: ['2020-01-01', '2020-12-31'],
        }, {
          dimension: 'visitors.createdAt',
          dateRange: ['2020-01-01', '2020-12-31'],
        }],
        order: [{ id: 'visitors.createdAt.fiscal_quarter', desc: false }],
        timezone: 'America/Los_Angeles'
}

Check List

  • Tests have been run in packages where changes made if available
  • Linter has been run for changed code
  • Tests for the changes have been added if not covered yet
  • Docs have been added / updated if required

KSDaemon added 2 commits May 29, 2025 16:10
…PI query order section

This also fix ordering sql building to use the lowest granularity if there are multiple granularities for the same td provided in the query
@KSDaemon KSDaemon marked this pull request as ready for review May 29, 2025 17:40
@KSDaemon KSDaemon requested review from a team as code owners May 29, 2025 17:40
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant