<s> """<STR_LIT>""" <EOL> class AggregateField ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , internal_type ) : <EOL> self . internal_type = internal_type <EOL> def get_internal_type ( self ) : <EOL> return self . internal_type <EOL> ordinal_aggregate_field = AggregateField ( '<STR_LIT>' ) <EOL> computed_aggregate_field = AggregateField ( '<STR_LIT>' ) <EOL> class Aggregate ( object ) : <EOL> """<STR_LIT>""" <EOL> is_ordinal = False <EOL> is_computed = False <EOL> sql_template = '<STR_LIT>' <EOL> def __init__ ( self , col , source = None , is_summary = False , ** extra ) : <EOL> """<STR_LIT>""" <EOL> self . col = col <EOL> self . source = source <EOL> self . is_summary = is_summary <EOL> self . extra = extra <EOL> tmp = self <EOL> while tmp and isinstance ( tmp , Aggregate ) : <EOL> if getattr ( tmp , '<STR_LIT>' , False ) : <EOL> tmp = ordinal_aggregate_field <EOL> elif getattr ( tmp , '<STR_LIT>' , False ) : <EOL> tmp = computed_aggregate_field <EOL> else : <EOL> tmp = tmp . source <EOL> self . field = tmp <EOL> def relabel_aliases ( self , change_map ) : <EOL> if isinstance ( self . col , ( list , tuple ) ) : <EOL> self . col = ( change_map . get ( self . col [ <NUM_LIT:0> ] , self . col [ <NUM_LIT:0> ] ) , self . col [ <NUM_LIT:1> ] ) <EOL> def as_sql ( self , qn , connection ) : <EOL> "<STR_LIT>" <EOL> if hasattr ( self . col , '<STR_LIT>' ) : <EOL> field_name = self . col . as_sql ( qn , connection ) <EOL> elif isinstance ( self . col , ( list , tuple ) ) : <EOL> field_name = '<STR_LIT:.>' . join ( [ qn ( c ) for c in self . col ] ) <EOL> else : <EOL> field_name = self . col <EOL> params = { <EOL> '<STR_LIT>' : self . sql_function , <EOL> '<STR_LIT>' : field_name <EOL> } <EOL> params . update ( self . extra ) <EOL> return self . sql_template % params <EOL> class Avg ( Aggregate ) : <EOL> is_computed = True <EOL> sql_function = '<STR_LIT>' <EOL> class Count ( Aggregate ) : <EOL> is_ordinal = True <EOL> sql_function = '<STR_LIT>' <EOL> sql_template = '<STR_LIT>' <EOL> def __init__ ( self , col , distinct = False , ** extra ) : <EOL> super ( Count , self ) . __init__ ( col , distinct = distinct and '<STR_LIT>' or '<STR_LIT>' , ** extra ) <EOL> class Max ( Aggregate ) : <EOL> sql_function = '<STR_LIT>' <EOL> class Min ( Aggregate ) : <EOL> sql_function = '<STR_LIT>' <EOL> class StdDev ( Aggregate ) : <EOL> is_computed = True <EOL> def __init__ ( self , col , sample = False , ** extra ) : <EOL> super ( StdDev , self ) . __init__ ( col , ** extra ) <EOL> self . sql_function = sample and '<STR_LIT>' or '<STR_LIT>' <EOL> class Sum ( Aggregate ) : <EOL> sql_function = '<STR_LIT>' <EOL> class Variance ( Aggregate ) : <EOL> is_computed = True <EOL> def __init__ ( self , col , sample = False , ** extra ) : <EOL> super ( Variance , self ) . __init__ ( col , ** extra ) <EOL> self . sql_function = sample and '<STR_LIT>' or '<STR_LIT>' </s>
