Skip to content

feat(dqp): support partial ordered topn set_var and external hints#116

Merged
hawkingrei merged 2 commits intomainfrom
feat/dqp-partial-ordered-topn-hint
Feb 15, 2026
Merged

feat(dqp): support partial ordered topn set_var and external hints#116
hawkingrei merged 2 commits intomainfrom
feat/dqp-partial-ordered-topn-hint

Conversation

@hawkingrei
Copy link
Copy Markdown
Owner

Summary

  • add DQP built-in SET_VAR toggles for tidb_opt_partial_ordered_index_for_topn:
    • SET_VAR(tidb_opt_partial_ordered_index_for_topn='COST')
    • SET_VAR(tidb_opt_partial_ordered_index_for_topn='DISABLE')
  • add config-driven external DQP hint injection via oracles.dqp_external_hints
  • support two external hint forms:
    • full hint strings (for example SET_VAR(...), HASH_JOIN(...))
    • var=value shorthand auto-wrapped to SET_VAR(var=value)
  • document usage in README

Motivation

tidb_opt_partial_ordered_index_for_topn is a new TiDB optimizer variable (SET_VAR-controllable). DQP should be able to stress this branch directly and also allow external, operator-specific hint extensions without changing code.

Scope of change

  • internal/oracle/hints.go
    • add partial ordered TopN SET_VAR constants
  • internal/oracle/dqp.go
    • include partial ordered TopN toggles in DQP SET_VAR candidates
    • add external hint parsing/splitting for base hints vs set_var hints
  • internal/config/config.go
    • add oracles.dqp_external_hints config field
  • tests
    • internal/oracle/dqp_test.go
    • internal/config/config_test.go
  • docs
    • README.md
    • AGENTS.md
    • docs/todo.md

Related references

Test

  • GOCACHE=/tmp/shiro-go-cache /Users/weizhenwang/.gvm/gos/go1.25.7/bin/go test ./internal/oracle
  • GOCACHE=/tmp/shiro-go-cache /Users/weizhenwang/.gvm/gos/go1.25.7/bin/go test ./internal/config
  • GOCACHE=/tmp/shiro-go-cache /Users/weizhenwang/.gvm/gos/go1.25.7/bin/go test ./cmd/shiro

Copilot AI review requested due to automatic review settings February 15, 2026 15:30
@cloudflare-workers-and-pages
Copy link
Copy Markdown

cloudflare-workers-and-pages bot commented Feb 15, 2026

Deploying with  Cloudflare Workers  Cloudflare Workers

The latest updates on your project. Learn more about integrating Git with Workers.

Status Name Latest Commit Preview URL Updated (UTC)
✅ Deployment successful!
View logs
shiro 860b037 Commit Preview URL

Branch Preview URL
Feb 15 2026, 04:52 PM

@gemini-code-assist
Copy link
Copy Markdown
Contributor

Summary of Changes

Hello @hawkingrei, 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 significantly enhances the DQP's flexibility and testability by integrating direct control over the tidb_opt_partial_ordered_index_for_topn optimizer variable and introducing a robust system for external hint injection. These changes empower users to fine-tune query optimization behavior and extend DQP's capabilities through configuration, facilitating more comprehensive testing and custom optimization strategies without requiring code modifications.

Highlights

  • DQP SET_VAR Toggles: Introduced built-in SET_VAR toggles for tidb_opt_partial_ordered_index_for_topn, allowing 'COST' and 'DISABLE' options to control partial ordered TopN index optimization.
  • External DQP Hint Injection: Implemented a configuration-driven mechanism for injecting external DQP hints via oracles.dqp_external_hints, enabling dynamic extension of DQP's hint capabilities.
  • Flexible Hint Formats: Supported two forms for external hints: full optimizer hint strings (e.g., HASH_JOIN(...)) and a var=value shorthand that is automatically wrapped into SET_VAR(var=value).
  • Documentation Updates: Updated README.md, AGENTS.md, and docs/todo.md to reflect the new DQP hint features and their usage.
Changelog
  • AGENTS.md
    • Added a new entry detailing the DQP hint support and external hint injection.
  • README.md
    • Added a new section "DQP external hint injection" with usage examples.
  • docs/todo.md
    • Updated with a new "Latest sync" entry for the DQP hint and external hint injection features.
  • internal/config/config.go
    • Added DQPExternalHints field to the OracleConfig struct to store external DQP hints.
  • internal/config/config_test.go
    • Added TestLoadDQPExternalHints to verify the correct loading of external DQP hints from a YAML configuration.
  • internal/oracle/dqp.go
    • Modified buildDQPVariants to incorporate external base and SET_VAR hints.
    • Updated dqpHintsForQuery and dqpSetVarHints to accept and integrate external hints.
    • Introduced dqpSetVarHintCandidates to generate SET_VAR hint options, including the new partial ordered TopN toggles.
    • Added dqpExternalHintCandidates to parse and categorize external hints into base and SET_VAR types.
    • Implemented normalizeSetVarHint to convert var=value strings into SET_VAR(var=value) format.
  • internal/oracle/dqp_test.go
    • Updated existing tests (TestDQPSetVarHintsCount, TestDQPHintsForQueryCount) to accommodate the new function signatures.
    • Added TestDQPSetVarHintCandidatesIncludePartialOrderedTopN to confirm the inclusion of new partial ordered TopN SET_VAR hints.
    • Added TestDQPExternalHintCandidates to validate the parsing and categorization of various external hint formats.
  • internal/oracle/hints.go
    • Defined new constants SetVarPartialOrderedTopNCost and SetVarPartialOrderedTopNDisable for the new optimizer variable.
Activity
  • The author, hawkingrei, validated the changes by running go test ./internal/oracle, go test ./internal/config, and go test ./cmd/shiro using Go 1.25.7.
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
Copy Markdown
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 introduces support for tidb_opt_partial_ordered_index_for_topn as a SET_VAR hint in DQP and a mechanism for injecting external hints via configuration. However, the DQPExternalHints configuration field introduces a critical SQL injection vulnerability, as arbitrary strings are injected into SQL queries without sanitization for the comment closer */. Additionally, there's a logic issue in the external hint parsing that could generate invalid SQL.

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This pull request adds support for TiDB's tidb_opt_partial_ordered_index_for_topn optimizer variable to the DQP (Differential Query Plan) oracle, along with a flexible external hint injection mechanism. The feature allows testing of the partial ordered index optimization for TopN operators and enables operator-specific hint extensions without code changes.

Changes:

  • Added built-in SET_VAR toggles for partial ordered TopN optimization (COST and DISABLE modes)
  • Introduced config-driven external DQP hint injection via oracles.dqp_external_hints field
  • Implemented dual-format hint support: full hint strings and var=value shorthand auto-wrapped to SET_VAR

Reviewed changes

Copilot reviewed 8 out of 8 changed files in this pull request and generated 2 comments.

Show a summary per file
File Description
internal/oracle/hints.go Added constants for partial ordered TopN SET_VAR hints
internal/oracle/dqp.go Implemented external hint parsing, splitting, and integration into DQP variant generation
internal/config/config.go Added DQPExternalHints field to OracleConfig
internal/oracle/dqp_test.go Added test coverage for partial ordered TopN hints and external hint processing
internal/config/config_test.go Added test for loading DQP external hints from config
README.md Documented DQP external hint injection feature with usage examples
AGENTS.md Updated recent changes log with implementation details
docs/todo.md Updated latest sync entry with implementation summary

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@hawkingrei hawkingrei requested a review from Copilot February 15, 2026 17:02
@hawkingrei
Copy link
Copy Markdown
Owner Author

/gemini review

Copy link
Copy Markdown
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 introduces support for external DQP hints via a new configuration option, oracles.dqp_external_hints, and adds built-in SET_VAR toggles for tidb_opt_partial_ordered_index_for_topn. The changes are well-structured, with new logic for parsing and classifying hints, along with comprehensive tests. My review includes a high-severity fix for a potential bug in hint normalization logic that could misclassify certain hints, and a medium-severity suggestion to improve test code robustness. All comments align with the repository's code review guidelines.

Comment on lines +326 to +328
if strings.Contains(trimmed, "=") {
return "SET_VAR(" + trimmed + ")", true, true
}
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.

high

The current logic for detecting SET_VAR shorthand hints is too broad. It uses strings.Contains(trimmed, "="), which could incorrectly classify a regular optimizer hint containing an equals sign (e.g., in a string literal argument) as a SET_VAR hint. The shorthand form var=value for session variables typically does not contain parentheses. To avoid this misclassification, the check should be more specific by also ensuring no parentheses are present in the shorthand form.

I also recommend adding a test case to TestNormalizeSetVarHintClassification for a base hint containing an equals sign to prevent future regressions, for example:

{
    name:     "base_hint_with_equals",
    raw:      "HYPOTHETICAL_HINT(key='value')",
    hint:     "",
    isSetVar: false,
    valid:    false,
},
Suggested change
if strings.Contains(trimmed, "=") {
return "SET_VAR(" + trimmed + ")", true, true
}
if strings.Contains(trimmed, "=") && !strings.Contains(trimmed, "(") {
return "SET_VAR(" + trimmed + ")", true, true
}

Comment on lines +230 to +238
if len(cfg.Oracles.DQPExternalHints) != 2 {
t.Fatalf("unexpected dqp external hints count: %d", len(cfg.Oracles.DQPExternalHints))
}
if cfg.Oracles.DQPExternalHints[0] != "SET_VAR(tidb_opt_partial_ordered_index_for_topn='COST')" {
t.Fatalf("unexpected first dqp external hint: %s", cfg.Oracles.DQPExternalHints[0])
}
if cfg.Oracles.DQPExternalHints[1] != "HASH_JOIN" {
t.Fatalf("unexpected second dqp external hint: %s", cfg.Oracles.DQPExternalHints[1])
}
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.

medium

The assertions for the DQPExternalHints slice can be made more robust and maintainable by comparing the entire slice at once using reflect.DeepEqual, rather than checking the length and each element individually. This makes the test cleaner and provides a more comprehensive diff if the test fails. This change requires importing the reflect package.

	expectedHints := []string{
		"SET_VAR(tidb_opt_partial_ordered_index_for_topn='COST')",
		"HASH_JOIN",
	}
	if !reflect.DeepEqual(cfg.Oracles.DQPExternalHints, expectedHints) {
		t.Fatalf("unexpected dqp external hints\ngot:  %v\nwant: %v", cfg.Oracles.DQPExternalHints, expectedHints)
	}

@hawkingrei hawkingrei merged commit 639c5e6 into main Feb 15, 2026
7 checks passed
@hawkingrei hawkingrei deleted the feat/dqp-partial-ordered-topn-hint branch February 15, 2026 17:05
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

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


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

candidates = append(candidates, toggleHints(SetVarEnableHashJoinOn, SetVarEnableHashJoinOff)...)
candidates = append(candidates, toggleHints(SetVarEnableOuterJoinReorderOn, SetVarEnableOuterJoinReorderOff)...)
candidates = append(candidates, toggleHints(SetVarEnableInlJoinInnerMultiOn, SetVarEnableInlJoinInnerMultiOff)...)
}
Copy link

Copilot AI Feb 15, 2026

Choose a reason for hiding this comment

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

The partial ordered TopN SET_VAR hints are added unconditionally to all query candidates (line 252), regardless of whether the query has ORDER BY or LIMIT clauses that would make TOP-N optimization relevant. Other hints in this function are conditionally added based on query features (e.g., hasJoin for join-related hints, hasSubquery for subquery hints, hasPartition for partition hints). Consider adding a condition like if hasOrderBy || hasLimit to only include these hints when they would be applicable, or document why these hints should apply to all queries.

Suggested change
}
}
// Apply partial ordered TopN SET_VAR hints to all queries intentionally. Even when
// there is no explicit ORDER BY or LIMIT, we want to exercise optimizer behavior
// controlled by these knobs across a wide range of plans.

Copilot uses AI. Check for mistakes.
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