Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: feature flags typing #15254

Merged
merged 7 commits into from
Nov 19, 2021
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions UPDATING.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ assists people when migrating to a new version.

### Breaking Changes

- [15254](https://github.com/apache/superset/pull/15254): Previously `QUERY_COST_FORMATTERS_BY_ENGINE` and `SQL_VALIDATORS_BY_ENGINE` were defined in the feature flag dictionary in the `config.py` file. These should now be defined as a top-level config, with the feature flag dictionary being reserved for boolean only values.
- [17290](https://github.com/apache/superset/pull/17290): Bumps pandas to `1.3.4` and pyarrow to `5.0.0`
- [16660](https://github.com/apache/incubator-superset/pull/16660): The `columns` Jinja parameter has been renamed `table_columns` to make the `columns` query object parameter available in the Jinja context.
- [16711](https://github.com/apache/incubator-superset/pull/16711): The `url_param` Jinja function will now by default escape the result. For instance, the value `O'Brien` will now be changed to `O''Brien`. To disable this behavior, call `url_param` with `escape_result` set to `False`: `url_param("my_key", "my default", escape_result=False)`.
Expand Down
8 changes: 6 additions & 2 deletions superset-frontend/src/SqlLab/components/SqlEditor/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,11 @@ const SET_QUERY_EDITOR_SQL_DEBOUNCE_MS = 2000;
const VALIDATION_DEBOUNCE_MS = 600;
const WINDOW_RESIZE_THROTTLE_MS = 100;

const appContainer = document.getElementById('app');
const bootstrapData = JSON.parse(appContainer.getAttribute('data-bootstrap'));
const validatorMap =
bootstrapData?.common?.conf?.SQL_VALIDATORS_BY_ENGINE || {};

const LimitSelectStyled = styled.span`
.ant-dropdown-trigger {
align-items: center;
Expand Down Expand Up @@ -392,8 +397,7 @@ class SqlEditor extends React.PureComponent {
canValidateQuery() {
// Check whether or not we can validate the current query based on whether
// or not the backend has a validator configured for it.
const validatorMap = window.featureFlags.SQL_VALIDATORS_BY_ENGINE;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here - instead of checking the featureFlags map, we should check bootstrap data.

if (this.props.database && validatorMap != null) {
if (this.props.database) {
return validatorMap.hasOwnProperty(this.props.database.backend);
}
return false;
Expand Down
10 changes: 6 additions & 4 deletions superset/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -798,10 +798,12 @@ class CeleryConfig: # pylint: disable=too-few-public-methods
#
# return out
#
# FEATURE_FLAGS = {
# "ESTIMATE_QUERY_COST": True,
# "QUERY_COST_FORMATTERS_BY_ENGINE": {"postgresql": postgres_query_cost_formatter},
# }
# Then on define the formatter on the config:
#
# "QUERY_COST_FORMATTERS_BY_ENGINE": {"postgresql": postgres_query_cost_formatter},
QUERY_COST_FORMATTERS_BY_ENGINE: Dict[
str, Callable[[List[Dict[str, Any]]], List[Dict[str, Any]]]
] = {}

# Flag that controls if limit should be enforced on the CTA (create table as queries).
SQLLAB_CTAS_NO_LIMIT = False
Expand Down
6 changes: 3 additions & 3 deletions superset/utils/feature_flag_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
# specific language governing permissions and limitations
# under the License.
from copy import deepcopy
from typing import Any, Dict
from typing import Dict

from flask import Flask

Expand All @@ -25,15 +25,15 @@ def __init__(self) -> None:
super().__init__()
self._get_feature_flags_func = None
self._is_feature_enabled_func = None
self._feature_flags: Dict[str, Any] = {}
self._feature_flags: Dict[str, bool] = {}

def init_app(self, app: Flask) -> None:
self._get_feature_flags_func = app.config["GET_FEATURE_FLAGS_FUNC"]
self._is_feature_enabled_func = app.config["IS_FEATURE_ENABLED_FUNC"]
self._feature_flags = app.config["DEFAULT_FEATURE_FLAGS"]
self._feature_flags.update(app.config["FEATURE_FLAGS"])

def get_feature_flags(self) -> Dict[str, Any]:
def get_feature_flags(self) -> Dict[str, bool]:
if self._get_feature_flags_func:
return self._get_feature_flags_func(deepcopy(self._feature_flags))
if callable(self._is_feature_enabled_func):
Expand Down
1 change: 1 addition & 0 deletions superset/views/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@
"DISPLAY_MAX_ROW",
"GLOBAL_ASYNC_QUERIES_TRANSPORT",
"GLOBAL_ASYNC_QUERIES_POLLING_DELAY",
"SQL_VALIDATORS_BY_ENGINE",
"SQLALCHEMY_DOCS_URL",
"SQLALCHEMY_DISPLAY_TEXT",
"GLOBAL_ASYNC_QUERIES_WEBSOCKET_URL",
Expand Down
9 changes: 4 additions & 5 deletions superset/views/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@
conf,
db,
event_logger,
get_feature_flags,
is_feature_enabled,
results_backend,
results_backend_use_msgpack,
Expand Down Expand Up @@ -2221,9 +2220,9 @@ def estimate_query_cost( # pylint: disable=no-self-use
return json_error_response(utils.error_msg_from_exception(ex))

spec = mydb.db_engine_spec
query_cost_formatters: Dict[str, Any] = get_feature_flags().get(
"QUERY_COST_FORMATTERS_BY_ENGINE", {}
)
query_cost_formatters: Dict[str, Any] = app.config[
"QUERY_COST_FORMATTERS_BY_ENGINE"
]
query_cost_formatter = query_cost_formatters.get(
spec.engine, spec.query_cost_formatter
)
Expand Down Expand Up @@ -2414,7 +2413,7 @@ def validate_sql_json(
)

spec = mydb.db_engine_spec
validators_by_engine = get_feature_flags().get("SQL_VALIDATORS_BY_ENGINE")
validators_by_engine = app.config["SQL_VALIDATORS_BY_ENGINE"]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It appears that SQL validation has never worked based on the SQL_VALIDATORS_BY_ENGINE top level config parameter, as this was referencing the feature flag map.

if not validators_by_engine or spec.engine not in validators_by_engine:
return json_error_response(
"no SQL validator is configured for {}".format(spec.engine), status=400
Expand Down
20 changes: 9 additions & 11 deletions tests/integration_tests/sql_validator_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,11 @@

from .base_tests import SupersetTestCase

PRESTO_TEST_FEATURE_FLAGS = {
"SQL_VALIDATORS_BY_ENGINE": {
"presto": "PrestoDBSQLValidator",
"sqlite": "PrestoDBSQLValidator",
"postgresql": "PrestoDBSQLValidator",
"mysql": "PrestoDBSQLValidator",
}
PRESTO_SQL_VALIDATORS_BY_ENGINE = {
"presto": "PrestoDBSQLValidator",
"sqlite": "PrestoDBSQLValidator",
"postgresql": "PrestoDBSQLValidator",
"mysql": "PrestoDBSQLValidator",
}


Expand All @@ -65,8 +63,8 @@ def test_validate_sql_endpoint_noconfig(self):

@patch("superset.views.core.get_validator_by_name")
@patch.dict(
"superset.extensions.feature_flag_manager._feature_flags",
PRESTO_TEST_FEATURE_FLAGS,
"superset.config.SQL_VALIDATORS_BY_ENGINE",
PRESTO_SQL_VALIDATORS_BY_ENGINE,
clear=True,
)
def test_validate_sql_endpoint_mocked(self, get_validator_by_name):
Expand Down Expand Up @@ -98,8 +96,8 @@ def test_validate_sql_endpoint_mocked(self, get_validator_by_name):

@patch("superset.views.core.get_validator_by_name")
@patch.dict(
"superset.extensions.feature_flag_manager._feature_flags",
PRESTO_TEST_FEATURE_FLAGS,
"superset.config.SQL_VALIDATORS_BY_ENGINE",
PRESTO_SQL_VALIDATORS_BY_ENGINE,
clear=True,
)
def test_validate_sql_endpoint_failure(self, get_validator_by_name):
Expand Down