betodealmeida commented on code in PR #34308:
URL: https://github.com/apache/superset/pull/34308#discussion_r2231992997
##########
superset/models/helpers.py:
##########
@@ -1258,6 +1258,65 @@ def _get_top_groups(
return or_(*groups)
+ def _apply_series_others_grouping(
+ self,
+ select_exprs: list[Any],
+ groupby_all_columns: dict[str, Any],
+ groupby_series_columns: dict[str, Any],
+ condition_factory: Callable[[str, Any], Any],
+ ) -> tuple[list[Any], dict[str, Any]]:
+ """
+ Apply "Others" grouping to series columns in both SELECT and GROUP BY
clauses.
+
+ This method encapsulates the common logic for replacing series columns
with
+ CASE expressions that group remaining series into an "Others" category
when
+ the series limit is reached.
+
+ Args:
+ select_exprs: List of SELECT expressions to modify
+ groupby_all_columns: Dict of GROUP BY columns to modify
+ groupby_series_columns: Dict of series columns to apply Others
grouping to
+ condition_factory: Function that takes (col_name, original_expr)
and returns
+ the condition for when to keep original value vs use "Others"
+
+ Returns:
+ Tuple of (modified_select_exprs, modified_groupby_all_columns)
+ """
+ # Modify SELECT expressions
+ modified_select_exprs = []
+ for expr in select_exprs:
+ if hasattr(expr, "name") and expr.name in groupby_series_columns:
+ # Create condition for this column using the factory function
+ condition = condition_factory(expr.name, expr)
+
+ # Create CASE expression: condition true -> original, else
"Others"
+ case_expr = sa.case(
+ [(condition, expr)], else_=sa.literal_column("'Others'")
Review Comment:
Instead of using `literal_column` and passing the single quotes manually
(which might not be universal across all DBs), it's better to:
```python
other = sa.literal("Others")
```
Same for line 1311. When compiled it should do the right thing:
```python
>>> str(sa.literal("Others").compile(compile_kwargs={"literal_binds": True}))
"'Others'"
```
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]