Может использоваться оконная функция:
val df = Seq(false, false, true, false, false, true, true, false).toDF("col1")
val ordered = df
.withColumn("id", monotonically_increasing_id())
.withColumn("increment", when($"col1" === true, 1).otherwise(0))
val idWindow = Window.orderBy("id").rowsBetween(Window.unboundedPreceding, Window.currentRow)
val result = ordered.select($"col1", sum($"increment").over(idWindow).alias("col2"))
Выход:
+-----+----+
|col1 |col2|
+-----+----+
|false|0 |
|false|0 |
|true |1 |
|false|1 |
|false|1 |
|true |2 |
|true |3 |
|false|3 |
+-----+----+