-
Notifications
You must be signed in to change notification settings - Fork 866
Min/Max on Datetimes result in interface{} as type #1574
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
Comments
Yeah I am experiencing this too. So then I changed it to cast the I then thought maybe I could use nulltime as a custom type but and I can't figure out how to use a custom type for custom queries like this. |
I have this problem too. I would like to get |
This problem reproduces not only for timestamp fields, but for int fields as well.
May be related to #1334 UPDATE I just built sqlc from the git to include all latest commits and this query still results into incorrect struct. min_col ends up being UPDATE 2
|
I think the problem lies in that definitions of But here this field is the only thing considered when we are inferring if the argument of the function is nullable or not https://github.com/kyleconroy/sqlc/blob/996a73a27144a625080326f7340a11f837bd94fe/internal/compiler/output_columns.go#L219 To solve this, I think we need to make so that for some built-in functions the output type should be inferred from their argument. UPDATE Here, we can parse function parameters using
The code above solves issue for group by queries using The problem is I am unsure for which functions we should apply such type inference and for which not. That will be dependent on the database and likely should be implemented somewhere at the catalog level. Also, aggregation functions may have array parameters, so those cases should be handled correctly too: https://www.postgresql.org/docs/9.5/functions-aggregate.html @kyleconroy , do you have an idea on how this issue should be handled? From the postgres doc it looks like all aggregation functions can return null, so maybe just mark all aggregation functions in catalogs for postgres, dolphin and sqlite? By that I mean to fill |
I'm not the maintainer, but I can take a look at this soon because I hit this issue in our production project. I believe a refactor of this portion of the codebase just happened, so the preferred solution may change. I was able to get around this for the time being by using:
And then just using |
@ryan-berger I think I have solved the issue in the linked pull request, but I need a bit more help from maintainers to fix tests |
@kdubovikov You are correct, it would just be sqlc-pg-gen that needs to change, and that the refactor didn't change much if Kyle goes with your other solution. I was thinking about the sqlc-pg-gen a bit more:
And I realized that The Postgres docs say:
Looking at @kyleconroy any thoughts on this? |
A few thoughts. We should manually tag the aggregate functions that can return null. I honestly do do not know that min and max would return null. As for type inference, it's a very large project to take on. |
I think it returns null if there are no rows in the set so:
|
I have summarized what has been discussed in this playground link: https://play.sqlc.dev/p/cbe160df51a07333f3b91b2e13d9b81a0293c3c84f451e2f312e553cdc4ff81d Bug 1 Workaround 1 Bug 2 Workaround 2 @kyleconroy I might use this opportunity to bring back up the "output nullability" idea I had a while ago, discussed here: I think this would allow developers to help the sqlc type inferrence / null-inference in complex cases. Here is an example of how it would look. Of course I agree that the type-inferrence, null-inferrence should be better, but bugs like this are showstoppers for many teams. CREATE TABLE activities (
account_id BIGINT NOT NULL,
event_time_nullable TIMESTAMP WITH TIME ZONE
);
-- name: Summary :one
SELECT
COUNT(*) as "NumOfActivities",
sqlc.nonnull(MIN(event_time_nullable)) as "MinDateNullable"
FROM activities
WHERE account_id = $1
AND event_time_nullable is not null
HAVING COUNT(*) > 0; @jose-zenledger @Neokil what do you think? |
I like your solution, I did figure out a workaround for bug 2 though. You have to use Query:
Then in
|
What is this As the title said, this PR wants to add support for CAST function in MySQL. This PR is based from PR by @ryanpbrewster here (which unfortunately he didn't send here, and only exist in his repository). Why is this PR created Currently sqlc unable to infer the correct type from SQL function like MAX, MIN, SUM, etc. For those function, sqlc will return its value as interface{}. This behavior can be seen in this playground. As workaround, it advised to use CAST function to explicitly tell what is the type for that column, as mentioned in #1574. Unfortunately, currently sqlc only support CAST function in PostgreSQL and not in MySQL. Thanks to this, right now MySQL users have to parse the interface{} manually, which is not really desirable. What does this PR do? Implement convertFuncCast function for MySQL. Add better nil pointer check in some functions that related to convertFuncCast. I haven't write any test because I'm not sure how and where to put it. However, as far as I know the code that handle ast.TypeCast for PostgreSQL also don't have any test, so I guess it's fine 🤷♂️ Related issues Support CAST ... AS #687, which currently is the oldest MySQL issue that still opened. Using MYSQL functions ( CONVERT and CAST) result in removing column from struct #1622 Unable to Type Alias #1866 sum in select result in model field type interface{} #1901 MIN() returns an interface{} #1965
* fix(compiler): Pull in array information from analyzer Fixes #1532 * test(analyzer): Add testcase for #1574 * test: Added test for #1634 * test: Add test case for #1646 * test: Add test for #1714 * Fixes #1912 * test: Add case for #1916 * test: Add two test cases #1917 #1545 * test: Add case for #1979 * test: Add case for #1990
This is fixed in v1.23.0 by enabling the database-backed query analyzer. We added a test case for this issue so it won’t break in the future. You can play around with the working example on the playground |
@kyleconroy please correct me if I'm wrong, but doesn't the fix only apply for Postgres? I'm having the same issue here with SQLite. |
I can reproduce the same issue with sqlite. I'm using a The above mentioned workaround worked for me though. |
Version
Other
What happened?
Hint: I am using v1.13.0 (installed via brew) but that is not available in the form.
I currently have a query that looks like this:
which creates a struct like this:
but I would expect
so it looks like after putting a date through an aggregate-function sqlc can no longer determine its type? but it seems only to be the case for datetimes as far as I can see it.
Relevant log output
No response
Database schema
SQL queries
Configuration
No response
Playground URL
No response
What operating system are you using?
macOS
What database engines are you using?
PostgreSQL
What type of code are you generating?
Go
The text was updated successfully, but these errors were encountered: