Skip to content

Commit

Permalink
Merge branch 'main' into dependabot/npm_and_yarn/playwright/test-1.47.2
Browse files Browse the repository at this point in the history
  • Loading branch information
aangelisc authored Sep 25, 2024
2 parents 323f3aa + 821d81d commit 89ac48c
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

- Added "Labels" column selector to the log query builder
- Datasource OTel configuration will now set default table names for logs and traces.
- Added support for IN operator in adhoc filters

### Fixes

Expand Down
33 changes: 33 additions & 0 deletions src/data/adHocFilter.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,39 @@ describe('AdHocManager', () => {
);
});

it('apply ad hoc filter IN operator with string values', () => {
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 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');
const val = ahm.apply('SELECT stuff FROM foo WHERE col = test', [
{ key: 'key', operator: 'IN', value: '(1, 2, 3)' },
] as AdHocVariableFilter[]);
expect(val).toEqual(
`SELECT stuff FROM foo WHERE col = test settings additional_table_filters={'foo' : ' key IN (1, 2, 3) '}`
);
});

it('does not apply an adhoc filter without "operator"', () => {
const ahm = new AdHocFilter();
ahm.setTargetTableFromQuery('SELECT * FROM foo');
Expand Down
17 changes: 15 additions & 2 deletions src/data/adHocFilter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ export class AdHocFilter {
})
.map((f, i) => {
const key = f.key.includes('.') ? f.key.split('.')[1] : f.key;
const value = `\\'${f.value}\\'`;
const value = escapeValueBasedOnOperator(f.value, f.operator);
const condition = i !== adHocFilters.length - 1 ? (f.condition ? f.condition : 'AND') : '';
const operator = convertOperatorToClickHouseOperator(f.operator);
return ` ${key} ${operator} ${value} ${condition}`;
Expand All @@ -58,6 +58,19 @@ function isValid(filter: AdHocVariableFilter): boolean {
return filter.key !== undefined && filter.operator !== undefined && filter.value !== undefined;
}

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

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

function convertOperatorToClickHouseOperator(operator: AdHocVariableFilterOperator): string {
if (operator === '=~') {
return 'ILIKE';
Expand All @@ -68,7 +81,7 @@ function convertOperatorToClickHouseOperator(operator: AdHocVariableFilterOperat
return operator;
}

type AdHocVariableFilterOperator = '>' | '<' | '=' | '!=' | '=~' | '!~';
type AdHocVariableFilterOperator = '>' | '<' | '=' | '!=' | '=~' | '!~' | 'IN';

export type AdHocVariableFilter = {
key: string;
Expand Down

0 comments on commit 89ac48c

Please sign in to comment.