Datasets:
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_v3internal_platform_db.case10_sec_app_hy_top_500_sites_tag_v1_v3Data 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:
- Field preparation: Select the following columns from the fact table:
date_keyas-ishost→ renamedsource_hostwhen(col("is_error") == 0, 1).otherwise(0)→success(is_erroris STRING; relies on Spark implicit conversion to compare with integer 0)when(col("is_error") == 1, 1).otherwise(0)→failureweb_typeas-isfrom_json(col("parse_html"), schema_of_json('{"content": "string"}')).getField("content")→contenterror_stepas-isfld→ renamedsource_fldThen
withColumn:content_length = length(content)
- Four independent aggregations (all grouped by
date_key,source_host,source_fld):
summary:
sum(success)→successful_parses,sum(failure)→failed_parses,count(*)→total_parsesindex_page_ratio:
sum(when(web_type=='索引页', 1).otherwise(0))→index_page_countcontent_length_ratio: first
filter(success==1), thensum(when(content_length<50, 1).otherwise(0))→short_content_counterror_reasons: first
filter(failure==1), then:
- Fact table filter:
sum(when(error_step==1, 1).otherwise(0))→error_step_1_countsum(when(error_step==2, 1).otherwise(0))→error_step_2_countsum(when(error_step==3, 1).otherwise(0))→error_step_3_countsum(when(error_step.isNotNull(), 1).otherwise(0))→total_error_steps(this field is not used or output subsequently)Merge: Left join the four aggregation results sequentially on
[date_key, source_host, source_fld]Compute ratios:
index_page_count_rate = index_page_count / total_parsesshort_content_count_rate = short_content_count / successful_parses(no divide-by-zero protection; produces null whensuccessful_parses=0)
- Hardcoded empty fields:
withColumn("error_info_list", lit(""))— fixed empty string, nocollect_listwithColumn("is_host", lit(""))— fixed empty string, not obtained from dimension table
- 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_host→host, source_fld→fld, 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_keyAll 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_keyis 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