# Buggy code
def _estimate_calc_memory(self, session_id, graph_key):
    graph_record = self._graph_records[(session_id, graph_key)]
    size_ctx = dict(
        (k, (v.chunk_size, v.chunk_size)) for k, v in graph_record.data_metas.items()
    )
    executor = Executor(
        storage=size_ctx, sync_provider_type=Executor.SyncProviderType.MOCK
    )
    res = executor.execute_graph(
        graph_record.graph, graph_record.chunk_targets, mock=True
    )

    targets = graph_record.chunk_targets
    target_sizes = dict(zip(targets, res))

    total_mem = sum(target_sizes[key][1] for key in targets)
    if total_mem:
        for key in targets:
            r = target_sizes[key]
            target_sizes[key] = (
                r[0],
                max(r[1], r[1] * executor.mock_max_memory // total_mem),
            )
    return target_sizes


# Traceback (mysql.connector.errors.ProgrammingError)
Traceback (most recent call last):
File "D:\Anaconda3\envs\py37\lib\site-packages\sqlalchemy\engine\base.py", line 1249, in _execute_context
cursor, statement, parameters, context
File "D:\Anaconda3\envs\py37\lib\site-packages\sqlalchemy\engine\default.py", line 580, in do_execute
cursor.execute(statement, parameters)
File "D:\Anaconda3\envs\py37\lib\site-packages\mysql\connector\cursor.py", line 551, in execute
self._handle_result(self._connection.cmd_query(stmt))
File "D:\Anaconda3\envs\py37\lib\site-packages\mysql\connector\connection.py", line 490, in cmd_query
result = self._handle_result(self._send_cmd(ServerCmd.QUERY, query))
File "D:\Anaconda3\envs\py37\lib\site-packages\mysql\connector\connection.py", line 395, in _handle_result
raise errors.get_exception(packet)
mysql.connector.errors.ProgrammingError: 1248 (42000): Every derived table must have its own alias

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

Traceback (most recent call last):
File "E:\mycodes\readdatabymars.py", line 17, in <module>
df = md.read_sql_query(sql1, con=engine)
File "D:\Anaconda3\envs\py37\lib\site-packages\mars\dataframe\datasource\read_sql.py", line 766, in read_sql_query
low_limit=low_limit, high_limit=high_limit)
File "D:\Anaconda3\envs\py37\lib\site-packages\mars\dataframe\datasource\read_sql.py", line 476, in _read_sql
return op(test_rows, chunk_size)
File "D:\Anaconda3\envs\py37\lib\site-packages\mars\dataframe\datasource\read_sql.py", line 250, in __call__
test_df, shape = self._collect_info(con, selectable, collect_cols, test_rows)
File "D:\Anaconda3\envs\py37\lib\site-packages\mars\dataframe\datasource\read_sql.py", line 196, in _collect_info
parse_dates=self._parse_dates)
File "D:\Anaconda3\envs\py37\lib\site-packages\pandas\io\sql.py", line 436, in read_sql
chunksize=chunksize,
File "D:\Anaconda3\envs\py37\lib\site-packages\pandas\io\sql.py", line 1218, in read_query
result = self.execute(*args)
File "D:\Anaconda3\envs\py37\lib\site-packages\pandas\io\sql.py", line 1087, in execute
return self.connectable.execute(*args, **kwargs)
File "D:\Anaconda3\envs\py37\lib\site-packages\sqlalchemy\engine\base.py", line 988, in execute
return meth(self, multiparams, params)
File "D:\Anaconda3\envs\py37\lib\site-packages\sqlalchemy\sql\elements.py", line 287, in _execute_on_connection
return connection._execute_clauseelement(self, multiparams, params)
File "D:\Anaconda3\envs\py37\lib\site-packages\sqlalchemy\engine\base.py", line 1107, in _execute_clauseelement
distilled_params,
File "D:\Anaconda3\envs\py37\lib\site-packages\sqlalchemy\engine\base.py", line 1253, in _execute_context
e, statement, parameters, cursor, context
File "D:\Anaconda3\envs\py37\lib\site-packages\sqlalchemy\engine\base.py", line 1473, in _handle_dbapi_exception
util.raise_from_cause(sqlalchemy_exception, exc_info)
File "D:\Anaconda3\envs\py37\lib\site-packages\sqlalchemy\util\compat.py", line 398, in raise_from_cause
reraise(type(exception), exception, tb=exc_tb, cause=cause)
File "D:\Anaconda3\envs\py37\lib\site-packages\sqlalchemy\util\compat.py", line 152, in reraise
raise value.with_traceback(tb)
File "D:\Anaconda3\envs\py37\lib\site-packages\sqlalchemy\engine\base.py", line 1249, in _execute_context
cursor, statement, parameters, context
File "D:\Anaconda3\envs\py37\lib\site-packages\sqlalchemy\engine\default.py", line 580, in do_execute
cursor.execute(statement, parameters)
File "D:\Anaconda3\envs\py37\lib\site-packages\mysql\connector\cursor.py", line 551, in execute
self._handle_result(self._connection.cmd_query(stmt))
File "D:\Anaconda3\envs\py37\lib\site-packages\mysql\connector\connection.py", line 490, in cmd_query
result = self._handle_result(self._send_cmd(ServerCmd.QUERY, query))
File "D:\Anaconda3\envs\py37\lib\site-packages\mysql\connector\connection.py", line 395, in _handle_result
raise errors.get_exception(packet)
sqlalchemy.exc.ProgrammingError: (mysql.connector.errors.ProgrammingError) 1248 (42000): Every derived table must have its own alias
[SQL: SELECT *
FROM (SELECT *
FROM (SELECT * FROM  table1) AS temp_6302b78c8a9347b8b02475d757cf17f7)
LIMIT %(param_1)s]
[parameters: {'param_1': 5}]
(Background on this error at: http://sqlalche.me/e/f405)

# Fixed code
def _estimate_calc_memory(self, session_id, graph_key):
    graph_record = self._graph_records[(session_id, graph_key)]
    data_metas = graph_record.data_metas
    size_ctx = dict((k, (v.chunk_size, v.chunk_size)) for k, v in data_metas.items())

    # update shapes
    for n in graph_record.graph:
        if isinstance(n.op, Fetch):
            try:
                meta = data_metas[n.key]
                if hasattr(n, "_shape") and meta.chunk_shape is not None:
                    n._shape = meta.chunk_shape
            except KeyError:
                pass

    executor = Executor(
        storage=size_ctx, sync_provider_type=Executor.SyncProviderType.MOCK
    )
    res = executor.execute_graph(
        graph_record.graph, graph_record.chunk_targets, mock=True
    )

    targets = graph_record.chunk_targets
    target_sizes = dict(zip(targets, res))

    total_mem = sum(target_sizes[key][1] for key in targets)
    if total_mem:
        for key in targets:
            r = target_sizes[key]
            target_sizes[key] = (
                r[0],
                max(r[1], r[1] * executor.mock_max_memory // total_mem),
            )
    return target_sizes