Skip to content

Commit

Permalink
Fixed references queries to correctly adapt the dimension and filters…
Browse files Browse the repository at this point in the history
… applied to the dimension when the dimension is wrapped with Rollup
  • Loading branch information
twheys committed Jun 17, 2019
1 parent 5eef25f commit ca3e97f
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 0 deletions.
3 changes: 3 additions & 0 deletions fireant/queries/reference_helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from functools import partial

from fireant.dataset.fields import Field
from fireant.dataset.modifiers import Rollup
from pypika.terms import (
ComplexCriterion,
Criterion,
Expand All @@ -15,6 +16,8 @@ def adapt_for_reference_query(reference_parts, database, dimensions, metrics, fi
return dimensions, metrics, filters

ref_dimension, time_unit, interval = reference_parts
# Unpack rolled up dimensions
ref_dimension = ref_dimension.dimension if isinstance(ref_dimension, Rollup) else ref_dimension

ref_metrics = _make_reference_metrics(metrics,
references[0].reference_type.alias)
Expand Down
53 changes: 53 additions & 0 deletions fireant/tests/queries/test_build_references.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from datetime import date

import fireant as f
from fireant import Rollup
from fireant.tests.dataset.mocks import mock_dataset

timestamp_daily = f.day(mock_dataset.fields.timestamp)
Expand Down Expand Up @@ -722,3 +723,55 @@ def test_filters_on_other_dimensions_are_not_adapted(self):
'GROUP BY "$timestamp" '
'ORDER BY "$timestamp"', str(queries[1]))


# noinspection SqlDialectInspection,SqlNoDataSourceInspection
class QueryBuilderReferencesWithRollupTests(TestCase):
maxDiff = None

def test_reference_with_rollup_dimension_and_date_range_filter(self):
queries = mock_dataset.query \
.widget(f.HighCharts()
.axis(f.HighCharts.LineSeries(mock_dataset.fields.votes))) \
.dimension(Rollup(timestamp_daily)) \
.reference(f.WeekOverWeek(mock_dataset.fields.timestamp)) \
.filter(mock_dataset.fields.timestamp
.between(date(2018, 1, 1), date(2018, 1, 31))) \
.sql

self.assertEqual(4, len(queries))

base, reference, base_rollup, reference_rollup = queries

with self.subTest('base query applies dimensions and date range filter'):
self.assertEqual('SELECT '
'TRUNC("timestamp",\'DD\') "$timestamp",'
'SUM("votes") "$votes" '
'FROM "politics"."politician" '
'WHERE "timestamp" BETWEEN \'2018-01-01\' AND \'2018-01-31\' '
'GROUP BY "$timestamp" '
'ORDER BY "$timestamp"', str(base))

with self.subTest('reference query shifts timestamp dimension and date range filter by a week'):
self.assertEqual('SELECT '
'TRUNC(TIMESTAMPADD(\'week\',1,TRUNC("timestamp",\'DD\')),\'DD\') "$timestamp",'
'SUM("votes") "$votes_wow" '
'FROM "politics"."politician" '
'WHERE TIMESTAMPADD(\'week\',1,"timestamp") BETWEEN \'2018-01-01\' AND \'2018-01-31\' '
'GROUP BY "$timestamp" '
'ORDER BY "$timestamp"', str(reference))

with self.subTest('totals query selects NULL for timestamp dimension'):
self.assertEqual('SELECT '
'NULL "$timestamp",'
'SUM("votes") "$votes" '
'FROM "politics"."politician" '
'WHERE "timestamp" BETWEEN \'2018-01-01\' AND \'2018-01-31\' '
'ORDER BY "$timestamp"', str(base_rollup))

with self.subTest('reference totals query selects NULL for timestamp dimension and shifts date range filter'):
self.assertEqual('SELECT '
'NULL "$timestamp",'
'SUM("votes") "$votes_wow" '
'FROM "politics"."politician" '
'WHERE TIMESTAMPADD(\'week\',1,"timestamp") BETWEEN \'2018-01-01\' AND \'2018-01-31\' '
'ORDER BY "$timestamp"', str(reference_rollup))

0 comments on commit ca3e97f

Please sign in to comment.