Skip to content
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

How to apply multiple filters to a group by query #94

Open
vikramsinghchandel opened this issue Jun 28, 2017 · 2 comments
Open

How to apply multiple filters to a group by query #94

vikramsinghchandel opened this issue Jun 28, 2017 · 2 comments

Comments

@vikramsinghchandel
Copy link

How can i apply multiple filters to a group by query following does not work
filters = (pydruid.utils.filters.Dimension("unit")=='000721') & (pydruid.utils.filters.Dimension("val") > 0)
query1 = query.groupby(
datasource=dataSource,
granularity='minute',
#intervals='2016-08-01/p12w',
#intervals='2016-08-01/pt24h',
intervals='2016-08-11/2016-08-15',
dimensions=["GPN"],
filter=filters,
aggregations={"val_sum": ag.doublesum("val"),"val_count": ag.count("val")},
post_aggregations={"Avg": (pag.Field("val_sum") / pag.Field("val_count"))},
context={"timeout": 600000}#,
# limit_spec={
#"type": "default",
# "limit": 50,
# "columns" : ["sensor_val_sum","sensor_val_count"]
# }
)

Gives following error
Traceback (most recent call last):
File "Test_Query_2Filters.py", line 41, in
context={"timeout": 600000}#,
File "/usr/local/lib/python2.7/dist-packages/pydruid/client.py", line 191, in groupby
query = self.query_builder.groupby(kwargs)
File "/usr/local/lib/python2.7/dist-packages/pydruid/query.py", line 316, in groupby
return self.build_query(query_type, args)
File "/usr/local/lib/python2.7/dist-packages/pydruid/query.py", line 250, in build_query
query_dict[key] = Filter.build_filter(val)
File "/usr/local/lib/python2.7/dist-packages/pydruid/utils/filters.py", line 90, in build_filter
filter['fields'] = [Filter.build_filter(f) for f in filter['fields']]
File "/usr/local/lib/python2.7/dist-packages/pydruid/utils/filters.py", line 87, in build_filter
filter = filter_obj.filter['filter']
AttributeError: 'bool' object has no attribute 'filter'

@var23rav
Copy link
Contributor

var23rav commented Jul 6, 2017

Import the Filter class

from pydruid.utils.filters import Filter
filter_unit_obj = Filter(dimension='unit', value='000721')

val > 0, these kind work can be handled by make use of javascript functionality

filter_val_obj = Filter(
                    type= 'javascript',
                    dimension= 'val',
                    function= "function(x){ return x > 0}"
                )

Now join them like which can be substitue to the query.groupby function.

query_filter_obj = Filter(
                     type= 'and',
                     fields= [ filter_unit_obj, filter_val_obj]
                    )

@DzakirinMD
Copy link

Hope they can add this into filter documentation. Really helpful

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants