babelfish-for-postgresql / babelfish_extensions

Babelfish for PostgreSQL provides the capability for PostgreSQL to work with applications written for Microsoft SQL Server. Babelfish understands the SQL Server wire-protocol and T-SQL, the Microsoft SQL Server query and procedural language, so you don’t have to switch database drivers or rewrite all of your application queries.
https://babelfishpg.org/
Apache License 2.0
265 stars 88 forks source link

Fix issues while CASTing string literal to datetime, datetime2 and datetimeoffset #2623

Closed basasairohan closed 4 weeks ago

basasairohan commented 1 month ago

Description

This PR addresses the behavioural differences in CASTing of string literals to datetime, datetime2 and datetimeoffset. The following changes have been made to fix the issue :

  1. Currently, we use native PG function named ParseDateTime for parsing the input string literals. This function does not handle some cases such as empty spaces between the fields. We have created a function named cleanup_input_str which will modify the input string to remove all the unnecessary spaces and then call the inbuilt ParseDateTime function.
  2. ParseDatetime splits the input string to different fields which then will be decoded in native function namedDecodeDateTime. But DecodeDatetime will not handle some cases such as accepting : as a separator for seconds and milliseconds. These additional cases were handled in a different function named tsql_decode_datetime_fields .
  3. Also, if there is a text month we check the REGEX of the input str so that it matches the T-SQL behaviour

Issues Resolved

BABEL-4328 Signed-off-by: Sai Rohan Basa bsrohan@amazon.com

Test Scenarios Covered

Check List

By submitting this pull request, I confirm that my contribution is under the terms of the Apache 2.0 and PostgreSQL licenses, and grant any person obtaining a copy of the contribution permission to relicense all or a portion of my contribution to the PostgreSQL License solely to contribute all or a portion of my contribution to the PostgreSQL open source project.

For more information on following Developer Certificate of Origin and signing off your commits, please check here.