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

fix: FindInBatches with offset limit #5255

Merged
merged 3 commits into from Apr 17, 2022
Merged

Conversation

a631807682
Copy link
Member

  • Do only one thing
  • Non breaking API changes
  • Tested

What did this pull request do?

I think when the user uses Offset in FindInBatches the meaning is to skip the previous N records, and user uses LIMIT in FindInBatches the meaning is find total of N records.

close #5252

User Case Description

}

// reset to offset to 0 in next batch
tx = tx.Offset(-1).Session(&Session{})
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we extract this call to another check? The offset is also buggy when using w/o limit.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we extract this call to another check? The offset is also buggy when using w/o limit.

What does offset is also buggy mean ? FindInBatches query condition is greater than primary key and limit.

Copy link

@lxdlam lxdlam Apr 14, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we extract this call to another check? The offset is also buggy when using w/o limit.

What does offset is also buggy mean ? FindInBatches query condition is greater than primary key and limit.

If we only using offset, which scenario will be like select all users which uid is larger than 100, then using dbInstance.Offset(100).OrderBy("user_id").FindInBatches(), it will skip 100 records in each of all subsequent queries.

The corresponding test is TestOffsetClausedFindInBatches in go-gorm/playground#466.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it will skip 100 records in first sub query.
here is reset offset to zero after first sub query that we can use id > ? and limit = ? to query.

Copy link

@lxdlam lxdlam Apr 14, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it will skip 100 records in first sub query. here is reset offset to zero after first sub query that we can use id > ? and limit = ? to query.

That's correct. But this call has been embedded in the limit check. I mean, we may need to extract it to another check that whether we have a Offset clause and the argument is large than 0 so we can both support only using Offset or togethered with Limit clause.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Offest and Limit use the same clause.Limit, so it support for only using Offset or togethered
https://github.com/go-gorm/gorm/blob/master/chainable_api.go#L245

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Only Offset cannot be used is caused by here, thank you.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Only Offset cannot be used is caused by here, thank you.

Got it. You're nice :).

@jinzhu jinzhu merged commit b49ae84 into go-gorm:master Apr 17, 2022
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

Successfully merging this pull request may close these issues.

FindInBatches in an unexpected manner with limit or offset in original query
3 participants