Antonio-RiveroMartnez opened a new issue, #33148:
URL: https://github.com/apache/superset/issues/33148
## [SIP] Proposal for Saved Filters on Datasets and Explore
### Motivation
Currently, metrics can be saved at the dataset level for reuse in Explore,
promoting consistency and reducing duplication. Filters—often just as essential
for defining the scope of a chart—lack this reusability.
Users have to recreate the same filtering logic in each chart individually,
which adds overhead, increases the chances of inconsistencies, and makes it
harder to align charts with shared filtering standards across a team or
organization.
### Proposed Change
We propose introducing Saved Filters, allowing users to define, manage, and
reuse filters at the dataset level, just like metrics. Filters will be exposed
through the Dataset REST API and made available in Explore for quick
application via drag-and-drop or selection.
This brings:
- Standardized filters across charts and dashboards
- Reduced repetition when building visualizations
### New or Changed Public Interfaces
Models:
- We will introduce a new model (`SqlFilter`) that parallels how `SqlMetric`
is defined.
```python
class SqlFilter(Base, AuditMixinNullable):
__tablename__ = "sql_filters"
...
table = relationship(
"SqlaTable",
back_populates="filters",
cascade="all, delete-orphan",
foreign_keys=[table_id],
)
...
```
- We would also need to update the `SqlaTable` model to include the new
filters
```python
class SqlaTable(...):
...
filters = relationship(
"SqlFilter",
back_populates="table",
cascade="all, delete-orphan"
)
...
```
APIs:
- The main `DatasetRestAPI` will have to be updated so it considers the new
field on the same places where we handle associated saved metrics, this implies
updating all the `commands` and `schemas` used by this API
```python
class DatasetRestApi(BaseSupersetModelRestApi):
datamodel = SQLAInterface(SqlaTable)
...
@expose("/<pk>", methods=("PUT",))
...
@expose("/import/", methods=("POST",))
...
@expose("/<int:pk>", methods=("GET",))
```
- Same as metrics we will have a dedicated Rest API for deletion operations
that would live in: `/superset/datasets` and that will have its own command
and exception classes.
```python
class DatasetFilterRestApi(BaseSupersetModelRestApi):
datamodel = SQLAInterface(TableColumn)
include_route_methods = {"delete"}
class_permission_name = "Dataset"
method_permission_name = MODEL_API_RW_METHOD_PERMISSION_MAP
resource_name = "dataset"
allow_browser_login = True
openapi_spec_tag = "Datasets"
@expose("/<int:pk>/filter/<int:filter_id>", methods=("DELETE",))
...
```
UI:
- **Dataset: Add Saved Filters Tab**
- Similar to the “Metrics” tab within Dataset editing.
- **Explore: Show Saved Filters Section (if any exist)**
- On the left panel, beneath Metrics, a new Saved Filters section
appears only if that dataset has at least one saved filter.
- Users can drag & drop a saved filter into the Filters area (similar to
how metrics/columns are dragged).
- Or users can select from a dropdown in the Filters section (similar to
“saved metrics”).
- Users can add new filters to a dataset from the Simple or Custom SQL
tabs and that would open the Dataset edit modal with pre-populated info
### Migration Plan and Compatibility
A new migration is needed to add the new table (`SqlFilter`) to our system
and its relationship to the `SqlaTable` model.
```python
# alembic migration script
def upgrade():
op.create_table(
"sql_filters",
sa.Column("table_id", sa.Integer(), sa.ForeignKey("tables.id"),
nullable=False),
...
)
```
No impact on existing charts or dashboards, as no filter logic is changed.
Datasets without saved filters remain unchanged.
The same dataset permissions that govern editing metrics or columns will
apply to editing filters. If a user can manage metrics, they can manage filters.
### Rejected Alternatives
Describe alternative approaches that were considered and rejected.
--
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]