-
Notifications
You must be signed in to change notification settings - Fork 14.4k
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
chore: Hugh/migrate estimate query cost to v1 #23226
Merged
+329
−15
Merged
Changes from all commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
f929d56
chore: Migrate /superset/estimate_query_cost/<database_id>/<schema>/ …
diegomedina248 a4ec247
Merge branch 'master' of github.com:apache/superset into dm/migrate-e…
diegomedina248 a7234a8
cleanup
diegomedina248 efc3e0e
Merge branch 'master' of github.com:apache/superset into dm/migrate-e…
diegomedina248 c660773
PR comments
diegomedina248 b8af81e
add superset
hughhhh 3c39fbf
address concerns
hughhhh 8bc92b0
Merge branch 'master' of https://github.com/apache/superset into hugh…
hughhhh 75899cb
fix lint
hughhhh ba3835c
add read permission
hughhhh 2fa8cd3
lit
hughhhh 2fdd828
Merge branch 'master' of https://github.com/apache/superset into hugh…
hughhhh 541cfd0
remove permission
hughhhh 3d465f0
ok
hughhhh 3883ad3
address concerns
hughhhh 715f491
Merge branch 'master' of https://github.com/apache/superset into hugh…
hughhhh 0e391f9
yerp
hughhhh 36d7b66
yup
hughhhh File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
# 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. | ||
from __future__ import annotations | ||
|
||
import logging | ||
from typing import Any, Dict, List | ||
|
||
from flask_babel import gettext as __ | ||
|
||
from superset import app, db | ||
from superset.commands.base import BaseCommand | ||
from superset.errors import ErrorLevel, SupersetError, SupersetErrorType | ||
from superset.exceptions import SupersetErrorException, SupersetTimeoutException | ||
from superset.jinja_context import get_template_processor | ||
from superset.models.core import Database | ||
from superset.sqllab.schemas import EstimateQueryCostSchema | ||
from superset.utils import core as utils | ||
|
||
config = app.config | ||
SQLLAB_QUERY_COST_ESTIMATE_TIMEOUT = config["SQLLAB_QUERY_COST_ESTIMATE_TIMEOUT"] | ||
stats_logger = config["STATS_LOGGER"] | ||
|
||
logger = logging.getLogger(__name__) | ||
|
||
|
||
class QueryEstimationCommand(BaseCommand): | ||
_database_id: int | ||
_sql: str | ||
_template_params: Dict[str, Any] | ||
_schema: str | ||
_database: Database | ||
|
||
def __init__(self, params: EstimateQueryCostSchema) -> None: | ||
self._database_id = params.get("database_id") | ||
self._sql = params.get("sql", "") | ||
self._template_params = params.get("template_params", {}) | ||
self._schema = params.get("schema", "") | ||
|
||
def validate(self) -> None: | ||
self._database = db.session.query(Database).get(self._database_id) | ||
if not self._database: | ||
raise SupersetErrorException( | ||
SupersetError( | ||
message=__("The database could not be found"), | ||
error_type=SupersetErrorType.RESULTS_BACKEND_ERROR, | ||
level=ErrorLevel.ERROR, | ||
), | ||
status=404, | ||
) | ||
|
||
def run( | ||
self, | ||
) -> List[Dict[str, Any]]: | ||
self.validate() | ||
|
||
sql = self._sql | ||
if self._template_params: | ||
template_processor = get_template_processor(self._database) | ||
sql = template_processor.process_template(sql, **self._template_params) | ||
|
||
timeout = SQLLAB_QUERY_COST_ESTIMATE_TIMEOUT | ||
timeout_msg = f"The estimation exceeded the {timeout} seconds timeout." | ||
try: | ||
with utils.timeout(seconds=timeout, error_message=timeout_msg): | ||
cost = self._database.db_engine_spec.estimate_query_cost( | ||
self._database, self._schema, sql, utils.QuerySource.SQL_LAB | ||
) | ||
except SupersetTimeoutException as ex: | ||
logger.exception(ex) | ||
raise SupersetErrorException( | ||
SupersetError( | ||
message=__( | ||
"The query estimation was killed after %(sqllab_timeout)s " | ||
"seconds. It might be too complex, or the database might be " | ||
"under heavy load.", | ||
sqllab_timeout=SQLLAB_QUERY_COST_ESTIMATE_TIMEOUT, | ||
), | ||
error_type=SupersetErrorType.SQLLAB_TIMEOUT_ERROR, | ||
level=ErrorLevel.ERROR, | ||
), | ||
status=500, | ||
) from ex | ||
|
||
spec = self._database.db_engine_spec | ||
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 | ||
) | ||
cost = query_cost_formatter(cost) | ||
return cost |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
what do you think about adding this endpoint to a
can read on SQLLab
permission name?