# 从 notebook_span_info_query_engine_026 和 notebook_engin (HiveSQL)

## Prompt
任务目标：从 Notebook span 信息和 engine 信息关联产出 killed 实例运行明细表。

输入：
- internal_platform_db.notebook_span_info_query_engine_026
- internal_platform_db.notebook_engine_info_query_engine_026

处理规则：
- 两表关联查询，关联条件需基于表结构确定
- 具体过滤条件和字段选择需基于表结构确定

输出要求：
- 输出表：internal_platform_db.dwd_notebook_killed_instance_detail_d_cand_query_engine_026
- 输出字段需基于目标表结构确定

写入要求：
- 写入目标表

请将最终 HiveSQL 写入 result.sql 并执行。

## Reference Solution
INSERT overwrite TABLE internal_platform_db.dwd_notebook_killed_instance_detail_d_query_engine_026 PARTITION (dt = '20260507')
WITH base_trace AS (
    SELECT trace_id
    FROM internal_platform_db.notebook_span_info_query_engine_026
    WHERE databus_imp_date >= '2026050700'
      AND databus_imp_date <= '2026050700'
      AND compute_type = 'ray'
      AND service_name = 'notebook-runner'
      AND span_name IN ('runner.execute', 'runner.killed')
    GROUP BY trace_id
    HAVING
        COUNT(CASE WHEN span_name = 'runner.killed' THEN 1 END) > 0
        AND
        COUNT(CASE WHEN span_name = 'runner.execute' THEN 1 END) = 0
),
combined_spans AS (
  SELECT
      trace_id,
      span_name,
      start_time,
      end_time,
      datawd_project_id,
      datawd_task_id,
      datawd_task_instance_id,
      compute_type,
      status_code
  FROM internal_platform_db.notebook_span_info_query_engine_026
  WHERE databus_imp_date >= '2026050400'
    AND databus_imp_date <= '2026050700'
    AND trace_id IN (SELECT trace_id FROM base_trace)
    AND span_name IN ('runner.killed', 'runner.start', 'execute.code', 'execute.code.cell', 'client.execute.code', 'set.permanent.compute', 'create.non.permanent.compute')

  UNION ALL

  SELECT
      trace_id,
      'runner.execute' AS span_name,
      MAX(CASE WHEN span_name = 'runner.start' THEN start_time END) AS start_time,
      MAX(CASE WHEN span_name = 'runner.killed' THEN end_time END) AS end_time,
      MAX(CASE WHEN span_name = 'runner.killed' THEN datawd_project_id END) AS datawd_project_id,
      MAX(CASE WHEN span_name = 'runner.killed' THEN datawd_task_id END) AS datawd_task_id,
      MAX(CASE WHEN span_name = 'runner.killed' THEN datawd_task_instance_id END) AS datawd_task_instance_id,
      MAX(CASE WHEN span_name = 'runner.killed' THEN compute_type END) AS compute_type,
      2 AS status_code
  FROM internal_platform_db.notebook_span_info_query_engine_026
  WHERE databus_imp_date >= '2026050400'
    AND databus_imp_date <= '2026050700'
    AND trace_id IN (SELECT trace_id FROM base_trace)
    AND span_name IN ('runner.start', 'runner.killed')
  GROUP BY trace_id
),
base_data_time_fixed AS (
  SELECT
      trace_id,
      span_name,
      CASE
          WHEN span_name = 'runner.killed'
            THEN MIN(CASE WHEN span_name IN ('execute.code', 'execute.code.cell', 'client.execute.code', 'runner.killed') THEN start_time END)
                    OVER(PARTITION BY trace_id)
          ELSE start_time
      END AS start_time,
      end_time,
      datawd_project_id,
      datawd_task_id,
      datawd_task_instance_id,
      compute_type,
      status_code
  FROM combined_spans
),
base_data AS (
  SELECT
      trace_id,
      span_name,
      start_time,
      end_time,
      datawd_project_id,
      datawd_task_id,
      datawd_task_instance_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,
      datediff(from_unixtime(CAST(end_time AS BIGINT) / 1000), from_unixtime(CAST(start_time AS BIGINT) / 1000)) 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,
        datawd_project_id,
        datawd_task_id,
        datawd_task_instance_id,
        compute_type,
        status_code,
        start_date,
        end_date,
        diff_days,
        date_add(b.start_date, s.pos) AS calc_date,
        CASE WHEN s.pos = 0 THEN b.start_time
             ELSE CAST(unix_timestamp(cast(date_add(b.start_date, s.pos) as timestamp)) * 1000 AS STRING)
        END AS start_time,
        CASE WHEN s.pos = b.diff_days THEN b.end_time
             ELSE CAST((unix_timestamp(cast(date_add(b.start_date, s.pos + 1) as timestamp)) * 1000) - 1 AS STRING)
        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(datawd_project_id) AS datawd_project_id,
        MAX(datawd_task_id) AS datawd_task_id,
        MAX(datawd_task_instance_id) AS datawd_task_instance_id,
        MAX(compute_type) AS compute_type,
        MAX(MAX(CASE WHEN span_name = 'runner.execute' THEN status_code END)) OVER(PARTITION BY trace_id) AS status_code,
        ROUND((MAX(CASE WHEN span_name = 'runner.execute' THEN CAST(end_time AS BIGINT) END) -
               MIN(CASE WHEN span_name = 'runner.execute' THEN CAST(start_time AS BIGINT) END)) / 1000.0) AS instance_run_time,
        ROUND((MAX(CASE WHEN span_name IN ('execute.code', 'client.execute.code', 'execute.code.cell', 'runner.killed' ) THEN CAST(end_time AS BIGINT) END) -
               MIN(CASE WHEN span_name IN ('execute.code', 'client.execute.code', 'execute.code.cell', 'runner.killed' ) THEN CAST(start_time AS BIGINT) END)) / 1000.0) AS code_run_time,
        ROUND((MAX(CASE WHEN span_name IN ('set.permanent.compute', 'create.non.permanent.compute') THEN CAST(end_time AS BIGINT) END) -
               MIN(CASE WHEN span_name IN ('set.permanent.compute', 'create.non.permanent.compute') THEN CAST(start_time AS BIGINT) END)) / 1000.0) AS resource_wait_time,
        from_unixtime(MIN(CASE WHEN span_name IN ('execute.code', 'client.execute.code', 'execute.code.cell', 'runner.killed' ) THEN CAST(start_time AS BIGINT) END) / 1000) AS code_start_time,
        from_unixtime(MAX(CASE WHEN span_name IN ('execute.code', 'client.execute.code', 'execute.code.cell', 'runner.killed' ) THEN CAST(end_time AS BIGINT) END) / 1000) AS code_end_time,
        from_unixtime(MIN(CASE WHEN span_name = 'runner.execute' THEN CAST(span_start_time AS BIGINT) END) / 1000) AS instance_start_time,
        from_unixtime(MAX(CASE WHEN span_name = 'runner.execute' THEN CAST(span_end_time AS BIGINT) END) / 1000) AS instance_end_time
    FROM daily_split_spans
    GROUP BY trace_id, calc_date
)
SELECT
  t1.calc_date AS p_date,
  t1.trace_id,
  t1.datawd_project_id,
  t1.datawd_task_id,
  t1.datawd_task_instance_id,
  t1.compute_type,
  t1.status_code,
  CAST(t1.instance_run_time AS INT) AS instance_run_time,
  CAST(t1.code_run_time AS INT) AS code_run_time,
  CAST(t1.resource_wait_time AS INT) AS resource_wait_time,
  t1.code_start_time,
  t1.code_end_time,
  t1.instance_start_time,
  t1.instance_end_time,
  t2.serving_id,
  CAST(t2.is_permanent AS BOOLEAN) AS is_permanent,
  CAST(t2.apply_for_gpu_count AS INT) AS apply_for_gpu_count
FROM trace_time_metrics t1 LEFT JOIN (
  SELECT
    trace_id,
    MAX(serving_id) AS serving_id,
    MAX(is_permanent) AS is_permanent,
    SUM(CAST(replicas AS INT) * CAST(num_gpu AS INT)) AS apply_for_gpu_count
  FROM internal_platform_db.notebook_engine_info_query_engine_026
  WHERE databus_imp_date >= '2026050400'
    AND databus_imp_date <= '2026050700'
    AND compute_type = 'ray'
    AND service_name = 'notebook-runner'
  GROUP BY trace_id
) t2 ON t1.trace_id = t2.trace_id
;