codeant-ai-for-open-source[bot] commented on code in PR #40954:
URL: https://github.com/apache/superset/pull/40954#discussion_r3391456338


##########
superset-frontend/packages/superset-ui-chart-controls/src/operators/anomalyDetectionOperator.ts:
##########
@@ -0,0 +1,58 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+import {
+  PostProcessingAnomalyDetection,
+  getXAxisLabel,
+} from '@superset-ui/core';
+import { PostProcessingFactory } from './types';
+
+export const anomalyDetectionOperator: PostProcessingFactory<
+  PostProcessingAnomalyDetection
+> = (formData, queryObject) => {
+  const xAxisLabel = getXAxisLabel(formData);
+  if (formData.anomalyDetectionEnabled && xAxisLabel) {
+    const method: string = formData.anomalyDetectionMethod || 'zscore';
+    // Prophet requires a temporal x-axis; skip if not time-based
+    if (
+      method === 'prophet' &&
+      !formData.granularity_sqla &&
+      !formData.x_axis
+    ) {
+      return undefined;
+    }

Review Comment:
   **Suggestion:** The Prophet guard only checks whether an x-axis field 
exists, not whether it is actually temporal. If a non-datetime `x_axis` value 
slips through (for example from saved form state or API payload), this still 
emits a Prophet anomaly post-processing step and backend Prophet processing 
will fail when it uses datetime accessors on non-datetime data. Add a real 
temporal-type check for the selected x-axis before allowing `method === 
'prophet'`. [logic error]
   
   <details>
   <summary><b>Severity Level:</b> Major ⚠️</summary>
   
   ```mdx
   - ❌ Prophet anomaly detection fails for charts using non-temporal x_axis.
   - ❌ Affected timeseries charts return errors instead of rendering.
   - ⚠️ Users can misconfigure x_axis via unrestricted dndXAxisControl.
   ```
   </details>
   <details>
   <summary><b>Steps of Reproduction ✅ </b></summary>
   
   ```mdx
   1. Configure an ECharts Timeseries Area chart whose control panel is defined 
in
   
`superset-frontend/plugins/plugin-chart-echarts/src/Timeseries/Area/controlPanel.tsx:24-30`,
   which includes `sections.echartsTimeSeriesQueryWithXAxisSort` (for `x_axis` 
and
   `time_grain_sqla`) and `sections.anomalyDetectionControls` (for anomaly 
detection form
   fields from `src/sections/anomalyDetection.tsx:18-23,37-52`).
   
   2. In the chart's Query section, choose a non-temporal column (for example a 
categorical
   dimension like `customer_segment`) as `x_axis` using the `x_axis` control 
wired to
   `dndXAxisControl`
   
(`packages/superset-ui-chart-controls/src/shared-controls/dndControls.tsx:293-296`),
 which
   does not enforce `isTemporal` and allows arbitrary group-by columns.
   
   3. In the Anomaly Detection section 
(`src/sections/anomalyDetection.tsx:18-23,37-52`),
   enable anomaly detection (`anomalyDetectionEnabled = true`) and select 
`prophet` as
   `anomalyDetectionMethod`, then run the chart so that the form data 
(including `x_axis:
   'customer_segment'`, `anomalyDetectionEnabled: true`, 
`anomalyDetectionMethod: 'prophet'`)
   is passed into the post-processing operator `anomalyDetectionOperator`
   (`src/operators/anomalyDetectionOperator.ts:25-40`).
   
   4. Observe that in `anomalyDetectionOperator` the Prophet guard only checks 
for the
   *presence* of `granularity_sqla` or `x_axis`, and with `method === 
'prophet'` and a
   non-empty `x_axis` it still returns a `PostProcessingAnomalyDetection` 
object with
   `method: 'prophet'` and `index` set to the non-temporal x-axis label 
(`getXAxisLabel` from
   `packages/superset-ui-core/src/query/getXAxis.ts:45-48`). This 
post-processing spec
   reaches the backend `anomaly_detection` implementation
   (`superset/superset/utils/pandas_postprocessing/anomaly.py:194-224`), which 
documents that
   it operates on a timeseries dataframe and uses the `index` column as x-axis 
data; when
   Prophet-based anomaly detection runs against a non-datetime index column, 
downstream
   Prophet code will attempt datetime-based operations on non-temporal data and 
fail, causing
   the chart query to error instead of returning results.
   ```
   </details>
   
   [Fix in 
Cursor](https://app.codeant.ai/fix-in-ide?tool=cursor&prompt_id=61eeaf7657c347fe8fe9dc3edeca40ad&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=61eeaf7657c347fe8fe9dc3edeca40ad&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/packages/superset-ui-chart-controls/src/operators/anomalyDetectionOperator.ts
   **Line:** 32:38
   **Comment:**
        *Logic Error: The Prophet guard only checks whether an x-axis field 
exists, not whether it is actually temporal. If a non-datetime `x_axis` value 
slips through (for example from saved form state or API payload), this still 
emits a Prophet anomaly post-processing step and backend Prophet processing 
will fail when it uses datetime accessors on non-datetime data. Add a real 
temporal-type check for the selected x-axis before allowing `method === 
'prophet'`.
   
   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%2F40954&comment_hash=9ee115c73b8a18e7a4f71ad665a7f08f9aff64cb2b1503593737a8086786fa88&reaction=like'>👍</a>
 | <a 
href='https://app.codeant.ai/feedback?pr_url=https%3A%2F%2Fgithub.com%2Fapache%2Fsuperset%2Fpull%2F40954&comment_hash=9ee115c73b8a18e7a4f71ad665a7f08f9aff64cb2b1503593737a8086786fa88&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]

Reply via email to