ó
Žâ0_c           @   s  d  Z  d d l m Z d d l m Z m Z d d l m Z m Z m	 Z	 d d d d d	 d
 d d g Z
 d e f d     YZ d e f d     YZ d e f d     YZ d e f d     YZ d	 e f d     YZ d
 e f d     YZ d e f d     YZ d e f d     YZ d S(   s>   
Classes to represent the definitions of aggregate functions.
i˙˙˙˙(   t
   FieldError(   t   Funct   Star(   t   DecimalFieldt
   FloatFieldt   IntegerFieldt	   Aggregatet   Avgt   Countt   Maxt   Mint   StdDevt   Sumt   Variancec           B   sD   e  Z e Z d Z d e d e e d   Z e d    Z	 d   Z
 RS(   c         C   sł   t  t |   j | | | |  } | sŻ | j   } xy t |  D]h \ } }	 |	 j r@ |  j   | }
 t |
 d  r} |
 j n	 t |
  } t	 d | j | | f   q@ q@ Wn  | S(   Nt   names-   Cannot compute %s('%s'): '%s' is an aggregate(
   t   superR   t   resolve_expressiont   get_source_expressionst	   enumeratet   contains_aggregatet   hasattrR   t   reprR    (   t   selft   queryt   allow_joinst   reuset	   summarizet   for_savet   ct   expressionst   indext   exprt   before_resolvedR   (    (    s=   /tmp/pip-unpacked-wheel-BAJOf3/django/db/models/aggregates.pyR      s    !	$&c         C   s_   |  j    } t |  d k rO t | d d  rO d | d j |  j j   f St d   d  S(   Ni   i    R   s   %s__%ss$   Complex expressions require an alias(   R   t   lenR   R   t   lowert	   TypeError(   R   R   (    (    s=   /tmp/pip-unpacked-wheel-BAJOf3/django/db/models/aggregates.pyt   default_alias   s    %c         C   s   g  S(   N(    (   R   (    (    s=   /tmp/pip-unpacked-wheel-BAJOf3/django/db/models/aggregates.pyt   get_group_by_cols$   s    N(   t   __name__t
   __module__t   TrueR   t   NoneR   t   FalseR   t   propertyR$   R%   (    (    (    s=   /tmp/pip-unpacked-wheel-BAJOf3/django/db/models/aggregates.pyR      s
   c           B   s&   e  Z d  Z d Z d   Z d   Z RS(   t   AVGR   c         C   sK   |  j    d } t | t t f  r4 t   |  _ n  t t |   j   d  S(   Ni    (	   t   get_source_fieldst
   isinstanceR   R   R   t   _output_fieldR   R   t   _resolve_output_field(   R   t   source_field(    (    s=   /tmp/pip-unpacked-wheel-BAJOf3/django/db/models/aggregates.pyR0   ,   s    c         C   ss   |  j  j   d k rZ |  j   d } d d l m } m } | j | t | |     St t |   j	 | |  S(   Nt   DurationFieldi    i˙˙˙˙(   t   IntervalToSecondst   SecondsToInterval(
   t   output_fieldt   get_internal_typeR   t#   django.db.backends.oracle.functionsR3   R4   t   compileR   R   t   as_sql(   R   t   compilert
   connectiont
   expressionR3   R4   (    (    s=   /tmp/pip-unpacked-wheel-BAJOf3/django/db/models/aggregates.pyt	   as_oracle2   s    (   R&   R'   t   functionR   R0   R=   (    (    (    s=   /tmp/pip-unpacked-wheel-BAJOf3/django/db/models/aggregates.pyR   (   s   	c           B   s8   e  Z d  Z d Z d Z e d  Z d   Z d   Z RS(   t   COUNTR   s)   %(function)s(%(distinct)s%(expressions)s)c         K   sP   | d k r t    } n  t t |   j | d | r9 d n d d t   | d  S(   Nt   *t   distincts	   DISTINCT t    R5   (   R   R   R   t   __init__R   (   R   R<   RA   t   extra(    (    s=   /tmp/pip-unpacked-wheel-BAJOf3/django/db/models/aggregates.pyRC   A   s    c         C   sK   d j  |  j j |  j j d   |  j D  |  j d d k rD d n d  S(   Ns   {}({}, distinct={})c         s   s   |  ] } t  |  Vq d  S(   N(   t   str(   t   .0t   arg(    (    s=   /tmp/pip-unpacked-wheel-BAJOf3/django/db/models/aggregates.pys	   <genexpr>J   s    RA   RB   R*   R(   (   t   formatt	   __class__R&   t
   arg_joinert   joint   source_expressionsRD   (   R   (    (    s=   /tmp/pip-unpacked-wheel-BAJOf3/django/db/models/aggregates.pyt   __repr__G   s    	c         C   s   | d  k r d St |  S(   Ni    (   R)   t   int(   R   t   valueR<   R;   t   context(    (    s=   /tmp/pip-unpacked-wheel-BAJOf3/django/db/models/aggregates.pyt   convert_valueN   s    (	   R&   R'   R>   R   t   templateR*   RC   RM   RQ   (    (    (    s=   /tmp/pip-unpacked-wheel-BAJOf3/django/db/models/aggregates.pyR   <   s   	c           B   s   e  Z d  Z d Z RS(   t   MAXR	   (   R&   R'   R>   R   (    (    (    s=   /tmp/pip-unpacked-wheel-BAJOf3/django/db/models/aggregates.pyR	   T   s   c           B   s   e  Z d  Z d Z RS(   t   MINR
   (   R&   R'   R>   R   (    (    (    s=   /tmp/pip-unpacked-wheel-BAJOf3/django/db/models/aggregates.pyR
   Y   s   c           B   s,   e  Z d  Z e d  Z d   Z d   Z RS(   R   c         K   s;   | r d n d |  _  t t |   j | d t   | d  S(   Nt   STDDEV_SAMPt
   STDDEV_POPR5   (   R>   R   R   RC   R   (   R   R<   t   sampleRD   (    (    s=   /tmp/pip-unpacked-wheel-BAJOf3/django/db/models/aggregates.pyRC   a   s    c         C   sG   d j  |  j j |  j j d   |  j D  |  j d k r@ d n d  S(   Ns   {}({}, sample={})c         s   s   |  ] } t  |  Vq d  S(   N(   RE   (   RF   RG   (    (    s=   /tmp/pip-unpacked-wheel-BAJOf3/django/db/models/aggregates.pys	   <genexpr>h   s    RV   R*   R(   (   RH   RI   R&   RJ   RK   RL   R>   (   R   (    (    s=   /tmp/pip-unpacked-wheel-BAJOf3/django/db/models/aggregates.pyRM   e   s    	c         C   s   | d  k r | St |  S(   N(   R)   t   float(   R   RO   R<   R;   RP   (    (    s=   /tmp/pip-unpacked-wheel-BAJOf3/django/db/models/aggregates.pyRQ   l   s    (   R&   R'   R   R*   RC   RM   RQ   (    (    (    s=   /tmp/pip-unpacked-wheel-BAJOf3/django/db/models/aggregates.pyR   ^   s   	c           B   s   e  Z d  Z d Z d   Z RS(   t   SUMR   c         C   ss   |  j  j   d k rZ |  j   d } d d l m } m } | j | t | |     St t |   j	 | |  S(   NR2   i    i˙˙˙˙(   R3   R4   (
   R5   R6   R   R7   R3   R4   R8   R   R   R9   (   R   R:   R;   R<   R3   R4   (    (    s=   /tmp/pip-unpacked-wheel-BAJOf3/django/db/models/aggregates.pyR=   v   s    (   R&   R'   R>   R   R=   (    (    (    s=   /tmp/pip-unpacked-wheel-BAJOf3/django/db/models/aggregates.pyR   r   s   c           B   s,   e  Z d  Z e d  Z d   Z d   Z RS(   R   c         K   s;   | r d n d |  _  t t |   j | d t   | d  S(   Nt   VAR_SAMPt   VAR_POPR5   (   R>   R   R   RC   R   (   R   R<   RW   RD   (    (    s=   /tmp/pip-unpacked-wheel-BAJOf3/django/db/models/aggregates.pyRC      s    c         C   sG   d j  |  j j |  j j d   |  j D  |  j d k r@ d n d  S(   Ns   {}({}, sample={})c         s   s   |  ] } t  |  Vq d  S(   N(   RE   (   RF   RG   (    (    s=   /tmp/pip-unpacked-wheel-BAJOf3/django/db/models/aggregates.pys	   <genexpr>   s    R[   R*   R(   (   RH   RI   R&   RJ   RK   RL   R>   (   R   (    (    s=   /tmp/pip-unpacked-wheel-BAJOf3/django/db/models/aggregates.pyRM      s    	c         C   s   | d  k r | St |  S(   N(   R)   RX   (   R   RO   R<   R;   RP   (    (    s=   /tmp/pip-unpacked-wheel-BAJOf3/django/db/models/aggregates.pyRQ      s    (   R&   R'   R   R*   RC   RM   RQ   (    (    (    s=   /tmp/pip-unpacked-wheel-BAJOf3/django/db/models/aggregates.pyR      s   	N(   t   __doc__t   django.core.exceptionsR    t   django.db.models.expressionsR   R   t   django.db.models.fieldsR   R   R   t   __all__R   R   R   R	   R
   R   R   R   (    (    (    s=   /tmp/pip-unpacked-wheel-BAJOf3/django/db/models/aggregates.pyt   <module>   s   