Uh oh!
There was an error while loading. Please reload this page.
Ability to return an iterator on rows - #3631
Conversation
kyleconroy
commented
Oct 7, 2024
Thank you so much for the first pass on iterator support. Could you update your PR description with an example of how to use the |
kyleconroy
commented
Oct 7, 2024
Fixes #720 |
viewsharp
commented
Oct 7, 2024
@kyleconroy |
viewsharp
commented
Oct 7, 2024
@kyleconroy |
viewsharp
commented
Oct 7, 2024
I found some example tests here https://github.com/sqlc-dev/sqlc/tree/main/examples and added iterator tests there |
f18de44 to
b6ce289Compareviewsharp
commented
Oct 19, 2024
@kyleconroy, hi |
PatrLind
commented
Nov 5, 2024
I see that the stdlib sometimes uses the suffix Seq() to indicate methods that returns iterators. I suggest to think a bit more if Iterate() is the appropriate name for this functionality. |
gbarr
commented
Nov 8, 2024
|
gbarr
commented
Nov 8, 2024
I do not think the |
viewsharp
commented
Nov 9, 2024
@gbarr, thanks for your comment
|
Instead of having an IterCities(ctxcontext.Context) (seq iter.Seq2[City, error], stopfunc())Usage would be: funcexample(ctx context.Context, q*Queries) error {
rows, stop:=q.IterCities(ctx)
deferstop()
forcity, err:=rangerows {
iferr!=nil {
returnerr
}
// do something with city
}
returnnil
}I think this is a bit cleaner than the above generated code, saving a whole new type for the iterator. |
viewsharp
commented
Nov 19, 2024
I thought about such an interface, but I'm afraid that if the user uses range with one variable, the behavior will not match the expected one |
I found these in the release notes for Go 1.24. The bytes package adds several functions that work with iterators:
I don't like the |
viewsharp
commented
Nov 26, 2024
Following this example, it seems more appropriate to rename Iterate() to Rows() / Items() / Values(). Because the Seq suffix is used when there was already a Foo() or FooBar() function, and we want a similar FooSec() function that returns a sequence |
gbarr
commented
Nov 26, 2024
I agree that the My preference would be for |
viewsharp
commented
Nov 26, 2024
I would prefer Items(). |
benjaco
commented
Dec 1, 2024
It's great to see iterators coming to sqlc. Adding memory pooling as an option on top of this would make this truly a game-changer for large result sets. I would imagine saving the pool in a public variable, and let the callside have the responsibility to put the object back. Thoughts? Maybe out of scope for this one? |
MrBanja
commented
Dec 20, 2024
I'm up for this one. The only thing that blocks us from migrating from pure SQLx is the absence of an iterator. |
Not to blow up our existing design, but I just read this post (https://blog.thibaut-rousseau.com/blog/writing-testing-a-paginated-api-iterator/) which makes me think we may want to consider the following: funcexample(ctx context.Context, q*Queries) {
forcity, err:=rangerows.IterCities(ctx) {
// do something with city
}
}The upside to this design is that it's difficult to use incorrectly. We don't have to decide on a name for the |
gbarr
commented
Dec 20, 2024
The problem with using Seq2 that returns err is that if the code |
Just to add my 2cts to the discussion: it seems weird to me to have the error, and have to check it, in every loop iteration. That’s an advantage of the initial proposal. Since we can start the query in the first iteration, and break inside the loop, we don’t need Close. We can use Err() to determine if the query or any of the row fetches failed. I personally prefer: Over: Simply because of the lack of err clutter inside the loop. This does assume that:
|
PatrLind
commented
Feb 13, 2025
After an error happens in the iterator, if it is never possible to continue it would make more sense to use Also, would it make sense to stop iterating and then continue iterating later? |
pierrre
commented
Apr 3, 2025
I think we should instead have a function that returns:
Code: funcexample(ctx context.Context, q*Queries) error {
cities, getCitiesErr:=q.IterCities(ctx)
forcity:=rangecities { // do something with city
}
returngetCitiesErr()
}
I prefer this design, because it solves several problems:
|
pierrre
commented
Aug 8, 2025
@viewsharp did you close this PR intentionally ? |
viewsharp
commented
Aug 8, 2025
I apologize. I wanted to sync my repository and didn't know that it could lead to closing the PR. I will restore it soon |
@viewsharp are you planning to proceed with the work on this iterator any time soon? Or at least restore the PR changes. |
Example of generated query:
Example of use:
UPD: updated the code according to the latest changes