<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
 def by_sql(cls, sql, engine_or_session):
 """ Query with sql statement or texture sql. """

        ses, auto_close = ensure_session(engine_or_session)
        result = ses.query(cls).from_statement(sql).all()
        if auto_close:
            ses.close()
        return result