<s> """<STR_LIT>""" <EOL> from . expression import ( Expression , BinaryOperator , AttributeRef , <EOL> NamedAttributeRef , UnnamedAttributeRef , <EOL> NamedStateAttributeRef ) <EOL> from . aggregate import BuiltinAggregateExpression , AggregateExpression <EOL> import copy <EOL> import inspect <EOL> class NestedAggregateException ( Exception ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , lineno ) : <EOL> self . lineno = lineno <EOL> def __str__ ( self ) : <EOL> return "<STR_LIT>" % self . lineno <EOL> def ensure_unnamed ( exprs , op ) : <EOL> """<STR_LIT>""" <EOL> if isinstance ( exprs , Expression ) : <EOL> if only_unnamed_refs ( exprs ) : <EOL> return exprs <EOL> return to_unnamed_recursive ( exprs , op . scheme ( ) ) <EOL> else : <EOL> if all ( only_unnamed_refs ( e ) for e in exprs ) : <EOL> return exprs <EOL> op_scheme = op . scheme ( ) <EOL> return [ to_unnamed_recursive ( e , op_scheme ) for e in exprs ] <EOL> def toUnnamed ( ref , scheme ) : <EOL> """<STR_LIT>""" <EOL> if issubclass ( ref . __class__ , UnnamedAttributeRef ) : <EOL> return ref <EOL> elif issubclass ( ref . __class__ , NamedAttributeRef ) : <EOL> return UnnamedAttributeRef ( scheme . getPosition ( ref . name ) ) <EOL> else : <EOL> raise TypeError ( "<STR_LIT>" % ref ) <EOL> def toNamed ( ref , scheme ) : <EOL> """<STR_LIT>""" <EOL> if issubclass ( ref . __class__ , UnnamedAttributeRef ) : <EOL> attrname = scheme [ ref . position ] [ <NUM_LIT:0> ] <EOL> return NamedAttributeRef ( attrname ) <EOL> elif issubclass ( ref . __class__ , NamedAttributeRef ) : <EOL> return ref <EOL> else : <EOL> raise TypeError ( "<STR_LIT>" ) <EOL> def to_unnamed_recursive ( sexpr , scheme ) : <EOL> """<STR_LIT>""" <EOL> def convert ( n ) : <EOL> if isinstance ( n , NamedAttributeRef ) : <EOL> n = toUnnamed ( n , scheme ) <EOL> n . apply ( convert ) <EOL> return n <EOL> return convert ( sexpr ) <EOL> def only_unnamed_refs ( sexpr ) : <EOL> """<STR_LIT>""" <EOL> return all ( isinstance ( e , UnnamedAttributeRef ) <EOL> for e in sexpr . walk ( ) <EOL> if isinstance ( e , AttributeRef ) ) <EOL> def all_classes ( ) : <EOL> """<STR_LIT>""" <EOL> import raco . expression as expr <EOL> return [ obj for _ , obj in inspect . getmembers ( expr , inspect . isclass ) ] <EOL> def aggregate_functions ( ) : <EOL> """<STR_LIT>""" <EOL> allclasses = all_classes ( ) <EOL> opclasses = [ opclass for opclass in allclasses <EOL> if issubclass ( opclass , BuiltinAggregateExpression ) <EOL> and not inspect . isabstract ( opclass ) ] <EOL> return opclasses <EOL> def binary_ops ( ) : <EOL> """<STR_LIT>""" <EOL> allclasses = all_classes ( ) <EOL> opclasses = [ opclass for opclass in allclasses <EOL> if issubclass ( opclass , BinaryOperator ) <EOL> and not inspect . isabstract ( opclass ) ] <EOL> return opclasses <EOL> def udf_undefined_vars ( expr , vars ) : <EOL> """<STR_LIT>""" <EOL> return [ ex . name for ex in expr . walk ( ) <EOL> if isinstance ( ex , NamedAttributeRef ) and ex . name not in vars ] <EOL> def resolve_function ( func_expr , arg_dict ) : <EOL> """<STR_LIT>""" <EOL> def convert ( n ) : <EOL> if isinstance ( n , NamedAttributeRef ) : <EOL> n = copy . deepcopy ( arg_dict [ n . name ] ) <EOL> else : <EOL> n . apply ( convert ) <EOL> return n <EOL> return convert ( copy . deepcopy ( func_expr ) ) <EOL> def resolve_state_vars ( expr , state_vars , mangled_names ) : <EOL> """<STR_LIT>""" <EOL> def convert ( n ) : <EOL> if isinstance ( n , NamedAttributeRef ) and n . name in state_vars : <EOL> n = NamedStateAttributeRef ( mangled_names [ n . name ] ) <EOL> else : <EOL> n . apply ( convert ) <EOL> return n <EOL> return convert ( copy . deepcopy ( expr ) ) <EOL> def accessed_columns ( expr ) : <EOL> """<STR_LIT>""" <EOL> for ex in expr . walk ( ) : <EOL> assert not isinstance ( ex , NamedAttributeRef ) <EOL> return set ( [ ex . position for ex in expr . walk ( ) <EOL> if isinstance ( ex , UnnamedAttributeRef ) ] ) <EOL> def rebase_expr ( expr , offset ) : <EOL> """<STR_LIT>""" <EOL> assert offset > <NUM_LIT:0> <EOL> for ex in expr . walk ( ) : <EOL> assert not isinstance ( ex , NamedAttributeRef ) <EOL> if isinstance ( ex , UnnamedAttributeRef ) : <EOL> ex . position -= offset <EOL> def reindex_expr ( expr , index_map ) : <EOL> """<STR_LIT>""" <EOL> for ex in expr . walk ( ) : <EOL> assert ( not isinstance ( ex , AttributeRef ) <EOL> or isinstance ( ex , UnnamedAttributeRef ) ) <EOL> if isinstance ( ex , UnnamedAttributeRef ) and ex . position in index_map : <EOL> ex . position = index_map [ ex . position ] <EOL> def expression_contains_aggregate ( ex ) : <EOL> """<STR_LIT>""" <EOL> return any ( isinstance ( sx , AggregateExpression ) for sx in ex . walk ( ) ) <EOL> def check_no_aggregate ( ex , lineno ) : <EOL> """<STR_LIT>""" <EOL> if expression_contains_aggregate ( ex ) : <EOL> raise NestedAggregateException ( lineno ) <EOL> def check_no_nested_aggregate ( ex , lineno ) : <EOL> """<STR_LIT>""" <EOL> def descend ( sx , in_aggregate ) : <EOL> is_aggregate = isinstance ( sx , AggregateExpression ) <EOL> if is_aggregate and in_aggregate : <EOL> raise NestedAggregateException ( lineno ) <EOL> for child in sx . get_children ( ) : <EOL> descend ( child , is_aggregate or in_aggregate ) <EOL> descend ( ex , False ) </s>
