Для Spark 2.4+ вы можете использовать функцию sequence
, чтобы сгенерировать массив дат диапазона и затем взорвать его:
SELECT id,
explode(sequence(to_timestamp(starttime), to_timestamp(endtime), interval 1 minute)) AS starttime,
endtime
FROM my_table
df = spark.createDataFrame([(1, "1970-01-01 07:00:00", "1970-01-01 07:03:00")], ["id", "starttime", "endtime"])
df.createOrReplaceTempView("my_table")
sql_query = """SELECT id,
explode(sequence(to_timestamp(starttime), to_timestamp(endtime), interval 1 minute)) as starttime,
endtime
FROM my_table
"""
spark.sql(sql_query).show()
#+---+-------------------+-------------------+
#| id| starttime| endtime|
#+---+-------------------+-------------------+
#| 1|1970-01-01 07:00:00|1970-01-01 07:03:00|
#| 1|1970-01-01 07:01:00|1970-01-01 07:03:00|
#| 1|1970-01-01 07:02:00|1970-01-01 07:03:00|
#| 1|1970-01-01 07:03:00|1970-01-01 07:03:00|
#+---+-------------------+-------------------+