# 新闻曝光数据全量迁移 (MySQL)

## Prompt
任务目标：将输入表数据同步至输出表，实现表间数据迁移。

输入：
- `internal_platform_db.dwd_news_dt_others_imp_hi_mysql_017`

处理规则：
- 无 Join，单表处理
- 无过滤条件，保留全部记录
- 无聚合操作
- 字段全量映射，保留源表所有字段

输出要求：
- 输出表：`internal_platform_db.dwd_news_dt_others_imp_si_cand_mysql_017`
- 输出字段顺序与输入表一致
- 不去重，保留所有记录

写入要求：
- 写入模式：覆盖写入（先清空目标表再写入）
- 请使用标准 MySQL 语法，不要使用 Hive/Spark SQL 方言

**环境与执行说明**：
- 你的最终产出必须写入文件 `/tmp_workspace/result.py`，不能写 result.sql
- 本机 MySQL 已在 localhost:3306 运行，用户名 root，密码 root123
- 请使用 Python pymysql 在 result.py 中执行 SQL（不要用 mysql 命令行）
- 脚本需要包含完整的建表（如目标表不存在）+ 写入数据的逻辑
- 写出 result.py 后，你必须自己执行 `python3 /tmp_workspace/result.py` 验证它能成功运行并产出正确数据
- 如果目标表不存在，请先按 MySQL InnoDB 标准建表，再写入数据

## Reference Solution
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
mysql_017 ground truth: 新闻曝光数据全量迁移

Task:
  Copy all rows from input table to output table (full field mapping, no filter).
"""
import pymysql
import sys

DB_NAME = "internal_platform_db"
INPUT_TABLE = "dwd_news_dt_others_imp_hi_mysql_017"
OUTPUT_TABLE = "dwd_news_dt_others_imp_si_cand_mysql_017"

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

ALL_COLUMNS = [
    "iceberg_imp_date", "watermark_ts", "ftime", "imp_hour", "time_range",
    "platform_user_id", "device_id", "social_uid", "os", "network_type", "app_version", "callfrom",
    "page_start_from", "start_article_id", "start_article_type", "article_uuid",
    "article_page", "article_type", "article_pos", "article_real_pos",
    "rcmd_reason", "is_tagversion", "event_time", "client_ts", "server_time",
    "social_openid", "suid", "session_id", "session_ts", "brand", "manufacturer",
    "model", "login_type", "country", "province", "pgid", "ref_pgid", "media_id",
    "chl_id", "video_id", "article_id", "article_ptype", "article_cmt_id",
    "is_hotnews", "is_cpfans", "is_xiaoshipin", "is_coldstart", "is_minivideo",
    "imp_date", "is_landingpage", "article_imp_pv", "video_imp_pv", "imgtext_imp_pv",
    "eid", "tag_id", "pg_tag_id", "pg_tag_type", "pg_article_type", "pg_article_id",
    "is_major_upgrade", "idfv", "android_id", "tab_id", "context_type",
    "element_path", "p1_article_ptype", "refpg_article_type", "tag_type",
    "pg_tab_id", "brand_type", "fulltext_imp_pv", "scheme_type", "module",
    "cmt_replyid", "comment_imp_pv", "bubble_msg_type", "untitled",
    "hot_rank_imp_pv", "pg_tag_scene", "header_type", "window_open_from",
    "user_more_id", "section_id", "search_cell_type", "pg_source2", "bar_name",
    "scheme_url", "member_btn_id", "vert_cell_scheme_url", "article_title",
    "pg_article_title", "bigevent_type", "schedule_type", "pg_path", "ussn",
    "top_banner_type", "pendant_type", "banner_url", "sort_menu_id", "video_pid",
    "error_tips", "gameid", "ad_atype", "ad_action", "article_module_pos",
    "refpg_chl_id", "refpg_last_clck_ele", "live_article_id", "nav_item_id",
    "nav_item_name", "have_redpoint", "nav_pos", "undetermined", "pg_tab2_from",
    "scheme_scene_type", "is_reservable", "is_reserve", "pg_subtab_id",
    "pg_article_live_status", "pg_article_relate_event_type", "pg_search_keyword",
    "vert_cell_title", "mod_article_type", "pub_btn_type", "pg_hotask_type",
    "mod_article_ptype", "question_id", "answer_id", "is_answerer",
    "article_review_status", "banner_module_id", "article_live_status",
    "article_pay_status", "pg_article_pay_status", "tag_scene", "pg_detail_type",
    "column_type", "pg_column_type", "is_column_purchased", "pg_is_column_purchased",
    "pay_product_id", "huaci_type", "panel_btn_id", "is_user_self", "crepg_chl_id",
    "crepg_article_id", "crepg_article_type", "dt_cre_pgid", "crepg_last_clck_ele",
    "dialog_type", "pg_subtab_name", "e_pos", "sug_word", "e_from", "e_type",
    "article_list_pos", "pg_article_bool_parad_platform", "city_level", "has_authority",
    "pg_is_audio", "etl_pgid", "search_keyword", "cardpanel_type", "mod_article_page",
    "mod_alg_info", "mod_article_real_pos", "article_id_list", "e_state", "user_suid",
    "pg_login_from", "pg_last_login_type", "is_ad", "hometown_adcode",
    "user_service_id", "user_cpcenter_id", "agent_id", "e_title", "tab_setid",
    "pg_page_start_from", "is_flash_keyword", "search_query_from",
    "is_query_from_cache", "p1_search_cell_type", "pg_search_query_from",
    "refpg_search_query_from",
]

col_list = ", ".join(ALL_COLUMNS)

gt_sql = f"""
INSERT INTO {DB_NAME}.{OUTPUT_TABLE} ({col_list})
SELECT {col_list}
FROM {DB_NAME}.{INPUT_TABLE}
"""

# Output table DDL (same schema as input)
OUTPUT_DDL = f"""
CREATE TABLE IF NOT EXISTS {DB_NAME}.{OUTPUT_TABLE} (
    `_id`                BIGINT AUTO_INCREMENT PRIMARY KEY COMMENT '自增代理主键',
    `iceberg_imp_date`   TEXT  NOT NULL COMMENT '分区时间',
    `watermark_ts`       BIGINT        NOT NULL COMMENT 'watermark时间毫秒',
    `ftime`              TEXT  NOT NULL COMMENT '数据上报时间',
    `imp_hour`           TEXT  NOT NULL COMMENT '时间分区',
    `time_range`         TEXT  NOT NULL COMMENT '时段分类',
    `platform_user_id`              VARCHAR(256)  NOT NULL COMMENT '用户platform_user_id',
    `device_id`            TEXT  NOT NULL COMMENT '用户device_id',
    `social_uid`                 TEXT  NOT NULL COMMENT 'social user id',
    `os`                 TEXT  NOT NULL COMMENT '操作系统',
    `network_type`       TEXT  NOT NULL COMMENT '网络类型',
    `app_version`        TEXT  NOT NULL COMMENT 'app版本号',
    `callfrom`           TEXT  NOT NULL COMMENT '启动方式',
    `page_start_from`    TEXT  NOT NULL COMMENT '落地页拉起来源',
    `start_article_id`   TEXT  NOT NULL COMMENT '外部拉起时的文章id',
    `start_article_type` TEXT  NOT NULL COMMENT '外部拉起时的文章类型',
    `article_uuid`       TEXT  NOT NULL COMMENT '文章唯一uuid',
    `article_page`       BIGINT        NOT NULL COMMENT '刷次',
    `article_type`       TEXT  NOT NULL COMMENT '文章类型',
    `article_pos`        BIGINT        NOT NULL COMMENT '文章位置',
    `article_real_pos`   BIGINT        NOT NULL COMMENT '文章真实位置',
    `rcmd_reason`        TEXT  NOT NULL COMMENT '推荐理由',
    `is_tagversion`      TEXT  NOT NULL COMMENT '是否tag新版本',
    `event_time`         TEXT  NOT NULL COMMENT '事件时间',
    `client_ts`          TEXT  NOT NULL COMMENT '事件时间戳',
    `server_time`        TEXT  NOT NULL COMMENT '上报时间',
    `social_openid`          TEXT  NOT NULL COMMENT '平台openid',
    `suid`               TEXT  NOT NULL COMMENT '用户登陆ID',
    `session_id`         TEXT  NOT NULL COMMENT '会话ID',
    `session_ts`         TEXT  NOT NULL COMMENT '会话时间戳',
    `brand`              TEXT  NOT NULL COMMENT '设备品牌',
    `manufacturer`       TEXT  NOT NULL COMMENT '设备制造商',
    `model`              TEXT  NOT NULL COMMENT '设备型号',
    `login_type`         TEXT  NOT NULL COMMENT '登陆类型',
    `country`            TEXT  NOT NULL COMMENT '国家',
    `province`           TEXT  NOT NULL COMMENT '省份',
    `pgid`               TEXT  NOT NULL COMMENT '页面类型',
    `ref_pgid`           TEXT  NOT NULL COMMENT '上一级页面类型',
    `media_id`           TEXT  NOT NULL COMMENT '用户媒体ID',
    `chl_id`             TEXT  NOT NULL COMMENT '频道ID',
    `video_id`           TEXT  NOT NULL COMMENT '视频ID',
    `article_id`         TEXT  NOT NULL COMMENT '文章ID',
    `article_ptype`      TEXT  NOT NULL COMMENT '展示样式类型',
    `article_cmt_id`     TEXT  NOT NULL COMMENT '文章评论ID',
    `is_hotnews`         TEXT  NOT NULL COMMENT '是否热点文章',
    `is_cpfans`          TEXT  NOT NULL COMMENT '是否关注状态',
    `is_xiaoshipin`      TEXT  NOT NULL COMMENT '是否小视频（接入层）',
    `is_coldstart`       TEXT  NOT NULL COMMENT '是否冷启动',
    `is_minivideo`       TEXT  NOT NULL COMMENT '是否小视频（内平）',
    `imp_date`           TEXT  NOT NULL COMMENT '日期',
    `is_landingpage`     TEXT  NOT NULL COMMENT '是否落地页',
    `article_imp_pv`     BIGINT        NOT NULL COMMENT '文章曝光pv',
    `video_imp_pv`       BIGINT        NOT NULL COMMENT '视频文章曝光pv',
    `imgtext_imp_pv`     BIGINT        NOT NULL COMMENT '图文文章曝光pv',
    `eid`                TEXT  NOT NULL COMMENT '元素id',
    `tag_id`             TEXT  NOT NULL COMMENT 'tag_id',
    `pg_tag_id`          TEXT  NOT NULL COMMENT '页面的tag_id',
    `pg_tag_type`        TEXT  NOT NULL COMMENT 'tag类型',
    `pg_article_type`    TEXT  NOT NULL COMMENT '页面文章类型',
    `pg_article_id`      TEXT  NOT NULL COMMENT '页面文章id',
    `is_major_upgrade`   TEXT  NOT NULL COMMENT 'NoComment',
    `idfv`               TEXT  NOT NULL COMMENT 'idfv',
    `android_id`         TEXT  NOT NULL COMMENT 'android_id',
    `tab_id`             TEXT  NOT NULL COMMENT '底部导航Tab Id',
    `context_type`       TEXT  NOT NULL COMMENT 'NoComment',
    `element_path`       TEXT  NOT NULL COMMENT '元素路径',
    `p1_article_ptype`   TEXT  NOT NULL COMMENT '父级文章展示类型',
    `refpg_article_type` TEXT  NOT NULL COMMENT '上一级页面的文章类型',
    `tag_type`           TEXT  NOT NULL COMMENT 'tag类型',
    `pg_tab_id`          TEXT  NOT NULL COMMENT '来源页面底部导航Tab Id',
    `brand_type`         TEXT  NOT NULL COMMENT 'brand_type',
    `fulltext_imp_pv`    BIGINT        NOT NULL COMMENT '展开全文按钮真实曝光',
    `scheme_type`        TEXT  NOT NULL COMMENT 'scheme类型',
    `module`             TEXT  NOT NULL COMMENT '模块',
    `cmt_replyid`        TEXT  NOT NULL COMMENT '评论id',
    `comment_imp_pv`     BIGINT        NOT NULL COMMENT '评论曝光pv',
    `bubble_msg_type`    TEXT  NOT NULL COMMENT '气泡类型',
    `untitled`           TEXT  NOT NULL COMMENT '待更名',
    `hot_rank_imp_pv`    BIGINT        NOT NULL COMMENT '热榜入口曝光',
    `pg_tag_scene`       TEXT  NOT NULL COMMENT '页面tag场景类型',
    `header_type`        TEXT  NOT NULL COMMENT '头部类型',
    `window_open_from`   TEXT  NOT NULL COMMENT '弹窗来源',
    `user_more_id`       TEXT  NOT NULL COMMENT 'user_more_id',
    `section_id`         TEXT  NOT NULL COMMENT 'section_id',
    `search_cell_type`   TEXT  NOT NULL COMMENT '搜索页模块种类',
    `pg_source2`         TEXT  NOT NULL COMMENT '长视频流量来源',
    `bar_name`           TEXT  NOT NULL COMMENT '提示条名称',
    `scheme_url`         TEXT  NOT NULL COMMENT 'scheme_url',
    `member_btn_id`      TEXT  NOT NULL COMMENT '元素id',
    `vert_cell_scheme_url` TEXT NOT NULL COMMENT 'vert元素的url',
    `article_title`      TEXT  NOT NULL COMMENT '元素名称标识',
    `pg_article_title`   TEXT  NOT NULL COMMENT '页面或者文章标题',
    `bigevent_type`      TEXT  NOT NULL COMMENT '大事件类型',
    `schedule_type`      TEXT  NOT NULL COMMENT '赛程类型',
    `pg_path`            TEXT  NOT NULL COMMENT '页面路径',
    `ussn`               TEXT  NOT NULL COMMENT '新版session_id',
    `top_banner_type`    TEXT  NOT NULL COMMENT '顶部banner类型',
    `pendant_type`       TEXT  NOT NULL COMMENT '挂件类型',
    `banner_url`         TEXT  NOT NULL COMMENT 'banner跳转的URL',
    `sort_menu_id`       TEXT  NOT NULL COMMENT '分类菜单id',
    `video_pid`          TEXT  NOT NULL COMMENT 'video_pid',
    `error_tips`         TEXT  NOT NULL COMMENT '敏感词拦截提示文案',
    `gameid`             TEXT  NOT NULL COMMENT '游戏ID',
    `ad_atype`           TEXT  NOT NULL COMMENT '广告类型',
    `ad_action`          TEXT  NOT NULL COMMENT '广告动作类型',
    `article_module_pos` TEXT  NOT NULL COMMENT '模块位置',
    `refpg_chl_id`       TEXT  NOT NULL COMMENT '来源页面chl_id',
    `refpg_last_clck_ele` TEXT NOT NULL COMMENT '上一级页面业务私参',
    `live_article_id`    TEXT  NOT NULL COMMENT '直播文章id',
    `nav_item_id`        TEXT  NOT NULL COMMENT '导航id',
    `nav_item_name`      TEXT  NOT NULL COMMENT '导航名称',
    `have_redpoint`      TEXT  NOT NULL COMMENT '是否带红点',
    `nav_pos`            TEXT  NOT NULL COMMENT '频道位置',
    `undetermined`       TEXT  NOT NULL COMMENT '废弃字段',
    `pg_tab2_from`       TEXT  NOT NULL COMMENT 'tab2来源',
    `scheme_scene_type`  TEXT  NOT NULL COMMENT '短带长类型',
    `is_reservable`      TEXT  NOT NULL COMMENT '是否带预约按钮',
    `is_reserve`         TEXT  NOT NULL COMMENT '是否预约状态',
    `pg_subtab_id`       TEXT  NOT NULL COMMENT '底层页子tab的id',
    `pg_article_live_status` TEXT NOT NULL COMMENT '直播状态',
    `pg_article_relate_event_type` TEXT NOT NULL COMMENT '关联事件类型',
    `pg_search_keyword`  TEXT  NOT NULL COMMENT '搜索词',
    `vert_cell_title`    TEXT  NOT NULL COMMENT 'cell 标题',
    `mod_article_type`   TEXT  NOT NULL COMMENT '模块文章类型',
    `pub_btn_type`       TEXT  NOT NULL COMMENT '发布按钮类型',
    `pg_hotask_type`     TEXT  NOT NULL COMMENT '热问页面类型',
    `mod_article_ptype`  TEXT  NOT NULL COMMENT '模块文章展示样式类型',
    `question_id`        TEXT  NOT NULL COMMENT '问题 id',
    `answer_id`          TEXT  NOT NULL COMMENT '外显回答 id',
    `is_answerer`        TEXT  NOT NULL COMMENT '是否答主',
    `article_review_status` TEXT NOT NULL COMMENT '回答状态',
    `banner_module_id`   TEXT  NOT NULL COMMENT 'banner模块 id',
    `article_live_status` TEXT NOT NULL COMMENT '文章直播状态',
    `article_pay_status` TEXT  NOT NULL COMMENT '文章付费状态',
    `pg_article_pay_status` TEXT NOT NULL COMMENT '文章付费状态（页面参数）',
    `tag_scene`          TEXT  NOT NULL COMMENT 'tag场景类型',
    `pg_detail_type`     TEXT  NOT NULL COMMENT '底层页类型',
    `column_type`        TEXT  NOT NULL COMMENT '专栏类型',
    `pg_column_type`     TEXT  NOT NULL COMMENT '页面专栏类型',
    `is_column_purchased` TEXT NOT NULL COMMENT '是否购买专栏',
    `pg_is_column_purchased` TEXT NOT NULL COMMENT '页面是否购买专栏',
    `pay_product_id`     TEXT  NOT NULL COMMENT '支付时所选的产品ID',
    `huaci_type`         TEXT  NOT NULL COMMENT '划词类型',
    `panel_btn_id`       TEXT  NOT NULL COMMENT '面板按钮ID',
    `is_user_self`       TEXT  NOT NULL COMMENT '是否用户本人',
    `crepg_chl_id`       TEXT  NOT NULL COMMENT '跳转起始页面频道ID',
    `crepg_article_id`   TEXT  NOT NULL COMMENT '跳转起始页面文章ID',
    `crepg_article_type` TEXT  NOT NULL COMMENT '跳转起始页面文章类型',
    `dt_cre_pgid`        TEXT  NOT NULL COMMENT '创造页页面ID',
    `crepg_last_clck_ele` TEXT NOT NULL COMMENT '创造页上一次点击元素信息',
    `dialog_type`        TEXT  NOT NULL COMMENT '对话类型',
    `pg_subtab_name`     TEXT  NOT NULL COMMENT '子 tab名称',
    `e_pos`              TEXT  NOT NULL COMMENT '元素位置',
    `sug_word`           TEXT  NOT NULL COMMENT '建议词条',
    `e_from`             TEXT  NOT NULL COMMENT '元素来源',
    `e_type`             TEXT  NOT NULL COMMENT '元素类型',
    `article_list_pos`   TEXT  NOT NULL COMMENT '绝对位置',
    `pg_article_bool_parad_platform` TEXT NOT NULL COMMENT '文章携带的布尔参数',
    `city_level`         TEXT  NOT NULL COMMENT '城市等级',
    `has_authority`      TEXT  NOT NULL COMMENT '是否同意隐私协议',
    `pg_is_audio`        TEXT  NOT NULL COMMENT '是否是音频',
    `etl_pgid`           TEXT  NOT NULL COMMENT 'ETL页面id',
    `search_keyword`     TEXT  NOT NULL COMMENT '搜索关键词',
    `cardpanel_type`     TEXT  NOT NULL COMMENT '卡片面板类型',
    `mod_article_page`   TEXT  NOT NULL COMMENT '模块文章所在刷次',
    `mod_alg_info`       TEXT  NOT NULL COMMENT '模块文章推荐信息',
    `mod_article_real_pos` TEXT NOT NULL COMMENT '模块文章在刷次中的位置',
    `article_id_list`    TEXT  NOT NULL COMMENT '文章id集合',
    `e_state`            TEXT  NOT NULL COMMENT '元素状态',
    `user_suid`          TEXT  NOT NULL COMMENT '用户suid',
    `pg_login_from`      TEXT  NOT NULL COMMENT '登录页来源',
    `pg_last_login_type` TEXT  NOT NULL COMMENT '上次登录类型',
    `is_ad`              TEXT  NOT NULL COMMENT '是否广告',
    `hometown_adcode`    TEXT  NOT NULL COMMENT '家乡code',
    `user_service_id`    TEXT  NOT NULL COMMENT '用户service id',
    `user_cpcenter_id`   TEXT  NOT NULL COMMENT '用户中心id',
    `agent_id`           TEXT  NOT NULL COMMENT '智能体ID',
    `e_title`            TEXT  NOT NULL COMMENT '元素文本',
    `tab_setid`          TEXT  NOT NULL COMMENT '发布按钮setid',
    `pg_page_start_from` TEXT  NOT NULL COMMENT '落地页拉起来源（页面参数）',
    `is_flash_keyword`   TEXT  NOT NULL COMMENT '轮播词query是否无价值曝光',
    `search_query_from`  TEXT  NOT NULL COMMENT '搜索发起来源',
    `is_query_from_cache` TEXT NOT NULL COMMENT 'query是否缓存',
    `p1_search_cell_type` TEXT NOT NULL COMMENT '文章所在模块的cell类型',
    `pg_search_query_from` TEXT NOT NULL COMMENT '页面搜索发起来源',
    `refpg_search_query_from` TEXT NOT NULL COMMENT '来源页面发起搜索的方式',
    KEY `idx_platform_user_id` (`platform_user_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4
"""


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

    try:
        with conn.cursor() as cur:
            # Ensure output table exists
            cur.execute(OUTPUT_DDL)

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

        conn.commit()
        print("mysql_017 ground_truth done: all 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()