Skip to content

Avoid fetching full entity rows in lookupEntityVersions#5038

Merged
dimas-b merged 1 commit into
apache:mainfrom
ayushtkn:lookupEntityVersions
Jul 15, 2026
Merged

Avoid fetching full entity rows in lookupEntityVersions#5038
dimas-b merged 1 commit into
apache:mainfrom
ayushtkn:lookupEntityVersions

Conversation

@ayushtkn

Copy link
Copy Markdown
Member

lookupEntityVersions is called by Resolver.bulkValidate on every JDBC-backed request that uses the entity cache, but it previously routed through lookupEntities and fetched all entity columns—including large JSON properties and internal_properties blobs—just to read entity_version and grant_records_version. This change adds a narrow VERSION_COLUMNS projection (id, catalog_id, entity_version, grant_records_version), a shared QueryGenerator helper, and an EntityVersionConverter so lookupEntityVersions issues a single lightweight SELECT instead.

Checklist

  • 🛡️ Don't disclose security issues! (contact security@apache.org)
  • 🔗 Clearly explained why the changes are needed, or linked related issues: Fixes #
  • 🧪 Added/updated tests with good coverage, or manually tested (and explained how)
  • 💡 Added comments for complex logic
  • 🧾 Updated CHANGELOG.md (if needed)
  • 📚 Updated documentation in site/content/in-dev/unreleased (if needed)

Copilot AI review requested due to automatic review settings July 11, 2026 06:10
@github-project-automation github-project-automation Bot moved this to PRs In Progress in Basic Kanban Board Jul 11, 2026

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

This PR optimizes the JDBC persistence hot path for cache validation by making lookupEntityVersions query only the version-related columns, avoiding fetching large JSON properties / internal_properties blobs when they aren’t needed.

Changes:

  • Added a ModelEntity.VERSION_COLUMNS projection and a QueryGenerator helper to generate a version-only SELECT.
  • Updated JdbcBasePersistenceImpl.lookupEntityVersions to use the new version-only query plus a dedicated EntityVersionConverter.
  • Added QueryGeneratorTest coverage for the new version-only query generation.

Reviewed changes

Copilot reviewed 5 out of 5 changed files in this pull request and generated 1 comment.

Show a summary per file
File Description
persistence/relational-jdbc/src/test/java/org/apache/polaris/persistence/relational/jdbc/QueryGeneratorTest.java Adds unit tests asserting the new version-only select query and that property columns aren’t selected.
persistence/relational-jdbc/src/main/java/org/apache/polaris/persistence/relational/jdbc/QueryGenerator.java Refactors entity-id select generation and introduces generateSelectQueryWithEntityIdsVersionOnly.
persistence/relational-jdbc/src/main/java/org/apache/polaris/persistence/relational/jdbc/models/ModelEntity.java Introduces VERSION_COLUMNS as a minimal column projection for version checks.
persistence/relational-jdbc/src/main/java/org/apache/polaris/persistence/relational/jdbc/models/EntityVersionConverter.java Adds a read-only ResultSet converter for the version-only projection.
persistence/relational-jdbc/src/main/java/org/apache/polaris/persistence/relational/jdbc/JdbcBasePersistenceImpl.java Switches lookupEntityVersions to the new lightweight query + converter.

Comment on lines 494 to +501
@Override
public List<PolarisChangeTrackingVersions> lookupEntityVersions(
@NonNull PolarisCallContext callCtx, List<PolarisEntityId> entityIds) {
Map<PolarisEntityId, ModelEntity> idToEntityMap =
lookupEntities(callCtx, entityIds).stream()
.filter(Objects::nonNull)
.collect(
Collectors.toMap(
entry -> new PolarisEntityId(entry.getCatalogId(), entry.getId()),
entry -> ModelEntity.fromEntity(entry, schemaVersion)));
return entityIds.stream()
.map(
entityId -> {
ModelEntity entity = idToEntityMap.getOrDefault(entityId, null);
return entity == null
? null
: new PolarisChangeTrackingVersions(
entity.getEntityVersion(), entity.getGrantRecordsVersion());
})
.collect(Collectors.toList());
if (entityIds == null || entityIds.isEmpty()) {
return new ArrayList<>();
}
PreparedQuery query =
QueryGenerator.generateSelectQueryWithEntityIdsVersionOnly(realmId, entityIds);
@ayushtkn
ayushtkn force-pushed the lookupEntityVersions branch from e1f5080 to aed7825 Compare July 11, 2026 06:30
@ayushtkn
ayushtkn force-pushed the lookupEntityVersions branch from aed7825 to 85d653f Compare July 11, 2026 06:59

@dimas-b dimas-b left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Nice fix, thanks, @ayushtkn !

@github-project-automation github-project-automation Bot moved this from PRs In Progress to Ready to merge in Basic Kanban Board Jul 14, 2026
@dimas-b

dimas-b commented Jul 14, 2026

Copy link
Copy Markdown
Contributor

Planning to merge on Jul 15 unless reviewers raise concerns.

@dimas-b
dimas-b merged commit 970cffb into apache:main Jul 15, 2026
25 checks passed
@github-project-automation github-project-automation Bot moved this from Ready to merge to Done in Basic Kanban Board Jul 15, 2026
Comment on lines 636 to 637
ModelEntity.getAllColumnNames(schemaVersion), ModelEntity.TABLE_NAME, params));
return b == null ? 0 : b.getGrantRecordsVersion();

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

IIUC, this does the entity load just to read the grant records version. Wonder if this should or can be remediated with the same treatment?

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

referenced my pr below

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.

5 participants