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: EXPOSED-217 Unnecessary query after calling with() and iteration #2017

Merged
merged 1 commit into from
Mar 4, 2024

Conversation

bog-walk
Copy link
Member

@bog-walk bog-walk commented Mar 1, 2024

Using all() to get a collection of entities returns an instance of an anonymous SizedIterable object.
When eager loading references from this object, with() first iterates over the object then preloads relations for each retrieved entity, which correctly generates 2 SQL statements:

val x: SizedIterable<School> = School.all().with(School::region)

// SELECT SCHOOL.ID, SCHOOL."name", SCHOOL.REGION_ID, SCHOOL.SECONDARY_REGION_ID FROM SCHOOL
// SELECT REGION.ID, REGION."name" FROM REGION WHERE REGION.ID = 1

If this eager loading is followed by iteration (using toList() or forEach() e.g.), the first SQL statement is executed again because the anonymous object delegates to the source iterator, AbstractQuery.iterator and its results (from the first execution) are discarded after being used to load references.
The retrieved references, however, are cached in each Entity.referenceCache, so the second SQL is not executed again:

val x: List<School> = School.all().with(School::region).toList()

// SELECT SCHOOL.ID, SCHOOL."name", SCHOOL.REGION_ID, SCHOOL.SECONDARY_REGION_ID FROM SCHOOL
// SELECT REGION.ID, REGION."name" FROM REGION WHERE REGION.ID = 1
// SELECT SCHOOL.ID, SCHOOL."name", SCHOOL.REGION_ID, SCHOOL.SECONDARY_REGION_ID FROM SCHOOL

This occurs because with() was refactored in PR #1585 to return the same invoking type, rather than a List. So the source iterator of the returned SizedIterable executes a query when called.

This fix stores the result of the first query in the SizedIterable instance and ensures the right iterator is invoked if a result has been cached.

When eager loading references from a SizedIterable of entities, with() first iterates
over the object then preloads relations, which correctly generates 2 SQL queries.

If this eager loading is followed by iteration, the first SQL query is executed again
because its results are discarded, while the retrieved references are cached.

This fix stores the result of the first query in the SizedIterable instance to
ensure that the right iterator is invoked.
@bog-walk bog-walk requested review from e5l and joc-a March 1, 2024 01:16
@bog-walk bog-walk linked an issue Mar 1, 2024 that may be closed by this pull request
Comment on lines +29 to +34
/** Represents the iterable elements of a database result, which are stored once loaded on first access. */
interface LazySizedIterable<T> : SizedIterable<T> {
/** The lazily loaded database result. */
var loadedResult: List<T>?
}

Copy link
Member Author

Choose a reason for hiding this comment

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

An alternative to this would be to add a function to the base SizedIterable interface, something like

fun cacheLoadedResult(result: List<*>) {}

that could be overriden in the anonymous object below to set a private variable.

This issue with that would be that cacheLoadedResult() would be available to users for every SizedIterable (which means every single query, as AbstractQuery implements it):

MyTable.selectAll().cacheLoadedResult()
// this would do nothing and I'm not sure what the expected behavior/value should/could be

Comment on lines +942 to +949
assertEquals(1, oneSchool.size)
// expected: 1 query to select all School, 1 query to select the referenced Regions,
// then 1 new query to select only first School
assertEquals(3, statementCount)
assertEquals(statementCount, statementStats.size)
statementStats.assertEachQueryExecutedOnlyOnce()

debug = false
Copy link
Member Author

Choose a reason for hiding this comment

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

The current set up of the anonymous SizedIterable is that it makes new copies of the invoking source iterable (without the cached result) when adjusting the underlying query with chained functions.
So even when a result has been cached, chaining functions afterwards changes the expected query and triggers a new SQL execution on iteration because loadedResult is reset to null.

If it turns out that this is not the expected behavior for users, loadedResult logic can be easily added to the overrides in the anonymous function.

@bog-walk bog-walk merged commit d5b298d into main Mar 4, 2024
5 checks passed
@bog-walk bog-walk deleted the bog-walk/fix-with-duplicate-query branch March 4, 2024 14:52
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.

Unnecessary query after calling with() and iteration.
3 participants