U
    ‰d¦&  ã                   @   s~   e ƒ ZG d d„ dƒZG dd„ dƒZG dd„ dƒZG dd„ deƒZG dd	„ d	eƒZG d
d„ dƒZG dd„ dƒZG dd„ dƒZ	dS )c                   @   s   e Zd Zddd„Zdd„ ZdS )ÚLimité    c                 C   s   || _ || _d S ©N)ÚoffsetÚcount)Úselfr   r   © r   úE/tmp/pip-unpacked-wheel-f5h5_hbx/redis/commands/search/aggregation.pyÚ__init__   s    zLimit.__init__c                 C   s$   | j rdt| jƒt| j ƒgS g S d S )NZLIMIT)r   Ústrr   ©r   r   r   r   Ú
build_args	   s    zLimit.build_argsN)r   r   ©Ú__name__Ú
__module__Ú__qualname__r	   r   r   r   r   r   r      s   
r   c                   @   s0   e Zd ZdZdZdd„ Zdd„ Zedd„ ƒZdS )	ÚReducerzr
    Base reducer object for all reducers.

    See the `redisearch.reducers` module for the actual reducers.
    Nc                 G   s   || _ d | _d | _d S r   )Ú_argsÚ_fieldÚ_alias©r   Úargsr   r   r   r	      s    zReducer.__init__c                 C   s.   |t kr$| jstdƒ‚| jdd… }|| _| S )a  
        Set the alias for this reducer.

        ### Parameters

        - **alias**: The value of the alias for this reducer. If this is the
            special value `aggregation.FIELDNAME` then this reducer will be
            aliased using the same name as the field upon which it operates.
            Note that using `FIELDNAME` is only possible on reducers which
            operate on a single field value.

        This method returns the `Reducer` object making it suitable for
        chaining.
        z(Cannot use FIELDNAME alias with no fieldé   N)Ú	FIELDNAMEr   Ú
ValueErrorr   )r   Úaliasr   r   r   r      s    zReducer.aliasc                 C   s   | j S r   )r   r   r   r   r   r   5   s    zReducer.args)	r   r   r   Ú__doc__ÚNAMEr	   r   Úpropertyr   r   r   r   r   r      s   r   c                   @   s   e Zd ZdZdZdd„ ZdS )ÚSortDirectionz@
    This special class is used to indicate sort direction.
    Nc                 C   s
   || _ d S r   )Úfield)r   r   r   r   r   r	   A   s    zSortDirection.__init__)r   r   r   r   Ú	DIRSTRINGr	   r   r   r   r   r   :   s   r   c                   @   s   e Zd ZdZdZdS )ÚAsczK
    Indicate that the given field should be sorted in ascending order
    ÚASCN©r   r   r   r   r    r   r   r   r   r!   E   s   r!   c                   @   s   e Zd ZdZdZdS )ÚDesczL
    Indicate that the given field should be sorted in descending order
    ZDESCNr#   r   r   r   r   r$   M   s   r$   c                   @   sl   e Zd ZdZddd„Zdd„ Zdd„ Zd	d
„ Zdd„ Zdd„ Z	dd„ Z
dd„ Zdd„ Zddd„Zdd„ ZdS )ÚAggregateRequestzH
    Aggregation request which can be passed to `Client.aggregate`.
    Ú*c                 C   s4   || _ g | _g | _d| _d| _d| _d| _g | _dS )a}  
        Create an aggregation request. This request may then be passed to
        `client.aggregate()`.

        In order for the request to be usable, it must contain at least one
        group.

        - **query** Query string for filtering records.

        All member methods (except `build_args()`)
        return the object itself, making them useful for chaining.
        Fr   N)Ú_queryÚ_aggregateplanÚ_loadfieldsÚ_loadallZ_maxÚ_with_schemaÚ	_verbatimÚ_cursor)r   Úqueryr   r   r   r	   Z   s    zAggregateRequest.__init__c                 G   s   |r| j  |¡ nd| _| S )aC  
        Indicate the fields to be returned in the response. These fields are
        returned in addition to any others implicitly specified.

        ### Parameters

        - **fields**: If fields not specified, all the fields will be loaded.
        Otherwise, fields should be given in the format of `@field`.
        T)r)   Úextendr*   )r   Úfieldsr   r   r   Úloadp   s    
zAggregateRequest.loadc                 G   s”   t |tƒr|gn|}t |tƒr$|gn|}dtt|ƒƒf|•}|D ]B}|d|jtt|jƒƒg7 }| |j¡ |jdk	r@|d|jg7 }q@| j |¡ | S )a|  
        Specify by which fields to group the aggregation.

        ### Parameters

        - **fields**: Fields to group by. This can either be a single string,
            or a list of strings. both cases, the field should be specified as
            `@field`.
        - **reducers**: One or more reducers. Reducers may be found in the
            `aggregation` module.
        ZGROUPBYÚREDUCENÚAS)	Ú
isinstancer
   r   Úlenr   r   r/   r   r(   )r   r0   ZreducersÚretZreducerr   r   r   Úgroup_by€   s    
zAggregateRequest.group_byc                 K   s>   |  ¡ D ]0\}}d|g}|dk	r,|d|g7 }| j |¡ q| S )aQ  
        Specify one or more projection expressions to add to each result

        ### Parameters

        - **kwexpr**: One or more key-value pairs for a projection. The key is
            the alias for the projection, and the value is the projection
            expression itself, for example `apply(square_root="sqrt(@foo)")`
        ZAPPLYNr3   )Úitemsr(   r/   )r   Zkwexprr   Úexprr6   r   r   r   Úapply™   s    
zAggregateRequest.applyc                 C   s   t ||ƒ}| j | ¡ ¡ | S )a  
        Sets the limit for the most recent group or query.

        If no group has been defined yet (via `group_by()`) then this sets
        the limit for the initial pool of results from the query. Otherwise,
        this limits the number of items operated on from the previous group.

        Setting a limit on the initial search results may be useful when
        attempting to execute an aggregation on a sample of a large data set.

        ### Parameters

        - **offset**: Result offset from which to begin paging
        - **num**: Number of results to return


        Example of sorting the initial results:

        ```
        AggregateRequest("@sale_amount:[10000, inf]")            .limit(0, 10)            .group_by("@state", r.count())
        ```

        Will only group by the states found in the first 10 results of the
        query `@sale_amount:[10000, inf]`. On the other hand,

        ```
        AggregateRequest("@sale_amount:[10000, inf]")            .limit(0, 1000)            .group_by("@state", r.count()            .limit(0, 10)
        ```

        Will group all the results matching the query, but only return the
        first 10 groups.

        If you only wish to return a *top-N* style query, consider using
        `sort_by()` instead.

        )r   r(   r/   r   )r   r   ÚnumZ_limitr   r   r   Úlimit«   s    *
zAggregateRequest.limitc                 O   s–   t |ttfƒr|g}g }|D ]*}t |tƒr<||j|jg7 }q||g7 }qdtt|ƒƒg}| |¡ | dd¡}|dkr†|dt|ƒg7 }| j |¡ | S )a,  
        Indicate how the results should be sorted. This can also be used for
        *top-N* style queries

        ### Parameters

        - **fields**: The fields by which to sort. This can be either a single
            field or a list of fields. If you wish to specify order, you can
            use the `Asc` or `Desc` wrapper classes.
        - **max**: Maximum number of results to return. This can be
            used instead of `LIMIT` and is also faster.


        Example of sorting by `foo` ascending and `bar` descending:

        ```
        sort_by(Asc("@foo"), Desc("@bar"))
        ```

        Return the top 10 customers:

        ```
        AggregateRequest()            .group_by("@customer", r.sum("@paid").alias(FIELDNAME))            .sort_by(Desc("@paid"), max=10)
        ```
        ZSORTBYÚmaxr   ÚMAX)	r4   r
   r   r   r    r5   r/   Úgetr(   )r   r0   ÚkwargsZfields_argsÚfr6   r=   r   r   r   Úsort_byÙ   s    

zAggregateRequest.sort_byc                 C   s.   t |tƒr|g}|D ]}| j d|g¡ q| S )zÿ
        Specify filter for post-query results using predicates relating to
        values in the result set.

        ### Parameters

        - **fields**: Fields to group by. This can either be a single string,
            or a list of strings.
        ZFILTER)r4   r
   r(   r/   )r   ZexpressionsZ
expressionr   r   r   Úfilter  s
    

zAggregateRequest.filterc                 C   s
   d| _ | S )z|
        If set, the `schema` property will contain a list of `[field, type]`
        entries in the result object.
        T)r+   r   r   r   r   Úwith_schema  s    zAggregateRequest.with_schemac                 C   s
   d| _ | S )NT)r,   r   r   r   r   Úverbatim"  s    zAggregateRequest.verbatimr   ç        c                 C   s<   dg}|r|dt |ƒg7 }|r2|dt |d ƒg7 }|| _| S )NZ
WITHCURSORÚCOUNTÚMAXIDLEiè  )r
   r-   )r   r   Úmax_idler   r   r   r   Úcursor&  s    zAggregateRequest.cursorc                 C   s”   | j g}| jr| d¡ | jr(| d¡ | jr8|| j7 }| jrT| d¡ | d¡ n0| jr„| d¡ | tt| jƒƒ¡ | 	| j¡ | 	| j
¡ |S )NZ
WITHSCHEMAZVERBATIMZLOADr&   )r'   r+   Úappendr,   r-   r*   r)   r
   r5   r/   r(   )r   r6   r   r   r   r   /  s     




zAggregateRequest.build_argsN)r&   )r   rF   )r   r   r   r   r	   r1   r7   r:   r<   rB   rC   rD   rE   rJ   r   r   r   r   r   r%   U   s   
./
	r%   c                   @   s   e Zd Zdd„ Zdd„ ZdS )ÚCursorc                 C   s   || _ d| _d| _d S )Nr   )ÚcidrI   r   ©r   rM   r   r   r   r	   J  s    zCursor.__init__c                 C   s@   t | jƒg}| jr$|dt | jƒg7 }| jr<|dt | jƒg7 }|S )NrH   rG   )r
   rM   rI   r   r   r   r   r   r   O  s    zCursor.build_argsNr   r   r   r   r   rL   I  s   rL   c                   @   s   e Zd Zdd„ Zdd„ ZdS )ÚAggregateResultc                 C   s   || _ || _|| _d S r   )ÚrowsrJ   Úschema)r   rP   rJ   rQ   r   r   r   r	   Y  s    zAggregateResult.__init__c              	   C   s@   | j r| j jnd}d| jj› dt| ƒd›dt| jƒ› d|› d	S )Néÿÿÿÿú<z at 0xÚxz Rows=z	, Cursor=ú>)rJ   rM   Ú	__class__r   Úidr5   rP   rN   r   r   r   Ú__repr__^  s    ,ÿzAggregateResult.__repr__N)r   r   r   r	   rX   r   r   r   r   rO   X  s   rO   N)
Úobjectr   r   r   r   r!   r$   r%   rL   rO   r   r   r   r   Ú<module>   s   * u