# Compute GPU card-hours for task instances within 5-minute windows, joining Pod mapping and task instance configuration, outputting hourly instanc (HiveSQL)

## Prompt
Task Objective: Compute GPU card-hours for task instances within 5-minute windows, outputting hourly instance-level GPU usage statistics.

Inputs:
- `internal_platform_db.gputj_gpu_info_parsed_query_engine_022`
- `internal_platform_db.dwd_ml_platform_instance_podname_query_engine_022`
- `internal_platform_db.dwd_task_management_taskinstance2_query_engine_022`
- `internal_platform_db.task_management_taskinstance2_query_engine_022`

Processing Rules: Join the Pod mapping table with the task instance configuration table, aggregate GPU card-hours by 5-minute windows, and roll up to hourly-level statistics. The specific join keys and aggregation logic must be determined based on the table structures.

Output Requirements: Output the task instance GPU usage statistics, including fields such as instance identifier, time window, and GPU card-hours. The specific field list must be determined based on the output table structure.

Write Requirements: Write the results to `internal_platform_db.task_instance_gpu_time_stats_cand_query_engine_022`.

Please write the final HiveSQL to `result.sql` and execute it.

## Reference Solution
INSERT OVERWRITE TABLE internal_platform_db.task_instance_gpu_time_stats_query_engine_022
PARTITION (dt='2026050700')
WITH
-- Step 1: 从GPU监控数据中提取pod运行记录，按分钟去重
pod_run_minutes AS (
    SELECT
        pod_name,
        gpu_name,
        FLOOR(CAST(pkg_time AS BIGINT) / 60) * 60 AS minute_timestamp,
        FLOOR(CAST(pkg_time AS BIGINT) / 300) * 300 AS time_5min
    FROM internal_platform_db.gputj_gpu_info_parsed_query_engine_022
    WHERE dt = '2026050700'
        AND metric IN ('k8s_container_vgpu_gpu_mem_usage', 'k8s_dcgm_fi_dev_fb_util')
    GROUP BY pod_name, gpu_name, FLOOR(CAST(pkg_time AS BIGINT) / 60) * 60, FLOOR(CAST(pkg_time AS BIGINT) / 300) * 300
),

-- Step 2: 统计每个pod在每个5分钟窗口内的运行分钟数
pod_run_time AS (
    SELECT
        pod_name,
        MAX(gpu_name) AS gpu_name,
        time_5min,
        CAST(COUNT(DISTINCT minute_timestamp) AS DOUBLE) AS run_time_m
    FROM pod_run_minutes
    GROUP BY pod_name, time_5min
),

-- Step 3: 关联pod与instance的映射关系
instance_run_time AS (
    SELECT
        p.time_5min,
        m.instance_uuid,
        MAX(p.gpu_name) AS gpu_name,
        CAST(SUM(p.run_time_m) AS DOUBLE) AS sum_run_time_m,
        COUNT(DISTINCT m.pod_name) AS pod_count
    FROM pod_run_time p
    INNER JOIN internal_platform_db.dwd_ml_platform_instance_podname_query_engine_022 m
        ON p.pod_name = m.pod_name
        AND m.dt = '2026050700'
    GROUP BY p.time_5min, m.instance_uuid
),

-- Step 4: 获取任务实例的GPU配置
latest_task_config AS (
    SELECT
        instance_uuid,
        CAST(host_gpu_num AS DOUBLE) AS host_gpu_num,
        CAST(host_num AS DOUBLE) AS host_num
    FROM (
        SELECT
            instance_uuid,
            host_gpu_num,
            host_num,
            last_modify,
            ROW_NUMBER() OVER (PARTITION BY instance_uuid ORDER BY last_modify DESC) AS rn
        FROM (
            SELECT
                instance_uuid,
                host_gpu_num,
                host_num,
                last_modify
            FROM internal_platform_db.dwd_task_management_taskinstance2_query_engine_022
            WHERE databus_imp_date = (
                SELECT MAX(databus_imp_date)
                FROM internal_platform_db.dwd_task_management_taskinstance2_query_engine_022
            )

            UNION ALL

            SELECT
                instance_uuid,
                host_gpu_num,
                host_num,
                last_modify
            FROM internal_platform_db.task_management_taskinstance2_query_engine_022
            WHERE databus_imp_date = (
                SELECT MAX(databus_imp_date)
                FROM internal_platform_db.task_management_taskinstance2_query_engine_022
            )
        ) combined
    ) ranked
    WHERE rn = 1
)

-- Step 5: 关联GPU配置并计算卡时
SELECT
    i.instance_uuid,
    i.time_5min,
    t.host_gpu_num,
    i.sum_run_time_m,
    CASE
        WHEN t.host_gpu_num IS NOT NULL AND i.sum_run_time_m IS NOT NULL
        THEN t.host_gpu_num * i.sum_run_time_m / 60.0
        ELSE NULL
    END AS gpu_hour,
    i.gpu_name,
    i.pod_count,
    t.host_num
FROM instance_run_time i
LEFT JOIN latest_task_config t
    ON i.instance_uuid = t.instance_uuid