Skip to content

Commit

Permalink
Merge pull request druid-io#14 from seanv507/having
Browse files Browse the repository at this point in the history
Having
  • Loading branch information
fjy committed Sep 15, 2014
2 parents 52ff514 + 4226a66 commit f959c70
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 1 deletion.
6 changes: 5 additions & 1 deletion pydruid/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
from utils.aggregators import *
from utils.postaggregator import *
from utils.filters import *
from utils.having import *
from utils.query_utils import *


Expand Down Expand Up @@ -279,6 +280,8 @@ def build_query(self, args):
query_dict['pagingSpec'] = val
elif key == "filter":
query_dict[key] = Filter.build_filter(val)
elif key == "having":
query_dict[key] = Having.build_having(val)
else:
query_dict[key] = val

Expand Down Expand Up @@ -400,6 +403,7 @@ def groupby(self, **kwargs):
Optional key/value pairs:
:param pydruid.utils.filters.Filter filter: Indicates which rows of data to include in the query
:param pydruid.utils.having.Having having: Indicates which groups in results set of query to keep
:param post_aggregations: A dict with string key = 'post_aggregator_name', and value pydruid.utils.PostAggregator
Example:
Expand All @@ -424,7 +428,7 @@ def groupby(self, **kwargs):
self.query_type = 'groupBy'
valid_parts = [
'datasource', 'granularity', 'filter', 'aggregations',
'post_aggregations', 'intervals', 'dimensions'
'having', 'post_aggregations', 'intervals', 'dimensions'
]
self.validate_query(valid_parts, kwargs)
self.build_query(kwargs)
Expand Down
82 changes: 82 additions & 0 deletions pydruid/utils/having.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
#
# Copyright 2013 Metamarkets Group Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
import simplejson as json


class Having:
def __init__(self, **args):

if args['type'] in ('equalTo', 'lessThan','greaterThan'):
self.having = {'having': {'type': args['type'],
'aggregation': args['aggregation'],
'value': args['value']}}

elif args['type'] == 'and':
self.having = {'having': {'type': 'and',
'havingSpecs': args['havingSpecs']}}

elif args['type'] == 'or':
self.having = {'having': {'type': 'or',
'havingSpecs': args['havingSpecs']}}

elif args['type'] == 'not':
self.having = {'having': {'type': 'not',
'havingSpec': args['havingSpec']}}
else:
raise NotImplemented(
'Having type: {0} does not exist'.format(args['type']))

def show(self):
print(json.dumps(self.having, indent=4))

def _combine(self, typ, x):
# collapse nested and/ors
if self.having['having']['type'] == typ:
havingSpecs = self.having['having']['havingSpecs'] + [x.having['having']]
return Having(type=typ, havingSpecs=havingSpecs)
elif x.having['having']['type']==typ:
havingSpecs = [self.having['having']] + x.having['having']['havingSpecs']
return Having(type=typ, havingSpecs=havingSpecs)
else:
return Having(type=typ,
havingSpecs=[self.having['having'], x.having['having']])

def __and__(self, x):
return self._combine('and', x)

def __or__(self, x):
return self._combine('or', x)

def __invert__(self):
return Having(type='not', havingSpec=self.having['having'])

@staticmethod
def build_having(having_obj):
return having_obj.having['having']


class Aggregation:
def __init__(self, agg):
self.aggregation = agg

def __eq__(self, other):
return Having(type='equalTo', aggregation=self.aggregation, value=other)

def __lt__(self, other):
return Having(type='lessThan', aggregation=self.aggregation, value=other)

def __gt__(self, other):
return Having(type='greaterThan', aggregation=self.aggregation, value=other)

0 comments on commit f959c70

Please sign in to comment.