fix(2meet-data-optimizer): join each routed group and drop the redundant GROUP BY

- posts_join joins the flat table of every routed group (a wp_core key next to a post type group no longer fails with "Unknown column")
- Flat tables are one row per post (UNIQUE post_id): remove the posts_groupby hook; skip the INNER JOIN status/date copy route for NOT EXISTS, which must match posts without a flat row
- Verify: tests/live/run.php router (9/9); qbench Q05 475 == native, no SQL error; price filter EXPLAIN has no Using temporary
- PR: PR-061
This commit is contained in:
2026-09-26 19:47:53 +08:00
parent b338925605
commit f2e261c522
5 changed files with 113 additions and 55 deletions
@@ -58,7 +58,7 @@ class PostQueryRouterCopyTest extends TestCase {
$mode->setValue( null, null );
}
/** @return array{join:string, where:string, orderby:string, groupby:string} */
/** @return array{join:string, where:string, orderby:string} */
private function sql( array $vars ): array {
$query = new WP_Query();
foreach ( array_merge(
@@ -77,7 +77,6 @@ class PostQueryRouterCopyTest extends TestCase {
'join' => $router->posts_join( '', $query ),
'where' => $router->posts_where( " AND wp_posts.post_status = 'publish'", $query ),
'orderby' => $router->posts_orderby( 'wp_posts.post_date DESC', $query ),
'groupby' => $router->posts_groupby( '', $query ),
);
}
@@ -85,7 +84,6 @@ class PostQueryRouterCopyTest extends TestCase {
$this->assertStringContainsString( 'LEFT JOIN', $sql['join'] );
$this->assertStringNotContainsString( self::ALIAS . '.`post_status`', $sql['where'] );
$this->assertSame( 'wp_posts.post_date DESC', $sql['orderby'] );
$this->assertSame( '`wp_itest_posts`.`ID`', $sql['groupby'] );
}
public function test_ready_copies_filter_status_and_order_by_date_on_the_flat_table(): void {
@@ -95,7 +93,6 @@ class PostQueryRouterCopyTest extends TestCase {
$this->assertStringContainsString( self::ALIAS . ".`post_status` IN ('publish')", $sql['where'] );
$this->assertStringContainsString( "wp_posts.post_status = 'publish'", $sql['where'], 'WordPress status clause stays' );
$this->assertSame( self::ALIAS . '.`post_date` ASC', $sql['orderby'] );
$this->assertSame( '', $sql['groupby'], 'one row per post: no GROUP BY, so LIMIT can stop early' );
}
public function test_several_explicit_statuses_are_kept(): void {
@@ -123,4 +120,9 @@ class PostQueryRouterCopyTest extends TestCase {
$this->assert_legacy_shape( $this->sql( array( 'post_status' => '' ) ) );
$this->assert_legacy_shape( $this->sql( array( 'post_status' => 'any' ) ) );
}
public function test_not_exists_keeps_the_legacy_shape(): void {
// The INNER JOIN would drop posts without a flat row, which NOT EXISTS must match.
$this->assert_legacy_shape( $this->sql( array( 'meta_query' => array( array( 'key' => 'rc_price', 'compare' => 'NOT EXISTS' ) ) ) ) );
}
}
+28
View File
@@ -214,6 +214,34 @@ class PostQueryRouterTest extends TestCase {
$this->assertStringContainsString( 'wpdo_post_wc_product', $join );
}
public function test_posts_join_joins_every_routed_group(): void {
$this->set_post_mode( 'aeav_only' );
$router = new WPDO_Post_Query_Router();
$query = $this->make_query( array(
'post_type' => 'product',
'meta_query' => array(
array( 'key' => '_price', 'value' => '50', 'compare' => '>=', 'type' => 'NUMERIC' ),
array( 'key' => '_thumbnail_id', 'compare' => 'EXISTS' ),
),
) );
$router->pre_get_posts( $query );
$join = $router->posts_join( '', $query );
$where = $router->posts_where( '', $query );
$this->assertStringContainsString( 'wpdo_post_wc_product', $join );
$this->assertStringContainsString( 'wpdo_post_wp_core', $join );
$this->assertStringContainsString( '`wpdoflat_product_wp_core`.`_thumbnail_id` IS NOT NULL', $where );
}
public function test_no_group_by_is_added_for_one_row_per_post_joins(): void {
unset( $GLOBALS['_wp_filter_callbacks']['posts_groupby'] );
( new WPDO_Post_Query_Router() )->register_hooks();
$this->assertEmpty( $GLOBALS['_wp_filter_callbacks']['posts_groupby'] ?? array() );
}
public function test_posts_where_appends_condition_for_routed_clause(): void {
$this->set_post_mode( 'aeav_only' );