
    +h*                         d dl mZmZ  e       Z G d d      Z G d d      Z G d d      Z G d d	e      Z G d
 de      Z	 G d d      Z
 G d d      Z G d d      Zy)    )ListUnionc                   (    e Zd ZddededdfdZd Zy)LimitoffsetcountreturnNc                      || _         || _        y N)r   r   )selfr   r   s      T/var/www/html/venv/lib/python3.12/site-packages/redis/commands/search/aggregation.py__init__zLimit.__init__   s    
    c                 t    | j                   r+dt        | j                        t        | j                         gS g S )NLIMIT)r   strr   r   s    r   
build_argszLimit.build_args   s,    ::S-s4::??Ir   )r   r   __name__
__module____qualname__intr   r    r   r   r   r      s!    s s 4 r   r   c                   V    e Zd ZdZdZdee   ddfdZdedd fdZe	dee   fd       Z
y)	Reducerzr
    Base reducer object for all reducers.

    See the `redisearch.reducers` module for the actual reducers.
    Nargsr	   c                 .    || _         d | _        d | _        y r   )_args_field_aliasr   r   s     r   r   zReducer.__init__   s    
r   aliasc                 p    |t         u 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   r#   s     r   r#   zReducer.alias    s:     I;; !KLLKKOEr   c                     | j                   S r   )r   r   s    r   r   zReducer.args7   s    zzr   )r   r   r   __doc__NAMEr   r   r   r#   propertyr   r   r   r   r   r      sW     Dd3i D 
3 9 . d3i  r   r   c                   $    e Zd ZdZdZdeddfdZy)SortDirectionz@
    This special class is used to indicate sort direction.
    Nfieldr	   c                     || _         y r   )r.   )r   r.   s     r   r   zSortDirection.__init__C   s	    
r   )r   r   r   r)   	DIRSTRINGr   r   r   r   r   r-   r-   <   s!     Ic d r   r-   c                       e Zd ZdZdZy)AsczK
    Indicate that the given field should be sorted in ascending order
    ASCNr   r   r   r)   r0   r   r   r   r2   r2   G   s     Ir   r2   c                       e Zd ZdZdZy)DesczL
    Indicate that the given field should be sorted in descending order
    DESCNr4   r   r   r   r6   r6   O   s     Ir   r6   c                       e Zd ZdZddeddfdZdee   dd fdZdee   dee	ee	   f   dd fd	Z
dd
Zdededd fdZdee   dd fdZdeeee   f   dd fdZddZddZddededd fdZdee   fdZdedd fdZy)AggregateRequestzH
    Aggregation request which can be passed to `Client.aggregate`.
    queryr	   Nc                     || _         g | _        g | _        d| _        d| _        d| _        d| _        g | _        d| _        y)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_loadall_max_with_schema	_verbatim_cursor_dialect)r   r:   s     r   r   zAggregateRequest.__init__\   sF      	!r   fieldsc                 R    |r| j                   j                  |       | S 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   rE   s     r   loadzAggregateRequest.loads   s0     ##F+  !DMr   reducersc           	         t        |t              r|gn|}t        |t              r|gn|}dt        t        |            g|}|D ]i  }|d|j                  t        t        |j
                              gz  }|j                  |j
                         |j                  Y|d|j                  gz  }k | j                  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.
        GROUPBYREDUCEAS)	
isinstancer   r   lenr*   r   rG   r!   r=   )r   rE   rI   retreducers        r   group_byzAggregateRequest.group_by   s     (4&&!+Hg!>H:H#c&k*4V4 	.GHgllCGLL0A,BCCCJJw||$~~)gnn--		. 	""3'r   c                     |j                         D ]-  \  }}d|g}||d|gz  }| j                  j                  |       / | 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)")`
        APPLYrM   )itemsr=   rG   )r   kwexprr#   exprrP   s        r   applyzAggregateRequest.apply   sU     "<<> 	,KE4D/C e}$&&s+		, r   r   numc                 p    t        ||      }| j                  j                  |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=   rG   r   )r   r   rY   _limits       r   limitzAggregateRequest.limit   s3    T vs#""6#4#4#67r   c                    t        |t        t        f      r|g}g }|D ]4  }t        |t              r||j                  |j                  gz  }/||gz  }6 dt        t        |            g}|j                  |       |j                  dd      }|dkD  r|dt        |      gz  }| j                  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)
        ```
        SORTBYmaxr   MAX)	rN   r   r-   r.   r0   rO   rG   getr=   )r   rE   kwargsfields_argsfrP   r_   s          r   sort_byzAggregateRequest.sort_by   s    8 fsM23XF 	#A!]+55s"		# S-./

;jj"7E3s8$$C""3'r   expressionsc                 t    t        |t              r|g}|D ]  }| j                  j                  d|g       ! | 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.
        FILTER)rN   r   r=   rG   )r   rf   
expressions      r   filterzAggregateRequest.filter  sD     k3'&-K% 	?J&&*'=>	? r   c                     d| _         | S )z|
        If set, the `schema` property will contain a list of `[field, type]`
        entries in the result object.
        T)rA   r   s    r   with_schemazAggregateRequest.with_schema  s    
 !r   c                     d| _         | S )NT)rB   r   s    r   verbatimzAggregateRequest.verbatim'  s    r   r   max_idlec                 h    dg}|r|dt        |      gz  }|r|dt        |dz        gz  }|| _        | S )N
WITHCURSORCOUNTMAXIDLEi  )r   rC   )r   r   ro   r   s       r   cursorzAggregateRequest.cursor+  sF    ~Wc%j))DYHtO 455Dr   c                 z   | j                   g}| j                  r|j                  d       | j                  r|j                  d       | j                  r|| j                  z  }| j
                  r#|j                  d       |j                  d       ne| j                  rY|j                  d       |j                  t        t        | j                                     |j                  | j                         | j                  r|j                  d| j                  g       |j                  | j                         |S )N
WITHSCHEMAVERBATIMLOAD*DIALECT)r<   rA   appendrB   rC   r?   r>   r   rO   rG   rD   r=   )r   rP   s     r   r   zAggregateRequest.build_args4  s    {{mJJ|$>>JJz"<<4<<C==JJvJJsOJJvJJs3t//012JJt''(==JJ	4==12

4&&'
r   dialectc                     || _         | S )z
        Add a dialect field to the aggregate command.

        - **dialect** - dialect version to execute the query under
        )rD   )r   r|   s     r   r|   zAggregateRequest.dialectP  s      r   )ry   )r	   r9   )r   g        )r   r   r   r)   r   r   r   rH   r   r   rR   rX   r   r\   re   rj   rl   rn   floatrt   r   r|   r   r   r   r9   r9   W   s   c D .DI *<  3i,1'4=2H,I	6$,C ,c ,.@ ,\-tCy -7I -^%T#Y"7 <N $C u ?Q DI 8s '9 r   r9   c                   "    e Zd ZdeddfdZd Zy)Cursorcidr	   Nc                 .    || _         d| _        d| _        y )Nr   )r   ro   r   r   r   s     r   r   zCursor.__init__[  s    
r   c                     t        | j                        g}| j                  r|dt        | j                        gz  }| j                  r|dt        | j                        gz  }|S )Nrs   rr   )r   r   ro   r   r"   s     r   r   zCursor.build_args`  sS    DHH==YDMM 233D::Wc$**o..Dr   r   r   r   r   r   r   Z  s    C D 
r   r   c                   ,    e Zd ZdeddfdZdeeffdZy)AggregateResultrt   r	   Nc                 .    || _         || _        || _        y r   )rowsrt   schema)r   r   rt   r   s       r   r   zAggregateResult.__init__j  s    	r   c           	          | 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=>)rt   r   	__class__r   idrO   r   r   s     r   __repr__zAggregateResult.__repr__o  sZ    !%dkkoo"''(r$xl ;		N#9SE4	
r   )r   r   r   r   r   r   r   r   r   r   r   r   i  s%    V  

3* 
r   r   N)typingr   r   objectr&   r   r   r-   r2   r6   r9   r   r   r   r   r   <module>r      sg    H		 	' 'T - = @ @F 
 
r   