codeant-ai-for-open-source[bot] commented on code in PR #39461:
URL: https://github.com/apache/superset/pull/39461#discussion_r3411619100
##########
superset-frontend/src/explore/components/controls/FilterControl/AdhocFilterControl/index.tsx:
##########
@@ -243,177 +214,205 @@ class AdhocFilterControl extends Component<
});
}
}
- }
+ }, [datasource]);
- componentDidUpdate(prevProps: AdhocFilterControlProps): void {
- if (this.props.columns !== prevProps.columns) {
- this.setState({ options: optionsForSelect(this.props) });
- }
- if (this.props.value !== prevProps.value) {
- this.setState({
- values: (this.props.value || []).map(filter =>
+ useEffect(() => {
+ if (value !== undefined) {
+ setValues(
+ (value || []).map(filter =>
isDictionaryForAdhocFilter(filter) ? new AdhocFilter(filter) :
filter,
),
- });
+ );
}
- }
+ }, [value]);
- removeFilter(index: number): void {
- const valuesCopy = [...this.state.values];
- valuesCopy.splice(index, 1);
- this.setState(prevState => ({
- ...prevState,
- values: valuesCopy,
- }));
- this.props.onChange?.(valuesCopy);
- }
+ const getMetricExpression = useCallback(
+ (savedMetricName: string): string => {
+ const metric = savedMetrics?.find(
+ savedMetric => savedMetric.metric_name === savedMetricName,
+ );
+ return metric?.expression ?? '';
+ },
+ [savedMetrics],
+ );
- onRemoveFilter(index: number): void {
- const { canDelete } = this.props;
- const { values } = this.state;
- const result = canDelete?.(values[index], values);
- if (typeof result === 'string') {
- warning({ title: t('Warning'), content: result });
- return;
- }
- this.removeFilter(index);
- }
+ const mapOption = useCallback(
+ (option: FilterOption | AdhocFilter): AdhocFilter | null => {
+ // already a AdhocFilter, skip
+ if (option instanceof AdhocFilter) {
+ return option;
+ }
+ // via datasource saved metric
+ if (option.saved_metric_name) {
+ return new AdhocFilter({
+ expressionType: ExpressionTypes.Sql,
+ subject: getMetricExpression(option.saved_metric_name),
+ operator:
+ OPERATOR_ENUM_TO_OPERATOR_TYPE[Operators.GreaterThan].operation,
+ comparator: 0,
+ clause: Clauses.Having,
+ });
+ }
+ // has a custom label, meaning it's custom column
+ if (option.label) {
+ return new AdhocFilter({
+ expressionType: ExpressionTypes.Sql,
+ subject: new AdhocMetric(option).translateToSql(),
+ operator:
+ OPERATOR_ENUM_TO_OPERATOR_TYPE[Operators.GreaterThan].operation,
+ comparator: 0,
+ clause: Clauses.Having,
+ });
+ }
+ // add a new filter item
+ if (option.column_name) {
+ return new AdhocFilter({
+ expressionType: ExpressionTypes.Simple,
+ subject: option.column_name,
+ operator: OPERATOR_ENUM_TO_OPERATOR_TYPE[Operators.Equals].operation,
+ comparator: '',
+ clause: Clauses.Where,
+ isNew: true,
+ });
+ }
+ return null;
+ },
+ [getMetricExpression],
+ );
- onNewFilter(newFilter: FilterOption | AdhocFilter): void {
- const mappedOption = this.mapOption(newFilter);
- if (mappedOption) {
- this.setState(
- prevState => ({
- ...prevState,
- values: [...prevState.values, mappedOption],
- }),
- () => {
- this.props.onChange?.(this.state.values);
- },
- );
- }
- }
+ const removeFilter = useCallback(
+ (index: number) => {
+ const valuesCopy = [...values];
+ valuesCopy.splice(index, 1);
+ setValues(valuesCopy);
+ onChange?.(valuesCopy);
+ },
+ [values, onChange],
+ );
- onFilterEdit(changedFilter: AdhocFilter): void {
- this.props.onChange?.(
- this.state.values.map(value => {
- if (value.filterOptionName === changedFilter.filterOptionName) {
- return changedFilter;
- }
- return value;
- }),
- );
- }
+ const onRemoveFilter = useCallback(
+ (index: number) => {
+ const result = canDelete?.(values[index], values);
+ if (typeof result === 'string') {
+ warning({ title: t('Warning'), content: result });
+ return;
+ }
+ removeFilter(index);
Review Comment:
**Suggestion:** The delete-guard contract is broken: when `canDelete`
returns `false` (used to block deletion after handling the case elsewhere),
this code still removes the filter. This will incorrectly delete protected
temporal filters that should be kept/reset. Only remove when `canDelete` is
`true` (or when no guard is provided), and keep the current warning behavior
for string returns. [incorrect condition logic]
<details>
<summary><b>Severity Level:</b> Major ⚠️</summary>
```mdx
- ❌ Explore adhoc filter UI can delete protected temporal filter.
- ⚠️ Dashboards lose expected time-range constraints in edge cases.
```
</details>
<details>
<summary><b>Steps of Reproduction ✅ </b></summary>
```mdx
1. In `superset-frontend/src/explore/components/ControlPanelsContainer.tsx`,
see how
controls named with `'adhoc_filters'` are wired: at lines ~532–560,
`restProps.canDelete`
is assigned a function that inspects each filter. For temporal-range filters
(`filter.operator === Operators.TemporalRange`) when
`controls?.time_range?.value` is
falsy, it counts temporal filters and, if there is exactly one, either
returns a warning
string (when `comparator === defaultTimeFilter`) or calls
`props.actions.setControlValue(...)` to reset the comparator to
`defaultTimeFilter` and
then `return false;` (lines ~19–2 in the snippets above).
2. That `canDelete` function is passed through to the generic `<Control>`
component and
ultimately into `AdhocFilterControl` as the `canDelete` prop when rendering
the Explore
control for `adhoc_filters` (same `ControlPanelsContainer.tsx` block,
returning `<Control
{...restProps} />` with `restProps.canDelete` attached).
3. In
`superset-frontend/src/explore/components/controls/FilterControl/AdhocFilterControl/index.tsx`,
the delete handler is `onRemoveFilter` (lines 293–301): it computes `const
result =
canDelete?.(values[index], values);` (line 294), shows a warning and returns
only when
`typeof result === 'string'` (lines 295–298), and unconditionally calls
`removeFilter(index);` (line 300) for all other return values—including
`false`.
4. When a user in Explore has exactly one temporal-range adhoc filter and no
separate
`time_range` control value, and they click the delete icon on that remaining
temporal
filter, `canDelete` in `ControlPanelsContainer.tsx` resets the filter
comparator to
`defaultTimeFilter` via `setControlValue(...)` and returns `false` (intended
as "do not
actually delete, just reset"), but `AdhocFilterControl` ignores the `false`
and still
calls `removeFilter(index)`. This removes the last temporal filter despite
the guard,
breaking the contract that the last temporal filter should be kept/reset
rather than
deleted and potentially leaving dashboards without the expected time-range
filter state.
```
</details>
[Fix in
Cursor](https://app.codeant.ai/fix-in-ide?tool=cursor&prompt_id=6a8568419e574e72ba0ac8d43596f4a0&service=github&base_url=https%3A%2F%2Fgithub.com&org=apache&repo=apache%2Fsuperset)
| [Fix in VSCode
Claude](https://app.codeant.ai/fix-in-ide?tool=vscode-claude&prompt_id=6a8568419e574e72ba0ac8d43596f4a0&service=github&base_url=https%3A%2F%2Fgithub.com&org=apache&repo=apache%2Fsuperset)
*(Use Cmd/Ctrl + Click for best experience)*
<details>
<summary><b>Prompt for AI Agent 🤖 </b></summary>
```mdx
This is a comment left during a code review.
**Path:**
superset-frontend/src/explore/components/controls/FilterControl/AdhocFilterControl/index.tsx
**Line:** 294:300
**Comment:**
*Incorrect Condition Logic: The delete-guard contract is broken: when
`canDelete` returns `false` (used to block deletion after handling the case
elsewhere), this code still removes the filter. This will incorrectly delete
protected temporal filters that should be kept/reset. Only remove when
`canDelete` is `true` (or when no guard is provided), and keep the current
warning behavior for string returns.
Validate the correctness of the flagged issue. If correct, How can I resolve
this? If you propose a fix, implement it and please make it concise.
Once fix is implemented, also check other comments on the same PR, and ask
user if the user wants to fix the rest of the comments as well. if said yes,
then fetch all the comments validate the correctness and implement a minimal fix
```
</details>
<a
href='https://app.codeant.ai/feedback?pr_url=https%3A%2F%2Fgithub.com%2Fapache%2Fsuperset%2Fpull%2F39461&comment_hash=d20f6344da537102893f883dab6fcc97ee6aff92895ad1771e30c20e4907c8c3&reaction=like'>👍</a>
| <a
href='https://app.codeant.ai/feedback?pr_url=https%3A%2F%2Fgithub.com%2Fapache%2Fsuperset%2Fpull%2F39461&comment_hash=d20f6344da537102893f883dab6fcc97ee6aff92895ad1771e30c20e4907c8c3&reaction=dislike'>👎</a>
--
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]