Datasets:
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_v3internal_platform_db.sec_app_hy_top_500_sites_tag_v1_copilot_v3Data 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:
- Field preparation: Select the following columns from the fact table:
date_keyretained as-ishost→ renamed tosource_hostfld→ renamed tosource_fldwhen(col("is_error") == 0, 1).otherwise(0)→success(note:is_erroris STRING; comparing with== 0triggers implicit type conversion in Spark)when(col("is_error") == 1, 1).otherwise(0)→failureweb_typeretained as-isfrom_json(col("parse_html"), schema_of_json('{"content": "string"}')).getField("content")→contenterror_stepretained as-isThen
withColumn("content_length", length(col("content")))
- Four 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 separatelysum(when(error_step==1/2/3, 1).otherwise(0))→error_step_1/2/3_count; additionallysum(when(error_step.isNotNull(), 1).otherwise(0))→total_error_steps(this field is not used subsequently)
- Merge: Left join the four aggregation results on
[date_key, source_host, source_fld]to formfinal_result - 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(""))— nocollect_list, directly fixed as empty stringwithColumn("is_host", lit(""))— not taken from the dimension table, directly fixed as empty string
- Join dimension table (key: OR condition):
final_result.join(
- Fact table filter:
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'slit("")),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'slit(""))
- 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
- Write:
final_result.write.mode("overwrite").insertInto(target table)
Output table: internal_platform_db.ai_engine_classify_parse_result_daily_detail_copilot_v3