

_V                 @   sL  d  Z  d d l Z d d l Z d d l m Z m Z d d l m Z d d l m	 Z	 d d l
 m Z d d d	 d
 d d d d g Z e j d e	 d d Gd d   d e  Z Gd d   d e  Z Gd d	   d	 e  Z Gd d
   d
 e  Z Gd d   d e  Z Gd d   d e  Z Gd d   d e  Z Gd d   d e  Z d S)z:
Classes to represent the default SQL aggregate functions
    N)
FloatFieldIntegerField)RegisterLookupMixin)RemovedInDjango110Warning)cached_property	AggregateAvgCountMaxMinStdDevSumVariancezWdjango.db.models.sql.aggregates is deprecated. Use django.db.models.aggregates instead.
stacklevel   c               @   s   e  Z d  Z d Z d Z d Z d Z d d d d  Z e d d    Z	 e d	 d
    Z
 d d   Z d d   Z d d   Z e d d    Z d S)r   z 
    Default SQL Aggregate.
    Fz%(function)s(%(field)s)Nc             K   s   | |  _  | |  _ | |  _ | |  _ |  } x^ | r t | t  r t | d d  r` |  j } q- t | d d  r~ |  j } q- | j } q- W| |  _	 d S)aJ  Instantiate an SQL aggregate

         * col is a column reference describing the subject field
           of the aggregate. It can be an alias, or a tuple describing
           a table and column name.
         * source is the underlying field or aggregate definition for
           the column reference. If the aggregate is not an ordinal or
           computed type, this reference is used to determine the coerced
           output type of the aggregate.
         * extra is a dictionary of additional data to provide for the
           aggregate definition

        Also utilizes the class variables:
         * sql_function, the name of the SQL function that implements the
           aggregate.
         * sql_template, a template string that is used to render the
           aggregate into SQL.
         * is_ordinal, a boolean indicating if the output of this aggregate
           is an integer (e.g., a count)
         * is_computed, a boolean indicating if this output of this aggregate
           is a computed float (e.g., an average), regardless of the input
           type.
        
is_ordinalFis_computedN)
colsource
is_summaryextra
isinstancer   getattr_ordinal_aggregate_field_computed_aggregate_fieldfield)selfr   r   r   r   tmp r   A/tmp/pip-build-ghmbqnp_/Django/django/db/models/sql/aggregates.py__init__   s    				zAggregate.__init__c             C   s   t    S)N)r   )r   r   r   r   r   K   s    z"Aggregate._ordinal_aggregate_fieldc             C   s   t    S)N)r   )r   r   r   r   r   O   s    z#Aggregate._computed_aggregate_fieldc             C   s^   t  j  |   } t |  j t t f  rZ | j |  j d |  j d  |  j d f | _ n  | S)Nr      )copyr   r   listtupleget)r   Z
change_mapcloner   r   r   relabeled_cloneS   s    3zAggregate.relabeled_clonec                s   g  } t  |  j d  r6 |  j j   |  \ } } nO t |  j t t f  rv d j   f d d   |  j D  } n   |  j  } i |  j d 6| d 6} | j |  j	  |  j
 | | f S)z6Return the aggregate, rendered as SQL with parameters.as_sql.c             3   s   |  ] }   |  Vq d  S)Nr   ).0c)compilerr   r   	<genexpr>`   s    z#Aggregate.as_sql.<locals>.<genexpr>functionr   )hasattrr   r(   r   r#   r$   joinsql_functionupdater   sql_template)r   r,   
connectionparams
field_nameZsubstitutionsr   )r,   r   r(   Y   s    (

zAggregate.as_sqlc             C   s   g  S)Nr   )r   r   r   r   get_group_by_colsl   s    zAggregate.get_group_by_colsc             C   s   |  j  S)N)r   )r   r   r   r   output_fieldo   s    zAggregate.output_field)__name__
__module____qualname____doc__r   r   r3   r    r   r   r   r'   r(   r7   propertyr8   r   r   r   r   r      s   .c               @   s   e  Z d  Z d Z d Z d S)r   TZAVGN)r9   r:   r;   r   r1   r   r   r   r   r   t   s   c                   s7   e  Z d  Z d Z d Z d Z d   f d d  Z   S)r	   TZCOUNTz#%(function)s(%(distinct)s%(field)s)Fc                s/   t  t |   j | d | r! d n d | d  S)Ndistinctz	DISTINCT  )superr	   r    )r   r   r>   r   )	__class__r   r   r    ~   s    zCount.__init__)r9   r:   r;   r   r1   r3   r    r   r   )rA   r   r	   y   s   c               @   s   e  Z d  Z d Z d S)r
   MAXN)r9   r:   r;   r1   r   r   r   r   r
      s   c               @   s   e  Z d  Z d Z d S)r   ZMINN)r9   r:   r;   r1   r   r   r   r   r      s   c                   s+   e  Z d  Z d Z d   f d d  Z   S)r   TFc                s2   t  t |   j | |  | r% d n d |  _ d  S)NZSTDDEV_SAMPZ
STDDEV_POP)r@   r   r    r1   )r   r   sampler   )rA   r   r   r       s    zStdDev.__init__)r9   r:   r;   r   r    r   r   )rA   r   r      s   c               @   s   e  Z d  Z d Z d S)r   ZSUMN)r9   r:   r;   r1   r   r   r   r   r      s   c                   s+   e  Z d  Z d Z d   f d d  Z   S)r   TFc                s2   t  t |   j | |  | r% d n d |  _ d  S)NZVAR_SAMPZVAR_POP)r@   r   r    r1   )r   r   rC   r   )rA   r   r   r       s    zVariance.__init__)r9   r:   r;   r   r    r   r   )rA   r   r      s   )r<   r"   warningsZdjango.db.models.fieldsr   r   Zdjango.db.models.query_utilsr   Zdjango.utils.deprecationr   Zdjango.utils.functionalr   __all__warnr   r   r	   r
   r   r   r   r   r   r   r   r   <module>   s$   _	