o
     i,                     @  s2  d Z ddlmZ ddlZddlmZ ddlmZmZmZm	Z	m
Z
mZmZ ddlmZ ddlmZ ddlmZ dd	lmZ dd
lmZ erbddlmZ ddlmZ ddlmZ ddlmZ ddlmZ G dd de Z!G dd deZ"G dd de"Z#G dd de"Z$G dd de"Z%d ddZ&G dd deZ'dS )!aU  Classes for representing aggregation queries for the Google Cloud Firestore API.

A :class:`~google.cloud.firestore_v1.aggregation.AggregationQuery` can be created directly from
a :class:`~google.cloud.firestore_v1.collection.Collection` and that can be
a more common way to create an aggregation query than direct usage of the constructor.
    )annotationsN)ABC)TYPE_CHECKINGAny	CoroutineListOptionalTupleUnion)gapic_v1)retry)_helpers)	FieldPath)StructuredAggregationQuery)transaction)AsyncStreamGenerator)ExplainOptions)QueryResultsList)StreamGeneratorc                   @  s$   e Zd ZdZddddZd	d
 ZdS )AggregationResulta  
    A class representing result from Aggregation Query
    :type alias: str
    :param alias: The alias for the aggregation.
    :type value: int
    :param value: The resulting value from the aggregation.
    :type read_time:
    :param value: The resulting read_time
    Naliasstrvaluefloatc                 C  s   || _ || _|| _d S Nr   r   	read_time)selfr   r   r    r   /var/www/snowflake_co_dev_github/snow_flake_back_end_deploy/env/lib/python3.10/site-packages/google/cloud/firestore_v1/base_aggregation.py__init__:   s   
zAggregationResult.__init__c                 C  s   d| j  d| j d| j dS )Nz<Aggregation alias=z, value=z, readtime=>r   r   r   r   r   __repr__?   s   zAggregationResult.__repr__r   )r   r   r   r   )__name__
__module____qualname____doc__r    r#   r   r   r   r   r   /   s    
r   c                   @  s&   e Zd Zdd	ddZejdd ZdS )
BaseAggregationNr   
str | Nonec                 C  s
   || _ d S r   r   r   r   r   r   r   r    D   s   
zBaseAggregation.__init__c                 C     dS )4Convert this instance to the protobuf representationNr   r"   r   r   r   _to_protobufG       zBaseAggregation._to_protobufr   r   r)   )r$   r%   r&   r    abcabstractmethodr.   r   r   r   r   r(   C   s    r(   c                      s(   e Zd Zdd	 fddZdd Z  ZS )
CountAggregationNr   r)   c                   s   t t| j|d d S Nr*   )superr3   r    r+   	__class__r   r   r    M   s   zCountAggregation.__init__c                 C  s    t  }| j|_t j |_|S r-   )r   Aggregationr   Countcountr   aggregation_pbr   r   r   r.   P   s   zCountAggregation._to_protobufr   r0   r$   r%   r&   r    r.   __classcell__r   r   r6   r   r3   L   s    r3   c                      (   e Zd Zd
d fddZdd	 Z  ZS )SumAggregationN	field_refstr | FieldPathr   r)   c                   .   t |tr	| }|| _tt| j|d d S r4   )
isinstancer   to_api_reprrB   r5   rA   r    r   rB   r   r6   r   r   r    Y      
zSumAggregation.__init__c                 C  ,   t  }| j|_t j |_| j|jj_|S r8   )r   r9   r   SumsumrB   field
field_pathr<   r   r   r   r.   `   
   zSumAggregation._to_protobufr   rB   rC   r   r)   r>   r   r   r6   r   rA   X       rA   c                      r@   )AvgAggregationNrB   rC   r   r)   c                   rD   r4   )rE   r   rF   rB   r5   rQ   r    rG   r6   r   r   r    j   rH   zAvgAggregation.__init__c                 C  rI   r8   )r   r9   r   AvgavgrB   rL   rM   r<   r   r   r   r.   q   rN   zAvgAggregation._to_protobufr   rO   r>   r   r   r6   r   rQ   i   rP   rQ   returnList[AggregationResult]c                   s     fdd j jj D }|S )Nc                   s4   g | ]}t | jj| jp jj| j jd qS )r   )r   resultaggregate_fieldsinteger_valuedouble_valuer   ).0keyresponse_pbr   r   
<listcomp>}   s    z-_query_response_to_result.<locals>.<listcomp>)rV   rW   pbkeys)r]   resultsr   r\   r   _query_response_to_resultz   s   

rb   c                   @  s   e Zd ZdZd2d3ddZed	d
 Zd2d4ddZd2d5ddZd2d5ddZ	d6ddZ
d7ddZd8ddZ				d9d:d%d&Zejdejjdfdd'd;d)d*Zejdejjdfdd'd<d0d1ZdS )=BaseAggregationQueryz5Represents an aggregation query to the Firestore API.Nr   r)   rT   Nonec                 C  s   || _ || _|j| _g | _d S r   )_nested_query_alias_parent_collection_ref_aggregations)r   nested_queryr   r   r   r   r       s   
zBaseAggregationQuery.__init__c                 C  s   | j jS r   )rh   _clientr"   r   r   r   rk      s   zBaseAggregationQuery._clientc                 C  s   t |d}| j| | S )z4
        Adds a count over the nested query
        r*   )r3   ri   append)r   r   count_aggregationr   r   r   r;      s   
zBaseAggregationQuery.countrB   rC   c                 C     t ||d}| j| | S )z2
        Adds a sum over the nested query
        r*   )rA   ri   rl   )r   rB   r   sum_aggregationr   r   r   rK         zBaseAggregationQuery.sumc                 C  rn   )z3
        Adds an avg over the nested query
        r*   )rQ   ri   rl   )r   rB   r   avg_aggregationr   r   r   rS      rp   zBaseAggregationQuery.avgaggregationr(   c                 C     | j | dS )z
        Adds an aggregation operation to the nested query

        :type aggregation: :class:`google.cloud.firestore_v1.aggregation.BaseAggregation`
        :param aggregation: An aggregation operation, e.g. a CountAggregation
        N)ri   rl   )r   rr   r   r   r   add_aggregation      z$BaseAggregationQuery.add_aggregationaggregationsList[BaseAggregation]c                 C  rs   )z
        Adds a list of aggregations to the nested query

        :type aggregations: list
        :param aggregations: a list of aggregation operations
        N)ri   extend)r   rv   r   r   r   add_aggregations   ru   z%BaseAggregationQuery.add_aggregationsr   c                 C  s6   t  }| j |_| jD ]}| }|j| q|S r   )r   re   r.   structured_queryri   rv   rl   )r   r_   rr   r=   r   r   r   r.      s   
z!BaseAggregationQuery._to_protobufr   6Union[retries.Retry, retries.AsyncRetry, None, object]timeoutfloat | Noneexplain_optionsOptional[ExplainOptions]Tuple[dict, dict]c           	      C  sH   | j  \}}||  t|d}|r| |d< t||}||fS )N)parentstructured_aggregation_queryr   r~   )rh   _parent_infor.   r   get_transaction_id_to_dictmake_retry_timeout_kwargs)	r   r   r   r|   r~   parent_pathexpected_prefixrequestkwargsr   r   r   _prep_stream   s   z!BaseAggregationQuery._prep_stream)r~   XQueryResultsList[AggregationResult] | Coroutine[Any, Any, List[List[AggregationResult]]]c                C  r,   )a  Runs the aggregation query.

        This sends a ``RunAggregationQuery`` RPC and returns a list of
        aggregation results in the stream of ``RunAggregationQueryResponse``
        messages.

        Args:
            transaction
                (Optional[:class:`~google.cloud.firestore_v1.transaction.Transaction`]):
                An existing transaction that this query will run in.
                If a ``transaction`` is used and it already has write operations
                added, this method cannot be used (i.e. read-after-write is not
                allowed).
            retry (google.api_core.retry.Retry): Designation of what errors, if any,
                should be retried.  Defaults to a system-specified policy.
            timeout (float): The timeout for this request.  Defaults to a
                system-specified value.
            explain_options
                (Optional[:class:`~google.cloud.firestore_v1.query_profile.ExplainOptions`]):
                Options to enable query profiling for this query. When set,
                explain_metrics will be available on the returned generator.

        Returns:
            (QueryResultsList[List[AggregationResult]] | Coroutine[Any, Any, List[List[AggregationResult]]]):
            The aggregation query results.
        Nr   r   r   r   r|   r~   r   r   r   get   r/   zBaseAggregationQuery.getr   !Optional[transaction.Transaction]2retries.Retry | retries.AsyncRetry | object | NoneOptional[float]XStreamGenerator[List[AggregationResult]] | AsyncStreamGenerator[List[AggregationResult]]c                C  r,   )a[  Runs the aggregation query.

        This sends a``RunAggregationQuery`` RPC and returns a generator in the stream of ``RunAggregationQueryResponse`` messages.

        Args:
            transaction
                (Optional[:class:`~google.cloud.firestore_v1.transaction.Transaction`]):
                An existing transaction that this query will run in.
            retry (Optional[google.api_core.retry.Retry]): Designation of what
                errors, if any, should be retried.  Defaults to a
                system-specified policy.
            timeout (Optinal[float]): The timeout for this request.  Defaults
                to a system-specified value.
            explain_options
                (Optional[:class:`~google.cloud.firestore_v1.query_profile.ExplainOptions`]):
                Options to enable query profiling for this query. When set,
                explain_metrics will be available on the returned generator.

        Returns:
            StreamGenerator[List[AggregationResult]] | AsyncStreamGenerator[List[AggregationResult]]:
            A generator of the query results.
        Nr   r   r   r   r   stream  r/   zBaseAggregationQuery.streamr   )r   r)   rT   rd   r0   rO   )rr   r(   rT   rd   )rv   rw   rT   rd   )rT   r   )NNNN)r   r{   r|   r}   r~   r   rT   r   )r   r{   r|   r}   r~   r   rT   r   )
r   r   r   r   r|   r   r~   r   rT   r   )r$   r%   r&   r'   r    propertyrk   r;   rK   rS   rt   ry   r.   r   r1   r2   r   methodDEFAULTr   r   r   r   r   r   rc      s<    


	
	(	rc   )rT   rU   )(r'   
__future__r   r1   r   typingr   r   r   r   r   r	   r
   google.api_corer   r   retriesgoogle.cloud.firestore_v1r   $google.cloud.firestore_v1.field_pathr   google.cloud.firestore_v1.typesr   r   0google.cloud.firestore_v1.async_stream_generatorr   'google.cloud.firestore_v1.query_profiler   'google.cloud.firestore_v1.query_resultsr   *google.cloud.firestore_v1.stream_generatorr   objectr   r(   r3   rA   rQ   rb   rc   r   r   r   r   <module>   s.   $	
