Skip to content

Commit

Permalink
[SQL Lab] Allow running multiple statements (apache#6112)
Browse files Browse the repository at this point in the history
* Allow running multiple statements from SQL Lab

* fix tests

* More tests

* merge heads

* fix heads

(cherry picked from commit d427db0)
(cherry picked from commit 2980687a2a3382e172bf50c332abe0d2c7048b72)
  • Loading branch information
mistercrunch authored and betodealmeida committed Jan 30, 2019
1 parent d6de1cb commit aee086e
Show file tree
Hide file tree
Showing 18 changed files with 348 additions and 175 deletions.
14 changes: 11 additions & 3 deletions superset/assets/src/SqlLab/components/ResultSet.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ const defaultProps = {

const SEARCH_HEIGHT = 46;

const LOADING_STYLES = { position: 'relative', height: 50 };
const LOADING_STYLES = { position: 'relative', minHeight: 100 };

export default class ResultSet extends React.PureComponent {
constructor(props) {
Expand Down Expand Up @@ -218,11 +218,19 @@ export default class ResultSet extends React.PureComponent {
</Button>
);
}
const progressMsg = query && query.extra && query.extra.progress ? query.extra.progress : null;
return (
<div style={LOADING_STYLES}>
<div>
{!progressBar && <Loading position="normal" />}
</div>
<QueryStateLabel query={query} />
{!progressBar && <Loading />}
{progressBar}
<div>
{progressMsg && <Alert bsStyle="success">{progressMsg}</Alert>}
</div>
<div>
{progressBar}
</div>
<div>
{trackingUrl}
</div>
Expand Down
1 change: 1 addition & 0 deletions superset/assets/src/SqlLab/main.less
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
@import "../../stylesheets/less/cosmo/variables.less";
body {
overflow: hidden;
}
Expand Down
29 changes: 18 additions & 11 deletions superset/assets/src/components/Loading.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,27 +3,34 @@ import PropTypes from 'prop-types';

const propTypes = {
size: PropTypes.number,
position: PropTypes.oneOf(['floating', 'normal']),
};
const defaultProps = {
size: 50,
position: 'floating',
};

export default function Loading({ size }) {
const FLOATING_STYLE = {
padding: 0,
margin: 0,
position: 'absolute',
left: '50%',
top: '50%',
transform: 'translate(-50%, -50%)',
};

export default function Loading({ size, position }) {
const style = position === 'floating' ? FLOATING_STYLE : {};
const styleWithWidth = {
...style,
size,
};
return (
<img
className="loading"
alt="Loading..."
src="/static/assets/images/loading.gif"
style={{
width: Math.min(size, 50),
// height is auto
padding: 0,
margin: 0,
position: 'absolute',
left: '50%',
top: '50%',
transform: 'translate(-50%, -50%)',
}}
style={styleWithWidth}
/>
);
}
Expand Down
6 changes: 3 additions & 3 deletions superset/db_engine_specs.py
Original file line number Diff line number Diff line change
Expand Up @@ -149,18 +149,18 @@ def apply_limit_to_sql(cls, sql, limit, database):
)
return database.compile_sqla_query(qry)
elif LimitMethod.FORCE_LIMIT:
parsed_query = sql_parse.SupersetQuery(sql)
parsed_query = sql_parse.ParsedQuery(sql)
sql = parsed_query.get_query_with_new_limit(limit)
return sql

@classmethod
def get_limit_from_sql(cls, sql):
parsed_query = sql_parse.SupersetQuery(sql)
parsed_query = sql_parse.ParsedQuery(sql)
return parsed_query.limit

@classmethod
def get_query_with_new_limit(cls, sql, limit):
parsed_query = sql_parse.SupersetQuery(sql)
parsed_query = sql_parse.ParsedQuery(sql)
return parsed_query.get_query_with_new_limit(limit)

@staticmethod
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
"""Add extra column to Query
Revision ID: 0b1f1ab473c0
Revises: 55e910a74826
Create Date: 2018-11-05 08:42:56.181012
"""
from alembic import op
import sqlalchemy as sa

# revision identifiers, used by Alembic.
revision = '0b1f1ab473c0'
down_revision = '55e910a74826'


def upgrade():
op.add_column('query', sa.Column('extra_json', sa.Text(), nullable=True))


def downgrade():
op.drop_column('query', 'extra_json')
18 changes: 18 additions & 0 deletions superset/migrations/versions/de021a1ca60d_.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
"""empty message
Revision ID: de021a1ca60d
Revises: ('0b1f1ab473c0', 'cefabc8f7d38')
Create Date: 2018-12-18 22:45:55.783083
"""
# revision identifiers, used by Alembic.
revision = 'de021a1ca60d'
down_revision = ('0b1f1ab473c0', 'cefabc8f7d38', '3e1b21cd94a4')


def upgrade():
pass


def downgrade():
pass
20 changes: 20 additions & 0 deletions superset/models/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -305,3 +305,23 @@ def __init__( # noqa
self.duration = duration
self.status = status
self.error_message = error_message


class ExtraJSONMixin:
"""Mixin to add an `extra` column (JSON) and utility methods"""
extra_json = sa.Column(sa.Text, default='{}')

@property
def extra(self):
try:
return json.loads(self.extra_json)
except Exception:
return {}

def set_extra_json(self, d):
self.extra_json = json.dumps(d)

def set_extra_json_key(self, key, value):
extra = self.extra
extra[key] = value
self.extra_json = json.dumps(extra)
10 changes: 7 additions & 3 deletions superset/models/sql_lab.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,16 @@
from sqlalchemy.orm import backref, relationship

from superset import security_manager
from superset.models.helpers import AuditMixinNullable
from superset.models.helpers import AuditMixinNullable, ExtraJSONMixin
from superset.models.tags import QueryUpdater
from superset.utils.core import QueryStatus, user_label


class Query(Model):
"""ORM model for SQL query"""
class Query(Model, ExtraJSONMixin):
"""ORM model for SQL query
Now that SQL Lab support multi-statement execution, an entry in this
table may represent multiple SQL statements executed sequentially"""

__tablename__ = 'query'
id = Column(Integer, primary_key=True)
Expand Down Expand Up @@ -106,6 +109,7 @@ def to_dict(self):
'limit_reached': self.limit_reached,
'resultsKey': self.results_key,
'trackingUrl': self.tracking_url,
'extra': self.extra,
}

@property
Expand Down
2 changes: 1 addition & 1 deletion superset/security.py
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ def datasource_access_by_fullname(
database, table_name, schema=table_schema)

def rejected_datasources(self, sql, database, schema):
superset_query = sql_parse.SupersetQuery(sql)
superset_query = sql_parse.ParsedQuery(sql)
return [
t for t in superset_query.tables if not
self.datasource_access_by_fullname(database, t, schema)]
Expand Down
Loading

0 comments on commit aee086e

Please sign in to comment.