# Buggy code
def _execute_and_fetch(self, session=None, **kw):
    try:
        # fetch first, to reduce the potential cost of submitting a graph
        return self.fetch(session=session)
    except ValueError:
        # not execute before
        return self.execute(session=session, **kw).fetch(session=session)


# Traceback (pymysql.err.InternalError)
D:\Anaconda3\envs\py37\python.exe E:/mycode/read_data.py
2020-08-03 15:54:52,383 INFO sqlalchemy.engine.base.Engine SHOW VARIABLES LIKE 'sql_mode'
2020-08-03 15:54:52,383 INFO sqlalchemy.engine.base.Engine {}
2020-08-03 15:54:52,387 INFO sqlalchemy.engine.base.Engine SHOW VARIABLES LIKE 'lower_case_table_names'
2020-08-03 15:54:52,387 INFO sqlalchemy.engine.base.Engine {}
2020-08-03 15:54:52,389 INFO sqlalchemy.engine.base.Engine SELECT DATABASE()
2020-08-03 15:54:52,389 INFO sqlalchemy.engine.base.Engine {}
2020-08-03 15:54:52,390 INFO sqlalchemy.engine.base.Engine show collation where `Charset` = 'utf8mb4' and `Collation` = 'utf8mb4_bin'
2020-08-03 15:54:52,390 INFO sqlalchemy.engine.base.Engine {}
2020-08-03 15:54:52,391 INFO sqlalchemy.engine.base.Engine SELECT CAST('test plain returns' AS CHAR(60)) AS anon_1
2020-08-03 15:54:52,391 INFO sqlalchemy.engine.base.Engine {}
2020-08-03 15:54:52,392 INFO sqlalchemy.engine.base.Engine SELECT CAST('test unicode returns' AS CHAR(60)) AS anon_1
2020-08-03 15:54:52,392 INFO sqlalchemy.engine.base.Engine {}
2020-08-03 15:54:52,393 INFO sqlalchemy.engine.base.Engine SELECT CAST('test collated returns' AS CHAR CHARACTER SET utf8mb4) COLLATE utf8mb4_bin AS anon_1
2020-08-03 15:54:52,393 INFO sqlalchemy.engine.base.Engine {}
D:\Anaconda3\envs\py37\lib\site-packages\pymysql\cursors.py:170: Warning: (1366, "Incorrect string value: '\\xD6\\xD0\\xB9\\xFA\\xB1\\xEA...' for column 'VARIABLE_VALUE' at row 485")
result = self._query(query)
2020-08-03 15:54:52,404 INFO sqlalchemy.engine.base.Engine SHOW CREATE TABLE `SELECT S_INFO_UNIQUECODE, REPORT_PERIOD, MYFIRST_INDICATOR FROM databasetable1`
2020-08-03 15:54:52,404 INFO sqlalchemy.engine.base.Engine {}
2020-08-03 15:54:52,405 INFO sqlalchemy.engine.base.Engine ROLLBACK
Traceback (most recent call last):
File "D:\Anaconda3\envs\py37\lib\site-packages\sqlalchemy\engine\base.py", line 1278, in _execute_context
cursor, statement, parameters, context
File "D:\Anaconda3\envs\py37\lib\site-packages\sqlalchemy\engine\default.py", line 593, in do_execute
cursor.execute(statement, parameters)
File "D:\Anaconda3\envs\py37\lib\site-packages\pymysql\cursors.py", line 170, in execute
result = self._query(query)
File "D:\Anaconda3\envs\py37\lib\site-packages\pymysql\cursors.py", line 328, in _query
conn.query(q)
File "D:\Anaconda3\envs\py37\lib\site-packages\pymysql\connections.py", line 517, in query
self._affected_rows = self._read_query_result(unbuffered=unbuffered)
File "D:\Anaconda3\envs\py37\lib\site-packages\pymysql\connections.py", line 732, in _read_query_result
result.read()
File "D:\Anaconda3\envs\py37\lib\site-packages\pymysql\connections.py", line 1075, in read
first_packet = self.connection._read_packet()
File "D:\Anaconda3\envs\py37\lib\site-packages\pymysql\connections.py", line 684, in _read_packet
packet.check_error()
File "D:\Anaconda3\envs\py37\lib\site-packages\pymysql\protocol.py", line 220, in check_error
err.raise_mysql_exception(self._data)
File "D:\Anaconda3\envs\py37\lib\site-packages\pymysql\err.py", line 109, in raise_mysql_exception
raise errorclass(errno, errval)
pymysql.err.InternalError: (1059, "Identifier name 'SELECT S_INFO_UNIQUECODE, REPORT_PERIOD, MYFIRST_INDICATOR FROM databasetable1' is too long")

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
File "E:/mycode/read_data.py", line 7,
df1 = md.read_sql('SELECT S_INFO_UNIQUECODE, REPORT_PERIOD, MYFIRST_INDICATOR FROM databasetable1', con=engine)
File "D:\Anaconda3\envs\py37\lib\site-packages\mars\dataframe\datasource\read_sql.py", line 577, in read_sql
low_limit=low_limit, high_limit=high_limit)
File "D:\Anaconda3\envs\py37\lib\site-packages\mars\dataframe\datasource\read_sql.py", line 479, in _read_sql
return op(test_rows, chunk_size)
File "D:\Anaconda3\envs\py37\lib\site-packages\mars\dataframe\datasource\read_sql.py", line 222, in __call__
selectable = self._get_selectable(con)
File "D:\Anaconda3\envs\py37\lib\site-packages\mars\dataframe\datasource\read_sql.py", line 175, in _get_selectable
autoload_with=engine_or_conn, schema=self._schema)
File "<string>", line 2, in __new__
File "D:\Anaconda3\envs\py37\lib\site-packages\sqlalchemy\util\deprecations.py", line 139, in warned
return fn(*args, **kwargs)
File "D:\Anaconda3\envs\py37\lib\site-packages\sqlalchemy\sql\schema.py", line 559, in __new__
metadata._remove_table(name, schema)
File "D:\Anaconda3\envs\py37\lib\site-packages\sqlalchemy\util\langhelpers.py", line 69, in __exit__
exc_value, with_traceback=exc_tb,
File "D:\Anaconda3\envs\py37\lib\site-packages\sqlalchemy\util\compat.py", line 178, in raise_
raise exception
File "D:\Anaconda3\envs\py37\lib\site-packages\sqlalchemy\sql\schema.py", line 554, in __new__
table._init(name, metadata, *args, **kw)
File "D:\Anaconda3\envs\py37\lib\site-packages\sqlalchemy\sql\schema.py", line 648, in _init
resolve_fks=resolve_fks,
File "D:\Anaconda3\envs\py37\lib\site-packages\sqlalchemy\sql\schema.py", line 672, in _autoload
_extend_on=_extend_on,
File "D:\Anaconda3\envs\py37\lib\site-packages\sqlalchemy\engine\base.py", line 1654, in run_callable
return callable_(self, *args, **kwargs)
File "D:\Anaconda3\envs\py37\lib\site-packages\sqlalchemy\engine\default.py", line 470, in reflecttable
table, include_columns, exclude_columns, resolve_fks, **opts
File "D:\Anaconda3\envs\py37\lib\site-packages\sqlalchemy\engine\reflection.py", line 649, in reflecttable
table_name, schema, **table.dialect_kwargs
File "D:\Anaconda3\envs\py37\lib\site-packages\sqlalchemy\engine\reflection.py", line 314, in get_table_options
self.bind, table_name, schema, info_cache=self.info_cache, **kw
File "<string>", line 2, in get_table_options
File "D:\Anaconda3\envs\py37\lib\site-packages\sqlalchemy\engine\reflection.py", line 52, in cache
ret = fn(self, con, *args, **kw)
File "D:\Anaconda3\envs\py37\lib\site-packages\sqlalchemy\dialects\mysql\base.py", line 2624, in get_table_options
connection, table_name, schema, **kw
File "D:\Anaconda3\envs\py37\lib\site-packages\sqlalchemy\dialects\mysql\base.py", line 2870, in _parsed_state_or_create
info_cache=kw.get("info_cache", None),
File "<string>", line 2, in _setup_parser
File "D:\Anaconda3\envs\py37\lib\site-packages\sqlalchemy\engine\reflection.py", line 52, in cache
ret = fn(self, con, *args, **kw)
File "D:\Anaconda3\envs\py37\lib\site-packages\sqlalchemy\dialects\mysql\base.py", line 2898, in _setup_parser
connection, None, charset, full_name=full_name
File "D:\Anaconda3\envs\py37\lib\site-packages\sqlalchemy\dialects\mysql\base.py", line 2998, in _show_create_table
).execute(st)
File "D:\Anaconda3\envs\py37\lib\site-packages\sqlalchemy\engine\base.py", line 1006, in execute
return self._execute_text(object_, multiparams, params)
File "D:\Anaconda3\envs\py37\lib\site-packages\sqlalchemy\engine\base.py", line 1181, in _execute_text
parameters,
File "D:\Anaconda3\envs\py37\lib\site-packages\sqlalchemy\engine\base.py", line 1318, in _execute_context
e, statement, parameters, cursor, context
File "D:\Anaconda3\envs\py37\lib\site-packages\sqlalchemy\engine\base.py", line 1512, in _handle_dbapi_exception
sqlalchemy_exception, with_traceback=exc_info[2], from_=e
File "D:\Anaconda3\envs\py37\lib\site-packages\sqlalchemy\util\compat.py", line 178, in raise_
raise exception
File "D:\Anaconda3\envs\py37\lib\site-packages\sqlalchemy\engine\base.py", line 1278, in _execute_context
cursor, statement, parameters, context
File "D:\Anaconda3\envs\py37\lib\site-packages\sqlalchemy\engine\default.py", line 593, in do_execute
cursor.execute(statement, parameters)
File "D:\Anaconda3\envs\py37\lib\site-packages\pymysql\cursors.py", line 170, in execute
result = self._query(query)
File "D:\Anaconda3\envs\py37\lib\site-packages\pymysql\cursors.py", line 328, in _query
conn.query(q)
File "D:\Anaconda3\envs\py37\lib\site-packages\pymysql\connections.py", line 517, in query
self._affected_rows = self._read_query_result(unbuffered=unbuffered)
File "D:\Anaconda3\envs\py37\lib\site-packages\pymysql\connections.py", line 732, in _read_query_result
result.read()
File "D:\Anaconda3\envs\py37\lib\site-packages\pymysql\connections.py", line 1075, in read
first_packet = self.connection._read_packet()
File "D:\Anaconda3\envs\py37\lib\site-packages\pymysql\connections.py", line 684, in _read_packet
packet.check_error()
File "D:\Anaconda3\envs\py37\lib\site-packages\pymysql\protocol.py", line 220, in check_error
err.raise_mysql_exception(self._data)
File "D:\Anaconda3\envs\py37\lib\site-packages\pymysql\err.py", line 109, in raise_mysql_exception
raise errorclass(errno, errval)
sqlalchemy.exc.InternalError: (pymysql.err.InternalError) (1059, "Identifier name 'SELECT S_INFO_UNIQUECODE, REPORT_PERIOD, MYFIRST_INDICATOR FROM databasetable1' is too long")
[SQL: SHOW CREATE TABLE `SELECT S_INFO_UNIQUECODE, REPORT_PERIOD, MYFIRST_INDICATOR FROM databasetable1`]
(Background on this error at: http://sqlalche.me/e/13/2j85)

Process finished with exit code 1

# Fixed code
def _execute_and_fetch(self, session=None, **kw):
    if session is None and len(self._executed_sessions) > 0:
        session = self._executed_sessions[-1]
    try:
        # fetch first, to reduce the potential cost of submitting a graph
        return self.fetch(session=session)
    except ValueError:
        # not execute before
        return self.execute(session=session, **kw).fetch(session=session)