o
    ¥g                     @   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    X/var/www/rescue_company/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rg|||||}|jd u rf|j|_nt||j}d |_t|||jd}|j|_|S )Nnamez-Cannot compute %s('%s'): '%s' is an aggregateresolve_expression)output_field)r   r2   r   r   r)   	enumeratecontains_aggregatehasattrr1   reprr   r   r%   r3   r   r
   
is_summary)r   queryallow_joinsreuse	summarizefor_savecr    indexexprbefore_resolvedr1   r   coalescer"   r#   r$   r2   <   s>   


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)   lenr6   r1   lowerr   )r   r    r#   r#   r$   default_aliasa   s   zAggregate.default_aliasc                 C   s   g S r,   r#   r*   r#   r#   r$   get_group_by_colsh   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)rI   r   r   )thenrC   )r   r   features supports_aggregate_filter_clauseas_sqlr   filter_templategetrI   r   copyr)   r   r/   r   r   )r   compiler
connectionextra_context
filter_sqlfilter_paramsrI   sqlparamsrP   r-   	conditionr"   r#   r$   rM   k   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$   rY      s   


zAggregate._get_repr_options)NTNFF)r   
__module____qualname__rI   r5   r1   rN   window_compatibler   r   r   r+   r)   r/   r2   propertyrF   rG   rM   rY   __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\   rc   r1   r	   r3   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   r[   r\   rc   r1   r#   r#   r#   r$   r          r   c                   @   rj   )r   MINNrl   r#   r#   r#   r$   r      rm   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rc   r   r   r   ri   sampler!   r"   r#   r$   r         zStdDev.__init__c                       i t   d| jdkiS )Nru   rq   r   rY   rc   r*   r"   r#   r$   rY         zStdDev._get_repr_optionsFr   r[   r\   r1   r   rY   r_   r#   r#   r"   r$   r          r   c                   @   r`   )r   SUMTNrb   r#   r#   r#   r$   r      rd   r   c                       ro   )r   Fc                    rp   )NVAR_SAMPVAR_POPrs   rt   r"   r#   r$   r      rv   zVariance.__init__c                    rw   )Nru   r~   rx   r*   r"   r#   r$   rY      ry   zVariance._get_repr_optionsrz   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    z