o
    ÕèFhÛ  ã                   @   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   © úT/var/www/html/loop/nvenv/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>1   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)   3   s   
z Aggregate.get_source_expressionsc                    s   | j o| ¡ | _ tƒ  |¡S r,   )r   Úpopr   Úset_source_expressions)r   Úexprsr"   r#   r$   r/   9   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_aliasi   s   zAggregate.default_aliasc                 C   s   g S r,   r#   r*   r#   r#   r$   Úget_group_by_colsp   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   s   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      