def sql_to_csv(sql, engine, filepath, chunksize=<NUM_LIT:1000>, overwrite=False):
if overwrite:  <EOL><INDENT>if os.path.exists(filepath):<EOL><INDENT>raise Exception("<STR_LIT>" % filepath)<EOL><DEDENT><DEDENT>import pandas as pd<EOL>columns = [str(column.name) for column in sql.columns]<EOL>with open(filepath, "<STR_LIT:w>") as f:<EOL><INDENT>df = pd.DataFrame([], columns=columns)<EOL>df.to_csv(f, header=True, index=False)<EOL>result_proxy = engine.execute(sql)<EOL>while True:<EOL><INDENT>data = result_proxy.fetchmany(chunksize)<EOL>if len(data) == <NUM_LIT:0>:<EOL><INDENT>break<EOL><DEDENT>else:<EOL><INDENT>df = pd.DataFrame(data, columns=columns)<EOL>df.to_csv(f, header=False, index=False)<EOL><DEDENT><DEDENT><DEDENT>
Export sql result to csv file.

:param sql: :class:`sqlalchemy.sql.selectable.Select` instance.
:param engine: :class:`sqlalchemy.engine.base.Engine`.
:param filepath: file path.
:param chunksize: number of rows write to csv each time.
:param overwrite: bool, if True, avoid to overite existing file.

**中文文档**

将执行sql的结果中的所有数据, 以生成器的方式(一次只使用一小部分内存), 将
整个结果写入csv文件。