Skip to content

Commit

Permalink
feat: Support multi-value filtering on same column through FILTER_PAR…
Browse files Browse the repository at this point in the history
…AMS (#2854) Thanks to @omab!

* Support multi-value filtering on same column

* Test multivalue filter on same column

* Simplify filter length check
  • Loading branch information
omab committed Aug 31, 2021
1 parent 3147e33 commit efc5745
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 16 deletions.
38 changes: 22 additions & 16 deletions packages/cubejs-schema-compiler/src/adapter/BaseQuery.js
Expand Up @@ -2405,26 +2405,32 @@ export class BaseQuery {
const cubeName = this.cubeEvaluator.cubeNameFromPath(name);
return new Proxy({ cube: cubeName }, {
get: (cubeNameObj, propertyName) => {
const filter =
allFilters.find(f => f.dimension === this.cubeEvaluator.pathFromArray([cubeNameObj.cube, propertyName]));
const filters =
allFilters.filter(f => f.dimension === this.cubeEvaluator.pathFromArray([cubeNameObj.cube, propertyName]));
return {
filter: (column) => {
const filterParams = filter && filter.filterParams();
if (
filterParams && filterParams.length
) {
if (typeof column === 'function') {
// eslint-disable-next-line prefer-spread
return column.apply(
null,
filterParams.map(this.paramAllocator.allocateParam.bind(this.paramAllocator))
);
} else {
return filter.conditionSql(column);
}
} else {
if (!filters.length) {
return '1 = 1';
}

return filters.map(filter => {
const filterParams = filter && filter.filterParams();
if (
filterParams && filterParams.length
) {
if (typeof column === 'function') {
// eslint-disable-next-line prefer-spread
return column.apply(
null,
filterParams.map(this.paramAllocator.allocateParam.bind(this.paramAllocator))
);
} else {
return filter.conditionSql(column);
}
} else {
return '1 = 1';
}
}).join(' AND ');
}
};
}
Expand Down
Expand Up @@ -240,6 +240,33 @@ describe('SQL Generation', () => {
}
})
cube('visitor_checkins_sources', {
sql: \`
select id, source from visitor_checkins WHERE \${FILTER_PARAMS.visitor_checkins_sources.source.filter('source')}
\`,
rewriteQueries: true,
joins: {
cards: {
relationship: 'hasMany',
sql: \`\${CUBE}.id = \${cards}.visitor_checkin_id\`
}
},
dimensions: {
id: {
type: 'number',
sql: 'id',
primaryKey: true
},
source: {
type: 'string',
sql: 'source'
}
}
})
cube('cards', {
sql: \`
select * from cards
Expand Down Expand Up @@ -1340,6 +1367,32 @@ describe('SQL Generation', () => {
])
);

it(
'contains multiple value filter',
() => runQueryTest({
measures: [],
dimensions: [
'visitor_checkins_sources.source'
],
timeDimensions: [],
timezone: 'America/Los_Angeles',
filters: [{
dimension: 'visitor_checkins_sources.source',
operator: 'contains',
values: ['goo']
}, {
dimension: 'visitor_checkins_sources.source',
operator: 'contains',
values: ['gle']
}],
order: [{
id: 'visitor_checkins_sources.source'
}]
}, [
{ visitor_checkins_sources__source: 'google' }
])
);

it(
'contains null filter',
() => runQueryTest({
Expand Down

0 comments on commit efc5745

Please sign in to comment.