#NEWLINE# def Evaluate( stack ) : #NEWLINE# #INDENT# 'Evaluates the postfix stack to find the result of the expression.\n\n  The <stack> is the expression to be resolved in postfix notation.\n\n  Arguments:\n    stack: Postfix notation expression whose result is wanted. The <stack>\n    will be modified as elements are popped off.\n\n  Raises:\n    ValueError: Too many arguments provided for functions/operators.\n\n  Returns:\n    The resulting value after resolving the postfix expression.\n  ' #NEWLINE# def Resolve( stack ) : #NEWLINE# #INDENT# 'Resolves the postfix stack and evaluates the expression into one value.\n\n    The <stack> is the expression in postfix notation.\n\n    Arguments:\n      stack: Postfix notation to be resolved.\n\n    Raises:\n      ValueError: If an invalid function name is given or not enough arguments\n      are provided for an operator/function.\n\n    Returns:\n      The resolution of the postfix notation.\n    ' #NEWLINE# if ( not stack ) : #NEWLINE# #INDENT# raise bigquery_client . BigqueryInvalidQueryError ( 'Not enough arguments.' , None , None , None ) #NEWLINE# #UNINDENT# top = stack . pop ( ) #NEWLINE# if isinstance ( top , util . OperatorToken ) : #NEWLINE# #INDENT# args = [ ] #NEWLINE# for unused_i in range ( top . num_args ) : #NEWLINE# #INDENT# args . append ( Resolve ( stack ) ) #NEWLINE# #UNINDENT# args . reverse ( ) #NEWLINE# if ( None in args ) : #NEWLINE# #INDENT# return None #NEWLINE# #UNINDENT# if ( top . num_args == 1 ) : #NEWLINE# #INDENT# return _UNARY_OPERATORS [ str ( top ) ] ( * args ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# try : #NEWLINE# #INDENT# return _BINARY_OPERATORS [ top ] ( * args ) #NEWLINE# #UNINDENT# except ZeroDivisionError : #NEWLINE# #INDENT# raise bigquery_client . BigqueryInvalidQueryError ( 'Division by zero.' , None , None , None ) #NEWLINE# #UNINDENT# #UNINDENT# #UNINDENT# else : #NEWLINE# #INDENT# if isinstance ( top , util . BuiltInFunctionToken ) : #NEWLINE# #INDENT# func_name = str ( top ) #NEWLINE# if ( func_name in _ZERO_ARGUMENT_FUNCTIONS ) : #NEWLINE# #INDENT# result = _ZERO_ARGUMENT_FUNCTIONS [ func_name ] #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# if ( func_name in _ONE_ARGUMENT_FUNCTIONS ) : #NEWLINE# #INDENT# op = Resolve ( stack ) #NEWLINE# if ( op is None ) : #NEWLINE# #INDENT# result = None #NEWLINE# #UNINDENT# result = _ONE_ARGUMENT_FUNCTIONS [ func_name ] ( op ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# if ( func_name in _TWO_ARGUMENT_FUNCTIONS ) : #NEWLINE# #INDENT# op2 = Resolve ( stack ) #NEWLINE# op1 = Resolve ( stack ) #NEWLINE# if ( ( op1 is None ) or ( op2 is None ) ) : #NEWLINE# #INDENT# result = None #NEWLINE# #UNINDENT# result = _TWO_ARGUMENT_FUNCTIONS [ func_name ] ( op1 , op2 ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# if ( func_name in _THREE_ARGUMENT_FUNCTIONS ) : #NEWLINE# #INDENT# op3 = Resolve ( stack ) #NEWLINE# op2 = Resolve ( stack ) #NEWLINE# op1 = Resolve ( stack ) #NEWLINE# if ( ( op1 is None ) or ( op2 is None ) or ( op3 is None ) ) : #NEWLINE# #INDENT# result = None #NEWLINE# #UNINDENT# result = _THREE_ARGUMENT_FUNCTIONS [ func_name ] ( op1 , op2 , op3 ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# raise bigquery_client . BigqueryInvalidQueryError ( ( ( 'No function ' + func_name ) + ' exists.' ) , None , None , None ) #NEWLINE# #UNINDENT# #UNINDENT# #UNINDENT# #UNINDENT# return result #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# if isinstance ( top , util . FieldToken ) : #NEWLINE# #INDENT# raise bigquery_client . BigqueryInvalidQueryError ( ( '%s does not exist as a column.' % str ( top ) ) , None , None , None ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# if isinstance ( top , util . LiteralToken ) : #NEWLINE# #INDENT# return top . value #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# return top #NEWLINE# #UNINDENT# #UNINDENT# #UNINDENT# #UNINDENT# #UNINDENT# result = Resolve ( stack ) #NEWLINE# if stack : #NEWLINE# #INDENT# raise bigquery_client . BigqueryInvalidQueryError ( 'Invaid number of arguments.' , None , None , None ) #NEWLINE# #UNINDENT# return result