Skip to content

Commit

Permalink
[get_df] Adding support for multi-statement SQL (apache#5060)
Browse files Browse the repository at this point in the history
  • Loading branch information
john-bodley authored and timifasubaa committed May 31, 2018
1 parent cc056ad commit d4c1946
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 2 deletions.
8 changes: 6 additions & 2 deletions superset/models/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -690,9 +690,13 @@ def get_quoter(self):
return self.get_dialect().identifier_preparer.quote

def get_df(self, sql, schema):
sql = sql.strip().strip(';')
sqls = [x.strip() for x in sql.strip().strip(';').split(';')]
eng = self.get_sqla_engine(schema=schema)
df = pd.read_sql_query(sql, eng)

for i in range(len(sqls) - 1):
eng.execute(sqls[i])

df = pd.read_sql_query(sqls[-1], eng)

def needs_conversion(df_series):
if df_series.empty:
Expand Down
14 changes: 14 additions & 0 deletions tests/model_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,20 @@ def test_grains_dict(self):
self.assertEquals(d.get('P1D').function, 'DATE({col})')
self.assertEquals(d.get('Time Column').function, '{col}')

def test_single_statement(self):
main_db = self.get_main_database(db.session)

if main_db.backend == 'mysql':
df = main_db.get_df('SELECT 1', None)
self.assertEquals(df.iat[0, 0], 1)

def test_multi_statement(self):
main_db = self.get_main_database(db.session)

if main_db.backend == 'mysql':
df = main_db.get_df('USE superset; SELECT 1', None)
self.assertEquals(df.iat[0, 0], 1)


class SqlaTableModelTestCase(SupersetTestCase):

Expand Down

0 comments on commit d4c1946

Please sign in to comment.