I'm trying to understand behaviour differences between pyspark.sql.currenttimestamp() and datetime.now()
If I create a Spark dataframe in DataBricks using these 2 mechanisms to create a timestamp column, everything works nicely as expected....
curDate2 = spark.range(10)\
.withColumn("current_date_lit",F.lit(date.today()))\
.withColumn("current_timestamp_lit",F.lit(F.current_timestamp()))\
.withColumn("current_timestamp",F.current_timestamp())\
.withColumn("now",F.lit(datetime.now()))
+---+----------------+---------------------+--------------------+--------------------+
| id|current_date_lit|current_timestamp_lit| current_timestamp| now|
+---+----------------+---------------------+--------------------+--------------------+
| 0| 2022-02-12| 2022-02-12 16:40:...|2022-02-12 16:40:...|2022-02-12 16:40:...|
| 1| 2022-02-12| 2022-02-12 16:40:...|2022-02-12 16:40:...|2022-02-12 16:40:...|
| 2| 2022-02-12| 2022-02-12 16:40:...|2022-02-12 16:40:...|2022-02-12 16:40:...|
+---+----------------+---------------------+--------------------+--------------------+
However, when I then call show() on the dataframe a couple of minutes later the columns based on currenttimestamp() show me the time NOW (16:44) whilst the datetime.now() column shows me the timestamp from the first creation of the dataframe (16:40)
Clearly one column holds a literal value & the other enumerates the function at runtime but I'm at a loss to understand why they behave differently
show() a few mins later...
+---+----------------+---------------------+--------------------+--------------------+
| id|current_date_lit|current_timestamp_lit| current_timestamp| now|
+---+----------------+---------------------+--------------------+--------------------+
| 0| 2022-02-12| 2022-02-12 16:44:...|2022-02-12 16:44:...|2022-02-12 16:40:...|
| 1| 2022-02-12| 2022-02-12 16:44:...|2022-02-12 16:44:...|2022-02-12 16:40:...|
| 2| 2022-02-12| 2022-02-12 16:44:...|2022-02-12 16:44:...|2022-02-12 16:40:...|
+---+----------------+---------------------+--------------------+--------------------+
Thanks - I hope this makes sense!