Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Update postgresql_type.go - check notNull and emitPointersForNull before SQLDriver #3839

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

Open
theAnuragMishra wants to merge 1 commit into sqlc-dev:main
base: main
Choose a base branch
Loading
from theAnuragMishra:patch-1

Conversation

Copy link
Contributor

@theAnuragMishra theAnuragMishra commented Feb 12, 2025

this fixes the issue where despite having emit_pointers_for_null_types set to true, the generate models don't use pointers for timestamp field.

erlangparasu reacted with thumbs up emoji
...ore SQLDriver
this fixes the issue where despite having emit_pointers_for_null_types set to true, the generate models don't use pointers for timestamp field.
@dosubot dosubot bot added size:S This PR changes 10-29 lines, ignoring generated files. 🔧 golang labels Feb 12, 2025
Copy link

benjaco commented Feb 14, 2025

Linked to issue #3837

I can see there is a lot of tests there fails, the question should then be if the new output is the desired or not? Is it to big of a breaking change? should it be behind a new flag?

Copy link

benjaco commented Feb 17, 2025

@theAnuragMishra maybe you could update the tests so it's easier to see the impact of this change? It needs to be updated anyway for it to get merged

Copy link

acartine commented Apr 7, 2025

is there a way i can commandeer this? Seems author might be tied up?

Copy link

benjaco commented Apr 8, 2025

@acartine I think you'd be more than welcome to give it a go in a new PR

Copy link

acartine commented Apr 9, 2025

@acartine I think you'd be more than welcome to give it a go in a new PR

ok i'll give it a shit. i'm on gmt+1 but hopefully it won't take too long

Copy link

really proud of my typo above, first class

Copy link

@benjaco from looking at the code more closely it appears there is an unclear configuration hierarchy

if you look at the first few types in

case "serial", "serial4", "pg_catalog.serial4":
if notNull {
return "int32"
}
if emitPointersForNull {
return "*int32"
}
if driver == opts.SQLDriverPGXV5 {
return "pgtype.Int4"
}
return "sql.NullInt32"
case "bigserial", "serial8", "pg_catalog.serial8":
if notNull {
return "int64"
}
if emitPointersForNull {
return "*int64"
}
if driver == opts.SQLDriverPGXV5 {
return "pgtype.Int8"
}
return "sql.NullInt64"
case "smallserial", "serial2", "pg_catalog.serial2":
if notNull {
return "int16"
}
if emitPointersForNull {
return "*int16"
}
if driver == opts.SQLDriverPGXV5 {
return "pgtype.Int2"
}
return "sql.NullInt16"
case "integer", "int", "int4", "pg_catalog.int4":
if notNull {
return "int32"
}
if emitPointersForNull {
return "*int32"
}
if driver == opts.SQLDriverPGXV5 {
return "pgtype.Int4"
}
return "sql.NullInt32"
case "bigint", "int8", "pg_catalog.int8":
if notNull {
return "int64"
}
if emitPointersForNull {
return "*int64"
}
if driver == opts.SQLDriverPGXV5 {
return "pgtype.Int8"
}
return "sql.NullInt64"
case "smallint", "int2", "pg_catalog.int2":
if notNull {
return "int16"
}
if emitPointersForNull {
return "*int16"
}
if driver == opts.SQLDriverPGXV5 {
return "pgtype.Int2"
}
return "sql.NullInt16"
case "float", "double precision", "float8", "pg_catalog.float8":
if notNull {
return "float64"
}
if emitPointersForNull {
return "*float64"
}
if driver == opts.SQLDriverPGXV5 {
return "pgtype.Float8"
}
return "sql.NullFloat64"
case "real", "float4", "pg_catalog.float4":
if notNull {
return "float32"
}
if emitPointersForNull {
return "*float32"
}
if driver == opts.SQLDriverPGXV5 {
return "pgtype.Float4"
}
return "sql.NullFloat64" // TODO: Change to sql.NullFloat32 after updating the go.mod file

the go types and emit-pointers-on-null supercede pgxv5

then for numerics/dates/times/uuid etc. that hierarchy is flipped.

this leads to a confusing devx because sometimes you get the go types / pointers and sometimes you get the pgtypes, which are more cumbersome to work with. the latter reason is probably why we try to use go types for nonnulls regardless of whether or not it is pgxv5 for the literal types.

i would think we need to draw some kind of line in the sand here and say there are two modes

  1. strict pgtypes (no null config, since pgtypes support null)
  2. go types with either pointers for null or sql.null. I would think just pointers because if you want values just use mode 1. keep it simple

is there a discord/slack for broader discussion about this?

erlangparasu reacted with thumbs up emoji

Copy link

benjaco commented Apr 10, 2025
edited
Loading

@acartine Issnt that what "emit_pointers_for_null_types" is suppose to configure?

Docs

  • emit_pointers_for_null_types:
    • If true, generated types for nullable columns are emitted as pointers (ie. *string) instead of database/sql null types (ie. NullString). Currently only supported for PostgreSQL if sql_package is pgx/v4 or pgx/v5, and for SQLite. Defaults to false.

So if emit_pointers_for_null_types is set to true, then the emitPointersForNull check should always come before the database type check in the typegen code.

There is no broader discussion, its all in the issue #3837 and in the pr's linked around that issue

erlangparasu reacted with thumbs up emoji

Copy link

i think there is a bit more clarification need. would you prefer i do it on that issue instead of this one?

in that issue you use the text/varchar example. And that's fine if that is the expected behavior.

however it should be noted that that style means that that the pgtype is used only if the type is nullable and !emitPointersForNull. It will never be used if the type is notNull.

that is a reasonably big change that will break any code that upgrades and does not use emitPointersForNull.

but if that's how you want me to implement it i'm happy to do it that way.

while we are at it, i can't seem to find a command to update the expected outputs, should I assume then that it is manual and not auto generated?

Copy link

benjaco commented Apr 10, 2025
edited
Loading

@acartine lets move the discussion to the issue
but to sum up short - i dont know the mindset balancing breaking changes and configs, we need some attention from Kyle maybe, but a summery of the different solutions would properly help him here - and i dont know the internal tooling of this project, I'm a first time contributer as well

Copy link

erlangparasu commented May 20, 2025
edited
Loading

agree. emitPointersForNull must be checks first before pgx checks

or using this workaround: #3837 (comment)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Reviewers
No reviews
Assignees
No one assigned
Labels
size:S This PR changes 10-29 lines, ignoring generated files. 🔧 golang
Projects
None yet
Milestone
No milestone
Development

Successfully merging this pull request may close these issues.

AltStyle によって変換されたページ (->オリジナル) /