def __init__(self, sql, env=None, udfs=None, data_sources=None, subqueries=None):
    """Initializes an instance of a Query object.

    Args:
      sql: the BigQuery SQL query string to execute
      env: a dictionary containing objects from the query execution context, used to get references
          to UDFs, subqueries, and external data sources referenced by the query
      udfs: list of UDFs names referenced in the SQL, or dictionary of names and UDF objects
      data_sources: list of external data sources names referenced in the SQL, or dictionary of
          names and data source objects
      subqueries: list of subqueries names referenced in the SQL, or dictionary of names and
          Query objects

    Raises:
      Exception if expansion of any variables failed.
      """
    self._sql = sql
    self._udfs = []
    self._subqueries = []
    self._data_sources = []
    self._env = env or {}

    # Validate given list or dictionary of objects that they are of correct type
    # and add them to the target dictionary
    def _expand_objects(obj_container, obj_type, target_list):
      for item in obj_container:
        # for a list of objects, we should find these objects in the given environment
        if isinstance(obj_container, list):
          value = self._env.get(item)
          if value is None:
            raise Exception('Cannot find object %s' % item)

        # for a dictionary of objects, each pair must be a string and object of the expected type
        elif isinstance(obj_container, dict):
          value = obj_container[item]
          if not isinstance(value, obj_type):
            raise Exception('Expected type: %s, found: %s.' % (obj_type, type(value)))

        else:
          raise Exception('Unexpected container for type %s. Expected a list or dictionary'
                          % obj_type)

        target_list.append((item, value))

    if subqueries:
      _expand_objects(subqueries, Query, self._subqueries)
    if udfs:
      _expand_objects(udfs, _udf.UDF, self._udfs)
    if data_sources:
      _expand_objects(data_sources, _external_data_source.ExternalDataSource, self._data_sources)

    if len(self._data_sources) > 1:
      raise Exception('Only one temporary external datasource is supported in queries.')
def __init__ ( self , sql , env = None , udfs = None , data_sources = None , subqueries = None ) : self . _sql = sql self . _udfs = [ ] self . _subqueries = [ ] self . _data_sources = [ ] self . _env = env or { } def _expand_objects ( obj_container , obj_type , target_list ) : for item in obj_container : if isinstance ( obj_container , list ) : value = self . _env . get ( item ) if value is None : raise Exception ( 'Cannot find object %s' % item ) elif isinstance ( obj_container , dict ) : value = obj_container [ item ] if not isinstance ( value , obj_type ) : raise Exception ( 'Expected type: %s, found: %s.' % ( obj_type , type ( value ) ) ) else : raise Exception ( 'Unexpected container for type %s. Expected a list or dictionary' % obj_type ) target_list . append ( ( item , value ) ) if subqueries : _expand_objects ( subqueries , Query , self . _subqueries ) if udfs : _expand_objects ( udfs , _udf . UDF , self . _udfs ) if data_sources : _expand_objects ( data_sources , _external_data_source . ExternalDataSource , self . _data_sources ) if len ( self . _data_sources ) > 1 : raise Exception ( 'Only one temporary external datasource is supported in queries.' )
Initializes an instance of a Query object.

    Args:
      sql: the BigQuery SQL query string to execute
      env: a dictionary containing objects from the query execution context, used to get references
          to UDFs, subqueries, and external data sources referenced by the query
      udfs: list of UDFs names referenced in the SQL, or dictionary of names and UDF objects
      data_sources: list of external data sources names referenced in the SQL, or dictionary of
          names and data source objects
      subqueries: list of subqueries names referenced in the SQL, or dictionary of names and
          Query objects

    Raises:
      Exception if expansion of any variables failed.