title: Why spark count action has executed in three stages
body: <p>I have loaded a csv file. Re-partitioned it to 4 and then took count of the DataFrame. And when I looked at the DAG I see this action is executed in 3 stages. </p>
  
  <p><a href="https://i.stack.imgur.com/HDwK4.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/HDwK4.png" alt="enter image description here"></a></p>
  
  <p>Why this simple action is executed into 3 stages. I suppose 1st stage is to load the file and 2nd is to find the count on each partition.</p>
  
  <p>So What is happening in the 3rd stage?</p>
  
  <p>Here is my code</p>
  
  <pre><code>val sample = spark.read.format("csv").option("header", "true").option("inferSchema", "true").option("delimiter", ";").load("sample_data.csv")
  
  sample.repartition(4).count()
  </code></pre>