-
Notifications
You must be signed in to change notification settings - Fork 1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Time data type precision difference between SQL and PostgreSQL #91
Comments
Datetime precision 6 is caused by the fact that T-SQL datetimes are stored as native Postgres datetimes. And the max precision Postgres supports is 6. Though this should not cause errors, as precision 7 is normally handled as it was specified as 6 in Babelfish. The overflow can be caused by datetime values outside of allowed range. I will recheck this. I also wonder whether you can collect a backtrace for this error? |
Yeah, I will check the log file with debug installer and let you know Alex |
Please disregard the previous comment, in this case this is actually a |
okay thanks |
The problem appeared to be happening on client side, inside the
For |
There seems to be a viable workaround on server side, for it to work the column needs to be created in T-SQL as To apply the workaround run the following SQL in PgAdmin in CREATE OR REPLACE FUNCTION sys.tsql_type_scale_helper(IN type TEXT, IN typemod INT, IN return_null_for_rest bool) RETURNS sys.TINYINT
AS $$
DECLARE
scale INT;
BEGIN
IF type IS NULL THEN
RETURN -1;
END IF;
IF typemod = -1 THEN
CASE type
WHEN 'date' THEN scale = 0;
WHEN 'datetime' THEN scale = 3;
WHEN 'smalldatetime' THEN scale = 0;
WHEN 'datetime2' THEN scale = 7;
WHEN 'datetimeoffset' THEN scale = 7;
WHEN 'decimal' THEN scale = 38;
WHEN 'numeric' THEN scale = 38;
WHEN 'money' THEN scale = 4;
WHEN 'smallmoney' THEN scale = 4;
WHEN 'time' THEN scale = 7;
WHEN 'tinyint' THEN scale = 0;
ELSE
IF return_null_for_rest
THEN scale = NULL;
ELSE scale = 0;
END IF;
END CASE;
RETURN scale;
END IF;
CASE type
WHEN 'decimal' THEN scale = (typemod - 4) & 65535;
WHEN 'numeric' THEN scale = (typemod - 4) & 65535;
WHEN 'smalldatetime' THEN scale = 0;
WHEN 'datetime2' THEN
CASE typemod
WHEN 0 THEN scale = 0;
WHEN 1 THEN scale = 1;
WHEN 2 THEN scale = 2;
WHEN 3 THEN scale = 3;
WHEN 4 THEN scale = 4;
WHEN 5 THEN scale = 5;
WHEN 6 THEN scale = 6;
-- typemod = 7 is not possible for datetime2 in Babelfish but
-- adding the case just in case we support it in future
WHEN 7 THEN scale = 7;
END CASE;
WHEN 'datetimeoffset' THEN
CASE typemod
WHEN 0 THEN scale = 0;
WHEN 1 THEN scale = 1;
WHEN 2 THEN scale = 2;
WHEN 3 THEN scale = 3;
WHEN 4 THEN scale = 4;
WHEN 5 THEN scale = 5;
WHEN 6 THEN scale = 6;
-- typemod = 7 is not possible for datetimeoffset in Babelfish
-- but adding the case just in case we support it in future
WHEN 7 THEN scale = 7;
END CASE;
WHEN 'time' THEN
CASE typemod
WHEN 0 THEN scale = 0;
WHEN 1 THEN scale = 1;
WHEN 2 THEN scale = 2;
WHEN 3 THEN scale = 3;
WHEN 4 THEN scale = 4;
WHEN 5 THEN scale = 5;
WHEN 6 THEN scale = 6;
-- typemod = 7 is not possible for time in Babelfish but
-- adding the case just in case we support it in future
WHEN 7 THEN scale = 7;
END CASE;
ELSE
IF return_null_for_rest
THEN scale = NULL;
ELSE scale = 0;
END IF;
END CASE;
RETURN scale;
END;
$$ LANGUAGE plpgsql IMMUTABLE STRICT; |
Thanks @staticlibs , Let me check |
Hi @staticlibs , You can try to recreate it with below query ` SET ANSI_NULLS ON |
Running the following on version 12.16.1: SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [AttendanceRegisterTest](
[TimeSignedIn] time(7) NOT NULL,
[ActualTimeSignedIn] time(7) NOT NULL,
[TimeSignedOut] time(7) NULL,
[ActualTimeSignedOut] time(7) NULL)
GO I can see the expected Confirming this in catalogs: select a.attname, a.atttypmod
from pg_catalog.pg_attribute a
join pg_catalog.pg_class c on c.oid = a.attrelid
where c.relname = 'attendanceregistertest'
|
Hi @staticlibs , Can you please give a try on the below attached whole query? |
Thanks for the details! Reproduced the problem, the following with unquoted type name in DDL creates a column with type modifier create table tab1(
col1 time(7)
) select a.attname, a.atttypmod
from pg_catalog.pg_attribute a
join pg_catalog.pg_class c on c.oid = a.attrelid
where c.relname = 'tab1'
and a.attname = 'col1'
But if the typename is quoted in T-SQL style, then the typemodifier is recorded as create table tab2(
col2 [time](7)
) select a.attname, a.atttypmod
from pg_catalog.pg_attribute a
join pg_catalog.pg_class c on c.oid = a.attrelid
where c.relname = 'tab2'
and a.attname = 'col2'
I see from where this discrepancy is coming, the |
The problem is fixed in update 13.17.1. |
Thanks @staticlibs, Does the above mentioned function sys.tsql_type_scale_helper is still needed? If yes, does this included in the mentioned release 13.17.1 ? If it is handled in babelfish, can you share me the upstream babelfish pull request link for us to have discussion with AWS too. |
Yes, it is included in ALTER EXTENSION "babelfishpg_tsql" UPDATE TO '3.3.3';
Upstream GH issues - #3120, #3128. There are no PRs for them, the patch for the former is provided there inline. The WiltonDB fix for the latter (engine, extension) is not upstreamable, it is too narrow, more wide fix needs to be developed inside the parser to handle |
Hi @staticlibs ,
Good Day!
We are facing a error during import that 'Datetime field overflow'. When I further investigate, I could see that time data type precision limit in SQL has 7 and postgreSQL equivalent table has 6.
Any idea on how to fix this issue?
Error during import
SQL
![image](https://private-user-images.githubusercontent.com/89133439/386085770-85cef686-147c-490f-ad5e-b4c241768031.png?jwt=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJnaXRodWIuY29tIiwiYXVkIjoicmF3LmdpdGh1YnVzZXJjb250ZW50LmNvbSIsImtleSI6ImtleTUiLCJleHAiOjE3Mzk0Njg1NDEsIm5iZiI6MTczOTQ2ODI0MSwicGF0aCI6Ii84OTEzMzQzOS8zODYwODU3NzAtODVjZWY2ODYtMTQ3Yy00OTBmLWFkNWUtYjRjMjQxNzY4MDMxLnBuZz9YLUFtei1BbGdvcml0aG09QVdTNC1ITUFDLVNIQTI1NiZYLUFtei1DcmVkZW50aWFsPUFLSUFWQ09EWUxTQTUzUFFLNFpBJTJGMjAyNTAyMTMlMkZ1cy1lYXN0LTElMkZzMyUyRmF3czRfcmVxdWVzdCZYLUFtei1EYXRlPTIwMjUwMjEzVDE3MzcyMVomWC1BbXotRXhwaXJlcz0zMDAmWC1BbXotU2lnbmF0dXJlPTllYWYzZTRmMjA5NGYxMGE1ODQxYWQ3OTQ1YzBjZWU5OWIyOGVhYTE5YTYxNjIwMGYyZmRjNzZlY2NkZWE3NmQmWC1BbXotU2lnbmVkSGVhZGVycz1ob3N0In0.AyzHVi8GJSkT8PeFPUHVobYqsBxs7XjLsSxPX3wkym8)
PostgreSQL
![image](https://private-user-images.githubusercontent.com/89133439/386085696-50c84ffd-0544-494b-bc1b-46693e3dd02f.png?jwt=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJnaXRodWIuY29tIiwiYXVkIjoicmF3LmdpdGh1YnVzZXJjb250ZW50LmNvbSIsImtleSI6ImtleTUiLCJleHAiOjE3Mzk0Njg1NDEsIm5iZiI6MTczOTQ2ODI0MSwicGF0aCI6Ii84OTEzMzQzOS8zODYwODU2OTYtNTBjODRmZmQtMDU0NC00OTRiLWJjMWItNDY2OTNlM2RkMDJmLnBuZz9YLUFtei1BbGdvcml0aG09QVdTNC1ITUFDLVNIQTI1NiZYLUFtei1DcmVkZW50aWFsPUFLSUFWQ09EWUxTQTUzUFFLNFpBJTJGMjAyNTAyMTMlMkZ1cy1lYXN0LTElMkZzMyUyRmF3czRfcmVxdWVzdCZYLUFtei1EYXRlPTIwMjUwMjEzVDE3MzcyMVomWC1BbXotRXhwaXJlcz0zMDAmWC1BbXotU2lnbmF0dXJlPTcxNGI5MDI0NjQ2NDY4YWRiNzE5MzA4MDMyNTgzM2FkNTNmN2ZiMTRiNWFjOTU0OTk4OTQyYWY5ZGI2ZTc4N2ImWC1BbXotU2lnbmVkSGVhZGVycz1ob3N0In0.cwusmDTHmlwlcFJH9mtlfZlwEy4M6PgRbDsy76vrb2Q)
The text was updated successfully, but these errors were encountered: