예제 #1
0
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))
예제 #2
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.
    """
    return Aggregation("count",
                       Column('all', None, type_indicator=1),
                       f=lambda a, v: a + (v or 1),
                       default=lambda: 0)
예제 #3
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)
예제 #4
0
파일: __init__.py 프로젝트: cih-y2k/hustle
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))
예제 #5
0
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)
예제 #6
0
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)
예제 #7
0
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)
예제 #8
0
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))
예제 #9
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)
예제 #10
0
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)
예제 #11
0
파일: __init__.py 프로젝트: cih-y2k/hustle
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))