Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions packages/cubejs-schema-compiler/src/adapter/BaseQuery.js
Original file line number Diff line number Diff line change
Expand Up @@ -2191,9 +2191,13 @@ export class BaseQuery {

let index;

index = this.dimensionsForSelect().findIndex(
d => equalIgnoreCase(d.dimension, id) || equalIgnoreCase(d.expressionName, id)
);
index = this.dimensionsForSelect()
// Not all time dimensions are used in select list, some are just filters,
// but they exist in this.timeDimensions, so need to filter them out
.filter(d => d.selectColumns())
.findIndex(
d => equalIgnoreCase(d.dimension, id) || equalIgnoreCase(d.expressionName, id)
);

if (index > -1) {
return index + 1;
Expand Down
57 changes: 57 additions & 0 deletions packages/cubejs-schema-compiler/test/unit/base-query.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
createCubeSchema,
createCubeSchemaWithCustomGranularities,
createCubeSchemaYaml,
createECommerceSchema,
createJoinedCubesSchema,
createSchemaYaml,
createSchemaYamlForGroupFilterParamsTests
Expand Down Expand Up @@ -360,6 +361,62 @@ describe('SQL Generation', () => {
const expectedParams = ['type_value', 'not_type_value', '3'];
expect(queryAndParams[1]).toEqual(expectedParams);
});

it('Simple query - order by for query with filtered timeDimension', async () => {
const compilersLocal = prepareYamlCompiler(
createSchemaYaml(createECommerceSchema())
);

await compilersLocal.compiler.compile();

let query = new PostgresQuery(compilersLocal, {
measures: [
'orders.count'
],
timeDimensions: [
{
dimension: 'orders.updated_at',
granularity: 'week'
},
{
dimension: 'orders.created_at',
dateRange: [
'2016-01-01',
'2018-01-01'
]
},
],
order: [{ id: 'orders.updated_at', desc: false }],
});

let queryAndParams = query.buildSqlAndParams();
expect(queryAndParams[0].includes('ORDER BY 1')).toBeTruthy();

// The order of time dimensions should have no effect on the `ORDER BY` clause

query = new PostgresQuery(compilersLocal, {
measures: [
'orders.count'
],
timeDimensions: [
{
dimension: 'orders.created_at',
dateRange: [
'2016-01-01',
'2018-01-01'
]
},
{
dimension: 'orders.updated_at',
granularity: 'week'
}
],
order: [{ id: 'orders.updated_at', desc: false }],
});

queryAndParams = query.buildSqlAndParams();
expect(queryAndParams[0].includes('ORDER BY 1')).toBeTruthy();
});
});

describe('Custom granularities', () => {
Expand Down
Loading