o
     i%                     @  s   d Z ddlmZ ddlmZmZmZmZmZm	Z	 ddl
mZmZ ddl
mZ ddlmZmZmZ ddlmZ ddlmZ erPdd	lmZ dd
lmZ ddlm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.
    )annotations)TYPE_CHECKINGAny	GeneratorListOptionalUnion)
exceptionsgapic_v1)retry)AggregationResultBaseAggregationQuery_query_response_to_result)QueryResultsList)StreamGenerator)transaction)ExplainMetrics)ExplainOptionsc                      s   e Zd ZdZd fddZdejjdfddd ddZd!ddZ	dd Z
dejjddfd"ddZdejjdfddd#ddZ  ZS )$AggregationQueryz5Represents an aggregation query to the Firestore API.returnNonec                   s   t t| | d S N)superr   __init__)selfnested_query	__class__ /var/www/snowflake_co_dev_github/snow_flake_back_end_deploy/env/lib/python3.10/site-packages/google/cloud/firestore_v1/aggregation.pyr   .   s   zAggregationQuery.__init__N)explain_optionsr   "Union[retries.Retry, None, object]timeoutfloat | Noner    Optional[ExplainOptions]#QueryResultsList[AggregationResult]c                C  s@   d}| j ||||d}t|}|du rd}n| }t|||S )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[AggregationResult]: The aggregation query results.

        Nr   r   r"   r    )streamlistget_explain_metricsr   )r   r   r   r"   r    explain_metricsresultresult_listr   r   r   get4   s   "zAggregationQuery.getc                 C  s2   |  ||||\}}| jjjd|| jjd|S )!Helper method for :meth:`stream`.)requestmetadataNr   )_prep_stream_client_firestore_apirun_aggregation_query_rpc_metadata)r   r   r   r"   r    r/   kwargsr   r   r   _get_stream_iteratorg   s   
z%AggregationQuery._get_stream_iteratorc                 C  s8   |du r|t jju r| jjj}|j}|j}||S dS )r.   NF)	r
   methodDEFAULTr2   r3   
_transportr4   _retry
_predicate)r   excr   r   	transportgapic_callabler   r   r   _retry_query_after_exceptionv   s   

z-AggregationQuery._retry_query_after_exceptionr   !Optional[transaction.Transaction]Optional[float]AGenerator[List[AggregationResult], Any, Optional[ExplainMetrics]]c           
   
   c  s    d}|  ||||}	 zt|d}W n" tjy5 } z| |||r0|  |||}W Y d}~q d}~ww |du r=	 |S |du rG|jrG|j}t|}	|	rP|	V  q)a  Internal method for stream(). Runs the aggregation query.

        This sends a ``RunAggregationQuery`` RPC and then returns a generator
        which consumes each document returned in the stream of
        ``RunAggregationQueryResponse`` messages.

        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).

        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 (Optional[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.

        Yields:
            List[AggregationResult]:
            The result of aggregations of this query.

        Returns:
            (Optional[google.cloud.firestore_v1.types.query_profile.ExplainMetrtics]):
            The results of query profiling, if received from the service.

        N)r7   nextr	   GoogleAPICallErrorr@   r*   r   )
r   r   r   r"   r    metricsresponse_iteratorresponser=   r+   r   r   r   _make_stream   s>   '	zAggregationQuery._make_stream#Optional['transaction.Transaction'](StreamGenerator[List[AggregationResult]]c                C  s   | j ||||d}t||S )a  Runs the aggregation query.

        This sends a ``RunAggregationQuery`` RPC and then returns a generator
        which consumes each document returned in the stream of
        ``RunAggregationQueryResponse`` messages.

        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).

        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]]`:
            A generator of the query results.
        r&   )rI   r   )r   r   r   r"   r    inner_generatorr   r   r   r'      s   #
zAggregationQuery.stream)r   r   )r   r!   r"   r#   r    r$   r   r%   r   )
r   rA   r   r!   r"   rB   r    r$   r   rC   )
r   rJ   r   r!   r"   rB   r    r$   r   rK   )__name__
__module____qualname____doc__r   r
   r8   r9   r-   r7   r@   rI   r'   __classcell__r   r   r   r   r   +   s,    
3Kr   N)rP   
__future__r   typingr   r   r   r   r   r   google.api_corer	   r
   r   retries*google.cloud.firestore_v1.base_aggregationr   r   r   'google.cloud.firestore_v1.query_resultsr   *google.cloud.firestore_v1.stream_generatorr   google.cloud.firestore_v1r   'google.cloud.firestore_v1.query_profiler   r   r   r   r   r   r   <module>   s    