Skip to content

Conversation

@renecannao
Copy link
Contributor

Summary

Fixes issue #5248: Setting cache_empty_result to "0" on individual mysql_query_rules doesn't work.

Problem: When cache_empty_result is set to "0" on a query rule, nothing gets cached at all (even non-empty resultsets). According to the issue reporter, they want to disable caching for empty resultsets but still cache non-empty resultsets.

Root Cause: The caching logic in MySQL_Session.cpp and PgSQL_Session.cpp only handled two cases:

  1. cache_empty_result == 1: Always cache
  2. cache_empty_result == -1: Use global setting OR cache if rows > 0
    Missing case: cache_empty_result == 0: Cache only if rows > 0

Solution: Added the missing condition (qpo->cache_empty_result == 0 && MyRS->num_rows) (MySQL) / (qpo->cache_empty_result == 0 && num_rows) (PgSQL) to allow caching when cache_empty_result=0 AND the resultset has rows.

Changes:

  1. MySQL_Session.cpp (lines 7495-7502): Added condition for cache_empty_result == 0
  2. PgSQL_Session.cpp (lines 4815-4821): Added condition for cache_empty_result == 0
  3. query_processor.h: Added extensive Doxygen documentation for the cache_empty_result field explaining its three possible values
  4. Query_Processor.cpp: Added inline comments clarifying the three possible values

Semantics of cache_empty_result:

  • -1: Use global setting (query_cache_stores_empty_result)
  • 0: Do NOT cache empty resultsets, but cache non-empty resultsets
  • 1: Always cache resultsets (both empty and non-empty)

Testing Considerations: The fix maintains backward compatibility:

  • cache_empty_result=1: Still caches everything
  • cache_empty_result=-1: Still uses global setting OR caches non-empty results
  • cache_empty_result=0: Now correctly caches non-empty results (previously cached nothing)

References:

The `cache_empty_result` field in query rules has three possible values:
• -1: Use global setting (`query_cache_stores_empty_result`)
•  0: Do NOT cache empty resultsets, but cache non-empty resultsets
•  1: Always cache resultsets (both empty and non-empty)

Previously, when `cache_empty_result` was set to 0, nothing was cached at all,
even for non-empty resultsets. This prevented users from disabling caching
for empty resultsets while still allowing caching of non-empty resultsets
on a per-rule basis.

Changes:
1. Modified caching logic in MySQL_Session.cpp and PgSQL_Session.cpp to
   add the condition `(qpo->cache_empty_result == 0 && MyRS->num_rows)`
   (MySQL) and `(qpo->cache_empty_result == 0 && num_rows)` (PgSQL)
   to allow caching when cache_empty_result=0 AND result has rows.

2. Added comprehensive Doxygen documentation in query_processor.h explaining
   the semantics of cache_empty_result values.

3. Updated Query_Processor.cpp with inline comments explaining the
   three possible values.

Now when cache_empty_result is set to 0:
- Empty resultsets (0 rows) are NOT cached
- Non-empty resultsets (>0 rows) ARE cached
- This matches the intended per-rule behavior described in issue #5248.

Fixes: #5248
@gemini-code-assist
Copy link

Summary of Changes

Hello @renecannao, 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 addresses a long-standing bug in the query caching mechanism where the cache_empty_result setting for query rules was not behaving as intended. Previously, configuring a rule to not cache empty resultsets (cache_empty_result=0) inadvertently disabled caching entirely for that rule. The changes rectify this by modifying the core caching logic in both MySQL and PostgreSQL session handlers to correctly differentiate between empty and non-empty resultsets when this setting is applied, ensuring that only empty results are excluded from the cache. Additionally, comprehensive documentation has been integrated to clarify the parameter's behavior for future development and maintenance.

Highlights

  • Fix for cache_empty_result=0 behavior: Resolved an issue where setting cache_empty_result to 0 on individual query rules incorrectly prevented all resultsets (including non-empty ones) from being cached. The intended behavior for cache_empty_result=0 is to only prevent empty resultsets from being cached, while still caching non-empty ones.
  • Updated Caching Logic: The caching logic in MySQL_Session.cpp and PgSQL_Session.cpp has been updated to correctly handle the cache_empty_result=0 setting, ensuring that non-empty resultsets are now cached as expected.
  • Enhanced Documentation: Extensive Doxygen documentation has been added to query_processor.h and inline comments to Query_Processor.cpp, MySQL_Session.cpp, and PgSQL_Session.cpp to clearly define the three possible values and semantics of the cache_empty_result field (-1, 0, 1).
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.

Copy link

@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 addresses issue #5248 by fixing the behavior of the cache_empty_result setting in query rules. Previously, setting cache_empty_result to 0 would prevent all caching, even for non-empty result sets. The changes update the caching logic in MySQL_Session.cpp and PgSQL_Session.cpp to ensure that when cache_empty_result is 0, only non-empty result sets are cached, while empty ones are not. Additionally, detailed Doxygen-style comments have been added to include/query_processor.h and lib/Query_Processor.cpp to clarify the meaning of the cache_empty_result values (-1 for global setting, 0 for caching non-empty only, and 1 for always caching).

@sonarqubecloud
Copy link

@renecannao renecannao merged commit 6ee087c into v3.0 Dec 17, 2025
5 of 6 checks passed
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