Repository: apache/superset
File: superset/models/helpers.py (Python)

Code before:
        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'")
                )
                case_expr = self.make_sqla_column_compatible(case_expr, expr.name)
                modified_select_exprs.append(case_expr)
            else:
                modified_select_exprs.append(expr)

        # Modify GROUP BY expressions
        modified_groupby_all_columns = {}
        for col_name, gby_expr in groupby_all_columns.items():
            if col_name in groupby_series_columns:
                # Create condition for this column using the factory function
                condition = condition_factory(col_name, gby_expr)

                # Create CASE expression for groupby
                case_expr = sa.case(
                    [(condition, gby_expr)],
                    else_=sa.literal_column("'Others'"),
                )
                case_expr = self.make_sqla_column_compatible(case_expr, col_name)
                modified_groupby_all_columns[col_name] = case_expr
            else:
                modified_groupby_all_columns[col_name] = gby_expr

        return modified_select_exprs, modified_groupby_all_columns


Review comment (suggestion):
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'"
```

Code after:
        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")
                )
                case_expr = self.make_sqla_column_compatible(case_expr, expr.name)
                modified_select_exprs.append(case_expr)
            else:
                modified_select_exprs.append(expr)

        # Modify GROUP BY expressions
        modified_groupby_all_columns = {}
        for col_name, gby_expr in groupby_all_columns.items():
            if col_name in groupby_series_columns:
                # Create condition for this column using the factory function
                condition = condition_factory(col_name, gby_expr)

                # Create CASE expression for groupby
                case_expr = sa.case(
                    [(condition, gby_expr)],
                    else_=sa.literal_column("Others"),
                )
                case_expr = self.make_sqla_column_compatible(case_expr, col_name)
                modified_groupby_all_columns[col_name] = case_expr
            else:
                modified_groupby_all_columns[col_name] = gby_expr

        return modified_select_exprs, modified_groupby_all_columns