dicemy's picture
Upload 655 files
e8c001c verified
|
Raw
History Blame Contribute Delete
5.01 kB
metadata
id: offline-compute_PySpark_pyspark_006
name: AI Search Parse Quality Daily Detail
category: offline-compute/PySpark
timeout_seconds: 900
modality: pure-text
engine: pyspark

Prompt

I need you to generate a PySpark script that produces the "AI Search Parse Quality Daily Detail".

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.case10_ai_engine_classify_parse_result_daily_v3

  • internal_platform_db.case10_sec_app_hy_top_500_sites_tag_v1_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, no pre-filtering

    Processing Logic:

    1. Field preparation: Select the following columns from the fact table:
    • date_key as-is

    • host → renamed source_host

    • when(col("is_error") == 0, 1).otherwise(0)success (is_error is STRING; relies on Spark implicit conversion to compare with integer 0)

    • when(col("is_error") == 1, 1).otherwise(0)failure

    • web_type as-is

    • from_json(col("parse_html"), schema_of_json('{"content": "string"}')).getField("content")content

    • error_step as-is

    • fld → renamed source_fld

    • Then withColumn: content_length = length(content)

    1. Four independent 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:

  • sum(when(error_step==1, 1).otherwise(0))error_step_1_count

  • sum(when(error_step==2, 1).otherwise(0))error_step_2_count

  • sum(when(error_step==3, 1).otherwise(0))error_step_3_count

  • sum(when(error_step.isNotNull(), 1).otherwise(0))total_error_steps (this field is not used or output subsequently)

    1. Merge: Left join the four aggregation results sequentially on [date_key, source_host, source_fld]

    2. 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)

    1. Hardcoded empty fields:
    • withColumn("error_info_list", lit("")) — fixed empty string, no collect_list

    • withColumn("is_host", lit("")) — fixed empty string, not obtained from dimension table

    1. Join dimension table (OR condition): final_result.join(

priority_info, — contains only host, fld, priority

(source_fld == priority_info.fld) OR (source_host == priority_info.host),

"left"

) 6. Key: The condition is OR (matching on either fld or host), not AND. One main table record may match multiple dimension table rows, causing row inflation. 7. Final select (16 columns, in this order): date_key, source_hosthost, source_fldfld, is_host (step5'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 (step5's lit("")) 8. Deduplication: dropDuplicates(["date_key", "host", "fld", "priority", "successful_parses", "total_parses", "index_page_count"]) (to converge inflated rows from the OR join) 9. Write: Write via createOrReplaceTempView + INSERT OVERWRITE TABLE, explicitly specifying column name order (the output table DDL column order differs from the DataFrame column order):

  • Output table column order: host, fld, is_host, priority, 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, date_key

  • All fields are written with CAST AS STRING

Output table: internal_platform_db.case10_ai_engine_classify_parse_result_daily_detail_v3

  • Output table DDL column order (note that date_key is last): host, fld, is_host, priority, 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, date_key
  • All column types are STRING