wy1433 opened a new pull request, #37298:
URL: https://github.com/apache/shardingsphere/pull/37298
Fixes #37297.
Changes proposed in this pull request:
- Add `InValueContext` to store IN expression structural information
(aligned with `InsertValueContext`)
- Add `ShardingInValuesToken` and `ShardingInValueItem` for IN values
rewriting based on route unit
- Add `ShardingInValuesTokenGenerator` to generate tokens for IN
expressions in WHERE clause
- Update `ShardingStandardRouteEngine` to split IN query conditions and
route each value individually
- Update `SelectStatementContext` to support IN expression context,
grouped parameter building, and `needGroupedRewrite` flag
- Update `SQLRewriteContext` to use `GroupedParameterBuilder` for IN
queries (aligned with INSERT VALUES pattern)
## Summary
This PR implements IN query batch split rewrite fully aligned with the
existing INSERT VALUES pattern. When a SELECT query contains IN expression on a
sharding key, each value is routed individually and
the SQL is rewritten per shard to only include values that route to that
specific shard.
**Architecture Design:**
| Phase | Responsibility | Storage |
|-------|----------------|---------|
| Binder | Extract IN parameter structure |
`SelectStatementContext.inValueContext` (static) |
| Router | Decide if grouping needed |
`SelectStatementContext.needGroupedRewrite` (dynamic) |
| Rewriter | Combine both for rewriting | From StatementContext +
RouteContext |
**Before:** All shards receive `SELECT * FROM t_order WHERE user_id IN
(100, 101, 102, 103)`
**After:**
- ds_0: `SELECT * FROM t_order_0 WHERE user_id IN (100, 102)`
- ds_1: `SELECT * FROM t_order_1 WHERE user_id IN (101, 103)`
## Technical Decisions
### Why use `needGroupedRewrite` flag?
**Problem**: Unlike INSERT VALUES, IN column may **not** be a sharding key.
**Solution**: Use `needGroupedRewrite` flag to distinguish:
- **Set to true**: IN column is sharding key → need parameter grouping
- **Set to false**: IN column is not sharding key → all shards execute
same SQL
### Why reuse `originalDataNodes`?
**Advantage**: Fully aligned with INSERT VALUES pattern - both use
`RouteContext.originalDataNodes` to store per-value routing information,
enabling unified `GroupedParameterBuilder` processing in rewrite
phase.
| Scenario | originalDataNodes | needGroupedRewrite | Behavior |
|----------|------------------|-------------------|----------|
| IN sharding key | Fine-grained `[[ds_0], [ds_1], ...]` | `true` |
Generate token, filter params ✅ |
| IN non-sharding key | Coarse-grained `[[ds_0, ds_1, ds_2]]` | `false` |
No token, all shards same SQL ✅ |
**Limitation**: Since `originalDataNodes` is a single-dimensional list,
this implementation currently supports only **single IN expression**. Multiple
IN expressions (e.g., `WHERE col1 IN (...) AND col2 IN
(...)`) would require multi-dimensional routing storage, which is not
supported in current design.
### Why rebuild InValueContext in setUpParameters()?
**Problem**: PreparedStatement uses empty parameters when creating
StatementContext, actual parameters provided via callback.
**Solution**: Rebuild `InValueContext` with actual parameters in
`setUpParameters()` callback, aligned with `InsertStatementContext` pattern.
## Key Benefits
- Zero infra module API changes (uses existing `ParameterAware` callback)
- Fully aligned with INSERT VALUES design pattern
- Clear separation of concerns between Binder/Router/Rewriter
- Supports both Statement and PreparedStatement modes
---
Before committing this PR, I'm sure that I have checked the following
options:
- [x] My code follows the [code of
conduct](https://shardingsphere.apache.org/community/en/involved/conduct/code/)
of this project.
- [x] I have self-reviewed the commit code.
- [x] I have (or in comment I request) added corresponding labels for the
pull request.
- [x] I have passed maven check locally : `./mvnw clean install -B -T1C
-Dmaven.javadoc.skip -Dmaven.jacoco.skip -e`.
- [ ] I have made corresponding changes to the documentation.
- [x] I have added corresponding unit tests for my changes.
- [x] I have updated the Release Notes of the current development version.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]