Skip to content

perf(bigquery-jdbc): optimize getExportedKeys performance using hybrid metadata lookup#13734

Open
keshavdandeva wants to merge 5 commits into
mainfrom
jdbc/getExportedKeys-infoSchema
Open

perf(bigquery-jdbc): optimize getExportedKeys performance using hybrid metadata lookup#13734
keshavdandeva wants to merge 5 commits into
mainfrom
jdbc/getExportedKeys-infoSchema

Conversation

@keshavdandeva

Copy link
Copy Markdown
Contributor

b/534326814

This PR optimizes DatabaseMetaData.getExportedKeys() by restoring the highly performant INFORMATION_SCHEMA SQL lookup path, correcting a performance regression introduced when the method was migrated to sequential REST API lookups.

To ensure complete contract safety across both standard native tables and external REST catalog tables (PCNT tables), we implement a hybrid routing strategy:

  1. PCNT Early Return (Fast Path):
    If schema contains a dot (.), we bypass SQL execution entirely and return a structured empty ResultSet instantly.
  2. Wildcard Fallback (REST API Path):
    If either catalog or schema arguments are null (representing wildcard metadata searches), the driver falls back to retrieving constraints concurrently via the Java Client REST API.
  3. Standard Introspection (SQL Path):
    For targeted native table queries, the driver executes the optimized DatabaseMetaData_GetExportedKeys.sql query template.

Integration Tests

  • Added PCNT table tests in ITDatabaseMetadataTest.java for:
    • testGetPrimaryKeys_pcntTable
    • testGetImportedKeys_pcntTable
    • testGetExportedKeys_pcntTable
    • testGetCrossReference_pcntTable

@gemini-code-assist gemini-code-assist Bot 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.

Code Review

This pull request updates the getExportedKeys method in BigQueryDatabaseMetaData to use a SQL-based approach instead of the REST API metadata scan when both catalog and schema are provided. It also introduces an early return for PCNT catalog schemas and adds corresponding integration tests. The review feedback suggests optimizing performance by lazy-loading and caching the SQL template file instead of reading it from the classpath on every query, and removing the redundant RTRIM function from the SQL query to allow BigQuery to utilize metadata optimizations.

@keshavdandeva

Copy link
Copy Markdown
Contributor Author

/gemini review

@gemini-code-assist gemini-code-assist Bot 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.

Code Review

This pull request optimizes the getExportedKeys method in BigQueryDatabaseMetaData by executing a SQL query against INFORMATION_SCHEMA instead of scanning metadata via the REST API, and adds integration tests for PCNT tables. The reviewer identified three key issues: correctness bugs in the SQL query related to multi-column foreign keys and duplicate rows, a potential SQL injection vulnerability when formatting parameters into backtick-quoted identifiers, and synchronization overhead in loading the SQL file content which can be optimized using the lazy initialization holder class idiom.

@keshavdandeva keshavdandeva marked this pull request as ready for review July 13, 2026 17:27
@keshavdandeva keshavdandeva requested review from a team as code owners July 13, 2026 17:27
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.

2 participants