21 of 55.
What is the behavior of the function date_sub(start, days) if a negative value is passed into the days parameter?
In Spark SQL, the function date_sub(startDate, days) returns the date that is days before startDate.
If the days parameter is negative, Spark interprets it as subtracting a negative number, which effectively adds days to the date.
Example:
from pyspark.sql.functions import date_sub, lit
df = spark.createDataFrame([( '2024-10-01', )], ['dt'])
df.select(date_sub('dt', -5).alias('new_date')).show()
Output:
+----------+
| new_date |
+----------+
|2024-10-06|
+----------+
Why the other options are incorrect:
B: No error occurs; negative values are supported.
C: The start date changes if days 0.
D: Subtracting days would move the date backward, not forward.
Spark SQL Functions --- date_sub(startDate, days) and date_add(startDate, days) behavior.
Databricks Exam Guide (June 2025): Section ''Using Spark SQL'' --- working with date and timestamp functions.
===========
Currently there are no comments in this discussion, be the first to comment!