<s> """<STR_LIT>""" <EOL> from django . core . exceptions import FieldError <EOL> from django . db . models . expressions import Func , Star <EOL> from django . db . models . fields import DecimalField , FloatField , IntegerField <EOL> __all__ = [ <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> ] <EOL> class Aggregate ( Func ) : <EOL> contains_aggregate = True <EOL> name = None <EOL> def resolve_expression ( self , query = None , allow_joins = True , reuse = None , summarize = False , for_save = False ) : <EOL> c = super ( Aggregate , self ) . resolve_expression ( query , allow_joins , reuse , summarize ) <EOL> if not summarize : <EOL> expressions = c . get_source_expressions ( ) <EOL> for index , expr in enumerate ( expressions ) : <EOL> if expr . contains_aggregate : <EOL> before_resolved = self . get_source_expressions ( ) [ index ] <EOL> name = before_resolved . name if hasattr ( before_resolved , '<STR_LIT:name>' ) else repr ( before_resolved ) <EOL> raise FieldError ( "<STR_LIT>" % ( c . name , name , name ) ) <EOL> return c <EOL> @ property <EOL> def default_alias ( self ) : <EOL> expressions = self . get_source_expressions ( ) <EOL> if len ( expressions ) == <NUM_LIT:1> and hasattr ( expressions [ <NUM_LIT:0> ] , '<STR_LIT:name>' ) : <EOL> return '<STR_LIT>' % ( expressions [ <NUM_LIT:0> ] . name , self . name . lower ( ) ) <EOL> raise TypeError ( "<STR_LIT>" ) <EOL> def get_group_by_cols ( self ) : <EOL> return [ ] <EOL> class Avg ( Aggregate ) : <EOL> function = '<STR_LIT>' <EOL> name = '<STR_LIT>' <EOL> def _resolve_output_field ( self ) : <EOL> source_field = self . get_source_fields ( ) [ <NUM_LIT:0> ] <EOL> if isinstance ( source_field , ( IntegerField , DecimalField ) ) : <EOL> self . _output_field = FloatField ( ) <EOL> super ( Avg , self ) . _resolve_output_field ( ) <EOL> def as_oracle ( self , compiler , connection ) : <EOL> if self . output_field . get_internal_type ( ) == '<STR_LIT>' : <EOL> expression = self . get_source_expressions ( ) [ <NUM_LIT:0> ] <EOL> from django . db . backends . oracle . functions import IntervalToSeconds , SecondsToInterval <EOL> return compiler . compile ( <EOL> SecondsToInterval ( Avg ( IntervalToSeconds ( expression ) ) ) <EOL> ) <EOL> return super ( Avg , self ) . as_sql ( compiler , connection ) <EOL> class Count ( Aggregate ) : <EOL> function = '<STR_LIT>' <EOL> name = '<STR_LIT>' <EOL> template = '<STR_LIT>' <EOL> def __init__ ( self , expression , distinct = False , ** extra ) : <EOL> if expression == '<STR_LIT:*>' : <EOL> expression = Star ( ) <EOL> super ( Count , self ) . __init__ ( <EOL> expression , distinct = '<STR_LIT>' if distinct else '<STR_LIT>' , output_field = IntegerField ( ) , ** extra ) <EOL> def __repr__ ( self ) : <EOL> return "<STR_LIT>" . format ( <EOL> self . __class__ . __name__ , <EOL> self . arg_joiner . join ( str ( arg ) for arg in self . source_expressions ) , <EOL> '<STR_LIT:False>' if self . extra [ '<STR_LIT>' ] == '<STR_LIT>' else '<STR_LIT:True>' , <EOL> ) <EOL> def convert_value ( self , value , expression , connection , context ) : <EOL> if value is None : <EOL> return <NUM_LIT:0> <EOL> return int ( value ) <EOL> class Max ( Aggregate ) : <EOL> function = '<STR_LIT>' <EOL> name = '<STR_LIT>' <EOL> class Min ( Aggregate ) : <EOL> function = '<STR_LIT>' <EOL> name = '<STR_LIT>' <EOL> class StdDev ( Aggregate ) : <EOL> name = '<STR_LIT>' <EOL> def __init__ ( self , expression , sample = False , ** extra ) : <EOL> self . function = '<STR_LIT>' if sample else '<STR_LIT>' <EOL> super ( StdDev , self ) . __init__ ( expression , output_field = FloatField ( ) , ** extra ) <EOL> def __repr__ ( self ) : <EOL> return "<STR_LIT>" . format ( <EOL> self . __class__ . __name__ , <EOL> self . arg_joiner . join ( str ( arg ) for arg in self . source_expressions ) , <EOL> '<STR_LIT:False>' if self . function == '<STR_LIT>' else '<STR_LIT:True>' , <EOL> ) <EOL> def convert_value ( self , value , expression , connection , context ) : <EOL> if value is None : <EOL> return value <EOL> return float ( value ) <EOL> class Sum ( Aggregate ) : <EOL> function = '<STR_LIT>' <EOL> name = '<STR_LIT>' <EOL> def as_oracle ( self , compiler , connection ) : <EOL> if self . output_field . get_internal_type ( ) == '<STR_LIT>' : <EOL> expression = self . get_source_expressions ( ) [ <NUM_LIT:0> ] <EOL> from django . db . backends . oracle . functions import IntervalToSeconds , SecondsToInterval <EOL> return compiler . compile ( <EOL> SecondsToInterval ( Sum ( IntervalToSeconds ( expression ) ) ) <EOL> ) <EOL> return super ( Sum , self ) . as_sql ( compiler , connection ) <EOL> class Variance ( Aggregate ) : <EOL> name = '<STR_LIT>' <EOL> def __init__ ( self , expression , sample = False , ** extra ) : <EOL> self . function = '<STR_LIT>' if sample else '<STR_LIT>' <EOL> super ( Variance , self ) . __init__ ( expression , output_field = FloatField ( ) , ** extra ) <EOL> def __repr__ ( self ) : <EOL> return "<STR_LIT>" . format ( <EOL> self . __class__ . __name__ , <EOL> self . arg_joiner . join ( str ( arg ) for arg in self . source_expressions ) , <EOL> '<STR_LIT:False>' if self . function == '<STR_LIT>' else '<STR_LIT:True>' , <EOL> ) <EOL> def convert_value ( self , value , expression , connection , context ) : <EOL> if value is None : <EOL> return value <EOL> return float ( value ) </s>
