human
Complete this python program:
def execute_sql(ctask, query_id, return_results=True, store_results=False):
assistant
#Here is the Python code you requested.

    """Executes the sql query returns the results.
    """
    session = get_session((not ctask.request.called_directly))
    query = get_query(query_id, session)
    payload = dict(query_id=query_id)
    database = query.database
    db_engine_spec = database.db_engine_spec
    db_engine_spec.patch()
    def handle_error(msg):
        'Local    method    handling    error    while    processing    the    SQL'
        query.error_message = msg
        query.status = QueryStatus.FAILED
        query.tmp_table_name = None
        session.commit()
        payload.update({'status': query.status, 'error_essage': msg})
        return payload
    if (store_results and (not results_backend)):
        return handle_error("Results    backend    isn't    configured.")
    superset_query = SupersetQuery(query.sql)
    executed_sql = superset_query.stripped()
    if ((not superset_query.is_select()) and (not database.allow_dml)):
        return handle_error('Only    `SELECT`    statements    are    allowed    against    this    database')
    if query.select_as_cta:
        if (not superset_query.is_select()):
            return handle_error('Only    `SELECT`    statements    can    be    used    with    the    CREATE    TABLE    feature.')
            return
        if (not query.tmp_table_name):
            start_dttm = datetime.fromtimestamp(query.start_time)
            query.tmp_table_name = 'tmp_{}_table_{}'.format(query.user_id, start_dttm.strftime('%Y_%m_%d_%H_%M_%S'))
        executed_sql = superset_query.as_create_table(query.tmp_table_name)
        query.select_as_cta_used = True
    elif (query.limit and superset_query.is_select() and (db_engine_spec.limit_method == LimitMethod.WRAP_SQL)):
        executed_sql = database.wrap_sql_limit(executed_sql, query.limit)
        query.limit_used = True
    try:
        template_processor = get_template_processor(database=database, query=query)
        executed_sql = template_processor.process_template(executed_sql)
    except Exception as e:
        logging.exception(e)
        msg = ('Template    rendering    failed:    ' +...