Skip to content

Commit bfe76bf

Browse files
authored
fix: Cube not found for path due to FILTER_PARAMS are used in members (#5417)
1 parent 041f591 commit bfe76bf

File tree

3 files changed

+39
-4
lines changed

3 files changed

+39
-4
lines changed

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

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2731,7 +2731,7 @@ class BaseQuery {
27312731

27322732
static emptyParametrizedContextSymbols(cubeEvaluator, allocateParam) {
27332733
return {
2734-
filterParams: BaseQuery.filterProxyFromAllFilters({}, cubeEvaluator, allocateParam),
2734+
filterParams: BaseQuery.filterProxyFromAllFilters(null, cubeEvaluator, allocateParam),
27352735
sqlUtils: {
27362736
convertTz: (field) => field,
27372737
},
@@ -2792,14 +2792,16 @@ class BaseQuery {
27922792
if (name === '_objectWithResolvedProperties') {
27932793
return true;
27942794
}
2795-
const cubeName = cubeEvaluator.cubeNameFromPath(name);
2795+
// allFilters is null whenever it's used to test if the member is owned by cube so it should always render to `1 = 1`
2796+
// and do not check cube validity as it's part of compilation step.
2797+
const cubeName = allFilters && cubeEvaluator.cubeNameFromPath(name);
27962798
return new Proxy({ cube: cubeName }, {
27972799
get: (cubeNameObj, propertyName) => {
27982800
const filters =
2799-
allFilters.filter(f => f.dimension === cubeEvaluator.pathFromArray([cubeNameObj.cube, propertyName]));
2801+
allFilters?.filter(f => f.dimension === cubeEvaluator.pathFromArray([cubeNameObj.cube, propertyName]));
28002802
return {
28012803
filter: (column) => {
2802-
if (!filters.length) {
2804+
if (!filters || !filters.length) {
28032805
return '1 = 1';
28042806
}
28052807

packages/cubejs-schema-compiler/src/compiler/CubeEvaluator.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,8 @@ export class CubeEvaluator extends CubeSymbols {
101101
if (member.sql && !member.subQuery) {
102102
const funcArgs = this.funcArguments(member.sql);
103103
const cubeReferences = this.collectUsedCubeReferences(cube.name, member.sql);
104+
// We won't check for FILTER_PARAMS here as it shouldn't affect ownership and it should obey the same reference rules.
105+
// To affect ownership FILTER_PARAMS can be declared as `${FILTER_PARAMS.Foo.bar.filter(`${Foo.bar}`)}`.
104106
if (funcArgs.length > 0 && cubeReferences.length === 0) {
105107
ownedByCube = false;
106108
}

packages/cubejs-schema-compiler/test/integration/postgres/cube-views.test.ts

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@ cube(\`Orders\`, {
1212
UNION ALL
1313
SELECT 2 as id, 2 as product_id, 'completed' as status, '2022-01-02T00:00:00.000Z'::timestamptz as created_at
1414
\`,
15+
16+
refreshKey: {
17+
sql: \`SELECT MAX(created_at) FROM \${Orders.sql()} orders WHERE \${FILTER_PARAMS.Orders.createdAt.filter('created_at')}\`
18+
},
1519
1620
preAggregations: {
1721
countByProductName: {
@@ -65,11 +69,22 @@ cube(\`Orders\`, {
6569
type: \`time\`
6670
},
6771
72+
productId: {
73+
sql: \`product_id\`,
74+
type: \`number\`,
75+
},
76+
6877
productAndCategory: {
6978
sql: \`\${Products.name} || '_' || \${Products.ProductCategories.name}\`,
7079
type: \`string\`
7180
},
7281
},
82+
83+
segments: {
84+
potatoOnly: {
85+
sql: \`\${CUBE}.product_id = 2 AND \${FILTER_PARAMS.Orders.productId.filter(\`\${CUBE.productId}\`)}\`,
86+
},
87+
},
7388
7489
dataSource: \`default\`
7590
});
@@ -217,6 +232,8 @@ view(\`OrdersView\`, {
217232

218233
console.log(query.buildSqlAndParams());
219234

235+
console.log(query.cacheKeyQueries());
236+
220237
const res = await dbRunner.evaluateQueryWithPreAggregations(query);
221238
console.log(JSON.stringify(res));
222239

@@ -328,4 +345,18 @@ view(\`OrdersView\`, {
328345
}, {
329346
orders_view__product_category: null,
330347
}]));
348+
349+
it('segment with filter params', async () => runQueryTest({
350+
measures: ['Orders.count'],
351+
segments: [
352+
'Orders.potatoOnly'
353+
],
354+
filters: [{
355+
member: 'Orders.productId',
356+
operator: 'equals',
357+
values: ['2'],
358+
}]
359+
}, [{
360+
orders__count: '1',
361+
}]));
331362
});

0 commit comments

Comments
 (0)