
    (\i4                        d Z ddlmZmZ ddlmZ ddlmZmZm	Z	m
Z
mZmZmZ ddlmZmZ ddlmZ ddlmZmZ g dZ G d	 d
e	      Z G d de
      Z G d de	      Z G d de      Z G d deee      Z G d de      Z G d de      Z G d de      Z G d dee      Z G d de	      Z  G d de      Z! G d d ee      Z" G d! d"ee      Z#y#)$z>
Classes to represent the definitions of aggregate functions.
    )
FieldErrorFullResultSet)NotSupportedError)CaseColPairsFuncOrderByListStarValueWhen)IntegerField	TextField)Coalesce)FixDurationInputMixinNumericOutputFieldMixin)
	AggregateAnyValueAvgCountMaxMinStdDev	StringAggSumVariancec                   <     e Zd ZdZdZ fdZed        Zd Z xZ	S )AggregateFilter   z FILTER (WHERE %(expressions)s)c                     |j                   j                  st        d      	 t        |   ||fi |S # t
        $ r Y yw xY w)NzDAggregate filter clauses are not supported on this database backend.)  )features supports_aggregate_filter_clauser   superas_sqlr   selfcompiler
connectionextra_context	__class__s       k/home/devuser/fomin/mqttuser/mqtt_project/myenv/lib/python3.12/site-packages/django/db/models/aggregates.pyr%   zAggregateFilter.as_sql)   sO    ""CC#V 	7>(JH-HH 		s   5 	A Ac                      | j                   d   S )Nr   )source_expressionsr'   s    r,   	conditionzAggregateFilter.condition3   s    &&q))    c                 Z    | j                   j                  d | j                  D              S )Nc              3   2   K   | ]  }t        |        y wN)str).0args     r,   	<genexpr>z*AggregateFilter.__str__.<locals>.<genexpr>8   s     #PCH#Ps   )
arg_joinerjoinr.   r/   s    r,   __str__zAggregateFilter.__str__7   s#    ###P8O8O#PPPr1   )
__name__
__module____qualname__aritytemplater%   propertyr0   r;   __classcell__r+   s   @r,   r   r   %   s,    E0H * *Qr1   r   c                   "     e Zd ZdZ fdZ xZS )AggregateOrderByz ORDER BY %(expressions)sc                 h    |j                   j                  st        d      t        |   ||fi |S )NzIThis database backend does not support specifying an order on aggregates.)r"   "supports_aggregate_order_by_clauser   r$   r%   r&   s       r,   r%   zAggregateOrderBy.as_sql>   s<    ""EE# 
 w~h
DmDDr1   )r<   r=   r>   r@   r%   rB   rC   s   @r,   rE   rE   ;   s    *HE Er1   rE   c                        e Zd ZdZdZdZdZdZdZdZ	ddddd fd
Z
 fdZ fdZ fd	Z	 d fd
	Zed        Zd Z fdZ fdZ xZS )r   ?%(function)s(%(distinct)s%(expressions)s%(order_by)s)%(filter)sTNF)distinctfilterdefaultorder_byc                   |r.| j                   s"t        d| j                  j                  z        |r.| j                  s"t        d| j                  j                  z        |.| j
                  "t        | j                  j                   d      || _        |d n
t        |      | _        || _	        t        j                  | j                  j                   d|      | _        t        | 8  |i | y )Nz%s does not allow distinct.z%s does not allow order_by.z does not allow default.z	.order_by)allow_distinct	TypeErrorr+   r<   allow_order_byempty_result_set_valuerJ   r   rK   rL   rE   
from_paramrM   r$   __init__)r'   rJ   rK   rL   rM   expressionsextrar+   s          r,   rT   zAggregate.__init__Q   s     D//9DNN<S<SSTTD//9DNN<S<SSTT4#>#>#Jt~~6677OPQQ $nd/&2I(33~~&&'y18
 	+//r1   c                 Z    t         |          D cg c]  }|j                   c}S c c}w r4   )r$   get_source_expressions_output_field_or_none)r'   er+   s     r,   get_source_fieldszAggregate.get_source_fieldsi   s'     271O1QRA''RRRs   (c                 X    t         |          }g || j                  | j                  S r4   )r$   rX   rK   rM   )r'   r.   r+   s     r,   rX   z Aggregate.get_source_expressionsn   s/    "W;=@#@T[[@$--@@r1   c                 B    |^ }| _         | _        t        |   |      S r4   )rK   rM   r$   set_source_expressions)r'   exprsr+   s     r,   r^   z Aggregate.set_source_expressionsr   s#    -2*T]w-e44r1   c           
         t         |   ||||      }|rN|j                         D ]:  }|j                  |   j                  st        d|j                   d| d| d       n| j                  st         t        |#         }t        |      D ]f  \  }	}
|
j                  s| j                         |	   }t        |d      r|j                  n
t        |      }t        d|j                  d|d|d       |j                  x}|S t        |d      r2|j                  ||||      }|j                  (|j                  |_        nt!        ||j                        }d |_        t#        |||j                        }|j                  |_        |S )NzCannot compute z('z'): 'z' is an aggregatenameresolve_expression)output_field)r$   rb   get_refsannotations
is_summaryr   ra   r   rX   	enumeratecontains_aggregatehasattrreprrL   rY   rc   r   r   )r'   queryallow_joinsreuse	summarizefor_savecrefrU   indexexprbefore_resolvedra   rL   coalescer+   s                  r,   rb   zAggregate.resolve_expressionv   s    G&uk5)Lzz| $$S)44$)!&&C5cUBST 
   	1DFK(5 t**&*&A&A&CE&JO #?F; (,,!/2 
 %664/  yy G)H70100UIVG,,4'('>'>$GQ%<%<=G	AwQ5L5LMllr1   c                     | j                         D cg c]  }||	 }}t        |      dk(  r;t        |d   d      r,|d   j                  d| j                  j	                         S t        d      c c}w )Nr   r   ra   __z$Complex expressions require an alias)rX   lenri   ra   lowerrP   )r'   rs   rU   s      r,   default_aliaszAggregate.default_alias   sy     "88:
d>ND
 
 {q W[^V%D*1~22DIIOO4EFF>??
s
   A5A5c                     g S r4   r!   r/   s    r,   get_group_by_colszAggregate.get_group_by_cols   s    	r1   c                    | j                   rH|j                  j                  s2t        t        |                dkD  rt        | j                   d      | j                   rdnd}d}g }d}g }| j                  x}	|j                  |	      \  }}| j                  	 |j                  | j                        \  }}|j#                  |||       t	        | @  ||fi |\  }}|g |||fS # t        $ r | j                         }
d |
_
        |
j                         }t        | j                  j                  |d         }|
j                  t        |      g|dd  z           |
j                   ||fi |cY S w xY w)Nr   zN does not support distinct with multiple expressions on this database backend.z	DISTINCT r    r   )then)rJ   rK   rM   )rJ   r"   -supports_aggregate_distinct_multiple_argumentrx   r$   rX   r   ra   rM   compilerK   copyr   r0   r^   r   r%   update)r'   r(   r)   r*   distinct_sqlorder_by_sqlorder_by_params
filter_sqlfilter_paramsrM   r   r.   r0   sqlparamsr+   s                  r,   r%   zAggregate.as_sql   s   MM''UUEG2459#99+ ) * 
 '+mm{
%H2,4,<,<X,F)L/;;"
J,4,<,<T[[,I)
M 	!! 	 	

 gnXzK]KV?f?????! % J yy{"%)%@%@%B" !6!6=OPQ=RS	++T)_,=@RSTSU@V,VW"t{{8ZI=IIJs   C. .B	E:9E:c                     t         |          }| j                  r| j                  |d<   | j                  r| j                  |d<   | j                  r| j                  |d<   |S )NrJ   rK   rM   )r$   _get_repr_optionsrJ   rK   rM   )r'   optionsr+   s     r,   r   zAggregate._get_repr_options   sT    '+-=="&--GJ;; $GH=="&--GJr1   )NTNFF)r<   r=   r>   r@   rh   ra   window_compatiblerO   rQ   rR   rT   r[   rX   r^   rb   rA   rz   r|   r%   r   rB   rC   s   @r,   r   r   H   s    PHDNN!
 00S
A5
 SX'R @ @'@R r1   r   c                   .     e Zd ZdZd ZdZdZ fdZ xZS )r   	ANY_VALUEr   Fc                 h    |j                   j                  st        d      t        |   ||fi |S )Nz4ANY_VALUE is not supported on this database backend.)r"   supports_any_valuer   r$   r%   r&   s       r,   r%   zAnyValue.as_sql   s:    ""55#F  w~h
DmDDr1   )	r<   r=   r>   functionra   r?   r   r%   rB   rC   s   @r,   r   r      s$    HDEE Er1   r   c                       e Zd ZdZd ZdZdZy)r   AVGTr   Nr<   r=   r>   r   ra   rO   r?   r!   r1   r,   r   r          HDNEr1   r   c                   P     e Zd ZdZd Z e       ZdZdZdZ	dZ
d fd	Z fdZ xZS )r   COUNTTr   r   c                     |dk(  r
t               }t        |t               r|t        d      t        |   |fd|i| y )N*z8Star cannot be used with filter. Please specify a field.rK   )r
   
isinstance
ValueErrorr$   rT   )r'   
expressionrK   rV   r+   s       r,   rT   zCount.__init__   sD    Jj$'F,>WXX<F<e<r1   c                     t        |   |i |}|j                         }t        |d   x}t              r>| j
                  rt        d      |j                         d   |d<   |j                  |       |S )Nr   z6COUNT(DISTINCT) doesn't support composite primary keys)	r$   rb   rX   r   r   rJ   r   get_colsr^   )r'   argskwargsresultr.   rs   r+   s         r,   rb   zCount.resolve_expression  s    +T<V<#::< 033dX>}} L  %)MMOA$6q!))*<=r1   r4   )r<   r=   r>   r   ra   r   rc   rO   rR   r?   allows_composite_expressionsrT   rb   rB   rC   s   @r,   r   r      s9    HD>LNE#' = r1   r   c                       e Zd ZdZd ZdZy)r   MAXr   Nr<   r=   r>   r   ra   r?   r!   r1   r,   r   r         HDEr1   r   c                       e Zd ZdZd ZdZy)r   MINr   Nr   r!   r1   r,   r   r     r   r1   r   c                   2     e Zd Zd ZdZd fd	Z fdZ xZS )r   r   c                 <    |rdnd| _         t        |   |fi | y )NSTDDEV_SAMP
STDDEV_POPr   r$   rT   r'   r   samplerV   r+   s       r,   rT   zStdDev.__init__%  s     )/\-u-r1   c                 F    i t         |          d| j                  dk(  iS )Nr   r   r$   r   r   r'   r+   s    r,   r   zStdDev._get_repr_options)  s&    X%'+-Xx-9WXXr1   Fr<   r=   r>   ra   r?   rT   r   rB   rC   s   @r,   r   r   !  s    DE.Y Yr1   r   c                   ,     e Zd ZdZdZ fdZd Z xZS )StringAggDelimiterr   z%(expressions)sc                 2    || _         t        | 	  |       y r4   )valuer$   rT   )r'   r   r+   s     r,   rT   zStringAggDelimiter.__init__1  s    
r1   c                 2    d} | j                   ||fd|i|S )Nz SEPARATOR %(expressions)sr@   )r%   r'   r(   r)   r*   r@   s        r,   as_mysqlzStringAggDelimiter.as_mysql5  s4    /t{{
 
 	
 	
r1   )r<   r=   r>   r?   r@   rT   r   rB   rC   s   @r,   r   r   -  s    E H 
r1   r   c                   R     e Zd ZdZdZd ZdZdZ e       Z	 fdZ
d Zd Zd Z xZS )r   rI   
STRING_AGGTc                 \    t        |      | _        t        |   || j                  fi | y r4   )r   	delimiterr$   rT   )r'   r   r   rV   r+   s       r,   rT   zStringAgg.__init__H  s'    +I6T^^=u=r1   c                 R    | j                   rd}nd} | j                  ||fd|d|S )NzO%(function)s(%(distinct)s%(expressions)s) WITHIN GROUP (%(order_by)s)%(filter)sz3%(function)s(%(distinct)s%(expressions)s)%(filter)sLISTAGG)r   r@   )rM   r%   r   s        r,   	as_oraclezStringAgg.as_oracleL  sK    == 
 MHt{{
 	

 
 	
r1   c                    d|d<   d}||d<   | j                         }g }|j                  r7|j                  |j                        \  }}|j                  d d |_        ||d<    |j                  ||fi |\  }}	|g |	|fS )NGROUP_CONCATr   zB%(function)s(%(distinct)s%(expressions)s%(order_by)s%(delimiter)s)r@   r   )r   r   r   r.   r%   )
r'   r(   r)   r*   r@   rp   delimiter_paramsdelimiter_sqlr   r   s
             r,   r   zStringAgg.as_mysql]  s    $2j!W$,j!IIK
 ;;.6.>.>q{{.K+M+#$#7#7#<A )6M+&ahhxE}EV0f0/000r1   c                 |    |j                         dk  r | j                  ||fddi|S  | j                  ||fi |S )N)   ,   r   r   )get_database_versionr%   )r'   r(   r)   r*   s       r,   	as_sqlitezStringAgg.as_sqlites  sZ    **,w64;; (  	  t{{8ZA=AAr1   )r<   r=   r>   r@   r   ra   rO   rQ   r   rc   rT   r   r   r   rB   rC   s   @r,   r   r   @  s8    PHHDNN;L>
"1,	Br1   r   c                       e Zd ZdZd ZdZdZy)r   SUMTr   Nr   r!   r1   r,   r   r     r   r1   r   c                   2     e Zd Zd ZdZd fd	Z fdZ xZS )r   r   c                 <    |rdnd| _         t        |   |fi | y )NVAR_SAMPVAR_POPr   r   s       r,   rT   zVariance.__init__  s     &,
)-u-r1   c                 F    i t         |          d| j                  dk(  iS )Nr   r   r   r   s    r,   r   zVariance._get_repr_options  s&    U%'+-Ux*9TUUr1   r   r   rC   s   @r,   r   r     s    DE.V Vr1   r   N)$__doc__django.core.exceptionsr   r   	django.dbr   django.db.models.expressionsr   r   r   r	   r
   r   r   django.db.models.fieldsr   r   django.db.models.functionsr   !django.db.models.functions.mixinsr   r   __all__r   rE   r   r   r   r   r   r   r   r   r   r   r   r!   r1   r,   <module>r      s    = '   < /
Qd Q,
E{ 
ET TnEy E
!8) I B) ) 	Y$i 	Y
 
&<B	 <B~
 	V&	 	Vr1   