-
-
Notifications
You must be signed in to change notification settings - Fork 116
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
Skip DB execution when all ids for @OptionalParent are nil #583
Conversation
This is already guaranteed by the // don't run eager loads if result set was empty
guard !all.isEmpty else {
return self.database.eventLoop.makeSucceededFuture(())
} |
Your comment is correct for many propertyWrappers, but not for |
Huh, so it can - I stand corrected! However, even in this case we still need to proceed through the remainder of the method in order for the logic around the |
I set |
I humbly stand corrected a second time; for some reason I missed that line when I looked at your changes before 🤦♀️. My apologies for the confusion! I've reopened this PR, and my only request for it is that you apply the same logic to the other optional relations:
|
This pull request sets up GitHub code scanning for this repository. Once the scans have completed and the checks have passed, the analysis results for this pull request branch will appear on this overview. Once you merge this pull request, the 'Security' tab will show more code scanning analysis results (for example, for the default branch). Depending on your configuration and choice of analysis tool, future pull requests will be annotated with code scanning analysis results. For more information about GitHub code scanning, check out the documentation. |
I checked the mentioned propertyWrappers, but none of them seem to be able to predict the results of the database execution. The current logic of func run(models: [From], on database: Database) -> EventLoopFuture<Void> {
var sets = Dictionary(grouping: models, by: { $0[keyPath: self.relationKey].id })
let nilParentModels = sets.removeValue(forKey: nil) ?? []
let builder = To.query(on: database)
.group(.or) { _ = sets.keys.reduce($0) { query, id in query.group(.and) { id!.input(to: QueryFilterInput(builder: $0)) } } }
if (self.withDeleted) {
builder.withDeleted()
}
return builder.all().flatMapThrowing {
let parents = Dictionary(uniqueKeysWithValues: $0.map { ($0.id!, $0) })
for (parentId, models) in sets {
guard let parent = parents[parentId!] else {
database.logger.debug(
"Missing parent model in eager-load lookup results.",
metadata: ["parent": "\(To.self)", "id": "\(parentId!)"]
)
throw FluentError.missingParentError(keyPath: self.relationKey, id: parentId!)
}
models.forEach { $0[keyPath: self.relationKey].value = .some(.some(parent)) }
}
nilParentModels.forEach { $0[keyPath: self.relationKey].value = .some(.none) }
}
} https://github.com/vapor/fluent-kit/blob/ccea9820fe31076f994f7c1c1d584009cad6bdb2/Sources/FluentKit/Properties/CompositeOptionalParent.swift#L219C1-L243 |
@gwynne Could you review this PR again? |
@gwynne ping |
Okay, this looks correct to me as it is now, and my apologies for the long delay in re-review! The only thing missing is a test of the behavior. |
Codecov ReportAttention: Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #583 +/- ##
==========================================
- Coverage 47.39% 47.33% -0.07%
==========================================
Files 106 106
Lines 4756 4758 +2
==========================================
- Hits 2254 2252 -2
- Misses 2502 2506 +4
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you add a test for the change? Thanks!
Thank you for reviewing the PR again. I investigated the unit tests for FluentKit and found that it already includes tests for
The test covers both the nil case and the non-nil case, which I believe to be sufficient. I understand the ideal approach is to verify that database execution is skipped, and I attempted to write a new test case to confirm that the DB execution is not run when the value is nil. I'm not familiar with this library, so please let me know if I miss something. |
Hm... yes, I believe that existing test suffices, thanks for looking into it! |
These changes are now available in 1.47.2
OptionalParentEagerLoader
doesn't need to fetchTo
objects when all ids of the given models arenil
, because the results of the DB execution will always be empty.Therefore, I've modified the logic to skip the DB execution in such cases to improve performance.