o
    {,h                      @   s   d Z ddlmZmZ ddlmZ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ColPairs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    Q/var/www/html/rh/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>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g S N)r   r*   r   )r    source_expressionsr#   r$   r%   r*   3   s   
z Aggregate.get_source_expressionsc                    s   |^ }| _ t |S r-   )r   r   set_source_expressions)r    exprsr#   r$   r%   r/   7   s   z Aggregate.set_source_expressionsc              	      s@  t  ||||}|jr|j||||nd |_|r7| D ]}|j| jr5td|j d| d| dqn2| jsit t|	 }t
|D ]#\}	}
|
jrh| 	 |	 }t|drZ|jnt|}td|j||f qE|j }d u rr|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   ;   sP   


zAggregate.resolve_expressionc                 C   sL   dd |   D }t|dkr"t|d dr"d|d j| j f S td)Nc                 S   s   g | ]}|d ur|qS r-   r$   )r'   rC   r$   r$   r%   r)   k   s    z+Aggregate.default_alias.<locals>.<listcomp>   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_colsr   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   u   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ZdS )r   AVGTrF   Nr   r^   r_   functionr1   r   arityr$   r$   r$   r%   r      
    r   c                       sH   e Zd ZdZd Ze ZdZdZdZ	dZ
d
 fdd	Z fdd	Z  ZS )r   COUNTTr   rF   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__c                    sR   t  j|i |}|jd }t|tr'| jrtd| }t|d |j	dS |S )Nr   z6COUNT(DISTINCT) doesn't support composite primary keys)r   )
r   r2   r.   rk   r   r   rl   get_colsr   r   )r    argskwargsresultrC   colsr#   r$   r%   r2      s   

zCount.resolve_expressionr-   )r   r^   r_   rf   r1   r
   r3   r   r   rg   allows_composite_expressionsr   r2   rb   r$   r$   r#   r%   r      s    r   c                   @      e Zd ZdZd ZdZdS )r   MAXrF   Nr   r^   r_   rf   r1   rg   r$   r$   r$   r%   r          r   c                   @   rt   )r   MINrF   Nrv   r$   r$   r$   r%   r      rw   r   c                       2   e Zd Zd ZdZd fdd	Z fddZ  ZS )r   rF   Fc                    &   |rdnd| _ t j|fi | d S )NSTDDEV_SAMP
STDDEV_POPrf   r   r   r    rm   sampler"   r#   r$   r%   r         zStdDev.__init__c                       i t   d| jdkiS )Nr   r{   r   r\   rf   r+   r#   r$   r%   r\         zStdDev._get_repr_optionsFr   r^   r_   r1   rg   r   r\   rb   r$   r$   r#   r%   r      
    r   c                   @   rc   )r   SUMTrF   Nre   r$   r$   r$   r%   r      rh   r   c                       ry   )r   rF   Fc                    rz   )NVAR_SAMPVAR_POPr}   r~   r#   r$   r%   r      r   zVariance.__init__c                    r   )Nr   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   r	   django.db.models.fieldsr
   django.db.models.functionsr   !django.db.models.functions.mixinsr   r   __all__r   r   r   r   r   r   r   r   r$   r$   r$   r%   <module>   s       !