Skip to main content
πŸ“˜ Complete PDF

PySpark Certification Practice Test 10

Debug Pipeline Questions

Question 1 of 20
πŸ“‹ Multiple Choice
Question 181
Problem in PySpark Pipeline: ```python # Developer attempts to filter records: df.filter(col("salary") > 10000) ``` Expected Output: 100 rows matching the salary threshold. Actual Output: The downstream pipeline still processes the full 1,000,000 un-filtered rows. What is the root cause of this pipeline bug?
A. Partition issue
βœ“
B. Result not assigned back to DataFrame
C. Cache issue
D. Broadcast issue
Answer
βœ… B. Result not assigned back to DataFrame
Explanation

1. Concept Overview In Apache Spark, DataFrames are **immutable**. Calling a transformation like `df.filter()`, `df.select()`, or `df.withColumn()` does not modify the existing DataFrame in-place. Instead, it constructs and returns a *new* DataFrame representing the updated logical plan. If the return value is not assigned back to a variable (e.g., `df = df.filter(...)`), the filtered result is discarded and downstream code continues executing against the original, unfiltered DataFrame.

2. Architecture & Design Principle 1. **Functional Immutability**: - Immutability guarantees thread safety, deterministic fault tolerance, and safe DAG lineage tracking. - Because `df` cannot be mutated in place, Spark can recompute lost partitions on worker failure by replaying lineage from the source. 2. **Variable Reassignment Pattern**: - In Python/PySpark, you must rebind the variable: `df = df.filter(col("salary") > 10000)`. - Chaining transformations (`df = df.filter(...).select(...)`) creates a seamless lineage graph.

3. Step-by-Step Code / Logic Execution 1. The buggy code:

   # Line 1: Creates original DataFrame with 1,000,000 rows
   df = spark.read.parquet("s3://bucket/salaries/")

   # Line 2: Creates a NEW filtered DataFrame, but assigns it to NOTHING!
   df.filter(col("salary") > 10000)

   # Line 3: References original unfiltered 'df' containing 1,000,000 rows!
   df.write.parquet("s3://bucket/output/")

2. The corrected code:

   # Assigns the transformed DataFrame back to 'df'
   df = df.filter(col("salary") > 10000)

3. Execution trace: Downstream write receives only the 100 qualifying rows.

4. Why the Correct Answer Is Optimal Option B is correct because: - PySpark DataFrames cannot be modified in place. - Failing to capture the return value of `df.filter()` leaves the original DataFrame unchanged.

5. Why Alternative Options Fail - Option A (Partition issue): Incorrect; partitioning affects data distribution, not the failure to assign a filter transformation. - Option C (Cache issue): Incorrect; caching does not alter immutability semantics. - Option D (Broadcast issue): Incorrect; broadcasting applies to joins, not single-table column filters.

6. Edge Cases & Production Pitfalls - **Accidental Discard in Helper Functions**: Passing a DataFrame to a Python function `def clean(df): df.filter(...) ; return df` does not modify the caller's DataFrame unless the function explicitly returns the result of the filter.

7. Performance & Optimization Considerations - Reassigning DataFrame variables introduces zero memory copying; Spark simply appends a `Filter` logical plan node to the DAG metadata.

8. Reliability & Operational Best Practices - Chain transformations cleanly or use explicit variable re-assignments: `df_filtered = df.filter(...)`. - Enforce automated unit testing with assertions on expected row counts after filtering.

9. Real-World Architectural Scenario A financial reporting job calculated tax liabilities on active accounts. A developer added `df.filter(col("status") == "ACTIVE")` without reassigning `df`. The pipeline processed 40 million inactive closed accounts, issuing erroneous tax audit notices before the bug was caught.

10. Official Documentation & Learning Resources - Immutability & Basics: [PySpark DataFrame Basics Tutorial](/tutorials/pyspark/pyspark-dataframe-basics)

Answered: 0 / 20
Question Navigator (Test 10)0% Answered