Skip to content

Fix JDBC hasChildren fetching all rows and columns#5066

Open
ayushtkn wants to merge 1 commit into
apache:mainfrom
ayushtkn:hasChildren
Open

Fix JDBC hasChildren fetching all rows and columns#5066
ayushtkn wants to merge 1 commit into
apache:mainfrom
ayushtkn:hasChildren

Conversation

@ayushtkn

Copy link
Copy Markdown
Member

JdbcBasePersistenceImpl.hasChildren was issuing a SELECT of all 15+ entity columns—including large JSON properties and internal_properties blobs—with no LIMIT, just to answer whether any children exist.

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 15, 2026 18:21
@github-project-automation github-project-automation Bot moved this to PRs In Progress in Basic Kanban Board Jul 15, 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 JdbcBasePersistenceImpl.hasChildren in the relational JDBC persistence layer to avoid selecting wide entity rows (including large JSON blob columns) when only row existence is needed.

Changes:

  • Add QueryGenerator.generateExistsQuery(...) to emit SELECT 1 ... LIMIT 1 existence checks.
  • Update JdbcBasePersistenceImpl.hasChildren to use the new existence query instead of a full entity-column SELECT.
  • Add/extend tests to verify LIMIT 1 usage and that properties / internal_properties are not fetched.

Reviewed changes

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

File Description
persistence/relational-jdbc/src/main/java/org/apache/polaris/persistence/relational/jdbc/QueryGenerator.java Adds an existence-query generator (SELECT 1 ... LIMIT 1).
persistence/relational-jdbc/src/main/java/org/apache/polaris/persistence/relational/jdbc/JdbcBasePersistenceImpl.java Switches hasChildren to the existence-query path to avoid fetching wide rows.
persistence/relational-jdbc/src/test/java/org/apache/polaris/persistence/relational/jdbc/QueryGeneratorTest.java Adds assertions ensuring the existence query uses SELECT 1 + LIMIT 1 and avoids blob columns.
persistence/relational-jdbc/src/test/java/org/apache/polaris/persistence/relational/jdbc/JdbcBasePersistenceImplTest.java Adds an integration-style test validating hasChildren issues a limited existence query (no blob columns).

Comment on lines +383 to +388
public static PreparedQuery generateExistsQuery(
@NonNull String tableName, @NonNull Map<String, Object> whereClause) {
QueryFragment where = generateWhereClause(whereClause.keySet(), whereClause, Map.of());
String sql =
"SELECT 1 FROM " + getFullyQualifiedTableName(tableName) + where.sql() + " LIMIT 1";
return new PreparedQuery(sql, where.parameters());

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

hmm, as of now it had one caller only, but fair enough, I have changed it & added a test as well around it

@ayushtkn
ayushtkn force-pushed the hasChildren branch 2 times, most recently from b1c7a3f to 606a6d7 Compare July 15, 2026 18:56
dimas-b
dimas-b previously approved these changes Jul 15, 2026

@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 optimization! Thanks, @ayushtkn !

@github-project-automation github-project-automation Bot moved this from PRs In Progress to Ready to merge in Basic Kanban Board Jul 15, 2026
adutra
adutra previously approved these changes Jul 17, 2026
@adutra

adutra commented Jul 17, 2026

Copy link
Copy Markdown
Contributor

@ayushtkn could you please fix the merge conflict? Thanks!

@ayushtkn
ayushtkn dismissed stale reviews from adutra and dimas-b via c71f6a9 July 17, 2026 15:35
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.

4 participants