forked from apache/superset
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: implement extra filter logic (apache#688)
* fix: implement extra filter logic * fix bugs and add tests * remove redundant changes * improve types * fix coverage * improve codevov
- Loading branch information
1 parent
dd874e0
commit fa0868b
Showing
9 changed files
with
209 additions
and
68 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
47 changes: 47 additions & 0 deletions
47
...rontend/temporary_superset_ui/superset-ui/packages/superset-ui-query/src/extractExtras.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
/* eslint-disable camelcase */ | ||
import { isDruidFormData, QueryFormData } from './types/QueryFormData'; | ||
import { QueryObject } from './types/Query'; | ||
|
||
export default function extractExtras(formData: QueryFormData): Partial<QueryObject> { | ||
const partialQueryObject: Partial<QueryObject> = { | ||
filters: formData.filters || [], | ||
extras: formData.extras || {}, | ||
}; | ||
|
||
const reservedColumnsToQueryField: Record<string, keyof QueryObject> = { | ||
__time_range: 'time_range', | ||
__time_col: 'granularity_sqla', | ||
__time_grain: 'time_grain_sqla', | ||
__time_origin: 'druid_time_origin', | ||
__granularity: 'granularity', | ||
}; | ||
|
||
(formData.extra_filters || []).forEach(filter => { | ||
if (filter.col in reservedColumnsToQueryField) { | ||
const queryField = reservedColumnsToQueryField[filter.col]; | ||
partialQueryObject[queryField] = filter.val; | ||
} else { | ||
// @ts-ignore | ||
partialQueryObject.filters.push(filter); | ||
} | ||
}); | ||
|
||
// map to undeprecated names and remove deprecated fields | ||
if (isDruidFormData(formData) && !partialQueryObject.druid_time_origin) { | ||
partialQueryObject.extras = { | ||
druid_time_origin: formData.druid_time_origin, | ||
}; | ||
delete partialQueryObject.druid_time_origin; | ||
} else { | ||
// SQL | ||
partialQueryObject.extras = { | ||
...partialQueryObject.extras, | ||
time_grain_sqla: partialQueryObject.time_grain_sqla || formData.time_grain_sqla, | ||
}; | ||
partialQueryObject.granularity = | ||
partialQueryObject.granularity_sqla || formData.granularity || formData.granularity_sqla; | ||
delete partialQueryObject.granularity_sqla; | ||
delete partialQueryObject.time_grain_sqla; | ||
} | ||
return partialQueryObject; | ||
} |
17 changes: 0 additions & 17 deletions
17
...rontend/temporary_superset_ui/superset-ui/packages/superset-ui-query/src/processExtras.ts
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
87 changes: 87 additions & 0 deletions
87
...d/temporary_superset_ui/superset-ui/packages/superset-ui-query/test/extractExtras.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
import extractExtras from '../src/extractExtras'; | ||
|
||
describe('extractExtras', () => { | ||
const baseQueryFormData = { | ||
datasource: '1__table', | ||
granularity_sqla: 'ds', | ||
time_grain_sqla: 'PT1M', | ||
viz_type: 'my_viz', | ||
filters: [ | ||
{ | ||
col: 'gender', | ||
op: '=', | ||
val: 'girl', | ||
}, | ||
], | ||
}; | ||
|
||
it('should override formData with double underscored date options', () => { | ||
expect( | ||
extractExtras({ | ||
...baseQueryFormData, | ||
extra_filters: [ | ||
{ | ||
col: '__time_col', | ||
op: '=', | ||
val: 'ds2', | ||
}, | ||
{ | ||
col: '__time_grain', | ||
op: '=', | ||
val: 'PT5M', | ||
}, | ||
{ | ||
col: '__time_range', | ||
op: '=', | ||
val: '2009-07-17T00:00:00 : 2020-07-17T00:00:00', | ||
}, | ||
], | ||
}), | ||
).toEqual({ | ||
extras: { | ||
time_grain_sqla: 'PT5M', | ||
}, | ||
filters: [ | ||
{ | ||
col: 'gender', | ||
op: '=', | ||
val: 'girl', | ||
}, | ||
], | ||
granularity: 'ds2', | ||
time_range: '2009-07-17T00:00:00 : 2020-07-17T00:00:00', | ||
}); | ||
}); | ||
|
||
it('should create regular filters from non-reserved columns', () => { | ||
expect( | ||
extractExtras({ | ||
...baseQueryFormData, | ||
extra_filters: [ | ||
{ | ||
col: 'name', | ||
op: 'IN', | ||
val: ['Eve', 'Evelyn'], | ||
}, | ||
], | ||
}), | ||
).toEqual({ | ||
extras: { | ||
time_grain_sqla: 'PT1M', | ||
}, | ||
filters: [ | ||
{ | ||
col: 'gender', | ||
op: '=', | ||
val: 'girl', | ||
}, | ||
{ | ||
col: 'name', | ||
op: 'IN', | ||
val: ['Eve', 'Evelyn'], | ||
}, | ||
], | ||
granularity: 'ds', | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.