-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
inconsistent data / type when using json subquery #1876
Comments
It's not a bug as the data type is JSON and it's overall type parser handles that as the data type. If you have more specific requirements for parsing the nested attributes with a more specific data type you would need to do that in your application code or add your own custom parser. A generalized "parse JSON as native types" is not possible as JSON does not include type information for the original source data. There's no way to tell differentiate between |
Yes, "generally" I agree with you. I am currently writing my own application logic to connect (coherently) databases in different time zones and I discovered "this thing", looking in |
As @sehrope explained for the timestamp: when you call So what you are comparing is a string generated by the database, and a JavaScript Date object stringified by node.js. And of course, JavaScript's So in regards to timestamps, I believe the driver does the right thing, and I'm not sure what should be documented, as it doesn't do anything unexpected. There are two ways to make the result uniform: either convert the timestamp columns to text in the query: The |
Thanks @boromisp for such a detailed explanation !!! I started with problems with the date, but now I see that this one can be quite simply solved - excluding the parser in my case. Of course, this is not a bug, but rather my ignorance of the "specifics". By default, I assumed that parsers work on all data, and after a long time I only came to the json subqueries that were stitched somewhere inside the application. But the matter of representation of numerical values is more serious (from what I have read in The idea behind this issue - just like in the title - is just to point out that the data may be inconsistent with the rest in this case and it is worth bearing in mind. Sample with number 2**53 + 1 SELECT
'9007199254740993'::numeric,
(SELECT row_to_json(r) FROM (
SELECT
'9007199254740993'::numeric
) r ); {
"numeric": "9007199254740993",
"row_to_json": {
"numeric": 9007199254740992
}
} |
UPDATE – expanding Title – & when working with Timezones and expanding Experiment with (postgresql.conf) and query: SELECT
current_setting('TIMEZONE') AS "TZ_current",
now() AS "now_______",
now()::timestamptz AS "now_tstz__",
now()::timestamp AS "now_ts____",
now() AT TIME ZONE 'Europe/Warsaw' AS "now_WAW___",
now() AT TIME ZONE 'UTC' AS "now_UTC___",
(SELECT row_to_json(r) FROM (
SELECT
now() AS "now_______",
now()::timestamptz AS "now_tstz__",
now()::timestamp AS "now_ts____",
now() AT TIME ZONE 'Europe/Warsaw' AS "now_WAW___",
now() AT TIME ZONE 'UTC' AS "now_UTC___"
) r ); the results look like this: console.log(process.env.TZ); //: (ok) Europe/Warsaw
console.log(new Date()); //: 2019-04-14T12:38:47.301Z (ok) UTC
console.log(new Date().getTimezoneOffset()/60): //: (ok) -2
{
"TZ_current": "Europe/Warsaw",
"now_______": "2019-04-14T12:38:47.301Z",
"now_tstz__": "2019-04-14T12:38:47.301Z",
"now_ts____": "2019-04-14T12:38:47.301Z",
"now_WAW___": "2019-04-14T12:38:47.301Z",
"now_UTC___": "2019-04-14T10:38:47.301Z",
"row_to_json": {
"now_______": "2019-04-14T14:38:47.301076+02:00",
"now_tstz__": "2019-04-14T14:38:47.301076+02:00",
"now_ts____": "2019-04-14T14:38:47.301076",
"now_WAW___": "2019-04-14T14:38:47.301076",
"now_UTC___": "2019-04-14T12:38:47.301076"
}
}
types.setTypeParser(1114, (s) => s); //: timestamp
types.setTypeParser(1184, (s) => s); //: timestamptz
{
"TZ_current": "Europe/Warsaw",
"now_______": "2019-04-14 14:39:35.911219+02",
"now_tstz__": "2019-04-14 14:39:35.911219+02",
"now_ts____": "2019-04-14 14:39:35.911219",
"now_WAW___": "2019-04-14 14:39:35.911219",
"now_UTC___": "2019-04-14 12:39:35.911219",
"row_to_json": {
"now_______": "2019-04-14T14:39:35.911219+02:00",
"now_tstz__": "2019-04-14T14:39:35.911219+02:00",
"now_ts____": "2019-04-14T14:39:35.911219",
"now_WAW___": "2019-04-14T14:39:35.911219",
"now_UTC___": "2019-04-14T12:39:35.911219"
}
} and for me this is a Problem – @boromisp – what do you think? |
The way I see it, the local time zone setting of both postgresql and nodejs is only supposed to make the string representation of utc timestamps more easily readable by humans. Of course there could be use cases I haven't considered. Would you mind expanding on yours? |
Can you please explain how did you solve it? |
It looks like
pg-types
(parsers) are not used inside json objects, resulting in inconsistent data. It could be considered a bug, or at least it's worth some note in the documentation. Sample code and result below:The text was updated successfully, but these errors were encountered: