Skip to content

Commit

Permalink
Add force_ctas_schema to query model when enabled (#1825)
Browse files Browse the repository at this point in the history
* Add force_ctas_schema to query model when enabled

* Add schema to temp_table_name

* Remove extra arg in create_table_as
  • Loading branch information
vera-liu authored Dec 15, 2016
1 parent b6cba13 commit e06a0cd
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 6 deletions.
5 changes: 4 additions & 1 deletion superset/assets/javascripts/SqlLab/components/QueryTable.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,10 @@ class QueryTable extends React.PureComponent {
/>
);
} else {
q.output = [q.schema, q.tempTable].filter((v) => (v)).join('.');
// if query was run using ctas and force_ctas_schema was set
// tempTable will have the schema
const schemaUsed = q.ctas && q.tempTable.includes('.') ? '' : q.schema;
q.output = [schemaUsed, q.tempTable].filter((v) => (v)).join('.');
}
q.progress = (
<ProgressBar
Expand Down
6 changes: 2 additions & 4 deletions superset/sql_lab.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ def dedup(l, suffix='__'):
return new_l


def create_table_as(sql, table_name, schema=None, override=False):
def create_table_as(sql, table_name, override=False):
"""Reformats the query into the create table as query.
Works only for the single select SQL statements, in all other cases
Expand All @@ -55,8 +55,6 @@ def create_table_as(sql, table_name, schema=None, override=False):
# TODO(bkyryliuk): drop table if allowed, check the namespace and
# the permissions.
# TODO raise if multi-statement
if schema:
table_name = schema + '.' + table_name
exec_sql = ''
if override:
exec_sql = 'DROP TABLE IF EXISTS {table_name};\n'
Expand Down Expand Up @@ -105,7 +103,7 @@ def handle_error(msg):
query.user_id,
start_dttm.strftime('%Y_%m_%d_%H_%M_%S'))
executed_sql = create_table_as(
executed_sql, query.tmp_table_name, database.force_ctas_schema)
executed_sql, query.tmp_table_name)
query.select_as_cta_used = True
elif (
query.limit and superset_query.is_select() and
Expand Down
10 changes: 9 additions & 1 deletion superset/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -2392,6 +2392,14 @@ def table_accessible(database, full_table_name, schema_name=None):
get_datasource_access_error_msg('{}'.format(rejected_tables)))
session.commit()

select_as_cta = request.form.get('select_as_cta') == 'true'
tmp_table_name = request.form.get('tmp_table_name')
if select_as_cta and mydb.force_ctas_schema:
tmp_table_name = '{}.{}'.format(
mydb.force_ctas_schema,
tmp_table_name
)

query = models.Query(
database_id=int(database_id),
limit=int(app.config.get('SQL_MAX_ROW', None)),
Expand All @@ -2402,7 +2410,7 @@ def table_accessible(database, full_table_name, schema_name=None):
tab_name=request.form.get('tab'),
status=QueryStatus.PENDING if async else QueryStatus.RUNNING,
sql_editor_id=request.form.get('sql_editor_id'),
tmp_table_name=request.form.get('tmp_table_name'),
tmp_table_name=tmp_table_name,
user_id=int(g.user.get_id()),
client_id=request.form.get('client_id'),
)
Expand Down

0 comments on commit e06a0cd

Please sign in to comment.