feat: Extend Bigquery detect_anomalies tool to support future data anomaly detection

ARIMA supports both historical data and future data anomaly detection. This CL add how the tool support future table anomaly detection.

PiperOrigin-RevId: 827803748
This commit is contained in:
Google Team Member
2025-11-03 23:02:02 -08:00
committed by Copybara-Service
parent d2888a3766
commit 38ea749c9c
2 changed files with 72 additions and 0 deletions
@@ -1100,6 +1100,7 @@ def detect_anomalies(
times_series_timestamp_col: str,
times_series_data_col: str,
horizon: Optional[int] = 10,
target_data: Optional[str] = None,
times_series_id_cols: Optional[list[str]] = None,
anomaly_prob_threshold: Optional[float] = 0.95,
*,
@@ -1121,6 +1122,9 @@ def detect_anomalies(
numerical values to be forecasted and anomaly detected.
horizon (int, optional): The number of time steps to forecast into the
future. Defaults to 10.
target_data (str, optional): The table id of the BigQuery table containing
the target time series data or a query statement that select the target
data.
times_series_id_cols (list, optional): The column names of the id columns
to indicate each time series when there are multiple time series in the
table. All elements must be strings. Defaults to None.
@@ -1264,6 +1268,18 @@ def detect_anomalies(
anomaly_detection_query = f"""
SELECT * FROM ML.DETECT_ANOMALIES(MODEL {model_name}, STRUCT({anomaly_prob_threshold} AS anomaly_prob_threshold))
"""
if target_data:
trimmed_upper_target_data = target_data.strip().upper()
if trimmed_upper_target_data.startswith(
"SELECT"
) or trimmed_upper_target_data.startswith("WITH"):
target_data_source = f"({target_data})"
else:
target_data_source = f"SELECT * FROM `{target_data}`"
anomaly_detection_query = f"""
SELECT * FROM ML.DETECT_ANOMALIES(MODEL {model_name}, STRUCT({anomaly_prob_threshold} AS anomaly_prob_threshold), {target_data_source})
"""
# Create a session and run the create model query.
original_write_mode = settings.write_mode