in nl2sql_src/nl2sql_generic.py [0:0]
def execute_query(self, query, dry_run=False):
"""
This function executes an SQL query using the configured
BigQuery client.
Parameters:
- query (str): The SQL query to be executed.
Returns:
pandas.DataFrame: The result of the executed query as a DataFrame.
"""
if dry_run:
job_config = bigquery.QueryJobConfig(dry_run=True,
use_query_cache=False)
query_job = client.query(query, job_config=job_config)
if query_job.total_bytes_processed > 0:
logger.info("Query is valid")
return True, 'Query is valid'
else:
return False, 'Invalid query. Regenerate'
else:
try:
# Run the SQL query
query_job = client.query(query)
# Wait for the job to complete
query_job.result()
# Fetch the result if needed
results = query_job.to_dataframe()
return results
except Exception as exc:
raise Exception(traceback.print_exc()) from exc