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

Skip DB execution when all ids for @OptionalParent are nil #583

Merged
merged 2 commits into from
Mar 23, 2024

Conversation

mtj0928
Copy link
Contributor

@mtj0928 mtj0928 commented Aug 27, 2023

These changes are now available in 1.47.2

OptionalParentEagerLoader doesn't need to fetch To objects when all ids of the given models are nil, 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.

@mtj0928 mtj0928 requested a review from gwynne as a code owner August 27, 2023 03:41
@gwynne
Copy link
Member

gwynne commented Aug 27, 2023

This is already guaranteed by the QueryBuilder logic here: https://github.com/vapor/fluent-kit/blob/main/Sources/FluentKit/Query/Builder/QueryBuilder.swift#L234-L237

                // don't run eager loads if result set was empty
                guard !all.isEmpty else {
                    return self.database.eventLoop.makeSucceededFuture(())
                }

@gwynne gwynne closed this Aug 27, 2023
@mtj0928
Copy link
Contributor Author

mtj0928 commented Aug 27, 2023

Your comment is correct for many propertyWrappers, but not for @OptionalParent.
With OptionalParent, even if the provided models by the QueryBuilder have several elements, the set passed to the query can be empty.

@gwynne
Copy link
Member

gwynne commented Aug 27, 2023

With OptionalParent, even if the provided models by the QueryBuilder have several elements, the set passed to the query can be empty.

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 nilParentModels array to take effect (an admittedly confusing bit of code whose purpose is to ensure that the relation is treated as "loaded" with a nil value (as opposed to not loaded at all) for purposes of interoperating with Codable, not overwriting data during model updates, etc.

@mtj0928
Copy link
Contributor Author

mtj0928 commented Aug 29, 2023

I set .some(nil) for all objects when the database execution is skipped.
Isn't that enough?

https://github.com/mtj0928/fluent-kit/blob/021f7d59d970e524b4639f5844273cac1719455e/Sources/FluentKit/Properties/OptionalParent.swift#L178

@gwynne
Copy link
Member

gwynne commented Aug 29, 2023

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:

  • @OptionalChild (when referring to an @OptionalParent)
  • @Children (when referring to an @OptionalParent)
  • @CompositeOptionalParent
  • @CompositeOptionalChild (when referring to a @CompositeOptionalParent)
  • @CompositeChildren (when referring to a @CompositeOptionalParent)

@gwynne gwynne reopened this Aug 29, 2023
@gwynne gwynne added enhancement New feature or request semver-patch Internal changes only labels Aug 29, 2023
@gwynne gwynne requested a review from 0xTim August 29, 2023 19:12
@github-advanced-security
Copy link

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.

@mtj0928
Copy link
Contributor Author

mtj0928 commented Aug 31, 2023

I checked the mentioned propertyWrappers, but none of them seem to be able to predict the results of the database execution.
Therefore, I believe the current changes are sufficient.

The current logic of @CompositeOptionalParent seems to be similar to that of @OptionalParent.

    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
However, the code fetches all To objects when the sets is empty. I'm unsure if the current logic is correct, so please let me know if it needs changes.

@mtj0928
Copy link
Contributor Author

mtj0928 commented Sep 10, 2023

@gwynne Could you review this PR again?

@0xTim
Copy link
Member

0xTim commented Nov 24, 2023

@gwynne ping

@gwynne
Copy link
Member

gwynne commented Mar 21, 2024

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.

Copy link

codecov bot commented Mar 21, 2024

Codecov Report

Attention: Patch coverage is 0% with 5 lines in your changes are missing coverage. Please review.

Project coverage is 47.33%. Comparing base (bb47433) to head (0b5b857).

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     
Files Coverage Δ
Sources/FluentKit/Properties/OptionalParent.swift 28.39% <0.00%> (-1.87%) ⬇️

... and 1 file with indirect coverage changes

Copy link
Member

@0xTim 0xTim left a 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!

@mtj0928
Copy link
Contributor Author

mtj0928 commented Mar 22, 2024

Thank you for reviewing the PR again.

I investigated the unit tests for FluentKit and found that it already includes tests for OptionalParent here:

public func testOptionalParent() throws {
try runTest(#function, [
UserMigration()
]) {
// seed
do {
let swift = User(
name: "Swift",
pet: .init(name: "Foo", type: .dog),
bestFriend: nil
)
try swift.save(on: self.database).wait()
let vapor = User(
name: "Vapor",
pet: .init(name: "Bar", type: .cat),
bestFriend: swift
)
try vapor.save(on: self.database).wait()
}
// test fetch user with nil parent
do {
let swift = try User.query(on: self.database)
.filter(\.$name == "Swift")
.first().wait()!
try XCTAssertNil(swift.$bestFriend.get(on: self.database).wait())
}
// test fetch user with non-nil parent
do {
let swift = try User.query(on: self.database)
.filter(\.$name == "Vapor")
.first().wait()!
try XCTAssertNotNil(swift.$bestFriend.get(on: self.database).wait())
}
// test
let users = try User.query(on: self.database)
.with(\.$bestFriend)
.with(\.$friends)
.all().wait()
for user in users {
switch user.name {
case "Swift":
XCTAssertEqual(user.bestFriend?.name, nil)
XCTAssertEqual(user.friends.count, 1)
case "Vapor":
XCTAssertEqual(user.bestFriend?.name, "Swift")
XCTAssertEqual(user.friends.count, 0)
default:
XCTFail("unexpected name: \(user.name)")
}
}
// test query with no ids
// https://github.com/vapor/fluent-kit/issues/85
let users2 = try User.query(on: self.database)
.with(\.$bestFriend)
.filter(\.$bestFriend.$id == nil)
.all().wait()
XCTAssertEqual(users2.count, 1)
XCTAssert(users2.first?.bestFriend == nil)
// Test deleted OptionalParent
try User.query(on: self.database).filter(\.$name == "Swift").delete().wait()
let users3 = try User.query(on: self.database)
.with(\.$bestFriend, withDeleted: true)
.all().wait()
XCTAssertEqual(users3.first?.bestFriend?.name, "Swift")
XCTAssertThrowsError(try User.query(on: self.database)
.with(\.$bestFriend)
.all().wait()
) { error in
guard case let .missingParent(from, to, key, _) = error as? FluentError else {
return XCTFail("Unexpected error \(error) thrown")
}
XCTAssertEqual(from, "User")
XCTAssertEqual(to, "User")
XCTAssertEqual(key, "bf_id")
}
}
}
.

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.
However, it's a bit difficult to do so in the current design, because DummyDatabaseForTestSQLSerializer always returns a DummyRow, and we cannot control the row from outside.

I'm not familiar with this library, so please let me know if I miss something.
Thnak you!

@gwynne
Copy link
Member

gwynne commented Mar 23, 2024

Hm... yes, I believe that existing test suffices, thanks for looking into it!

@gwynne gwynne merged commit 5f0938a into vapor:main Mar 23, 2024
13 of 14 checks passed
@mtj0928
Copy link
Contributor Author

mtj0928 commented Mar 24, 2024

@gwynne @0xTim
The changes will help the performance of my app, thank you for responding to my PR!!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request semver-patch Internal changes only
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants