NASA-AMMOS / aerie

A software framework for modeling spacecraft.
https://nasa-ammos.github.io/aerie-docs/
MIT License
73 stars 19 forks source link

Remove unnecessary cast from `hasura.get_event_logs` #1477

Closed Mythicaeda closed 5 months ago

Mythicaeda commented 5 months ago

Description

Grabbing the message field from errors as a json rather than as text that is then converted back to jsonb is more efficient, more straightforward, and avoids unnecessary errors when message is not a valid json.

Additionally, the type of errors in the function has been updated from jsonb to json to reflect the type that Hasura stores the underlying field as.

Verification

Query was tested against the aerie_dev database, which contains mission model logs where message is not a valid json.

mattdailis commented 5 months ago

and avoids unnecessary errors when message is not a valid json

Should I take this to mean that json is allowed to be any text, while jsonb must be valid json?

Mythicaeda commented 5 months ago

Should I take this to mean that json is allowed to be any text, while jsonb must be valid json?

No, it's the active cast that's the issue, and this issue would occur regardless of if we were casting to jsonb or json.

When you do -> it comes out as "JSON object field" according to the docs. I'm not sure exactly what Postgres is doing under the hood, but a "JSON object field" is flexible in definition (ie it's allowed to be a string or an int or a nested JSON Object, etc). However, when you actively cast to json via ::json, Postgres is looking for either null or something like '{}' or '5' or 'true' or '"a"'. Since '' is none of these, the cast throws an exception rather than converting to the JSON Object version of the string ''.