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 ddl
mZ ddlmZ ddlmZ ddlmZmZ dd	lmZ er]dd
lmZ ddlmZmZ ddlm  m  m  mZ  G dd deZ!dS )aq  Classes for representing Async aggregation queries for the Google Cloud Firestore API.

A :class:`~google.cloud.firestore_v1.async_aggregation.AsyncAggregationQuery` can be created directly from
a :class:`~google.cloud.firestore_v1.async_collection.AsyncCollection` and that can be
a more common way to create an aggregation query than direct usage of the constructor.
    )annotations)TYPE_CHECKINGAnyAsyncGeneratorListOptionalUnion)gapic_v1)retry_async)transaction)AsyncStreamGenerator)BaseAggregationQuery_query_response_to_result)QueryResultsList)AggregationResult)ExplainMetricsExplainOptionsNc                      sr   e Zd ZdZd fddZdejjdfddd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 )AsyncAggregationQueryz5Represents 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/async_aggregation.pyr   -   s   zAsyncAggregationQuery.__init__N)explain_optionsretry'Union[retries.AsyncRetry, None, object]timeoutfloat | Noner   Optional[ExplainOptions])QueryResultsList[List[AggregationResult]]c                  sx   d}| j ||||d}z!dd |2 I dH }|du rd}n| I dH }W | I dH  n| I dH  w 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[List[AggregationResult]]: The aggregation query results.

        Nr   r    r"   r   c                   s   g | z3 d H W }|q6 S r   r   ).0aggregationr   r   r   
<listcomp>\   s    z-AsyncAggregationQuery.get.<locals>.<listcomp>)streamget_explain_metricsacloser   )r   r   r    r"   r   explain_metricsstream_resultresultr   r   r   get3   s      zAsyncAggregationQuery.getr   !Optional[transaction.Transaction]"retries.AsyncRetry | object | NoneOptional[float]NAsyncGenerator[List[AggregationResult] | query_profile_pb.ExplainMetrics, Any]c                 C sv   |  ||||\}}| jjjd|| jjd|I dH }|2 z3 dH W }t|}	|	r.|	V  |jr7|j}
|
V  q6 dS )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 the 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] | query_profile_pb.ExplainMetrics:
            The result of aggregations of this query. Query results will be
            yielded as `List[AggregationResult]`. When the result contains
            returned explain metrics, yield `query_profile_pb.ExplainMetrics`
            individually.
        )requestmetadataNr   )_prep_stream_client_firestore_apirun_aggregation_query_rpc_metadatar   r-   )r   r   r    r"   r   r5   kwargsresponse_iteratorresponser/   metricsr   r   r   _make_streamg   s,   &
z"AsyncAggregationQuery._make_stream-AsyncStreamGenerator[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 the 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.

        Returns:
            `AsyncStreamGenerator[List[AggregationResult]]`:
                A generator of the query results.
        r&   )r@   r   )r   r   r    r"   r   inner_generatorr   r   r   r*      s   $
zAsyncAggregationQuery.stream)r   r   )r    r!   r"   r#   r   r$   r   r%   )
r   r1   r    r2   r"   r3   r   r$   r   r4   )
r   r1   r    r2   r"   r3   r   r$   r   rA   )__name__
__module____qualname____doc__r   r	   methodDEFAULTr0   r@   r*   __classcell__r   r   r   r   r   *   s(    6>r   )"rF   
__future__r   typingr   r   r   r   r   r   google.api_corer	   r
   retriesgoogle.cloud.firestore_v1r   0google.cloud.firestore_v1.async_stream_generatorr   *google.cloud.firestore_v1.base_aggregationr   r   'google.cloud.firestore_v1.query_resultsr   r   'google.cloud.firestore_v1.query_profiler   r   -google.cloud.firestore_v1.types.query_profilecloudfirestore_v1typesquery_profilequery_profile_pbr   r   r   r   r   <module>   s    