MaxGekk opened a new pull request #26022: [SPARK-29355][SQL] Support timestamps 
subtraction
URL: https://github.com/apache/spark/pull/26022
 
 
   ### What changes were proposed in this pull request?
   
   Added new expression `TimestampDiff` for timestamp subtractions. It accepts 
2 timestamp expressions and returns another one of the `CalendarIntervalType`. 
While creating an instance of `CalendarInterval`, it initializes only the 
microsecond field by difference of the given timestamps in microseconds, and 
set the `months` field to zero. Also I added an rule for conversion `Subtract` 
to `TimestampDiff`, and enabled already ported test queries in 
`postgreSQL/timestamp.sql`.
   
   ### Why are the changes needed?
   To maintain feature parity with PostgreSQL which allows to get timestamp 
difference:
   ```sql
   # select timestamp'today' - timestamp'yesterday';
    ?column? 
   ----------
    1 day
   (1 row)
   ```
   
   ### Does this PR introduce any user-facing change?
   Yes, previously users got the following error from timestamp subtraction:
   ```sql
   spark-sql> select timestamp'today' - timestamp'yesterday';
   Error in query: cannot resolve '(TIMESTAMP('2019-10-04 00:00:00') - 
TIMESTAMP('2019-10-03 00:00:00'))' due to data type mismatch: 
'(TIMESTAMP('2019-10-04 00:00:00') - TIMESTAMP('2019-10-03 00:00:00'))' 
requires (numeric or interval) type, not timestamp; line 1 pos 7;
   'Project [unresolvedalias((1570136400000000 - 1570050000000000), None)]
   +- OneRowRelation
   ``` 
   after the changes they should get an interval:
   ```sql
   spark-sql> select timestamp'today' - timestamp'yesterday';
   interval 1 days
   ```
   
   
   ### How was this patch tested?
   - Added tests for `TimestampDiff` to `DateExpressionsSuite`
   - By new test in `TypeCoercionSuite`.
   - enable tests in `postgreSQL/timestamp.sql`.
   

----------------------------------------------------------------
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]


With regards,
Apache Git Services

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

Reply via email to