Avoid redundant table joins in WP_Tax_Query.

IN clauses that are connected by OR require only a single table join. To avoid
extraneous joins, keep track of generated table aliases, and let sibling
clauses piggy-back on those aliases when possible.

Introduces WP_Tax_Query::sanitize_relation() to reduce some repeated code.

Adds unit tests to verify the JOIN consolidation, and integration tests for
cases where JOINS are being combined.

Props boonebgorges, otto42, jakub.tyrcha.
Fixes #18105.

git-svn-id: https://develop.svn.wordpress.org/trunk@29902 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Boone Gorges
2014-10-15 16:39:19 +00:00
parent c995866623
commit 56c82e534f
3 changed files with 284 additions and 13 deletions

View File

@@ -218,4 +218,138 @@ class Tests_Tax_Query extends WP_UnitTestCase {
$this->assertTrue( is_wp_error( $tq->queries[0] ) );
}
/**
* @ticket 18105
*/
public function test_get_sql_relation_or_operator_in() {
register_taxonomy( 'wptests_tax', 'post' );
$t1 = $this->factory->term->create( array(
'taxonomy' => 'wptests_tax',
) );
$t2 = $this->factory->term->create( array(
'taxonomy' => 'wptests_tax',
) );
$t3 = $this->factory->term->create( array(
'taxonomy' => 'wptests_tax',
) );
$tq = new WP_Tax_Query( array(
'relation' => 'OR',
array(
'taxonomy' => 'wptests_tax',
'field' => 'term_id',
'terms' => $t1,
),
array(
'taxonomy' => 'wptests_tax',
'field' => 'term_id',
'terms' => $t2,
),
array(
'taxonomy' => 'wptests_tax',
'field' => 'term_id',
'terms' => $t3,
),
) );
global $wpdb;
$sql = $tq->get_sql( $wpdb->posts, 'ID' );
// Only one JOIN is required with OR + IN.
$this->assertSame( 1, substr_count( $sql['join'], 'JOIN' ) );
_unregister_taxonomy( 'wptests_tax' );
}
/**
* @ticket 18105
*/
public function test_get_sql_relation_and_operator_in() {
register_taxonomy( 'wptests_tax', 'post' );
$t1 = $this->factory->term->create( array(
'taxonomy' => 'wptests_tax',
) );
$t2 = $this->factory->term->create( array(
'taxonomy' => 'wptests_tax',
) );
$t3 = $this->factory->term->create( array(
'taxonomy' => 'wptests_tax',
) );
$tq = new WP_Tax_Query( array(
'relation' => 'AND',
array(
'taxonomy' => 'wptests_tax',
'field' => 'term_id',
'terms' => $t1,
),
array(
'taxonomy' => 'wptests_tax',
'field' => 'term_id',
'terms' => $t2,
),
array(
'taxonomy' => 'wptests_tax',
'field' => 'term_id',
'terms' => $t3,
),
) );
global $wpdb;
$sql = $tq->get_sql( $wpdb->posts, 'ID' );
$this->assertSame( 3, substr_count( $sql['join'], 'JOIN' ) );
_unregister_taxonomy( 'wptests_tax' );
}
/**
* @ticket 18105
*/
public function test_get_sql_nested_relation_or_operator_in() {
register_taxonomy( 'wptests_tax', 'post' );
$t1 = $this->factory->term->create( array(
'taxonomy' => 'wptests_tax',
) );
$t2 = $this->factory->term->create( array(
'taxonomy' => 'wptests_tax',
) );
$t3 = $this->factory->term->create( array(
'taxonomy' => 'wptests_tax',
) );
$tq = new WP_Tax_Query( array(
'relation' => 'OR',
array(
'taxonomy' => 'wptests_tax',
'field' => 'term_id',
'terms' => $t1,
),
array(
'relation' => 'OR',
array(
'taxonomy' => 'wptests_tax',
'field' => 'term_id',
'terms' => $t2,
),
array(
'taxonomy' => 'wptests_tax',
'field' => 'term_id',
'terms' => $t3,
),
),
) );
global $wpdb;
$sql = $tq->get_sql( $wpdb->posts, 'ID' );
$this->assertSame( 2, substr_count( $sql['join'], 'JOIN' ) );
_unregister_taxonomy( 'wptests_tax' );
}
}