Skip to content

Commit

Permalink
fix(schema-compiler): Athena, Mysql and BigQuery doesn't respect mult…
Browse files Browse the repository at this point in the history
…iple contains filter
  • Loading branch information
paveltiunov committed Apr 17, 2019
1 parent ca092f9 commit 0a8f324
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 31 deletions.
21 changes: 8 additions & 13 deletions packages/cubejs-schema-compiler/adapter/BaseFilter.js
Original file line number Diff line number Diff line change
Expand Up @@ -101,26 +101,21 @@ class BaseFilter extends BaseDimension {
}

containsWhere(column) {
if (this.isArrayValues()) {
return this.likeOr(column);
}

return `${column} ILIKE '%' || ? || '%'`;
return this.likeOr(column);
}

notContainsWhere(column) {
if (this.isArrayValues()) {
return this.likeOr(column, true);
}

return `${column} NOT ILIKE '%' || ? || '%' OR ${column} IS NULL`;
return this.likeOr(column, true);
}

likeOr(column, not) {
const basePart = `${column} ${not ? 'NOT' : ''} ILIKE '%' || ? || '%'`;
const multiplePart = `${not ? 'AND' : 'OR'} ${basePart}`;
const basePart = this.likeIgnoreCase(column, not);
const nullCheck = `${not ? ` OR ${column} IS NULL` : ''}`;
return `${basePart} ${join(' ', repeat(multiplePart, this.values.length - 1))} ${nullCheck}`;
return `${join(not ? ' AND ' : ' OR ', repeat(basePart, this.values.length))}${nullCheck}`;
}

likeIgnoreCase(column, not) {
return `${column}${not ? ' NOT' : ''} ILIKE '%' || ? || '%'`;
}

equalsWhere(column) {
Expand Down
8 changes: 2 additions & 6 deletions packages/cubejs-schema-compiler/adapter/BigqueryQuery.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,8 @@ const GRANULARITY_TO_INTERVAL = {
};

class BigqueryFilter extends BaseFilter {
containsWhere(column) {
return `LOWER(${column}) LIKE CONCAT('%', LOWER(?) ,'%')`;
}

notContainsWhere(column) {
return `LOWER(${column}) NOT LIKE CONCAT('%', LOWER(?) ,'%') OR ${column} IS NULL`;
likeIgnoreCase(column, not) {
return `LOWER(${column})${not ? ' NOT' : ''} LIKE CONCAT('%', LOWER(?) ,'%')`;
}

castParameter() {
Expand Down
8 changes: 2 additions & 6 deletions packages/cubejs-schema-compiler/adapter/MysqlQuery.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,8 @@ const GRANULARITY_TO_INTERVAL = {
};

class MysqlFilter extends BaseFilter {
containsWhere(column) {
return `${column} LIKE CONCAT('%', ?, '%')`;
}

notContainsWhere(column) {
return `${column} NOT LIKE CONCAT('%', ?, '%') OR ${column} IS NULL`;
likeIgnoreCase(column, not) {
return `${column}${not ? ' NOT' : ''} LIKE CONCAT('%', ?, '%')`;
}
}

Expand Down
8 changes: 2 additions & 6 deletions packages/cubejs-schema-compiler/adapter/PrestodbQuery.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,8 @@ const GRANULARITY_TO_INTERVAL = {
};

class PrestodbFilter extends BaseFilter {
containsWhere(column) {
return `LOWER(${column}) LIKE CONCAT('%', LOWER(?) ,'%')`;
}

notContainsWhere(column) {
return `LOWER(${column}) NOT LIKE CONCAT('%', LOWER(?) ,'%') OR ${column} IS NULL`;
likeIgnoreCase(column, not) {
return `LOWER(${column})${not ? ' NOT' : ''} LIKE CONCAT('%', LOWER(?) ,'%')`;
}

castParameter() {
Expand Down
22 changes: 22 additions & 0 deletions packages/cubejs-schema-compiler/test/GraphBuilderTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -1236,6 +1236,28 @@ describe('JoinGraph', () => {
])
);

it(
'contains filter',
() => runQueryTest({
measures: [],
dimensions: [
'visitors.source'
],
timeDimensions: [],
timezone: 'America/Los_Angeles',
filters: [{
dimension: 'visitor_checkins.source',
operator: 'contains',
values: ['goo']
}],
order: [{
id: 'visitors.source'
}]
}, [
{ 'visitors.source': 'some' }
])
);

it('year granularity', () =>
runQueryTest({
measures: [
Expand Down

0 comments on commit 0a8f324

Please sign in to comment.