Open timvandam opened 5 months ago
Thanks for the detailed reproduction. I'll look into it.
Thanks for the detailed reproduction. I'll look into it.
Nice
I forgot to include the data in the issue, here are inserts for the data I used to test this:
insert into users (id, public_id, username, created_at, updated_at)
values (1, 'test', 'test', '2024-06-10 12:31:20.240877 +00:00', '2024-06-10 12:31:20.240877 +00:00');
insert into user_authentication_methods (id, user_id, name, data, created_at, updated_at)
values (1, 1, 'test1', '{"some_data": 123}', '2024-06-10 12:31:38.655757 +00:00', '2024-06-10 12:31:38.655757 +00:00'),
(2, 1, 'test2', '{"some_other_data": 321}', '2024-06-10 12:31:47.674517 +00:00', '2024-06-10 12:31:51.324642 +00:00');
I've been doing some debugging to see if I can fix this. I've ended up here, and realized that this inner type should be made nullable if the argument given to json_agg may be null (such as in the case of a LEFT JOIN in my case). I am checking this with isDescribedColumnNullable
, however this is not correct as I wan't to know whether the argument is nullable, while this applies to a DescribedColumn. Is there any way of doing this? E.g. I want to know whether x
may be null in the case of jsonb_agg(x)
I see that context.resolver.sources
contains some basic information, but it does not tell me whether tables/columns used within the query can be nullable. I'm not sure whether this is even possible.
Also, detecting whether the value in the final selection can be null (e.g. in the case above where I use GROUP BY) would require a more sophisticated way of constructing context.nonNullableColumns
. For instance in my case the GROUP BY pretty much filters out any rows where jsonb_agg would have returned null, so this would need to be detected. Also seems very challenging.
I think it would make most sense to assume that jsonb_agg can contain null values whenever its argument is not the table in the FROM clause. Not 100% accurate but close enough I think
This is somewhat related to https://github.com/ts-safeql/safeql/discussions/210
I have unstaged changes that attempts to solve this issue by introducing a new "group" node which can be "nullable" and contain "children". This is quite a fundamental change from the way I initially wrote the ast-describe part. Another required fundamental change would be to merge the non nullable columns logic with the descriptor, since sometimes it needs additional context. Once I have enough time keep working on it, I'll send some updates. In the meantime, feel free to explore this path.
That's a nice approach. I think this wouldn't fully solve the GROUP BY causing any potentially null results from being omitted, but it is probably not even needed since you can just COALESCE to get rid of that null type
Describe the bug A clear and concise description of what the bug is.
When using jsonb_agg safeql is suggesting an incorrect TS type.
To Reproduce Steps to reproduce the behavior:
DB setup:
Example of wrong type:
Expected behavior A clear and concise description of what you expected to happen.
I would expect json_agg to have the type
Screenshots If applicable, add screenshots to help explain your problem.
Desktop (please complete the following information):
Additional context Add any other context about the problem here.
Another bonus issue I found while playing around:
method is definitely not a number