Skip to content

Conversation

@arthurschreiber
Copy link
Member

@arthurschreiber arthurschreiber commented May 22, 2025

Description

This PR adds two "naive" improvements to allow UNION queries to be merged in more cases.

The first change allows UNION subqueries that use IN conditions to be merged together correctly. It only works if tuples on the two subqueries we try to merge match exactly (same elements in the exact same order), or the tuple was normalized to the same bind variable (see the change below). This could be improved upon by doing a true "set" comparison (so that the order of values and duplicates don't affect the merge result).

This probably should also be extended to allow merging queries where one side uses foobar IN (1) and the other side uses foobar = 1. Merging foobar IN (1) with foobar = 1 already works, so I just added a test case to make sure it's covered.

This change is currently missing test coverage, but I'll make sure to add some.
Various test cases to cover this change have been added.

The second change improves bind variable normalization so that bind variables for ValTuple get de-duplicated, similar to other bind variables. That de-duplication only happens if the tuples contain exactly the same values (with matching types) in the same order.

This could probably be improved upon by pre-sorting and de-duplicating the tuple values, to ensure that tuples like (1, 2, 3) (1, 1, 2, 3) and (3, 2, 1) all end up with the same bind variable value (and thus allow UNION queries that use such tuples for routing to be merged).

Related Issue(s)

#18288

Checklist

  • "Backport to:" labels have been added if this change should be back-ported to release branches
  • If this change is to be back-ported to previous releases, a justification is included in the PR description
  • Tests were added or are not required
  • Did the new or modified tests pass consistently locally and on CI?
  • Documentation was added or is not required

Deployment Notes

@vitess-bot
Copy link
Contributor

vitess-bot bot commented May 22, 2025

Review Checklist

Hello reviewers! 👋 Please follow this checklist when reviewing this Pull Request.

General

  • Ensure that the Pull Request has a descriptive title.
  • Ensure there is a link to an issue (except for internal cleanup and flaky test fixes), new features should have an RFC that documents use cases and test cases.

Tests

  • Bug fixes should have at least one unit or end-to-end test, enhancement and new features should have a sufficient number of tests.

Documentation

  • Apply the release notes (needs details) label if users need to know about this change.
  • New features should be documented.
  • There should be some code comments as to why things are implemented the way they are.
  • There should be a comment at the top of each new or modified test to explain what the test does.

New flags

  • Is this flag really necessary?
  • Flag names must be clear and intuitive, use dashes (-), and have a clear help text.

If a workflow is added or modified:

  • Each item in Jobs should be named in order to mark it as required.
  • If the workflow needs to be marked as required, the maintainer team must be notified.

Backward compatibility

  • Protobuf changes should be wire-compatible.
  • Changes to _vt tables and RPCs need to be backward compatible.
  • RPC changes should be compatible with vitess-operator
  • If a flag is removed, then it should also be removed from vitess-operator and arewefastyet, if used there.
  • vtctl command output order should be stable and awk-able.

@vitess-bot vitess-bot bot added NeedsBackportReason If backport labels have been applied to a PR, a justification is required NeedsDescriptionUpdate The description is not clear or comprehensive enough, and needs work NeedsIssue A linked issue is missing for this Pull Request NeedsWebsiteDocsUpdate What it says labels May 22, 2025
@github-actions github-actions bot added this to the v23.0.0 milestone May 22, 2025
@arthurschreiber arthurschreiber changed the title Arthur/improve union merging Improve UNION query merging May 22, 2025
@arthurschreiber arthurschreiber added Backport to: release-20.0 Backport to: release-22.0 Needs to be backport to release-22.0 and removed NeedsDescriptionUpdate The description is not clear or comprehensive enough, and needs work NeedsWebsiteDocsUpdate What it says NeedsIssue A linked issue is missing for this Pull Request NeedsBackportReason If backport labels have been applied to a PR, a justification is required labels May 22, 2025
@codecov
Copy link

codecov bot commented May 22, 2025

Codecov Report

Attention: Patch coverage is 90.00000% with 3 lines in your changes missing coverage. Please review.

Project coverage is 67.47%. Comparing base (fcfa0c8) to head (5b6a8c2).
Report is 4 commits behind head on main.

Files with missing lines Patch % Lines
go/vt/sqlparser/normalizer.go 84.61% 2 Missing ⚠️
.../vt/vtgate/planbuilder/operators/route_planning.go 91.66% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main   #18289      +/-   ##
==========================================
- Coverage   67.48%   67.47%   -0.02%     
==========================================
  Files        1603     1603              
  Lines      262337   262384      +47     
==========================================
+ Hits       177034   177039       +5     
- Misses      85303    85345      +42     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

Comment on lines +476 to +489
Copy link
Member Author

@arthurschreiber arthurschreiber May 23, 2025

Choose a reason for hiding this comment

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

This is a bit awkward - We can't use bval directly as the map key, because Values on querypb.BindVariable is a slice, and structs containing slices can't be used as map keys.

So I'm using MarshalVT in combination with string to generate a value that can be used as a key. This is probably not super-optimal, but 🤷

@systay systay added Type: Enhancement Logical improvement (somewhere between a bug and feature) Component: Query Serving labels May 23, 2025
@arthurschreiber arthurschreiber force-pushed the arthur/improve-union-merging branch from a8d2976 to b94dd79 Compare May 23, 2025 09:12
@arthurschreiber arthurschreiber force-pushed the arthur/improve-union-merging branch from b94dd79 to 5b6a8c2 Compare May 26, 2025 08:48
@arthurschreiber arthurschreiber force-pushed the arthur/improve-union-merging branch from 5b6a8c2 to 73dc771 Compare May 26, 2025 09:38
@arthurschreiber arthurschreiber merged commit 5290b86 into vitessio:main May 26, 2025
99 of 103 checks passed
arthurschreiber added a commit that referenced this pull request May 26, 2025
Signed-off-by: Arthur Schreiber <[email protected]>
arthurschreiber added a commit that referenced this pull request May 26, 2025
Signed-off-by: Arthur Schreiber <[email protected]>
arthurschreiber pushed a commit that referenced this pull request May 26, 2025
Signed-off-by: Arthur Schreiber <[email protected]>
arthurschreiber added a commit to github/vitess-gh that referenced this pull request May 26, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Backport to: release-22.0 Needs to be backport to release-22.0 Component: Query Serving Type: Enhancement Logical improvement (somewhere between a bug and feature)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants