Skip to content

Commit

Permalink
Merge pull request #68 from lyft/merge_apache_20190917a
Browse files Browse the repository at this point in the history
Merge apache 20190917a
  • Loading branch information
Beto Dealmeida committed Sep 18, 2019
2 parents 3a582f5 + 7d7751a commit f15343c
Show file tree
Hide file tree
Showing 9 changed files with 428 additions and 93 deletions.
72 changes: 72 additions & 0 deletions superset/assets/src/SqlLab/components/ShowSQL.jsx
@@ -0,0 +1,72 @@
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
import React from 'react';
import PropTypes from 'prop-types';
import SyntaxHighlighter, { registerLanguage } from 'react-syntax-highlighter/dist/light';
import sql from 'react-syntax-highlighter/dist/languages/hljs/sql';
import github from 'react-syntax-highlighter/dist/styles/hljs/github';

import { t } from '@superset-ui/translation';

import Link from './Link';
import ModalTrigger from '../../components/ModalTrigger';

registerLanguage('sql', sql);

const propTypes = {
tooltipText: PropTypes.string,
title: PropTypes.string,
sql: PropTypes.string,
};

const defaultProps = {
tooltipText: t('Show SQL'),
title: t('SQL statement'),
sql: '',
};

export default class ShowSQL extends React.PureComponent {
renderModalBody() {
return (
<div>
<SyntaxHighlighter language="sql" style={github}>
{this.props.sql}
</SyntaxHighlighter>
</div>
);
}
render() {
return (
<ModalTrigger
modalTitle={this.props.title}
triggerNode={
<Link
className="fa fa-eye pull-left m-l-2"
tooltip={this.props.tooltipText}
href="#"
/>
}
modalBody={this.renderModalBody()}
/>
);
}
}

ShowSQL.propTypes = propTypes;
ShowSQL.defaultProps = defaultProps;
8 changes: 8 additions & 0 deletions superset/assets/src/SqlLab/components/TableElement.jsx
Expand Up @@ -25,6 +25,7 @@ import { t } from '@superset-ui/translation';
import CopyToClipboard from '../../components/CopyToClipboard';
import Link from './Link';
import ColumnElement from './ColumnElement';
import ShowSQL from './ShowSQL';
import ModalTrigger from '../../components/ModalTrigger';
import Loading from '../../components/Loading';

Expand Down Expand Up @@ -172,6 +173,13 @@ class TableElement extends React.PureComponent {
tooltipText={t('Copy SELECT statement to the clipboard')}
/>
}
{table.view &&
<ShowSQL
sql={table.view}
tooltipText={t('Show CREATE VIEW statement')}
title={t('CREATE VIEW statement')}
/>
}
<Link
className="fa fa-times table-remove pull-left m-l-2"
onClick={this.removeTable}
Expand Down
31 changes: 31 additions & 0 deletions superset/assets/src/components/TableSelector.jsx
Expand Up @@ -110,6 +110,7 @@ export default class TableSelector extends React.PureComponent {
schema: o.schema,
label: o.label,
title: o.title,
type: o.type,
}));
return ({ options });
});
Expand Down Expand Up @@ -140,6 +141,7 @@ export default class TableSelector extends React.PureComponent {
schema: o.schema,
label: o.label,
title: o.title,
type: o.type,
}));
this.setState(() => ({
tableLoading: false,
Expand Down Expand Up @@ -203,6 +205,33 @@ export default class TableSelector extends React.PureComponent {
{db.database_name}
</span>);
}
renderTableOption({ focusOption, focusedOption, key, option, selectValue, style, valueArray }) {
const classNames = ['Select-option'];
if (option === focusedOption) {
classNames.push('is-focused');
}
if (valueArray.indexOf(option) >= 0) {
classNames.push('is-selected');
}
return (
<div
className={classNames.join(' ')}
key={key}
onClick={() => selectValue(option)}
onMouseEnter={() => focusOption(option)}
style={style}
>
<span>
<span className="m-r-5">
<small className="text-muted">
<i className={`fa fa-${option.type === 'view' ? 'eye' : 'table'}`} />
</small>
</span>
{option.label}
</span>
</div>
);
}
renderSelectRow(select, refreshBtn) {
return (
<div className="section">
Expand Down Expand Up @@ -280,6 +309,7 @@ export default class TableSelector extends React.PureComponent {
onChange={this.changeTable}
options={options}
value={this.state.tableName}
optionRenderer={this.renderTableOption}
/>) : (
<Select
async
Expand All @@ -291,6 +321,7 @@ export default class TableSelector extends React.PureComponent {
onChange={this.changeTable}
value={this.state.tableName}
loadOptions={this.getTableNamesBySubStr}
optionRenderer={this.renderTableOption}
/>);
return this.renderSelectRow(
select,
Expand Down
12 changes: 8 additions & 4 deletions superset/db_engine_specs/base.py
Expand Up @@ -130,6 +130,13 @@ class BaseEngineSpec:
def get_allow_cost_estimate(cls, version: str = None) -> bool:
return False

@classmethod
def get_engine(cls, database, schema=None, source=None):
user_name = utils.get_username()
return database.get_sqla_engine(
schema=schema, nullpool=True, user_name=user_name, source=source
)

@classmethod
def get_timestamp_expr(
cls, col: ColumnClause, pdf: Optional[str], time_grain: Optional[str]
Expand Down Expand Up @@ -688,10 +695,7 @@ def estimate_query_cost(
parsed_query = sql_parse.ParsedQuery(sql)
statements = parsed_query.get_statements()

engine = database.get_sqla_engine(
schema=schema, nullpool=True, user_name=user_name, source=source
)

engine = cls.get_engine(database, schema=schema, source=source)
costs = []
with closing(engine.raw_connection()) as conn:
with closing(conn.cursor()) as cursor:
Expand Down

0 comments on commit f15343c

Please sign in to comment.