-
Notifications
You must be signed in to change notification settings - Fork 14.3k
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: always denorm column value before querying values #25919
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
d282b04
always denorm column value before querying values
hughhhh b1051a7
cleanup
hughhhh 29b45fb
refactor
hughhhh 2ee8408
ok
hughhhh e132c43
Merge branch 'master' of https://github.com/preset-io/superset into f…
hughhhh ec13b90
refactor get_fetch_values_predicate
hughhhh 16825c3
fix linting
hughhhh eff8fa3
fix helpers
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -705,10 +705,7 @@ class ExploreMixin: # pylint: disable=too-many-public-methods | |
"MIN": sa.func.MIN, | ||
"MAX": sa.func.MAX, | ||
} | ||
|
||
@property | ||
def fetch_value_predicate(self) -> str: | ||
return "fix this!" | ||
fetch_values_predicate = None | ||
|
||
@property | ||
def type(self) -> str: | ||
|
@@ -785,17 +782,20 @@ def sql(self) -> str: | |
def columns(self) -> list[Any]: | ||
raise NotImplementedError() | ||
|
||
def get_fetch_values_predicate( | ||
self, template_processor: Optional[BaseTemplateProcessor] = None | ||
) -> TextClause: | ||
raise NotImplementedError() | ||
|
||
def get_extra_cache_keys(self, query_obj: dict[str, Any]) -> list[Hashable]: | ||
raise NotImplementedError() | ||
|
||
def get_template_processor(self, **kwargs: Any) -> BaseTemplateProcessor: | ||
raise NotImplementedError() | ||
|
||
def get_fetch_values_predicate( | ||
self, | ||
template_processor: Optional[ # pylint: disable=unused-argument | ||
BaseTemplateProcessor | ||
] = None, # pylint: disable=unused-argument | ||
) -> TextClause: | ||
return self.fetch_values_predicate | ||
|
||
def get_sqla_row_level_filters( | ||
self, | ||
template_processor: BaseTemplateProcessor, | ||
|
@@ -1341,36 +1341,34 @@ def get_time_filter( # pylint: disable=too-many-arguments | |
return and_(*l) | ||
|
||
def values_for_column(self, column_name: str, limit: int = 10000) -> list[Any]: | ||
"""Runs query against sqla to retrieve some | ||
sample values for the given column. | ||
""" | ||
cols = {} | ||
for col in self.columns: | ||
if isinstance(col, dict): | ||
cols[col.get("column_name")] = col | ||
else: | ||
cols[col.column_name] = col | ||
|
||
target_col = cols[column_name] | ||
tp = None # todo(hughhhh): add back self.get_template_processor() | ||
# always denormalize column name before querying for values | ||
db_dialect = self.database.get_dialect() | ||
denomalized_col_name = self.database.db_engine_spec.denormalize_name( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: denomalized -> denormalized |
||
db_dialect, column_name | ||
) | ||
cols = {col.column_name: col for col in self.columns} | ||
target_col = cols[denomalized_col_name] | ||
tp = self.get_template_processor() | ||
tbl, cte = self.get_from_clause(tp) | ||
|
||
if isinstance(target_col, dict): | ||
sql_column = sa.column(target_col.get("name")) | ||
else: | ||
sql_column = target_col | ||
|
||
qry = sa.select([sql_column]).select_from(tbl).distinct() | ||
qry = ( | ||
sa.select([target_col.get_sqla_col(template_processor=tp)]) | ||
.select_from(tbl) | ||
.distinct() | ||
) | ||
if limit: | ||
qry = qry.limit(limit) | ||
|
||
if self.fetch_values_predicate: | ||
qry = qry.where(self.get_fetch_values_predicate(template_processor=tp)) | ||
|
||
with self.database.get_sqla_engine_with_context() as engine: | ||
sql = qry.compile(engine, compile_kwargs={"literal_binds": True}) | ||
sql = self._apply_cte(sql, cte) | ||
sql = self.mutate_query_from_config(sql) | ||
|
||
df = pd.read_sql_query(sql=sql, con=engine) | ||
return df[column_name].to_list() | ||
return df[denomalized_col_name].to_list() | ||
|
||
def get_timestamp_expression( | ||
self, | ||
|
@@ -1942,7 +1940,7 @@ def get_sqla_query( # pylint: disable=too-many-arguments,too-many-locals,too-ma | |
) | ||
having_clause_and += [self.text(having)] | ||
|
||
if apply_fetch_values_predicate and self.fetch_values_predicate: # type: ignore | ||
if apply_fetch_values_predicate and self.fetch_values_predicate: | ||
qry = qry.where( | ||
self.get_fetch_values_predicate(template_processor=template_processor) | ||
) | ||
|
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.
denomarlized name will return the original value if it can't be adjusted by the engine:
preset-io/superset@60e1526/superset/db_engine_specs/base.py#L1950