Skip to content

fix: include cross-JOIN ON referenced columns in count subquery for MySQL - #878

Open
youdaoSu wants to merge 2 commits into
APIJSON:mainfrom
youdaoSu:fix/issue-824-mysql-join-subquery-columns
Open

youdaoSu wants to merge 2 commits into
APIJSON:mainfrom
youdaoSu:fix/issue-824-mysql-join-subquery-columns

Conversation

@youdaoSu

Copy link
Copy Markdown

Summary

Fix for issue #824 (multi-JOIN count pagination). When HEAD/count pagination is used with multiple JOINs, the subquery for each JOIN table only selected its own ON key (on.getKey()). If another JOIN's ON clause references a column of this table, that column was missing from the subquery, causing an Unknown column error.

Example (from the issue):

SELECT count(*) AS `count` FROM `Inventory` AS `Inventory`  
   LEFT JOIN ( SELECT `code` FROM `Location_info` ) AS `Location_info` ON `Location_info`.`code` = `Inventory`.`location_code`  
   LEFT JOIN ( SELECT `id` FROM `Warehose_info` ) AS `Warehose_info` ON `Warehose_info`.`id` = `Location_info`.`warehouse_id`

Location_info's subquery only selects code, but the second ON clause references Location_info.warehouse_id.

Change

In AbstractSQLConfig.parseJoin (MySQL-family databases only), when building the subquery column list for a JOIN table, also collect the targetKey of any ON clause (from any JOIN in the list) whose target table/alias matches the current JOIN table. Uses a LinkedHashSet to keep insertion order and deduplicate.

PostgreSQL (and other non-MySQL databases) keep their existing SELECT * fallback, which already covers this case.

Tests

Added AbstractSQLConfigJoinCountTest with 2 cases:

  • PostgreSQL: subqueries fall back to SELECT * (regression guard).
  • MySQL: Location_info subquery must include both code (its own ON key) and warehouse_id (referenced by the second JOIN's ON).

Full suite: 38 tests, 0 failures.

fixes #824

…ySQL

When HEAD (count pagination) uses multiple JOINs, the subquery for each
JOIN table only selected its own ON key (on.getKey()). If another JOIN's
ON clause references a column of this table (e.g.
Warehose_info.id@ = /Location_info/warehouse_id), that column was
missing from the subquery, causing an Unknown column error on MySQL.

Now, for MySQL-family databases, also collect the targetKey of any ON
clause (from any JOIN in the list) whose target table/alias matches the
current JOIN table. PostgreSQL keeps its SELECT * fallback.

issue APIJSON#824
fixes APIJSON#824

This branch has not been deployed

No deployments
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.

[Bug] join 关键字 在 count 分页的时候,会报关联的子表的字段找不到

1 participant