o
    lf                     @   s   d Z ddlmZmZ ddl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mZ g dZG dd	 d	eZG d
d dee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eZG dd deeZG dd deeZdS )z>
Classes to represent the definitions of aggregate functions.
    )
FieldErrorFullResultSet)CaseFuncStarValueWhen)IntegerField)Coalesce)FixDurationInputMixinNumericOutputFieldMixin)	AggregateAvgCountMaxMinStdDevSumVariancec                       s   e Zd ZdZdZdZdZdZdZdZ	dddd fdd
Z
 fd	d
Z fddZ fddZ	d fdd	Zedd Zdd Z fddZ fddZ  ZS )r   z)%(function)s(%(distinct)s%(expressions)s)TNz%s FILTER (WHERE %%(filter)s)F)distinctfilterdefaultc                   sf   |r| j std| jj |d ur| jd urt| jj d|| _|| _|| _t j	|i | d S )Nz%s does not allow distinct.z does not allow default.)
allow_distinct	TypeError	__class____name__empty_result_set_valuer   r   r   super__init__)selfr   r   r   expressionsextrar    _/var/www/ticemtrilhas/avaliacao_env/lib/python3.10/site-packages/django/db/models/aggregates.pyr   "   s   
zAggregate.__init__c                    s   dd t   D S )Nc                 S   s   g | ]}|j qS r#   )_output_field_or_none).0er#   r#   r$   
<listcomp>0   s    z/Aggregate.get_source_fields.<locals>.<listcomp>)r   get_source_expressionsr   r"   r#   r$   get_source_fields.   s   zAggregate.get_source_fieldsc                    s    t   }| jr|| jg S |S N)r   r)   r   )r   source_expressionsr"   r#   r$   r)   2   s   
z Aggregate.get_source_expressionsc                    s   | j o| | _ t |S r,   )r   popr   set_source_expressions)r   exprsr"   r#   r$   r/   8   s   z Aggregate.set_source_expressionsc              	      s<  t  ||||}|jo|j|||||_|r5| D ]}|j| jr3td|j d| d| dqn2| jsgt t|	 }t
|D ]#\}	}
|
jrf| 	 |	 }t|drX|jnt|}td|j||f qC|j }d u rp|S t|dr|||||}|jd u r|j|_nt||j}d |_t|||jd}|j|_|S )	NzCannot compute z('z'): 'z' is an aggregatenamez-Cannot compute %s('%s'): '%s' is an aggregateresolve_expression)output_field)r   r2   r   get_refsannotations
is_summaryr   r1   r   r)   	enumeratecontains_aggregatehasattrreprr   r%   r3   r   r
   )r   queryallow_joinsreuse	summarizefor_savecrefr    indexexprbefore_resolvedr1   r   coalescer"   r#   r$   r2   <   sN   


zAggregate.resolve_expressionc                 C   sB   |   }t|dkrt|d drd|d j| j f S td)N   r   r1   z%s__%sz$Complex expressions require an alias)r)   lenr9   r1   lowerr   )r   r    r#   r#   r$   default_aliash   s   zAggregate.default_aliasc                 C   s   g S r,   r#   r*   r#   r#   r$   get_group_by_colso   s   zAggregate.get_group_by_colsc                    s  | j rdnd|d< | jrw|jjrHz| j||\}}W n	 ty$   Y nSw | j|d| j }t	 j||f||d|\}}|g ||R fS | 
 }	d |	_|	 }
t| j|
d d}|	t|g|
dd    t	t|	j||fi |S t	 j||fi |S )	Nz	DISTINCT  r   template)rL   r   r   )thenrF   )r   r   features supports_aggregate_filter_clauseas_sqlr   filter_templategetrL   r   copyr)   r   r/   r   r   )r   compiler
connectionextra_context
filter_sqlfilter_paramsrL   sqlparamsrS   r-   	conditionr"   r#   r$   rP   r   s@   

zAggregate.as_sqlc                    s.   t   }| jr| j|d< | jr| j|d< |S )Nr   r   )r   _get_repr_optionsr   r   )r   optionsr"   r#   r$   r\      s   


zAggregate._get_repr_options)NTNFF)r   
__module____qualname__rL   r8   r1   rQ   window_compatibler   r   r   r+   r)   r/   r2   propertyrI   rJ   rP   r\   __classcell__r#   r#   r"   r$   r      s(    ,
r   c                   @      e Zd ZdZd ZdZdS )r   AVGTNr   r^   r_   functionr1   r   r#   r#   r#   r$   r          r   c                       s4   e Zd ZdZd Ze ZdZdZd fdd	Z	  Z
S )r   COUNTTr   Nc                    sD   |dkrt  }t|t r|d urtdt j|fd|i| d S )N*z8Star cannot be used with filter. Please specify a field.r   )r   
isinstance
ValueErrorr   r   )r   
expressionr   r!   r"   r#   r$   r      s
   zCount.__init__r,   )r   r^   r_   rf   r1   r	   r3   r   r   r   rb   r#   r#   r"   r$   r      s    r   c                   @      e Zd ZdZd ZdS )r   MAXNr   r^   r_   rf   r1   r#   r#   r#   r$   r          r   c                   @   rm   )r   MINNro   r#   r#   r#   r$   r      rp   r   c                       .   e Zd Zd Zd fdd	Z fddZ  ZS )r   Fc                    &   |rdnd| _ t j|fi | d S )NSTDDEV_SAMP
STDDEV_POPrf   r   r   r   rl   sampler!   r"   r#   r$   r         zStdDev.__init__c                       i t   d| jdkiS )Nrx   rt   r   r\   rf   r*   r"   r#   r$   r\         zStdDev._get_repr_optionsFr   r^   r_   r1   r   r\   rb   r#   r#   r"   r$   r          r   c                   @   rc   )r   SUMTNre   r#   r#   r#   r$   r      rg   r   c                       rr   )r   Fc                    rs   )NVAR_SAMPVAR_POPrv   rw   r"   r#   r$   r      ry   zVariance.__init__c                    rz   )Nrx   r   r{   r*   r"   r#   r$   r\      r|   zVariance._get_repr_optionsr}   r~   r#   r#   r"   r$   r      r   r   N)__doc__django.core.exceptionsr   r   django.db.models.expressionsr   r   r   r   r   django.db.models.fieldsr	   %django.db.models.functions.comparisonr
   !django.db.models.functions.mixinsr   r   __all__r   r   r   r   r   r   r   r   r#   r#   r#   r$   <module>   s      