| --- |
| id: offline-compute_PySpark_pyspark_005 |
| name: AI Search Parse Quality Daily Detail (case9 version) |
| category: offline-compute/PySpark |
| timeout_seconds: 900 |
| modality: pure-text |
| engine: pyspark |
| --- |
| ## Prompt |
| Business Background: Compute daily details of AI search web page parse quality by site + directory dimensions. Take data from the past few days, aggregate success rate, index page ratio, short content ratio, and failure step counts by the (date_key, host, fld) dimension, and join with the Top500 site priority labels. |
| |
| **Input Tables**: |
| - `internal_platform_db.ai_engine_classify_parse_result_daily_copilot_v3` |
| - `internal_platform_db.sec_app_hy_top_500_sites_tag_v1_copilot_v3` |
|
|
| Data Range: |
| - Fact table filter: `date_key >= '20260508' AND date_key <= '20260513'` (hardcoded, today='20260513', looking back 5 days) |
| - Dimension table participates in full |
|
|
| Processing Logic: |
|
|
| 1. Field preparation: Select the following columns from the fact table: |
|
|
| - `date_key` retained as-is |
|
|
| - `host` → renamed to `source_host` |
|
|
| - `fld` → renamed to `source_fld` |
|
|
| - `when(col("is_error") == 0, 1).otherwise(0)` → `success` (note: `is_error` is STRING; comparing with `== 0` triggers implicit type conversion in Spark) |
|
|
| - `when(col("is_error") == 1, 1).otherwise(0)` → `failure` |
|
|
| - `web_type` retained as-is |
|
|
| - `from_json(col("parse_html"), schema_of_json('{"content": "string"}')).getField("content")` → `content` |
|
|
| - `error_step` retained as-is |
|
|
| - Then `withColumn("content_length", length(col("content")))` |
| 2. Four aggregations (all grouped by `date_key`, `source_host`, `source_fld`): |
|
|
|
|
| - summary: `sum(success)` → `successful_parses`, `sum(failure)` → `failed_parses`, `count(*)` → `total_parses` |
|
|
| - index_page_ratio: `sum(when(web_type=='索引页', 1).otherwise(0))` → `index_page_count` |
|
|
|
|
| - content_length_ratio: first `filter(success==1)`, then `sum(when(content_length<50, 1).otherwise(0))` → `short_content_count` |
|
|
| - error_reasons: first `filter(failure==1)`, then separately `sum(when(error_step==1/2/3, 1).otherwise(0))` → `error_step_1/2/3_count`; additionally `sum(when(error_step.isNotNull(), 1).otherwise(0))` → `total_error_steps` (this field is not used subsequently) |
| 3. Merge: Left join the four aggregation results on `[date_key, source_host, source_fld]` to form `final_result` |
| 4. Compute ratios: |
|
|
| - `index_page_count_rate = index_page_count / total_parses` |
|
|
| - `short_content_count_rate = short_content_count / successful_parses` (no divide-by-zero protection; produces null when `successful_parses=0`) |
| 5. Hardcoded empty fields: |
|
|
|
|
| - `withColumn("error_info_list", lit(""))` — no `collect_list`, directly fixed as empty string |
|
|
| - `withColumn("is_host", lit(""))` — not taken from the dimension table, directly fixed as empty string |
|
|
| 6. Join dimension table (key: OR condition): |
| `final_result.join(` |
|
|
| `priority_info`, — contains only `host`, `fld`, `priority` columns |
|
|
| `(final_result.source_fld == priority_info.fld) OR` |
|
|
| `(final_result.source_host == priority_info.host),` |
|
|
| `"left"` |
|
|
| `)` |
| 6. Note: The join condition is OR (matching on either fld or host), not AND. This may cause one main table record to match multiple rows in the dimension table. |
| 7. Final select (16 columns, in this order): |
|
|
| - `date_key`, `source_host`→`host`, `source_fld`→`fld`, `is_host` (from step 5's `lit("")`), `priority` (from dimension table join), `successful_parses`, `failed_parses`, `total_parses`, `index_page_count`, `index_page_count_rate`, `short_content_count`, |
| `short_content_count_rate`, `error_step_1_count`, `error_step_2_count`, `error_step_3_count`, `error_info_list` (from step 5's `lit("")`) |
| 8. Deduplication: `dropDuplicates(["date_key", "host", "fld", "priority", "successful_parses", "total_parses", "index_page_count"])` |
|
|
| - Note: Because the OR join may produce multiple rows (one main table row matching multiple dimension table rows), deduplication is used to converge the inflated rows |
| 9. Write: `final_result.write.mode("overwrite").insertInto(target table)` |
|
|
| Output table: `internal_platform_db.ai_engine_classify_parse_result_daily_detail_copilot_v3` |
|
|