-
Notifications
You must be signed in to change notification settings - Fork 13.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[druid] Allow custom druid postaggregators #3146
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -718,51 +718,29 @@ def values_for_column(self, | |
def get_query_str(self, query_obj, phase=1, client=None): | ||
return self.run_query(client=client, phase=phase, **query_obj) | ||
|
||
def run_query( # noqa / druid | ||
self, | ||
groupby, metrics, | ||
granularity, | ||
from_dttm, to_dttm, | ||
filter=None, # noqa | ||
is_timeseries=True, | ||
timeseries_limit=None, | ||
timeseries_limit_metric=None, | ||
row_limit=None, | ||
inner_from_dttm=None, inner_to_dttm=None, | ||
orderby=None, | ||
extras=None, # noqa | ||
select=None, # noqa | ||
columns=None, phase=2, client=None, form_data=None): | ||
"""Runs a query against Druid and returns a dataframe. | ||
""" | ||
# TODO refactor into using a TBD Query object | ||
client = client or self.cluster.get_pydruid_client() | ||
if not is_timeseries: | ||
granularity = 'all' | ||
inner_from_dttm = inner_from_dttm or from_dttm | ||
inner_to_dttm = inner_to_dttm or to_dttm | ||
|
||
# add tzinfo to native datetime with config | ||
from_dttm = from_dttm.replace(tzinfo=DRUID_TZ) | ||
to_dttm = to_dttm.replace(tzinfo=DRUID_TZ) | ||
timezone = from_dttm.tzname() | ||
|
||
query_str = "" | ||
metrics_dict = {m.metric_name: m for m in self.metrics} | ||
@staticmethod | ||
def _metrics_and_post_aggs(metrics, metrics_dict): | ||
all_metrics = [] | ||
post_aggs = {} | ||
|
||
columns_dict = {c.column_name: c for c in self.columns} | ||
|
||
def recursive_get_fields(_conf): | ||
_fields = _conf.get('fields', []) | ||
print(_conf) | ||
_type = _conf.get('type') | ||
_field = _conf.get('field', None) | ||
_fields = _conf.get('fields', None) | ||
|
||
field_names = [] | ||
for _f in _fields: | ||
_type = _f.get('type') | ||
if _type in ['fieldAccess', 'hyperUniqueCardinality']: | ||
field_names.append(_f.get('fieldName')) | ||
elif _type == 'arithmetic': | ||
if _type in ['fieldAccess', 'hyperUniqueCardinality', | ||
'quantile', 'quantiles']: | ||
field_names.append(_conf.get('fieldName', '')) | ||
|
||
if _field is not None: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I suppose. I prefer to be explicit when I care about None vs. when I want to reject all falsy values. https://docs.python.org/2.4/lib/truth.html I'll change the PR. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
field_names += recursive_get_fields(_field) | ||
|
||
if _fields is not None: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. why not use the same if block as the one right above this one? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. _field vs _fields. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this is mostly a matter of supporting a newer and older postAggregators that have different customs for storing the dependent fields. |
||
for _f in _fields: | ||
field_names += recursive_get_fields(_f) | ||
|
||
return list(set(field_names)) | ||
|
||
for metric_name in metrics: | ||
|
@@ -799,11 +777,54 @@ def recursive_get_fields(_conf): | |
post_aggs[metric_name] = HyperUniqueCardinality( | ||
mconf.get('name') | ||
) | ||
else: | ||
elif mconf.get('type') == 'arithmetic': | ||
post_aggs[metric_name] = Postaggregator( | ||
mconf.get('fn', "/"), | ||
mconf.get('fields', []), | ||
mconf.get('name', '')) | ||
else: | ||
post_aggs[metric_name] = Postaggregator( | ||
None, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. it would help readability to use keyword args here There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. fixed by making a CustomPostAggregator class that encompasses this logic. Should make more sense that way. |
||
None, | ||
mconf.get('name', '')) | ||
post_aggs[metric_name].post_aggregator = mconf | ||
return all_metrics, post_aggs | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm never a big fan of returning tuples but I think it's ok in this case There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. yeah, for an internal api, I feel like it's kinda defensible. |
||
|
||
def run_query( # noqa / druid | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm not sure why git is getting the diffing all wrong here, things are not lining up. Makes it hard to see what has changed in this section. Maybe moving There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. moved up to other static methods. |
||
self, | ||
groupby, metrics, | ||
granularity, | ||
from_dttm, to_dttm, | ||
filter=None, # noqa | ||
is_timeseries=True, | ||
timeseries_limit=None, | ||
timeseries_limit_metric=None, | ||
row_limit=None, | ||
inner_from_dttm=None, inner_to_dttm=None, | ||
orderby=None, | ||
extras=None, # noqa | ||
select=None, # noqa | ||
columns=None, phase=2, client=None, form_data=None): | ||
"""Runs a query against Druid and returns a dataframe. | ||
""" | ||
# TODO refactor into using a TBD Query object | ||
client = client or self.cluster.get_pydruid_client() | ||
if not is_timeseries: | ||
granularity = 'all' | ||
inner_from_dttm = inner_from_dttm or from_dttm | ||
inner_to_dttm = inner_to_dttm or to_dttm | ||
|
||
# add tzinfo to native datetime with config | ||
from_dttm = from_dttm.replace(tzinfo=DRUID_TZ) | ||
to_dttm = to_dttm.replace(tzinfo=DRUID_TZ) | ||
timezone = from_dttm.tzname() | ||
|
||
query_str = "" | ||
metrics_dict = {m.metric_name: m for m in self.metrics} | ||
|
||
columns_dict = {c.column_name: c for c in self.columns} | ||
|
||
all_metrics, post_aggs = self._metrics_and_post_aggs(metrics, metrics_dict) | ||
|
||
aggregations = OrderedDict() | ||
for m in self.metrics: | ||
|
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.
, None
is implicit, that's the default behavior