This repository was archived by the owner on Dec 10, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 267
fix: implement extra filter logic #688
Merged
villebro
merged 6 commits into
apache-superset:master
from
preset-io:villebro/fix-extra-filters
Jul 21, 2020
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
7ae8dd8
fix: implement extra filter logic
villebro af67573
fix bugs and add tests
villebro 9562f19
remove redundant changes
villebro d06c817
improve types
villebro ad69bac
fix coverage
villebro 4ec35e9
improve codevov
villebro File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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; | ||
| } | ||
This file was deleted.
Oops, something went wrong.
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
druid_time_originis optional on the data type checked byisDruidFormData. Should a default be specified here, e.g.druid_time_origin: formData.druid_time_origin || someDefault,?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The backend expects most of these to be undefined if they haven't been given an explicit value (
time_grain,druid_time_originetc). So these should be safe to leave as is.