示例#1
0
    def _try_fetch(self, size=None):
        """Try to start fetching data, if not yet started.

        Mutates self to indicate that iteration has started.
        """
        if self._query_job is None:
            raise exceptions.InterfaceError(
                "No query results: execute() must be called before fetch.")

        if self._query_job.dry_run:
            self._query_data = iter([])
            return

        is_dml = (self._query_job.statement_type
                  and self._query_job.statement_type.upper() != "SELECT")
        if is_dml:
            self._query_data = iter([])
            return

        if self._query_data is None:
            client = self.connection._client
            bqstorage_client = self.connection._bqstorage_client

            if bqstorage_client is not None:
                rows_iterable = self._bqstorage_fetch(bqstorage_client)
                self._query_data = _helpers.to_bq_table_rows(rows_iterable)
                return

            rows_iter = client.list_rows(
                self._query_job.destination,
                selected_fields=self._query_job._query_results.schema,
                page_size=self.arraysize,
            )
            self._query_data = iter(rows_iter)
示例#2
0
    def _try_fetch(self, size=None):
        """Try to start fetching data, if not yet started.

        Mutates self to indicate that iteration has started.
        """
        if self._query_job is None:
            raise exceptions.InterfaceError(
                "No query results: execute() must be called before fetch.")

        is_dml = (self._query_job.statement_type
                  and self._query_job.statement_type.upper() != "SELECT")
        if is_dml:
            self._query_data = iter([])
            return

        if self._query_data is None:
            client = self.connection._client
            bqstorage_client = self.connection._bqstorage_client

            if bqstorage_client is not None:
                try:
                    rows_iterable = self._bqstorage_fetch(bqstorage_client)
                    self._query_data = _helpers.to_bq_table_rows(rows_iterable)
                    return
                except google.api_core.exceptions.GoogleAPICallError as exc:
                    # NOTE: Forbidden is a subclass of GoogleAPICallError
                    if isinstance(exc, google.api_core.exceptions.Forbidden):
                        # Don't hide errors such as insufficient permissions to create
                        # a read session, or the API is not enabled. Both of those are
                        # clearly problems if the developer has explicitly asked for
                        # BigQuery Storage API support.
                        raise

                    # There is an issue with reading from small anonymous
                    # query results tables. If such an error occurs, we silence
                    # it in order to try again with the tabledata.list API.
                    _LOGGER.debug(
                        "Error fetching data with BigQuery Storage API, "
                        "falling back to tabledata.list API.")

            rows_iter = client.list_rows(
                self._query_job.destination,
                selected_fields=self._query_job._query_results.schema,
                page_size=self.arraysize,
            )
            self._query_data = iter(rows_iter)
示例#3
0
    def _try_fetch(self, size=None):
        """Try to start fetching data, if not yet started.

        Mutates self to indicate that iteration has started.
        """
        if self._query_job is None:
            raise exceptions.InterfaceError(
                "No query results: execute() must be called before fetch.")

        if self._query_job.dry_run:
            self._query_data = iter([])
            return

        if self._query_data is None:
            bqstorage_client = self.connection._bqstorage_client

            if bqstorage_client is not None:
                rows_iterable = self._bqstorage_fetch(bqstorage_client)
                self._query_data = _helpers.to_bq_table_rows(rows_iterable)
                return

            rows_iter = self._query_job.result(page_size=self.arraysize)
            self._query_data = iter(rows_iter)