o
    ˷e                      @   s   d Z ddlmZ ddl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)CaseFuncStar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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    R/var/www/ideatree/venv/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|||||_|sGt t| }t|D ]#\}}	|	jrF|  | }
t|
dr8|
jnt	|
}t
d|j||f q#|j }d u rP|S t|dr]|||||}d |_t|||jd}|j|_|S )Nnamez-Cannot compute %s('%s'): '%s' is an aggregateresolve_expression)output_field)r   r0   r   r   r'   	enumeratecontains_aggregatehasattrr/   reprr   r   r   r#   
is_summary)r   queryallow_joinsreuse	summarizefor_savecr   indexexprbefore_resolvedr/   r   coalescer    r!   r"   r0   <   s6   

zAggregate.resolve_expressionc                 C   sB   |   }t|dkrt|d drd|d j| j f S td)N   r   r/   z%s__%sz$Complex expressions require an alias)r'   lenr4   r/   lowerr   )r   r   r!   r!   r"   default_alias]   s   zAggregate.default_aliasc                 C   s   g S r*   r!   )r   aliasr!   r!   r"   get_group_by_colsd   s   zAggregate.get_group_by_colsc                    s   | j rdnd|d< | jrk|jjr<| j||\}}| 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)rH   r   r   )thenrA   )r   r   features supports_aggregate_filter_clauseas_sqlfilter_templategetrH   r   copyr'   r   r-   r   r   )r   compiler
connectionextra_context
filter_sqlfilter_paramsrH   sqlparamsrO   r+   	conditionr    r!   r"   rL   g   s8   

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"   rX      s   


zAggregate._get_repr_options)NTNFFr*   )r   
__module____qualname__rH   r3   r/   rM   window_compatibler   r   r   r)   r'   r-   r0   propertyrD   rF   rL   rX   __classcell__r!   r!   r    r"   r      s(    !

r   c                   @      e Zd ZdZd ZdZdS )r   AVGTNr   rZ   r[   functionr/   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   rZ   r[   rb   r/   r   r1   r   r   r   r^   r!   r!   r    r"   r      s    r   c                   @      e Zd ZdZd ZdS )r   MAXNr   rZ   r[   rb   r/   r!   r!   r!   r"   r          r   c                   @   ri   )r   MINNrk   r!   r!   r!   r"   r      rl   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rb   r   r   r   rh   sampler   r    r!   r"   r         zStdDev.__init__c                       i t   d| jdkiS )Nrt   rp   r   rX   rb   r(   r    r!   r"   rX         zStdDev._get_repr_optionsFr   rZ   r[   r/   r   rX   r^   r!   r!   r    r"   r          r   c                   @   r_   )r   SUMTNra   r!   r!   r!   r"   r      rc   r   c                       rn   )r   Fc                    ro   )NVAR_SAMPVAR_POPrr   rs   r    r!   r"   r      ru   zVariance.__init__c                    rv   )Nrt   r}   rw   r(   r    r!   r"   rX      rx   zVariance._get_repr_optionsry   rz   r!   r!   r    r"   r      r{   r   N)__doc__django.core.exceptionsr   django.db.models.expressionsr   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    r