# Build Volume-Lift Candidate Ad List (MySQL)

## Prompt
I need you to generate a MySQL script that builds a volume-lift candidate ad list, outputting to table `internal_platform_db.dwm_union_raise_candidate_new_ads_hf_cand_mysql_020` with partition key `p_partition='2026060905'`.

**Business Background and Objective**: The ad volume-lift system needs to jointly filter qualifying ads from multiple dimensions (creative, ad group, ecology budget, advertiser, conversion link, volume-lift configuration, creative platform) to generate a candidate list for downstream consumption. This task requires multi-table JOINs, aggregation, window function ranking, and filtering across 7 input tables, ultimately writing to the output table.

**Input Tables (full name + brief description)**:
- `internal_platform_db.dim_creative_info_f_mysql_020` (creative information)
- `internal_platform_db.dim_adgroup_info_f_mysql_020` (ad group information)
- `internal_platform_db.dim_adgroup_ecology_budget_info_di_mysql_020` (ecology budget information)
- `internal_platform_db.f_union_dim_advertiser_info_d_mysql_020` (advertiser information)
- `internal_platform_db.t_daily_conv_link_tid_dimension_mysql_020` (conversion link dimension)
- `internal_platform_db.dim_union_ad_raised_config_hf_mysql_020` (volume-lift configuration)
- `internal_platform_db.dim_tbl_creative_f_mysql_020` (creative platform information)

(Please connect to the database and query to confirm the table structures and field semantics.)

**Processing Rules**:
1. Main table a (`dim_creative_info_f`): `creative_id > 0`, aggregate by `creative_id` taking `MAX(adgroup_id)`, `MAX(advertiser_id)`, `MAX(landing_page_type)`
2. Subquery e (`dim_adgroup_info_f`) INNER JOIN:
   - Filter `adgroup_id > 0`
   - Filter `placement_group_id_list` containing 15 or 136 (using the `FIND_IN_SET` function)
   - `begin_time >= MIN(begintime)` from the config table AND `<= MAX(endtime)`, config table conditions: `partition_time = 2026060905`, `strategyid > 0`, `20260609` within the `begintime-endtime` range (use `FROM_UNIXTIME` to convert unix timestamps to date format `yyyyMMdd` for comparison)
   - Aggregate by `adgroup_id` taking `MAX(product_id)`, `MAX(optimization_goal)`, `MAX(second_optimization_goal)`, `MAX(deep_conversion_optimization_goal)`, `MAX(marketing_target_id)`, `MAX(begin_time)`, `MAX(end_time)`, `MAX(created_time)`, `MAX(exploration_strategy_id)`, `MAX(placement_group_id_list)`
3. Subquery b (`dim_adgroup_ecology_budget_info_di`) LEFT JOIN: `partition_time` between 20260607–20260608, take the latest partition's `ecology_level2_id` per `creative_id` (using the `ROW_NUMBER` window function ranked by `partition_time DESC`, taking rn=1)
4. Subquery c (`f_union_dim_advertiser_info_d`) LEFT JOIN: `partition_time` between 20260607–20260608, first aggregate by `advertiser_id + partition_time` taking `MAX(operation_industry_name) AS team`, `MAX(short_advertiser_name)`, then take the latest partition's `team`, `short_advertiser_name` per `advertiser_id` (ROW_NUMBER)
5. Subquery d (`t_daily_conv_link_tid_dimension`) LEFT JOIN: `partition_time` between 20260607–20260608, first aggregate by `tid + partition_time` taking `MAX(landingpage_link_type)`, then take the latest partition's `landingpage_link_type` per `tid` (ROW_NUMBER), join condition `a.creative_id = d.tid`
6. Subquery f (`dim_tbl_creative_f`) LEFT JOIN: `ftid > 0`, aggregate by `ftid` taking `MAX(fsmartdeliveryplatform) AS smart_delivery_platform`, join condition `a.creative_id = f.ftid`
7. Final SELECT: `partition_time = 2026060905`, `adgroup_id`, `COALESCE(advertiser_id, 0)`, `COALESCE(product_id, '')`, `CONCAT(COALESCE(optimization_goal, 0), '_', COALESCE(second_optimization_goal, 0), '_', COALESCE(deep_conversion_optimization_goal, 0)) AS mix_goal`, `COALESCE(landing_page_type, '')`, `COALESCE(marketing_target_id, 0)`, `COALESCE(ecology_level2_id, 0)`, `COALESCE(team, '')`, `COALESCE(short_advertiser_name, '')`, `COALESCE(landingpage_link_type, '')`, `COALESCE(begin_time, 0)`, `COALESCE(end_time, 0)`, `COALESCE(created_time, 0)`, `COALESCE(smart_delivery_platform, 0)`, `COALESCE(exploration_strategy_id, 0)`, `COALESCE(placement_group_id_list, '')`

**Output Requirements**:
- Target table: `internal_platform_db.dwm_union_raise_candidate_new_ads_hf_cand_mysql_020`
- Output field order: `partition_time`, `adgroup_id`, `advertiser_id`, `product_id`, `mix_goal`, `landing_page_type`, `marketing_target_id`, `ecology_level2_id`, `team`, `short_advertiser_name`, `landingpage_link_type`, `begin_time`, `end_time`, `created_time`, `smart_delivery_platform`, `exploration_strategy_id`, `placement_group_id_list`, `p_partition`
- `mix_goal` is generated by concatenating three optimization goal fields with underscores; null values are replaced with defaults using COALESCE (0 for numeric, empty string for string)
- Final deduplication by GROUP BY on all non-`partition_time` fields
- `p_partition` is fixed as `'2026060905'`
- If the target table does not exist, first create it using standard MySQL InnoDB format, then write the data
- Use standard MySQL syntax; do not use Hive/Spark SQL dialects (e.g., `INSERT OVERWRITE`, `ARRAY` type, `array_contains`, `concat_ws` for arrays, and other Hive-specific functions are not supported)

**Environment and Execution Notes**:
- Your final output must be written to the file `/tmp_workspace/result.py`, not `result.sql`
- The local MySQL is running at localhost:3306, username `root`, password `root123`
- Use Python `pymysql` in `result.py` to execute the SQL (do not use the `mysql` command-line tool)
- The script must include complete table creation (if the target table does not exist) and data writing logic
- After writing `result.py`, you must execute `python3 /tmp_workspace/result.py` yourself to verify that it runs successfully and produces correct data

## Reference Solution
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
mysql_020 ground truth: 构建提量候选广告列表

Task:
  Build raise-candidate ad list from 7 input tables via multi-table JOIN,
  aggregation, window functions, and filtering, write to output table
  with p_partition='2026060905'.
"""
import pymysql
import sys

DB_NAME = "internal_platform_db"
INPUT_TABLE_CREATIVE = "dim_creative_info_f_mysql_020"
INPUT_TABLE_ADGROUP = "dim_adgroup_info_f_mysql_020"
INPUT_TABLE_ECOLOGY = "dim_adgroup_ecology_budget_info_di_mysql_020"
INPUT_TABLE_ADVERTISER = "f_union_dim_advertiser_info_d_mysql_020"
INPUT_TABLE_CONV_LINK = "t_daily_conv_link_tid_dimension_mysql_020"
INPUT_TABLE_CONFIG = "dim_union_ad_raised_config_hf_mysql_020"
INPUT_TABLE_CREATIVE_PLATFORM = "dim_tbl_creative_f_mysql_020"
OUTPUT_TABLE = "dwm_union_raise_candidate_new_ads_hf_cand_mysql_020"

MYSQL_CONFIG = {
    "host": "localhost",
    "port": 3306,
    "user": "root",
    "password": "root123",
    "charset": "utf8mb4",
}

gt_sql = f"""
INSERT INTO {DB_NAME}.{OUTPUT_TABLE}
    (partition_time, adgroup_id, advertiser_id, product_id, mix_goal,
     landing_page_type, marketing_target_id, ecology_level2_id, team,
     short_advertiser_name, landingpage_link_type, begin_time, end_time,
     created_time, smart_delivery_platform, exploration_strategy_id,
     placement_group_id_list, p_partition)
SELECT
    2026060905 AS partition_time,
    a.adgroup_id,
    COALESCE(a.advertiser_id, 0) AS advertiser_id,
    COALESCE(e.product_id, '') AS product_id,
    CONCAT(COALESCE(e.optimization_goal, 0), '_',
           COALESCE(e.second_optimization_goal, 0), '_',
           COALESCE(e.deep_conversion_optimization_goal, 0)) AS mix_goal,
    COALESCE(a.landing_page_type, '') AS landing_page_type,
    COALESCE(e.marketing_target_id, 0) AS marketing_target_id,
    COALESCE(b.ecology_level2_id, 0) AS ecology_level2_id,
    COALESCE(c.team, '') AS team,
    COALESCE(c.short_advertiser_name, '') AS short_advertiser_name,
    COALESCE(d.landingpage_link_type, '') AS landingpage_link_type,
    COALESCE(e.begin_time, 0) AS begin_time,
    COALESCE(e.end_time, 0) AS end_time,
    COALESCE(e.created_time, 0) AS created_time,
    COALESCE(f.smart_delivery_platform, 0) AS smart_delivery_platform,
    COALESCE(e.exploration_strategy_id, 0) AS exploration_strategy_id,
    COALESCE(e.placement_group_id_list, '') AS placement_group_id_list,
    '2026060905' AS p_partition
FROM (
    SELECT creative_id,
           MAX(adgroup_id) AS adgroup_id,
           MAX(advertiser_id) AS advertiser_id,
           MAX(landing_page_type) AS landing_page_type
    FROM {DB_NAME}.{INPUT_TABLE_CREATIVE}
    WHERE creative_id > 0
    GROUP BY creative_id
) a
LEFT JOIN (
    SELECT creative_id, ecology_level2_id
    FROM (
        SELECT creative_id, partition_time, ecology_level2_id,
               ROW_NUMBER() OVER (PARTITION BY creative_id ORDER BY partition_time DESC) AS rn
        FROM {DB_NAME}.{INPUT_TABLE_ECOLOGY}
        WHERE partition_time BETWEEN 20260607 AND 20260608
    ) ranked
    WHERE rn = 1
) b ON a.creative_id = b.creative_id
LEFT JOIN (
    SELECT advertiser_id, team, short_advertiser_name
    FROM (
        SELECT advertiser_id, partition_time, team, short_advertiser_name,
               ROW_NUMBER() OVER (PARTITION BY advertiser_id ORDER BY partition_time DESC) AS rn
        FROM (
            SELECT advertiser_id, partition_time,
                   MAX(operation_industry_name) AS team,
                   MAX(short_advertiser_name) AS short_advertiser_name
            FROM {DB_NAME}.{INPUT_TABLE_ADVERTISER}
            WHERE partition_time BETWEEN 20260607 AND 20260608
            GROUP BY advertiser_id, partition_time
        ) agg
    ) ranked
    WHERE rn = 1
) c ON a.advertiser_id = c.advertiser_id
LEFT JOIN (
    SELECT tid, landingpage_link_type
    FROM (
        SELECT tid, partition_time, landingpage_link_type,
               ROW_NUMBER() OVER (PARTITION BY tid ORDER BY partition_time DESC) AS rn
        FROM (
            SELECT tid, partition_time,
                   MAX(landingpage_link_type) AS landingpage_link_type
            FROM {DB_NAME}.{INPUT_TABLE_CONV_LINK}
            WHERE partition_time BETWEEN 20260607 AND 20260608
            GROUP BY tid, partition_time
        ) agg
    ) ranked
    WHERE rn = 1
) d ON a.creative_id = d.tid
JOIN (
    SELECT adgroup_id,
           MAX(product_id) AS product_id,
           MAX(optimization_goal) AS optimization_goal,
           MAX(second_optimization_goal) AS second_optimization_goal,
           MAX(deep_conversion_optimization_goal) AS deep_conversion_optimization_goal,
           MAX(marketing_target_id) AS marketing_target_id,
           MAX(begin_time) AS begin_time,
           MAX(end_time) AS end_time,
           MAX(created_time) AS created_time,
           MAX(exploration_strategy_id) AS exploration_strategy_id,
           MAX(placement_group_id_list) AS placement_group_id_list
    FROM {DB_NAME}.{INPUT_TABLE_ADGROUP}
    WHERE adgroup_id > 0
      AND (FIND_IN_SET(15, placement_group_id_list) > 0 OR FIND_IN_SET(136, placement_group_id_list) > 0)
      AND begin_time >= (
          SELECT MIN(begintime)
          FROM {DB_NAME}.{INPUT_TABLE_CONFIG}
          WHERE partition_time = 2026060905
            AND strategyid > 0
            AND 20260609 >= DATE_FORMAT(FROM_UNIXTIME(begintime), '%Y%m%d')
            AND 20260609 <= DATE_FORMAT(FROM_UNIXTIME(endtime), '%Y%m%d')
      )
      AND begin_time <= (
          SELECT MAX(endtime)
          FROM {DB_NAME}.{INPUT_TABLE_CONFIG}
          WHERE partition_time = 2026060905
            AND strategyid > 0
            AND 20260609 >= DATE_FORMAT(FROM_UNIXTIME(begintime), '%Y%m%d')
            AND 20260609 <= DATE_FORMAT(FROM_UNIXTIME(endtime), '%Y%m%d')
      )
    GROUP BY adgroup_id
) e ON a.adgroup_id = e.adgroup_id
LEFT JOIN (
    SELECT ftid,
           MAX(fsmartdeliveryplatform) AS smart_delivery_platform
    FROM {DB_NAME}.{INPUT_TABLE_CREATIVE_PLATFORM}
    WHERE ftid > 0
    GROUP BY ftid
) f ON a.creative_id = f.ftid
GROUP BY
    a.adgroup_id,
    COALESCE(a.advertiser_id, 0),
    COALESCE(e.product_id, ''),
    CONCAT(COALESCE(e.optimization_goal, 0), '_',
           COALESCE(e.second_optimization_goal, 0), '_',
           COALESCE(e.deep_conversion_optimization_goal, 0)),
    COALESCE(a.landing_page_type, ''),
    COALESCE(e.marketing_target_id, 0),
    COALESCE(b.ecology_level2_id, 0),
    COALESCE(c.team, ''),
    COALESCE(c.short_advertiser_name, ''),
    COALESCE(d.landingpage_link_type, ''),
    COALESCE(e.begin_time, 0),
    COALESCE(e.end_time, 0),
    COALESCE(e.created_time, 0),
    COALESCE(f.smart_delivery_platform, 0),
    COALESCE(e.exploration_strategy_id, 0),
    COALESCE(e.placement_group_id_list, '')
"""


def main():
    conn = pymysql.connect(**MYSQL_CONFIG)

    try:
        with conn.cursor() as cur:
            # Ensure output table exists
            cur.execute(f"""
                CREATE TABLE IF NOT EXISTS {DB_NAME}.{OUTPUT_TABLE} (
                    partition_time            BIGINT       NOT NULL COMMENT '分区时间',
                    adgroup_id                BIGINT       NOT NULL COMMENT '广告组ID',
                    advertiser_id             BIGINT       NOT NULL COMMENT '广告主ID',
                    product_id                VARCHAR(256) NOT NULL COMMENT '产品ID',
                    mix_goal                  VARCHAR(256) NOT NULL COMMENT '混合优化目标',
                    landing_page_type         VARCHAR(256) NOT NULL COMMENT '落地页类型',
                    marketing_target_id       BIGINT       NOT NULL COMMENT '营销目标ID',
                    ecology_level2_id         INT          NOT NULL COMMENT '生态二级ID',
                    team                      VARCHAR(256) NOT NULL COMMENT '团队',
                    short_advertiser_name     VARCHAR(256) NOT NULL COMMENT '广告主简称',
                    landingpage_link_type     VARCHAR(256) NOT NULL COMMENT '落地页链接类型',
                    begin_time                BIGINT       NOT NULL COMMENT '开始时间',
                    end_time                  BIGINT       NOT NULL COMMENT '结束时间',
                    created_time              BIGINT       NOT NULL COMMENT '创建时间',
                    smart_delivery_platform   INT          NOT NULL COMMENT '智能投放平台',
                    exploration_strategy_id   BIGINT       NOT NULL COMMENT '探索策略ID',
                    placement_group_id_list   VARCHAR(256) NOT NULL COMMENT '广告位组ID列表',
                    p_partition               VARCHAR(256) NOT NULL COMMENT '分区键'
                ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4
            """)

            # Truncate + insert
            cur.execute(f"TRUNCATE TABLE {DB_NAME}.{OUTPUT_TABLE}")
            cur.execute(gt_sql)

        conn.commit()
        print("mysql_020 ground_truth done: rows written to output table")
    except Exception as e:
        print(f"ground_truth error: {e}", file=sys.stderr)
        conn.rollback()
        sys.exit(1)
    finally:
        conn.close()


if __name__ == "__main__":
    main()