You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@shardingsphere.apache.org by "thswlsqls (via GitHub)" <gi...@apache.org> on 2026/09/17 02:06:23 UTC
[PR] Rewrite generated ORDER BY column owners to actual table names [shardingsphere]
thswlsqls opened a new pull request, #39866:
URL: https://github.com/apache/shardingsphere/pull/39866
Fixes #39865.
Changes proposed in this pull request:
- `ShardingOrderByTokenGenerator` used `ColumnOrderByItemSegment.getText()` and `OrderByToken` only implemented `Attachable`, so the generated ORDER BY kept the logic table owner on every route unit: `SELECT t_account.status, COUNT(*) FROM t_account GROUP BY t_account.status` became `... FROM t_account_0 GROUP BY t_account_0.status ORDER BY t_account.status ASC ` and failed on the database (same defect as #18122).
- Make `OrderByToken` `RouteUnitAware` with per-route-unit item texts, mirroring `ProjectionsToken`.
- Make `ShardingOrderByTokenGenerator` `RouteContextAware` with `ShardingRule` injected; resolve owners through `ShardingTokenUtils.getLogicAndActualTableMap()` (binding tables included) when `TableExtractor.needRewrite()` accepts them. Alias, unqualified and unmapped owners keep their text and quotes.
- Update `ShardingOrderByTokenGeneratorTest` and `OrderByTokenTest` for `toString(routeUnit)`; add owner, quoted owner, binding table, alias and unmapped owner cases.
- Add two table-owner cases to the rewriter IT `select.xml`; both fail on master.
- Verification scope: `./mvnw install -pl features/sharding/core -am -B -T1C -Dmaven.javadoc.skip -Dmaven.jacoco.skip -e` (BUILD SUCCESS, 882 tests, 0 failures), `ShardingSQLRewriterIT` in `test/it/rewriter` (780 cases, 0 failures), spotless, checkstyle and apache-rat with `-Pcheck`; full-repository `clean install` and E2E not run locally.
AI assistance: Claude Code was used to draft the change, its unit tests in `features/sharding/core`, and the rewriter IT cases in `test/it/rewriter`. I reviewed and verified every line submitted.
---
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.
- [ ] I have (or in comment I request) added corresponding labels for the pull request.
- [ ] I have passed maven check locally : `./mvnw clean install -B -T1C -Dmaven.javadoc.skip -Dmaven.jacoco.skip -e`. Ran the module-scoped equivalent instead: `./mvnw install -pl features/sharding/core -am -B -T1C -Dmaven.javadoc.skip -Dmaven.jacoco.skip -e` (BUILD SUCCESS), `./mvnw -pl test/it/rewriter -am test -Dtest=ShardingSQLRewriterIT` (780 cases passed), and `spotless:apply`, `checkstyle:check`, `apache-rat:check` with `-Pcheck` on `features/sharding/core` and `test/it/rewriter`.
- [ ] I have made corresponding changes to the documentation. No user-facing documentation describes this rewrite; none needed.
- [x] I have added corresponding unit tests for my changes.
- [x] I have updated the Release Notes of the current development version. For more details, see [Update Release Note](https://shardingsphere.apache.org/community/en/involved/contribute/contributor/)
- [x] I have disclosed the tool and affected files or scope for any material AI assistance, as required by the [AI-assisted contribution policy].
[AI-assisted contribution policy]: https://github.com/apache/shardingsphere/blob/master/AI_POLICY.md
--
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: notifications-unsubscribe@shardingsphere.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Rewrite generated ORDER BY column owners to actual table names [shardingsphere]
Posted by "terrymanu (via GitHub)" <gi...@apache.org>.
terrymanu merged PR #39866:
URL: https://github.com/apache/shardingsphere/pull/39866
--
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: notifications-unsubscribe@shardingsphere.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Rewrite generated ORDER BY column owners to actual table names [shardingsphere]
Posted by "thswlsqls (via GitHub)" <gi...@apache.org>.
thswlsqls commented on PR #39866:
URL: https://github.com/apache/shardingsphere/pull/39866#issuecomment-5867150837
@terrymanu Thanks for the review. You're right — the owner rewrite kept only the new owner and the column name, so nested attributes such as `.city` were dropped. It now appends the nested attributes after the column, and `ShardingOrderByTokenGeneratorTest#assertGenerateSQLTokenWithNestedAttribute` covers `t_account.address.city` rendering as `t_account_0.address.city`; the test fails on the previous code. Also rebased onto master to clear the release-note conflict.
--
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: notifications-unsubscribe@shardingsphere.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Rewrite generated ORDER BY column owners to actual table names [shardingsphere]
Posted by "thswlsqls (via GitHub)" <gi...@apache.org>.
thswlsqls commented on PR #39866:
URL: https://github.com/apache/shardingsphere/pull/39866#issuecomment-5867952048
@rishi-rana Thanks for the careful comparison. I agree both generators should resolve owners by one rule; since the `ShardingProjectionsTokenGenerator` lookup predates this PR, I'd keep this PR focused and leave the extraction to a follow-up, if the maintainers agree.
--
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: notifications-unsubscribe@shardingsphere.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Rewrite generated ORDER BY column owners to actual table names [shardingsphere]
Posted by "rishi-rana (via GitHub)" <gi...@apache.org>.
rishi-rana commented on PR #39866:
URL: https://github.com/apache/shardingsphere/pull/39866#issuecomment-5737749863
One observation, offered as a suggestion rather than a required change.
`ShardingProjectionsTokenGenerator` already performs the same owner rewrite that the new `getColumnLabel(ColumnOrderByItemSegment, ...)` introduces: for a given route unit it takes a `ColumnOrderByItemSegment`, resolves the owner to the actual table, and wraps it in the owner's original quote character ([generateNewColumnOrderByItem, lines 120-132](https://github.com/apache/shardingsphere/blob/101c3d56ccd7ee3c4a390a8f37550bed58a4751c/features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/rewrite/token/generator/impl/ShardingProjectionsTokenGenerator.java#L120-L132)). That generator emits the derived projection (`t_account_0.status AS ORDER_BY_DERIVED_0`) and this one emits the matching `ORDER BY`, so both resolve the same owner in the same statement, now through two independent implementations that do it differently:
1. **Table lookup.** `getActualTableName` scans `routeUnit.getTableMappers()` only ([line 134](https://github.com/apache/shardingsphere/blob/101c3d56ccd7ee3c4a390a8f37550bed58a4751c/features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/rewrite/token/generator/impl/ShardingProjectionsTokenGenerator.java#L134)), while the new path uses `ShardingTokenUtils.getLogicAndActualTableMap()`, which also merges `getLogicAndActualTablesFromBindingTable(...)` ([ShardingTokenUtils line 50](https://github.com/apache/shardingsphere/blob/101c3d56ccd7ee3c4a390a8f37550bed58a4751c/features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/rewrite/token/pojo/ShardingTokenUtils.java#L50)). A binding-table owner therefore resolves in one path but not the other.
2. **Unmapped owner.** The new path keeps the original text when the owner is not in the map; `getActualTableName` throws `IllegalStateException` ([line 140](https://github.com/apache/shardingsphere/blob/101c3d56ccd7ee3c4a390a8f37550bed58a4751c/features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/rewrite/token/generator/impl/ShardingProjectionsTokenGenerator.java#L140)).
The behaviour in this PR looks like the better of the two, since binding-table resolution is the more complete rule. The part worth raising is that the more correct version now lives in only one of the two places, so the two can drift apart as either is maintained.
Would it be worth extracting the owner rewrite into `ShardingTokenUtils`, next to `getLogicAndActualTableMap`, so both generators share it? That would keep the derived projection and the generated `ORDER BY` resolving owners by the same rule.
I have not constructed a query that actually diverges between the two paths, so this is a maintainability observation rather than a reported defect, and it may well be better as a follow-up since the older behaviour predates this change.
--
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: notifications-unsubscribe@shardingsphere.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org