Skip to content

Commit

Permalink
[adhoc-filters] Adding adhoc-filters to all viz types (#5206)
Browse files Browse the repository at this point in the history
  • Loading branch information
john-bodley committed Jun 18, 2018
1 parent 1fc4ee0 commit d483ed1
Show file tree
Hide file tree
Showing 28 changed files with 1,012 additions and 980 deletions.
Empty file added babel-node
Empty file.
743 changes: 743 additions & 0 deletions package-lock.json

Large diffs are not rendered by default.

24 changes: 0 additions & 24 deletions superset/assets/backendSync.json
Expand Up @@ -1683,18 +1683,6 @@
"renderTrigger": true,
"default": ""
},
"where": {
"type": "TextControl",
"label": "Custom WHERE clause",
"default": "",
"description": "The text in this box gets included in your query's WHERE clause, as an AND to other criteria. You can include complex expression, parenthesis and anything else supported by the backend it is directed towards."
},
"having": {
"type": "TextControl",
"label": "Custom HAVING clause",
"default": "",
"description": "The text in this box gets included in your query's HAVING clause, as an AND to other criteria. You can include complex expression, parenthesis and anything else supported by the backend it is directed towards."
},
"compare_lag": {
"type": "TextControl",
"label": "Comparison Period Lag",
Expand Down Expand Up @@ -2628,25 +2616,13 @@
"default": "",
"description": "Labels for the marker lines"
},
"filters": {
"type": "FilterControl",
"label": "",
"default": [],
"description": ""
},
"annotation_layers": {
"type": "AnnotationLayerControl",
"label": "",
"default": [],
"description": "Annotation Layers",
"renderTrigger": true
},
"having_filters": {
"type": "FilterControl",
"label": "",
"default": [],
"description": ""
},
"slice_id": {
"type": "HiddenControl",
"label": "Slice ID",
Expand Down
Expand Up @@ -11,7 +11,15 @@ import TooltipWrapper from '../../../src/components/TooltipWrapper';

const defaultProps = {
origFormData: {
filters: [{ col: 'a', op: '==', val: 'hello' }],
adhoc_filters: [
{
clause: 'WHERE',
comparator: 'hello',
expressionType: 'SIMPLE',
operator: '==',
subject: 'a',
},
],
y_axis_bounds: [10, 20],
column_collection: [{ 1: 'a', b: ['6', 'g'] }],
bool: false,
Expand All @@ -21,7 +29,15 @@ const defaultProps = {
ever: { a: 'b', c: 'd' },
},
currentFormData: {
filters: [{ col: 'b', op: 'in', val: ['hello', 'my', 'name'] }],
adhoc_filters: [
{
clause: 'WHERE',
comparator: ['hello', 'my', 'name'],
expressionType: 'SIMPLE',
operator: 'in',
subject: 'b',
},
],
y_axis_bounds: [15, 16],
column_collection: [{ 1: 'a', b: [9, '15'], t: 'gggg' }],
bool: true,
Expand All @@ -33,9 +49,25 @@ const defaultProps = {
};

const expectedDiffs = {
filters: {
before: [{ col: 'a', op: '==', val: 'hello' }],
after: [{ col: 'b', op: 'in', val: ['hello', 'my', 'name'] }],
adhoc_filters: {
before: [
{
clause: 'WHERE',
comparator: 'hello',
expressionType: 'SIMPLE',
operator: '==',
subject: 'a',
},
],
after: [
{
clause: 'WHERE',
comparator: ['hello', 'my', 'name'],
expressionType: 'SIMPLE',
operator: 'in',
subject: 'b',
},
],
},
y_axis_bounds: {
before: [10, 20],
Expand Down Expand Up @@ -211,25 +243,49 @@ describe('AlteredSliceTag', () => {
});

it('returns "[]" for empty filters', () => {
expect(wrapper.instance().formatValue([], 'filters')).to.equal('[]');
expect(wrapper.instance().formatValue([], 'adhoc_filters')).to.equal('[]');
});

it('correctly formats filters with array values', () => {
const filters = [
{ col: 'a', op: 'in', val: ['1', 'g', '7', 'ho'] },
{ col: 'b', op: 'not in', val: ['hu', 'ho', 'ha'] },
{
clause: 'WHERE',
comparator: ['1', 'g', '7', 'ho'],
expressionType: 'SIMPLE',
operator: 'in',
subject: 'a',
},
{
clause: 'WHERE',
comparator: ['hu', 'ho', 'ha'],
expressionType: 'SIMPLE',
operator: 'not in',
subject: 'b',
},
];
const expected = 'a in [1, g, 7, ho], b not in [hu, ho, ha]';
expect(wrapper.instance().formatValue(filters, 'filters')).to.equal(expected);
expect(wrapper.instance().formatValue(filters, 'adhoc_filters')).to.equal(expected);
});

it('correctly formats filters with string values', () => {
const filters = [
{ col: 'a', op: '==', val: 'gucci' },
{ col: 'b', op: 'LIKE', val: 'moshi moshi' },
{
clause: 'WHERE',
comparator: 'gucci',
expressionType: 'SIMPLE',
operator: '==',
subject: 'a',
},
{
clause: 'WHERE',
comparator: 'moshi moshi',
expressionType: 'SIMPLE',
operator: 'LIKE',
subject: 'b',
},
];
const expected = 'a == gucci, b LIKE moshi moshi';
expect(wrapper.instance().formatValue(filters, 'filters')).to.equal(expected);
expect(wrapper.instance().formatValue(filters, 'adhoc_filters')).to.equal(expected);
});
});
});
Expand Up @@ -33,18 +33,9 @@ const columns = [
{ type: 'DOUBLE', column_name: 'value' },
];

const legacyFilter = { col: 'value', op: '>', val: '5' };
const legacyHavingFilter = { col: 'SUM(value)', op: '>', val: '10' };
const whereFilterText = 'target in (\'alpha\')';
const havingFilterText = 'SUM(value) < 20';

const formData = {
filters: [legacyFilter],
having: havingFilterText,
having_filters: [legacyHavingFilter],
metric: undefined,
metrics: [sumValueAdhocMetric, savedMetric.saved_metric_name],
where: whereFilterText,
};

function setup(overrides) {
Expand All @@ -68,49 +59,6 @@ describe('AdhocFilterControl', () => {
expect(wrapper.find(OnPasteSelect)).to.have.lengthOf(1);
});

it('will translate legacy filters into adhoc filters if no adhoc filters are present', () => {
const { wrapper } = setup({ value: undefined });
expect(wrapper.state('values')).to.have.lengthOf(4);
expect(wrapper.state('values')[0].equals((
new AdhocFilter({
expressionType: EXPRESSION_TYPES.SIMPLE,
subject: 'value',
operator: '>',
comparator: '5',
clause: CLAUSES.WHERE,
})
))).to.be.true;
expect(wrapper.state('values')[1].equals((
new AdhocFilter({
expressionType: EXPRESSION_TYPES.SIMPLE,
subject: 'SUM(value)',
operator: '>',
comparator: '10',
clause: CLAUSES.HAVING,
})
))).to.be.true;
expect(wrapper.state('values')[2].equals((
new AdhocFilter({
expressionType: EXPRESSION_TYPES.SQL,
sqlExpression: 'target in (\'alpha\')',
clause: CLAUSES.WHERE,
})
))).to.be.true;
expect(wrapper.state('values')[3].equals((
new AdhocFilter({
expressionType: EXPRESSION_TYPES.SQL,
sqlExpression: 'SUM(value) < 20',
clause: CLAUSES.HAVING,
})
))).to.be.true;
});

it('will ignore legacy filters if adhoc filters are present', () => {
const { wrapper } = setup();
expect(wrapper.state('values')).to.have.lengthOf(1);
expect(wrapper.state('values')[0]).to.equal(simpleAdhocFilter);
});

it('handles saved metrics being selected to filter on', () => {
const { wrapper, onChange } = setup({ value: [] });
const select = wrapper.find(OnPasteSelect);
Expand Down
Expand Up @@ -26,6 +26,6 @@ describe('ControlPanelsContainer', () => {
});

it('renders ControlPanelSections', () => {
expect(wrapper.find(ControlPanelSection)).to.have.lengthOf(7);
expect(wrapper.find(ControlPanelSection)).to.have.lengthOf(6);
});
});

0 comments on commit d483ed1

Please sign in to comment.