Skip to content
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

add a pggen.optional() directive #85

Open
breathe opened this issue Jun 15, 2023 · 5 comments
Open

add a pggen.optional() directive #85

breathe opened this issue Jun 15, 2023 · 5 comments

Comments

@breathe
Copy link

breathe commented Jun 15, 2023

It would be nice to be able to explicitly signal when an argument can be null.

I want to be able to explicitly use NULL values to turn off the effect of 'optional' where clauses for some queries

eg:
select 1 as a where (pggen.arg('foo')::bigint is NULL OR pggen.arg('foo')::bigint = a);

But the default inference for this query will infer a non-pointer type for 'foo' above ... It'd be great if I could instead write something like:

select 1 as a where (pggen.optional('foo')::bigint is NULL OR pggen.optional('foo')::bigint = a);

and be given a pointer type so that my go program can pass nil to the query and be certain that the value will be received as NULL by postgres.

@jschaf
Copy link
Owner

jschaf commented Jun 16, 2023

The problem is that inputs types are always assumed to be non-null:

goType, err := tm.resolver.Resolve(input.PgType /*nullable*/, false, pkgPath)

I probably can't change that decision without breaking existing code.

We use sentinel values as a stand-in for null with a few helper functions. This works pretty well with Go's default values. Here's what it looks like:

SELECT
  default_if_empty(pggen.arg('foo'), 'my_default_value')

If Foo is an empty string, it'll trigger the default value. Here's the function definition (overloaded with other types):

CREATE FUNCTION default_if_empty(s text, default_str text) RETURNS text AS $$
SELECT coalesce(nullif(s, ''), default_str)
$$ LANGUAGE sql IMMUTABLE PARALLEL SAFE;

@breathe
Copy link
Author

breathe commented Jun 30, 2023

Closing this - I'm gathering there is no interest to accept this

@breathe breathe closed this as completed Jun 30, 2023
@breathe breathe closed this as not planned Won't fix, can't repro, duplicate, stale Jun 30, 2023
@jschaf
Copy link
Owner

jschaf commented Jul 7, 2023

Hi there, I'm interested but life is a bit crazy (we just had our second kiddo). I like the idea of pggen.optional with maybe some tweaks to naming. Optional args fill a gap in pggen.

sqlc calls this sqlc.narg https://docs.sqlc.dev/en/stable/howto/named_parameters.html

@jschaf jschaf reopened this Jul 7, 2023
@breathe
Copy link
Author

breathe commented Jul 7, 2023

Congratulations on the baby! 🍼

When you have time to circle back around to this, feel free to drop your thoughts and I'm happy to update that PR as needed. :)

Cheers!

@DavidArchibald
Copy link

DavidArchibald commented Mar 22, 2024

I added this along with some other features over at mypricehealth@5ed3985.

I don't plan to keep this as a separate fork forever but I'd consider what I'm doing there pretty experimental and I need to discuss with jschaf how to get my changes merged in and which ones they like (once they're well rested of course!).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants