def h_min(col): """ Return an aggregation for the minimum of the given column. Like the SQL min() function:: select(h_min(employee.salary), employee.department, where=employee) returns the lowest salary in each department. :type col: :class:`hustle.core.marble.Column` :param col: the column to aggregate """ import mdb if col.is_numeric: return Aggregation("min", col, f=lambda a, v: a if a < v else v, default=lambda: 9223372036854775807, result_spec=Column('_min_type', type_indicator=mdb.MDB_INT_32)) else: return Aggregation("min", col, f=lambda a, v: a if a < v else v, default=lambda: unichr(0xFFFF))
def h_count(): """ Return an aggregation for the count of each grouped key in a query. Like SQL count() function:: select(h_count(), employee.department, where=employee) returns a count of the number of employees in each department. """ return Aggregation("count", Column('all', None, type_indicator=1), f=lambda a, v: a + (v or 1), default=lambda: 0)
def h_sum(col): """ Return an aggregation for the sum of the given column. Like SQL sum() function. This is used in :func:`hustle.select` calls to specify the sum aggregation over a column in a query:: select(h_sum(employee.salary), employee.department, where=employee.age > 25) returns the total salaries for each departments employees over 25 years old :type col: :class:`hustle.core.marble.Column` :param col: the column to aggregate """ return Aggregation("sum", col, f=lambda a, v: a + v, default=lambda: 0)
def h_count(): """ Return an aggregation for the count of each grouped key in a query. Like SQL count() function:: select(h_count(), employee.department, where=employee) returns a count of the number of employees in each department. """ import mdb return Aggregation("count", Column(name='_count', type_indicator=1), f=lambda a, v: a + v, default=lambda: 0, result_spec=Column('_count_type', type_indicator=mdb.MDB_UINT_32))
def h_min(col): """ Return an aggregation for the minimum of the given column. Like the SQL min() function:: select(h_min(employee.salar), employee.department, where=employee) returns the lowest salary in each department. :type col: :class:`hustle.core.marble.Column` :param col: the column to aggregate """ return Aggregation("min", col, f=lambda a, v: a if a < v else v, default=lambda: 9223372036854775807)
def h_max(col): """ Return an aggregation for the maximum of the given column. Like the SQL max() function:: select(h_max(employee.salary), employee.department, where=employee) returns the highest salary for each department. :type col: :class:`hustle.core.marble.Column` :param col: the column to aggregate """ return Aggregation("max", col, f=lambda a, v: a if a > v else v, default=lambda: -9223372036854775808)
def h_union(col): def _inner_deault(): from cardunion import Cardunion return Cardunion(12) def _inner_hll_accumulate(a, v): a.bunion([v]) return a return Aggregation("union", col, f=_inner_hll_accumulate, g=lambda a, c: a.dumps(), h=lambda a: a.dumps(), default=_inner_deault)
def h_avg(col): """ Return an aggregation for the average of the given column. Like the SQL avg() function:: select(h_avg(employee.salary), employee.department, where=employee) returns the average salary in each department :type col: :class:`hustle.core.marble.Column` :param col: the column to aggregate """ return Aggregation("avg", col, f=lambda (a, c), v: (a + v, c + 1), g=lambda (a, c): float(a) / c, default=lambda: (0, 0))
def h_minhash_merge(col): def _inner_deault(): from maxhash import MaxHash return MaxHash() def _inner_hll_accumulate(a, v): from maxhash import MaxHash a.merge(MaxHash.loads(v)) return a return Aggregation("minhash_merge", col, f=_inner_hll_accumulate, g=lambda a, c: a.dumps(), h=lambda a: a.dumps(), default=_inner_deault)
def h_cardinality(col): """ """ def _inner_deault(): from cardunion import Cardunion return Cardunion(12) def _inner_hll_accumulate(a, v): a.bunion([v]) return a return Aggregation("cardinality", col, f=_inner_hll_accumulate, g=lambda a: a.count(), h=lambda a: a.dumps(), default=_inner_deault)
def h_combine(col, separator=','): """ Return a combination of the given column. Like the join function. :type col: :class:`hustle.core.marble.Column` :param col: the column to combine :type separator: strnig :param separator: the separator of the combination """ import mdb import functools func = functools.partial(_h_combine, separator=separator) functools.update_wrapper(func, _h_combine) return Aggregation("combine", col, f=func, default=lambda: None, result_spec=Column('_combine_type', type_indicator=mdb.MDB_STR))