Skip to content

Commit 00a589f

Browse files
authored
fix(schema-compiler): Fix filtering by time measure (#9544)
* fix(schema-compiler): Fix filtering by time measure * tests
1 parent 7c9f298 commit 00a589f

File tree

2 files changed

+54
-0
lines changed

2 files changed

+54
-0
lines changed

packages/cubejs-schema-compiler/src/adapter/BaseFilter.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,17 @@ export class BaseFilter extends BaseDimension {
9090
}
9191
}
9292

93+
/**
94+
* BaseFilter inherits from BaseDimension while Filter may be measure-based !!
95+
*/
96+
public override dateFieldType() {
97+
if (this.measure) {
98+
return this.measureDefinition().type; // There is no fieldType in measure, but it seems that it's enough
99+
} else {
100+
return this.dimensionDefinition().fieldType;
101+
}
102+
}
103+
93104
public cube() {
94105
return this.query.cubeEvaluator.cubeFromPath(this.measure || this.dimension);
95106
}

packages/cubejs-schema-compiler/test/integration/postgres/sql-generation.test.ts

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -240,6 +240,10 @@ describe('SQL Generation', () => {
240240
sql: \`\${revenue}\`,
241241
type: 'sum',
242242
group_by: [id]
243+
},
244+
min_created_at: {
245+
type: 'time',
246+
sql: 'MIN(created_at)'
243247
}
244248
},
245249
@@ -1589,6 +1593,45 @@ SELECT 1 AS revenue, cast('2024-01-01' AS timestamp) as time UNION ALL
15891593
});
15901594
});
15911595

1596+
it('having filter (time measure)', async () => {
1597+
await compiler.compile();
1598+
1599+
const query = new PostgresQuery({ joinGraph, cubeEvaluator, compiler }, {
1600+
measures: [
1601+
'visitors.min_created_at'
1602+
],
1603+
dimensions: [
1604+
'visitors.source'
1605+
],
1606+
timeDimensions: [],
1607+
timezone: 'America/Los_Angeles',
1608+
filters: [{
1609+
dimension: 'visitors.min_created_at',
1610+
operator: 'inDateRange',
1611+
values: ['2017-01-01', '2018-01-01']
1612+
}],
1613+
order: [{
1614+
id: 'visitors.source'
1615+
}]
1616+
});
1617+
1618+
console.log(query.buildSqlAndParams());
1619+
1620+
return dbRunner.testQuery(query.buildSqlAndParams()).then(res => {
1621+
console.log(JSON.stringify(res));
1622+
expect(res).toEqual([
1623+
{
1624+
visitors__min_created_at: '2017-01-06T00:00:00.000Z',
1625+
visitors__source: 'google',
1626+
},
1627+
{
1628+
visitors__min_created_at: '2017-01-03T00:00:00.000Z',
1629+
visitors__source: 'some',
1630+
},
1631+
]);
1632+
});
1633+
});
1634+
15921635
it('having filter without measure', async () => {
15931636
await compiler.compile();
15941637

0 commit comments

Comments
 (0)