def run_query()

in src/mozanalysis/bq.py [0:0]


    def run_query(self, sql, results_table=None, replace_tables=False):
        """Run a query and return the result.

        If ``results_table`` is provided, then save the results
        into there (or just query from there if it already exists).
        Returns a ``google.cloud.bigquery.table.RowIterator``

        Args:
            sql (str): A SQL query.
            results_table (str, optional): A table name, not including
                a project_id or dataset_id. The table name is used as a
                cache key (if the table already exists, we ignore ``sql``
                and return the table's contents), so it is wise for
                ``results_table`` to include a hash of ``sql``.
            replace_tables (bool): Indicates if the results table should
                be replaced with new results, if that table is found.
        """
        if not results_table:
            return self.client.query(sql).result()

        if replace_tables:
            self.client.delete_table(
                self.fully_qualify_table_name(results_table),
                not_found_ok=True,
            )

        try:
            full_res = self.client.query(
                sql,
                job_config=bigquery.QueryJobConfig(
                    destination=self.client.dataset(self.dataset_id).table(
                        results_table
                    )
                ),
            ).result()
            logger.info("Saved into", results_table)
            return full_res

        except Conflict:
            logger.info("Table already exists. Reusing", results_table)
            return self.client.list_rows(self.fully_qualify_table_name(results_table))