Skip to content

Commit

Permalink
Adds support for values without parentheses
Browse files Browse the repository at this point in the history
  • Loading branch information
Kenterfie committed Sep 14, 2024
1 parent 0092145 commit fb1c1e2
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 3 deletions.
11 changes: 11 additions & 0 deletions src/data/adHocFilter.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,17 @@ describe('AdHocManager', () => {
);
});

it('apply ad hoc filter IN operator without parentheses', () => {
const ahm = new AdHocFilter();
ahm.setTargetTableFromQuery('SELECT * FROM foo');
const val = ahm.apply('SELECT stuff FROM foo WHERE col = test', [
{ key: 'key', operator: 'IN', value: '\'val1\', \'val2\'' },
] as AdHocVariableFilter[]);
expect(val).toEqual(
`SELECT stuff FROM foo WHERE col = test settings additional_table_filters={'foo' : ' key IN (\\'val1\\', \\'val2\\') '}`
);
});

it('apply ad hoc filter IN operator with integer values', () => {
const ahm = new AdHocFilter();
ahm.setTargetTableFromQuery('SELECT * FROM foo');
Expand Down
11 changes: 8 additions & 3 deletions src/data/adHocFilter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,15 @@ function isValid(filter: AdHocVariableFilter): boolean {

function escapeValueBasedOnOperator(s: string, operator: AdHocVariableFilterOperator): string {
if (operator === 'IN') {
return `${s}`.replace(/'/g, "\\'");
}
// Allow list of values without parentheses
if (s.length > 2 && s[0] !== '(' && s[s.length - 1] !== ')') {
s = `(${s})`
}

return `\\'${s}\\'`;
return s.replace(/'/g, "\\'");
} else {
return `\\'${s}\\'`;
}
}

function convertOperatorToClickHouseOperator(operator: AdHocVariableFilterOperator): string {
Expand Down

0 comments on commit fb1c1e2

Please sign in to comment.