Skip to content

Commit

Permalink
fix test
Browse files Browse the repository at this point in the history
  • Loading branch information
dpgaspar committed May 19, 2022
2 parents 9612f06 + 660af40 commit 7bab7f5
Show file tree
Hide file tree
Showing 11 changed files with 126 additions and 46 deletions.
18 changes: 9 additions & 9 deletions RESOURCES/INTHEWILD.md
Original file line number Diff line number Diff line change
Expand Up @@ -120,10 +120,10 @@ Join our growing community!

### Education
- [Brilliant.org](https://brilliant.org/)
- [Udemy](https://www.udemy.com/) [@sungjuly]
- [VIPKID](https://www.vipkid.com.cn/) [@illpanda]
- [Sunbird](https://www.sunbird.org/) [@eksteporg]
- [The GRAPH Network](https://thegraphnetwork.org/)[@fccoelho]
- [Udemy](https://www.udemy.com/) [@sungjuly]
- [VIPKID](https://www.vipkid.com.cn/) [@illpanda]

### Energy
- [Airboxlab](https://foobot.io) [@antoine-galataud]
Expand All @@ -144,10 +144,10 @@ Join our growing community!
- [Symmetrics](https://www.symmetrics.fyi)

### Others
- [Dropbox](https://www.dropbox.com/) [@bkyryliuk]
- [Grassroot](https://www.grassrootinstitute.org/)
- [komoot](https://www.komoot.com/) [@christophlingg]
- [Let's Roam](https://www.letsroam.com/)
- [Twitter](https://twitter.com/)
- [VLMedia](https://www.vlmedia.com.tr/) [@ibotheperfect]
- [Yahoo!](https://yahoo.com/)
- [Dropbox](https://www.dropbox.com/) [@bkyryliuk]
- [Grassroot](https://www.grassrootinstitute.org/)
- [komoot](https://www.komoot.com/) [@christophlingg]
- [Let's Roam](https://www.letsroam.com/)
- [Twitter](https://twitter.com/)
- [VLMedia](https://www.vlmedia.com.tr/) [@ibotheperfect]
- [Yahoo!](https://yahoo.com/)
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,10 @@ const StyledDiv = styled.div`
margin-left: ${theme.gridUnit}px;
}
.reactable-data tr {
font-feature-settings: 'tnum' 1;
}
.reactable-data tr,
.reactable-header-sortable {
-webkit-transition: ease-in-out 0.1s;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@ export const Styles = styled.div`
top: 0;
}
table tbody tr {
font-feature-settings: 'tnum' 1;
}
table.pvtTable thead tr th,
table.pvtTable tbody tr th {
background-color: ${theme.colors.grayscale.light5};
Expand Down
4 changes: 4 additions & 0 deletions superset-frontend/src/components/Chart/Chart.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,10 @@ const Styles = styled.div`
.slice_container {
height: ${p => p.height}px;
.pivot_table tbody tr {
font-feature-settings: 'tnum' 1;
}
}
`;

Expand Down
1 change: 1 addition & 0 deletions superset-frontend/src/components/TableCollection/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,7 @@ export const Table = styled.table`
}
.table-cell {
font-feature-settings: 'tnum' 1;
text-overflow: ellipsis;
overflow: hidden;
max-width: 320px;
Expand Down
8 changes: 8 additions & 0 deletions superset/queries/dao.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
# under the License.
import logging
from datetime import datetime
from typing import Any, Dict

from superset.dao.base import BaseDAO
from superset.extensions import db
Expand Down Expand Up @@ -48,3 +49,10 @@ def update_saved_query_exec_info(query_id: int) -> None:
saved_query.rows = query.rows
saved_query.last_run = datetime.now()
db.session.commit()

@staticmethod
def save_metadata(query: Query, payload: Dict[str, Any]) -> None:
# pull relevant data from payload and store in extra_json
columns = payload.get("columns", {})
db.session.add(query)
query.set_extra_json_key("columns", columns)
25 changes: 13 additions & 12 deletions superset/sqllab/command.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
QueryIsForbiddenToAccessException,
SqlLabException,
)
from superset.sqllab.execution_context_convertor import ExecutionContextConvertor
from superset.sqllab.limiting_factor import LimitingFactor

if TYPE_CHECKING:
Expand All @@ -42,6 +43,7 @@
from superset.sqllab.sql_json_executer import SqlJsonExecutor
from superset.sqllab.sqllab_execution_context import SqlJsonExecutionContext


logger = logging.getLogger(__name__)

CommandResult = Dict[str, Any]
Expand Down Expand Up @@ -94,11 +96,19 @@ def run( # pylint: disable=too-many-statements,useless-suppression
status = SqlJsonExecutionStatus.QUERY_ALREADY_CREATED
else:
status = self._run_sql_json_exec_from_scratch()

self._execution_context_convertor.set_payload(
self._execution_context, status
)

# save columns into metadata_json
self._query_dao.save_metadata(
self._execution_context.query, self._execution_context_convertor.payload
)

return {
"status": status,
"payload": self._execution_context_convertor.to_payload(
self._execution_context, status
),
"payload": self._execution_context_convertor.serialize_payload(),
}
except (SqlLabException, SupersetErrorsException) as ex:
raise ex
Expand Down Expand Up @@ -209,12 +219,3 @@ def validate(self, query: Query) -> None:
class SqlQueryRender:
def render(self, execution_context: SqlJsonExecutionContext) -> str:
raise NotImplementedError()


class ExecutionContextConvertor:
def to_payload(
self,
execution_context: SqlJsonExecutionContext,
execution_status: SqlJsonExecutionStatus,
) -> str:
raise NotImplementedError()
47 changes: 24 additions & 23 deletions superset/sqllab/execution_context_convertor.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,52 +16,53 @@
# under the License.
from __future__ import annotations

from typing import TYPE_CHECKING
import logging
from typing import Any, Dict, TYPE_CHECKING

import simplejson as json

import superset.utils.core as utils
from superset.sqllab.command import ExecutionContextConvertor
from superset.sqllab.command_status import SqlJsonExecutionStatus
from superset.sqllab.utils import apply_display_max_row_configuration_if_require

logger = logging.getLogger(__name__)

if TYPE_CHECKING:
from superset.models.sql_lab import Query
from superset.sqllab.sql_json_executer import SqlResults
from superset.sqllab.sqllab_execution_context import SqlJsonExecutionContext


class ExecutionContextConvertorImpl(ExecutionContextConvertor):
class ExecutionContextConvertor:
_max_row_in_display_configuration: int # pylint: disable=invalid-name
_exc_status: SqlJsonExecutionStatus
payload: Dict[str, Any]

def set_max_row_in_display(self, value: int) -> None:
self._max_row_in_display_configuration = value # pylint: disable=invalid-name

def to_payload(
def set_payload(
self,
execution_context: SqlJsonExecutionContext,
execution_status: SqlJsonExecutionStatus,
) -> str:

) -> None:
self._exc_status = execution_status
if execution_status == SqlJsonExecutionStatus.HAS_RESULTS:
return self._to_payload_results_based(
execution_context.get_execution_result() or {}
)
return self._to_payload_query_based(execution_context.query)
self.payload = execution_context.get_execution_result() or {}
else:
self.payload = execution_context.query.to_dict()

def _to_payload_results_based(self, execution_result: SqlResults) -> str:
return json.dumps(
apply_display_max_row_configuration_if_require(
execution_result, self._max_row_in_display_configuration
),
default=utils.pessimistic_json_iso_dttm_ser,
ignore_nan=True,
encoding=None,
)
def serialize_payload(self) -> str:
if self._exc_status == SqlJsonExecutionStatus.HAS_RESULTS:
return json.dumps(
apply_display_max_row_configuration_if_require(
self.payload, self._max_row_in_display_configuration
),
default=utils.pessimistic_json_iso_dttm_ser,
ignore_nan=True,
encoding=None,
)

def _to_payload_query_based( # pylint: disable=no-self-use
self, query: Query
) -> str:
return json.dumps(
{"query": query.to_dict()}, default=utils.json_int_dttm_ser, ignore_nan=True
{"query": self.payload}, default=utils.json_int_dttm_ser, ignore_nan=True
)
4 changes: 2 additions & 2 deletions superset/views/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@
QueryIsForbiddenToAccessException,
SqlLabException,
)
from superset.sqllab.execution_context_convertor import ExecutionContextConvertorImpl
from superset.sqllab.execution_context_convertor import ExecutionContextConvertor
from superset.sqllab.limiting_factor import LimitingFactor
from superset.sqllab.query_render import SqlQueryRenderImpl
from superset.sqllab.sql_json_executer import (
Expand Down Expand Up @@ -2406,7 +2406,7 @@ def _create_sql_json_command(
sql_json_executor = Superset._create_sql_json_executor(
execution_context, query_dao
)
execution_context_convertor = ExecutionContextConvertorImpl()
execution_context_convertor = ExecutionContextConvertor()
execution_context_convertor.set_max_row_in_display(
int(config.get("DISPLAY_MAX_ROW")) # type: ignore
)
Expand Down
2 changes: 2 additions & 0 deletions tests/integration_tests/security_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,8 @@ def test_set_perm_sqla_table_none(self):
"datasource_access", f"[None].[tmp_perm_table](id:{stored_table.id})"
)
)
session.delete(table)
session.commit()

def test_set_perm_database(self):
session = db.session
Expand Down
55 changes: 55 additions & 0 deletions tests/unit_tests/dao/queries_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
# 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 json
from typing import Iterator

import pytest
from sqlalchemy.orm.session import Session


def test_query_dao_save_metadata(app_context: None, session: Session) -> None:
from superset.models.core import Database
from superset.models.sql_lab import Query

engine = session.get_bind()
Query.metadata.create_all(engine) # pylint: disable=no-member

db = Database(database_name="my_database", sqlalchemy_uri="sqlite://")

query_obj = Query(
client_id="foo",
database=db,
tab_name="test_tab",
sql_editor_id="test_editor_id",
sql="select * from bar",
select_sql="select * from bar",
executed_sql="select * from bar",
limit=100,
select_as_cta=False,
rows=100,
error_message="none",
results_key="abc",
)

session.add(db)
session.add(query_obj)

from superset.queries.dao import QueryDAO

query = session.query(Query).one()
QueryDAO.save_metadata(query=query, payload={"columns": []})
assert query.extra.get("columns", None) == []

0 comments on commit 7bab7f5

Please sign in to comment.