Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds support for IN operator as adhoc filter #986

Merged
merged 4 commits into from
Sep 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
aangelisc marked this conversation as resolved.
Show resolved Hide resolved

### 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\')' },
aangelisc marked this conversation as resolved.
Show resolved Hide resolved
] 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
Loading