Skip to content

Commit 1c8ce4f

Browse files
authored
fix(schema-compiler): Fix ORDER BY clause generation for queries with td with filters (#9296)
* fix(schema-compiler): Fix ORDER BY clause generation for queries with td with filters * add tests
1 parent add4822 commit 1c8ce4f

File tree

2 files changed

+64
-3
lines changed

2 files changed

+64
-3
lines changed

packages/cubejs-schema-compiler/src/adapter/BaseQuery.js

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2191,9 +2191,13 @@ export class BaseQuery {
21912191

21922192
let index;
21932193

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

21982202
if (index > -1) {
21992203
return index + 1;

packages/cubejs-schema-compiler/test/unit/base-query.test.ts

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import {
55
createCubeSchema,
66
createCubeSchemaWithCustomGranularities,
77
createCubeSchemaYaml,
8+
createECommerceSchema,
89
createJoinedCubesSchema,
910
createSchemaYaml,
1011
createSchemaYamlForGroupFilterParamsTests
@@ -360,6 +361,62 @@ describe('SQL Generation', () => {
360361
const expectedParams = ['type_value', 'not_type_value', '3'];
361362
expect(queryAndParams[1]).toEqual(expectedParams);
362363
});
364+
365+
it('Simple query - order by for query with filtered timeDimension', async () => {
366+
const compilersLocal = prepareYamlCompiler(
367+
createSchemaYaml(createECommerceSchema())
368+
);
369+
370+
await compilersLocal.compiler.compile();
371+
372+
let query = new PostgresQuery(compilersLocal, {
373+
measures: [
374+
'orders.count'
375+
],
376+
timeDimensions: [
377+
{
378+
dimension: 'orders.updated_at',
379+
granularity: 'week'
380+
},
381+
{
382+
dimension: 'orders.created_at',
383+
dateRange: [
384+
'2016-01-01',
385+
'2018-01-01'
386+
]
387+
},
388+
],
389+
order: [{ id: 'orders.updated_at', desc: false }],
390+
});
391+
392+
let queryAndParams = query.buildSqlAndParams();
393+
expect(queryAndParams[0].includes('ORDER BY 1')).toBeTruthy();
394+
395+
// The order of time dimensions should have no effect on the `ORDER BY` clause
396+
397+
query = new PostgresQuery(compilersLocal, {
398+
measures: [
399+
'orders.count'
400+
],
401+
timeDimensions: [
402+
{
403+
dimension: 'orders.created_at',
404+
dateRange: [
405+
'2016-01-01',
406+
'2018-01-01'
407+
]
408+
},
409+
{
410+
dimension: 'orders.updated_at',
411+
granularity: 'week'
412+
}
413+
],
414+
order: [{ id: 'orders.updated_at', desc: false }],
415+
});
416+
417+
queryAndParams = query.buildSqlAndParams();
418+
expect(queryAndParams[0].includes('ORDER BY 1')).toBeTruthy();
419+
});
363420
});
364421

365422
describe('Custom granularities', () => {

0 commit comments

Comments
 (0)