Skip to content

Commit

Permalink
fix(query): equals operator, duplicate filters and time range endpoin…
Browse files Browse the repository at this point in the history
…ts (#700)

* fix(query): correct equals operator and duplicate filters

* add time range endpoints
  • Loading branch information
villebro authored and zhaoyongjie committed Nov 26, 2021
1 parent 018da38 commit 0e16cbb
Show file tree
Hide file tree
Showing 9 changed files with 69 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,18 +36,18 @@ export default function buildQueryObject<T extends QueryFormData>(formData: T):
const { metrics, groupby, columns } = extractQueryFields(residualFormData, queryFields);
const groupbySet = new Set([...columns, ...groupby]);

const extraFilters = extractExtras(formData);
const extras = extractExtras(formData);
const extrasAndfilters = processFilters({
...formData,
...extraFilters,
...extras,
});

return {
time_range,
since,
until,
granularity,
...extraFilters,
...extras,
...extrasAndfilters,
groupby: processGroupby(Array.from(groupbySet)),
is_timeseries: groupbySet.has(DTTM_ALIAS),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,5 +43,10 @@ export default function extractExtras(formData: QueryFormData): Partial<QueryObj
delete partialQueryObject.granularity_sqla;
delete partialQueryObject.time_grain_sqla;
}

// map time range endpoints:
if (formData.time_range_endpoints)
partialQueryObject.extras.time_range_endpoints = formData.time_range_endpoints;

return partialQueryObject;
}
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ export default function processFilters(formData: QueryFormData) {

return {
// eslint-disable-next-line @typescript-eslint/no-unsafe-call
filters: (formData.filters || []).concat(simpleWhere),
filters: simpleWhere,
extras,
};
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
const UNARY_OPERATORS = ['IS NOT NULL', 'IS NULL'] as const;

/** List of operators that require another operand that is a single value */
const BINARY_OPERATORS = ['=', '!=', '>', '<', '>=', '<=', 'LIKE', 'REGEX'] as const;
const BINARY_OPERATORS = ['==', '!=', '>', '<', '>=', '<=', 'LIKE', 'REGEX'] as const;

/** List of operators that require another operand that is a set */
const SET_OPERATORS = ['IN', 'NOT IN'] as const;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ export type QueryFormResidualData = {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
[key: string]: any;
};
export type TimeRangeEndpoint = 'unknown' | 'inclusive' | 'exclusive';
export type TimeRangeEndpoints = [TimeRangeEndpoint, TimeRangeEndpoint];

// Currently only Binary and Set filters are supported
export type QueryFields = {
Expand Down Expand Up @@ -69,6 +71,7 @@ export type BaseFormData = {
result_format?: string;
result_type?: string;
queryFields?: QueryFields;
time_range_endpoints?: TimeRangeEndpoints;
} & TimeRange &
QueryFormResidualData;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,12 @@ describe('convertFilter', () => {
expressionType: 'SIMPLE',
clause: 'WHERE',
subject: 'topping',
operator: '=',
operator: '==',
comparator: 'grass jelly',
}),
).toEqual({
col: 'topping',
op: '=',
op: '==',
val: 'grass jelly',
});
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,42 +9,44 @@ describe('extractExtras', () => {
filters: [
{
col: 'gender',
op: '=',
op: '==',
val: 'girl',
},
],
};

it('should override formData with double underscored date options', () => {
it('should populate time range endpoints and override formData with double underscored date options', () => {
expect(
extractExtras({
...baseQueryFormData,
time_range_endpoints: ['inclusive', 'exclusive'],
extra_filters: [
{
col: '__time_col',
op: '=',
op: '==',
val: 'ds2',
},
{
col: '__time_grain',
op: '=',
op: '==',
val: 'PT5M',
},
{
col: '__time_range',
op: '=',
op: '==',
val: '2009-07-17T00:00:00 : 2020-07-17T00:00:00',
},
],
}),
).toEqual({
extras: {
time_grain_sqla: 'PT5M',
time_range_endpoints: ['inclusive', 'exclusive'],
},
filters: [
{
col: 'gender',
op: '=',
op: '==',
val: 'girl',
},
],
Expand Down Expand Up @@ -72,7 +74,7 @@ describe('extractExtras', () => {
filters: [
{
col: 'gender',
op: '=',
op: '==',
val: 'girl',
},
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,48 @@ describe('processFilters', () => {
).toEqual({});
});

it('should merge simple adhoc_filters and filters', () => {
expect(
processFilters({
granularity: 'something',
viz_type: 'custom',
datasource: 'boba',
filters: [
{
col: 'name',
op: '==',
val: 'Aaron',
},
],
adhoc_filters: [
{
expressionType: 'SIMPLE',
clause: 'WHERE',
subject: 'gender',
operator: 'IS NOT NULL',
},
],
}),
).toEqual({
extras: {
having: '',
having_druid: [],
where: '',
},
filters: [
{
col: 'name',
op: '==',
val: 'Aaron',
},
{
col: 'gender',
op: 'IS NOT NULL',
},
],
});
});

it('should handle an empty array', () => {
expect(
processFilters({
Expand Down Expand Up @@ -47,7 +89,7 @@ describe('processFilters', () => {
expressionType: 'SIMPLE',
clause: 'WHERE',
subject: 'milk',
operator: '=',
operator: '==',
comparator: 'almond',
},
{
Expand Down Expand Up @@ -110,7 +152,7 @@ describe('processFilters', () => {
},
{
col: 'milk',
op: '=',
op: '==',
val: 'almond',
},
],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ describe('Filter type guards', () => {
expressionType: 'SIMPLE',
clause: 'WHERE',
subject: 'tea',
operator: '=',
operator: '==',
comparator: 'matcha',
}),
).toEqual(false);
Expand Down

0 comments on commit 0e16cbb

Please sign in to comment.