-
-
Notifications
You must be signed in to change notification settings - Fork 22
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
Boolean created by comparing two not null timestamps is incorrectly nullable. #199
Comments
I also get this problem with a |
Good catch. As a workaround for now, you could wrap the condition with |
I can confirm that this happens, also for // 💥 Query has incorrect type annotation.
// Expected: { ... is_mammal: boolean; }
// Actual: { ... is_mammal: boolean | null; }[]
await sql<{ id: number, first_name: string, is_mammal: boolean }[]>`
SELECT
animals.id,
animals.first_name,
mammals.id IS NOT NULL AS is_mammal
FROM
animals
LEFT JOIN mammals ON animals.id = mammals.animal_id
`; |
WorkaroundThe await sql<{ id: number, first_name: string, is_mammal: boolean }[]>`
SELECT
animals.id,
animals.first_name,
- mammals.id IS NOT NULL AS is_mammal
+ -- coalesce() workaround for SafeQL boolean
+ -- https://github.com/ts-safeql/safeql/issues/199#issuecomment-2356407115
+ coalesce(mammals.id IS NOT NULL)::bool AS is_mammal
FROM
animals
LEFT JOIN mammals ON animals.id = mammals.animal_id
`; However, the // 💥 Query has incorrect type annotation.
// Expected: { ... is_mammal: boolean; }
// Actual: { ... is_mammal: boolean | null; }[]
await sql<{ id: number, first_name: string, is_mammal: boolean }[]>`
SELECT
derived_table.*
FROM
(
SELECT
animals.id,
animals.first_name,
coalesce(mammals.id IS NOT NULL)::bool AS is_mammal
FROM
animals
LEFT JOIN mammals ON animals.id = mammals.animal_id
) AS derived_table
`; |
@Newbie012 would this change be a small special-casing change, similar to this commit here? cc @timvandam in case you'd like to try out a small PR |
Ok, it seems this is a very different case when it comes to the code. I've tried out my hand at a first PR, not sure if this makes sense like this: |
I'll try to open another PR today to fix expressions with comparison operators ( |
@karlhorky The above-mentioned example is a bit more complex since you can't tell whether it's nullable or not just by the AST. If the column reference is nullable, then |
Describe the bug
When comparing two not null timestamps, the result will never be null, but SafeQL interprets it at nullable.
To Reproduce
Steps to reproduce the behavior:
Create a table with a
NOT NULL
timestampz
column:Query that table, checking if the timestamp is in the past:
SafeQL now reports an error,
Expected behavior
SafeQL doesn't report an error.
Desktop (please complete the following information):
The text was updated successfully, but these errors were encountered: