MrPowers opened a new pull request #31073:
URL: https://github.com/apache/spark/pull/31073


   ### What changes were proposed in this pull request?
   
   This pull request exposes the `make_interval` function, [as suggested 
here](https://github.com/apache/spark/pull/31000#pullrequestreview-560812433), 
and as agreed to 
[here](https://github.com/apache/spark/pull/31000#issuecomment-754856820) and 
[here](https://github.com/apache/spark/pull/31000#issuecomment-755040234).
   
   This powerful little function allows for idiomatic datetime arithmetic via 
the Scala API:
   
   ```scala
   // add two hours
   df.withColumn("plus_2_hours", col("first_datetime") + make_interval(hours = 
lit(2)))
   
   // subtract one week and 30 seconds
   col("d") - make_interval(weeks = lit(1), secs = lit(30))
   ```
   
   The `make_interval` [SQL 
function](https://github.com/apache/spark/pull/26446) already exists.
   
   Here is [this JIRA 
ticket](https://issues.apache.org/jira/browse/SPARK-33995) for this PR.
   
   ### Why are the changes needed?
   
   The Spark API makes it easy to perform datetime addition / subtraction with 
months (`add_months`) and days (`date_add`).  Users need to write code like 
this to perform datetime addition with years, weeks, hours, minutes, or seconds:
   
   ```scala
   df.withColumn("plus_2_hours", expr("first_datetime + INTERVAL 2 hours"))
   ```
   
   We don't want to force users to manipulate SQL strings when they're using 
the Scala API.
   
   ### Does this PR introduce _any_ user-facing change?
   
   Yes, this PR adds `make_interval` to the `org.apache.spark.sql.functions` 
API.  
   
   This single function will benefit a lot of users.  It's a small increase in 
the surface of the API for a big gain.
   
   ### How was this patch tested?
   
   This was tested via unit tests.
   
   cc: @MaxGekk 


----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
[email protected]



---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to