Follow one query from SQL text to generated code: parse, analyze, optimise, plan — and learn to read explain().
Whether you write spark.sql("SELECT …") or chain DataFrame methods, Spark builds the same thing: a logical plan, a tree of relational operators — scan, filter, project, join, aggregate. Catalyst, Spark SQL's optimiser, turns that tree into an efficient physical plan; Tungsten makes the physical plan run fast on the executors.
That's why SQL and the DataFrame API perform the same, and why PySpark DataFrame code isn't slower than Scala: your Python only describes the plan, and execution happens in the JVM. (Python UDFs are the exception — they run in Python worker processes.)
q1 = spark.sql("""
SELECT c.country, sum(o.amount) AS revenue
FROM orders o JOIN customers c ON o.customer_id = c.customer_id
WHERE o.status = 'delivered'
GROUP BY c.country
""")
q2 = (orders.filter(F.col("status") == "delivered")
.join(customers, "customer_id")
.groupBy("country")
.agg(F.sum("amount").alias("revenue")))
q1.explain() # the same physical plan ...
q2.explain() # ... as this oneReach the last step for the run summary: the key takeaways from this lesson.