# Data Pipeline Killed Instance Analysis (PrestoSQL)

## Prompt
Join pipeline span info and resource info tables to produce killed instance detail table. Specific filtering conditions and field selection need to be determined based on table structure.

**输入表**：
- `internal_platform_db.t_pipeline_span_info_prestosql_009`（Pipeline span信息表）
  - `databus_imp_date` STRING — 日期分区
  - `trace_id` STRING — 链路标识
  - `span_name` STRING — span名称
  - `start_time` STRING — 开始时间(epoch毫秒)
  - `end_time` STRING — 结束时间(epoch毫秒)
  - `status_code` INT — 状态码
  - `project_id` STRING — 项目ID
  - `task_id` STRING — 任务ID
  - `compute_type` STRING — 计算类型

- `internal_platform_db.t_pipeline_resource_info_prestosql_009`（Pipeline资源信息表）
  - `databus_imp_date` STRING — 日期分区
  - `trace_id` STRING — 链路标识
  - `compute_type` STRING — 计算类型
  - `resource_id` STRING — 资源ID
  - `is_dedicated` STRING — 是否独占
  - `cpu_cores` INT — CPU核数

**输出要求**：
- 目标表: `internal_platform_db.t_pipeline_killed_instance_detail_cand_prestosql_009`
- 具体过滤条件、字段选择、计算逻辑需根据表结构和数据特征自行分析确定
- 如果目标表不存在，请先建表再写入数据
- 请使用Presto/Trino SQL语法，不要使用Hive/Spark SQL方言

**环境与执行说明**：
- Presto已启动，通过Hive catalog连接
- 执行SQL: `presto-cli --catalog hive --schema internal_platform_db -f /tmp_workspace/result.sql`
- 写出result.sql后，必须自己执行验证它能成功运行并产出正确数据

## Reference Solution
INSERT INTO internal_platform_db.t_pipeline_killed_instance_detail_prestosql_009
WITH base_trace AS (
    SELECT trace_id
    FROM internal_platform_db.t_pipeline_span_info_prestosql_009
    WHERE databus_imp_date = '20260608'
      AND span_name IN ('pipeline.execute', 'pipeline.killed')
    GROUP BY trace_id
    HAVING
        COUNT(CASE WHEN span_name = 'pipeline.killed' THEN 1 END) > 0
        AND
        COUNT(CASE WHEN span_name = 'pipeline.execute' THEN 1 END) = 0
),
combined_spans AS (
  SELECT
      trace_id,
      span_name,
      start_time,
      end_time,
      project_id,
      task_id,
      compute_type,
      status_code
  FROM internal_platform_db.t_pipeline_span_info_prestosql_009
  WHERE databus_imp_date = '20260608'
    AND trace_id IN (SELECT trace_id FROM base_trace)
    AND span_name IN ('pipeline.killed', 'pipeline.start', 'task.run', 'task.compile', 'resource.request')

  UNION ALL

  SELECT
      trace_id,
      'pipeline.execute' AS span_name,
      MAX(CASE WHEN span_name = 'pipeline.start' THEN start_time END) AS start_time,
      MAX(CASE WHEN span_name = 'pipeline.killed' THEN end_time END) AS end_time,
      MAX(CASE WHEN span_name = 'pipeline.killed' THEN project_id END) AS project_id,
      MAX(CASE WHEN span_name = 'pipeline.killed' THEN task_id END) AS task_id,
      MAX(CASE WHEN span_name = 'pipeline.killed' THEN compute_type END) AS compute_type,
      2 AS status_code
  FROM internal_platform_db.t_pipeline_span_info_prestosql_009
  WHERE databus_imp_date = '20260608'
    AND trace_id IN (SELECT trace_id FROM base_trace)
    AND span_name IN ('pipeline.start', 'pipeline.killed')
  GROUP BY trace_id
),
base_data_time_fixed AS (
  SELECT
      trace_id,
      span_name,
      CASE
          WHEN span_name = 'pipeline.killed'
            THEN MIN(CASE WHEN span_name IN ('task.run', 'pipeline.killed') THEN start_time END)
                    OVER(PARTITION BY trace_id)
          ELSE start_time
      END AS start_time,
      end_time,
      project_id,
      task_id,
      compute_type,
      status_code
  FROM combined_spans
),
base_data AS (
  SELECT
      trace_id,
      span_name,
      start_time,
      end_time,
      project_id,
      task_id,
      compute_type,
      status_code,
      from_unixtime(CAST(start_time AS BIGINT) / 1000) AS start_date,
      from_unixtime(CAST(end_time AS BIGINT) / 1000) AS end_date,
      date_diff('day', CAST(from_unixtime(CAST(start_time AS BIGINT) / 1000) AS TIMESTAMP), CAST(from_unixtime(CAST(end_time AS BIGINT) / 1000) AS TIMESTAMP)) AS diff_days
  FROM base_data_time_fixed
),
pos_series AS (
    SELECT 0 AS pos UNION ALL SELECT 1 AS pos UNION ALL SELECT 2 AS pos UNION ALL SELECT 3 AS pos
),
daily_split_spans AS (
    SELECT
        trace_id,
        span_name,
        project_id,
        task_id,
        compute_type,
        status_code,
        start_date,
        end_date,
        diff_days,
        CAST(date_add('day', s.pos, CAST(b.start_date AS TIMESTAMP)) AS VARCHAR) AS calc_date,
        CASE WHEN s.pos = 0 THEN b.start_time
             ELSE CAST(CAST(to_unixtime(CAST(date_add('day', s.pos, CAST(b.start_date AS TIMESTAMP)) AS TIMESTAMP)) AS BIGINT) * 1000 AS VARCHAR)
        END AS start_time,
        CASE WHEN s.pos = b.diff_days THEN b.end_time
             ELSE CAST(CAST(to_unixtime(CAST(date_add('day', s.pos + 1, CAST(b.start_date AS TIMESTAMP)) AS TIMESTAMP)) AS BIGINT) * 1000 - 1 AS VARCHAR)
        END AS end_time,
        b.start_time AS span_start_time,
        b.end_time AS span_end_time
    FROM base_data b
    INNER JOIN pos_series s ON s.pos <= b.diff_days
),
trace_time_metrics AS (
    SELECT
        trace_id,
        calc_date,
        MAX(project_id) AS project_id,
        MAX(task_id) AS task_id,
        MAX(compute_type) AS compute_type,
        MAX(MAX(CASE WHEN span_name = 'pipeline.execute' THEN status_code END)) OVER(PARTITION BY trace_id) AS status_code,
        CAST(ROUND((MAX(CASE WHEN span_name = 'pipeline.execute' THEN CAST(end_time AS BIGINT) END) -
               MIN(CASE WHEN span_name = 'pipeline.execute' THEN CAST(start_time AS BIGINT) END)) / 1000.0) AS INT) AS instance_run_time,
        CAST(ROUND((MAX(CASE WHEN span_name IN ('task.run', 'task.compile', 'pipeline.killed') THEN CAST(end_time AS BIGINT) END) -
               MIN(CASE WHEN span_name IN ('task.run', 'task.compile', 'pipeline.killed') THEN CAST(start_time AS BIGINT) END)) / 1000.0) AS INT) AS code_run_time,
        CAST(ROUND((MAX(CASE WHEN span_name = 'resource.request' THEN CAST(end_time AS BIGINT) END) -
               MIN(CASE WHEN span_name = 'resource.request' THEN CAST(start_time AS BIGINT) END)) / 1000.0) AS INT) AS resource_wait_time
    FROM daily_split_spans
    GROUP BY trace_id, calc_date
)
SELECT
  t1.calc_date AS p_date,
  t1.trace_id,
  t1.project_id,
  t1.task_id,
  t1.compute_type,
  t1.status_code,
  t1.instance_run_time,
  t1.code_run_time,
  t1.resource_wait_time,
  t2.resource_id,
  CAST(t2.is_dedicated AS BOOLEAN) AS is_dedicated
FROM trace_time_metrics t1 LEFT JOIN (
  SELECT
    trace_id,
    MAX(resource_id) AS resource_id,
    MAX(is_dedicated) AS is_dedicated
  FROM internal_platform_db.t_pipeline_resource_info_prestosql_009
  WHERE databus_imp_date = '20260608'
  GROUP BY trace_id
) t2 ON t1.trace_id = t2.trace_id
;