Skip to content

Use parameter equality when only single value for V3 node#1796

Merged
n-h-diaz merged 2 commits intodatacommonsorg:masterfrom
n-h-diaz:equality
Mar 6, 2026
Merged

Use parameter equality when only single value for V3 node#1796
n-h-diaz merged 2 commits intodatacommonsorg:masterfrom
n-h-diaz:equality

Conversation

@n-h-diaz
Copy link
Contributor

@n-h-diaz n-h-diaz commented Mar 5, 2026

For parameterized queries, "IN UNNEST()" and "=" are treated differently even for just a single parameter value

Based on analysis with Spanner team, explicitly using "=" when there is a single parameter value has significant latency improvements, as Spanner can make a more optimized query plan, so this PR switches query templates to use equality whenever possible

As a side effect of the new query plans (based on some testing) it now seems better to add filters after the main statement, so this PR also reverses the order of subqueries

I did some testing of some logged production slow Spanner queries,

  • nodes=wikidataId/Q1312844&property=<-containedInPlace+{typeOf:Village} [autopush: 7.41s, local: 2.51s]
  • nodes=wikidataId/Q2222026&property=<-containedInPlace+{typeOf:AdministrativeArea4} [autopush: 12.07s, local: 2.69s]
  • nodes=wikidataId/Q1797336&property=<-containedInPlace+{typeOf:Place} [autopush: 28.96s, local: 3.22s]

@gemini-code-assist
Copy link
Contributor

Summary of Changes

Hello, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request focuses on improving the performance of Spanner queries by modifying query templates to use equality checks (=) when dealing with single parameter values. This change allows Spanner to generate more optimized query plans, resulting in reduced latency. Additionally, the PR reorders subqueries to add filters after the main statement, further enhancing query performance based on testing results.

Highlights

  • Performance Optimization: This PR optimizes Spanner queries by using equality checks (=) instead of IN UNNEST() for single parameter values, leading to significant latency improvements.
  • Query Plan Improvement: The changes in query plans now favor adding filters after the main statement, which has shown better performance in testing.
  • Template Modification: Modified query templates to use equality checks when possible, enhancing Spanner's ability to create optimized query plans.
Changelog
  • internal/server/spanner/golden/query_builder/get_node_edges_first_page.sql
    • Replaced IN with = for m.subject_id.
  • internal/server/spanner/golden/query_builder/get_node_edges_first_page_chain.sql
    • Replaced IN with = for m.subject_id.
  • internal/server/spanner/golden/query_builder/get_node_edges_in_bracket_props.sql
    • Replaced IN with = for m.subject_id.
  • internal/server/spanner/golden/query_builder/get_node_edges_in_chain.sql
    • Replaced IN with = for m.subject_id.
  • internal/server/spanner/golden/query_builder/get_node_edges_in_filter.sql
    • Reordered subqueries and replaced IN with = for m.subject_id.
  • internal/server/spanner/golden/query_builder/get_node_edges_in_single_prop.sql
    • Replaced IN with = for m.subject_id and e.predicate.
  • internal/server/spanner/golden/query_builder/get_node_edges_linked_contained_in_place.sql
    • Reordered subqueries and replaced IN with = for m.subject_id.
  • internal/server/spanner/golden/query_builder/get_node_edges_malicious.sql
    • Reordered subqueries and replaced IN with = for m.subject_id.
  • internal/server/spanner/golden/query_builder/get_node_edges_out_bracket_props.sql
    • Replaced IN with = for m.subject_id.
  • internal/server/spanner/golden/query_builder/get_node_edges_out_chain.sql
    • Replaced IN with = for m.subject_id.
  • internal/server/spanner/golden/query_builder/get_node_edges_out_filter.sql
    • Reordered subqueries and replaced IN with = for m.subject_id.
  • internal/server/spanner/golden/query_builder/get_node_edges_out_single_prop.sql
    • Replaced IN with = for m.subject_id and e.predicate.
  • internal/server/spanner/golden/query_builder/get_node_edges_second_page.sql
    • Replaced IN with = for m.subject_id.
  • internal/server/spanner/golden/query_builder/get_node_edges_second_page_chain.sql
    • Replaced IN with = for m.subject_id.
  • internal/server/spanner/query_builder.go
    • Modified GetNodePropsQuery to use equality checks for single IDs.
    • Modified GetNodeEdgesByIDQuery to use equality checks for single IDs and reordered subqueries.
  • internal/server/spanner/statements.go
    • Added getId and getIds to statements.
    • Updated getPropsBySubjectID and getPropsByObjectID to use the new id formats.
    • Updated getEdgesBySubjectID, getChainedEdgesBySubjectID, getEdgesByObjectID, and getChainedEdgesByObjectID to use the new id formats.
    • Updated filterPredicate and added filterPredicates.
    • Updated filterProperty and added filterValue and filterValues.
Activity
  • The PR author, n-h-diaz, has addressed performance concerns by switching from IN UNNEST() to = for single parameter values in Spanner queries.
  • Testing was conducted on logged production slow Spanner queries, showing significant latency improvements.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

@n-h-diaz n-h-diaz marked this pull request as ready for review March 5, 2026 22:06
Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

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 aims to improve Spanner query latency by replacing IN UNNEST() with = for single-value parameters and reordering subqueries. While a security audit found no vulnerabilities, particularly concerning SQL injection with fmt.Sprintf due to proper parameterization, a flaw was identified in the handling of filter values. Specifically, addObjectValues is called unconditionally, which results in incorrect queries for DCID-based properties like typeOf and prevents the intended performance optimization. A specific code suggestion has been provided to resolve this.

@n-h-diaz n-h-diaz requested review from keyurva and vish-cs March 5, 2026 22:14
Copy link
Contributor

@keyurva keyurva left a comment

Choose a reason for hiding this comment

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

Thanks Natalie - amazing latency improvements!

@n-h-diaz n-h-diaz added this pull request to the merge queue Mar 6, 2026
Merged via the queue into datacommonsorg:master with commit 139b123 Mar 6, 2026
4 of 5 checks passed
@n-h-diaz n-h-diaz deleted the equality branch March 6, 2026 22:29
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