<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>[29902] trunk: Avoid redundant table joins in WP_Tax_Query.</title>
</head>
<body>

<style type="text/css"><!--
#msg dl.meta { border: 1px #006 solid; background: #369; padding: 6px; color: #fff; }
#msg dl.meta dt { float: left; width: 6em; font-weight: bold; }
#msg dt:after { content:':';}
#msg dl, #msg dt, #msg ul, #msg li, #header, #footer, #logmsg { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt;  }
#msg dl a { font-weight: bold}
#msg dl a:link    { color:#fc3; }
#msg dl a:active  { color:#ff0; }
#msg dl a:visited { color:#cc6; }
h3 { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; font-weight: bold; }
#msg pre { overflow: auto; background: #ffc; border: 1px #fa0 solid; padding: 6px; }
#logmsg { background: #ffc; border: 1px #fa0 solid; padding: 1em 1em 0 1em; }
#logmsg p, #logmsg pre, #logmsg blockquote { margin: 0 0 1em 0; }
#logmsg p, #logmsg li, #logmsg dt, #logmsg dd { line-height: 14pt; }
#logmsg h1, #logmsg h2, #logmsg h3, #logmsg h4, #logmsg h5, #logmsg h6 { margin: .5em 0; }
#logmsg h1:first-child, #logmsg h2:first-child, #logmsg h3:first-child, #logmsg h4:first-child, #logmsg h5:first-child, #logmsg h6:first-child { margin-top: 0; }
#logmsg ul, #logmsg ol { padding: 0; list-style-position: inside; margin: 0 0 0 1em; }
#logmsg ul { text-indent: -1em; padding-left: 1em; }#logmsg ol { text-indent: -1.5em; padding-left: 1.5em; }
#logmsg > ul, #logmsg > ol { margin: 0 0 1em 0; }
#logmsg pre { background: #eee; padding: 1em; }
#logmsg blockquote { border: 1px solid #fa0; border-left-width: 10px; padding: 1em 1em 0 1em; background: white;}
#logmsg dl { margin: 0; }
#logmsg dt { font-weight: bold; }
#logmsg dd { margin: 0; padding: 0 0 0.5em 0; }
#logmsg dd:before { content:'\00bb';}
#logmsg table { border-spacing: 0px; border-collapse: collapse; border-top: 4px solid #fa0; border-bottom: 1px solid #fa0; background: #fff; }
#logmsg table th { text-align: left; font-weight: normal; padding: 0.2em 0.5em; border-top: 1px dotted #fa0; }
#logmsg table td { text-align: right; border-top: 1px dotted #fa0; padding: 0.2em 0.5em; }
#logmsg table thead th { text-align: center; border-bottom: 1px solid #fa0; }
#logmsg table th.Corner { text-align: left; }
#logmsg hr { border: none 0; border-top: 2px dashed #fa0; height: 1px; }
#header, #footer { color: #fff; background: #636; border: 1px #300 solid; padding: 6px; }
#patch { width: 100%; }
#patch h4 {font-family: verdana,arial,helvetica,sans-serif;font-size:10pt;padding:8px;background:#369;color:#fff;margin:0;}
#patch .propset h4, #patch .binary h4 {margin:0;}
#patch pre {padding:0;line-height:1.2em;margin:0;}
#patch .diff {width:100%;background:#eee;padding: 0 0 10px 0;overflow:auto;}
#patch .propset .diff, #patch .binary .diff  {padding:10px 0;}
#patch span {display:block;padding:0 10px;}
#patch .modfile, #patch .addfile, #patch .delfile, #patch .propset, #patch .binary, #patch .copfile {border:1px solid #ccc;margin:10px 0;}
#patch ins {background:#dfd;text-decoration:none;display:block;padding:0 10px;}
#patch del {background:#fdd;text-decoration:none;display:block;padding:0 10px;}
#patch .lines, .info {color:#888;background:#fff;}
--></style>
<div id="msg">
<dl class="meta" style="font-size: 105%">
<dt style="float: left; width: 6em; font-weight: bold">Revision</dt> <dd><a style="font-weight: bold" href="https://core.trac.wordpress.org/changeset/29902">29902</a><script type="application/ld+json">{"@context":"http://schema.org","@type":"EmailMessage","description":"Review this Commit","action":{"@type":"ViewAction","url":"https://core.trac.wordpress.org/changeset/29902","name":"Review Commit"}}</script></dd>
<dt style="float: left; width: 6em; font-weight: bold">Author</dt> <dd>boonebgorges</dd>
<dt style="float: left; width: 6em; font-weight: bold">Date</dt> <dd>2014-10-15 16:39:19 +0000 (Wed, 15 Oct 2014)</dd>
</dl>

<pre style='padding-left: 1em; margin: 2em 0; border-left: 2px solid #ccc; line-height: 1.25; font-size: 105%; font-family: sans-serif'>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 <a href="https://core.trac.wordpress.org/ticket/18105">#18105</a>.</pre>

<h3>Modified Paths</h3>
<ul>
<li><a href="#trunksrcwpincludestaxonomyphp">trunk/src/wp-includes/taxonomy.php</a></li>
<li><a href="#trunktestsphpunittestspostqueryphp">trunk/tests/phpunit/tests/post/query.php</a></li>
<li><a href="#trunktestsphpunitteststermqueryphp">trunk/tests/phpunit/tests/term/query.php</a></li>
</ul>

</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunksrcwpincludestaxonomyphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/src/wp-includes/taxonomy.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/wp-includes/taxonomy.php        2014-10-15 15:42:45 UTC (rev 29901)
+++ trunk/src/wp-includes/taxonomy.php  2014-10-15 16:39:19 UTC (rev 29902)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -715,8 +715,8 @@
</span><span class="cx" style="display: block; padding: 0 10px">         * }
</span><span class="cx" style="display: block; padding: 0 10px">         */
</span><span class="cx" style="display: block; padding: 0 10px">        public function __construct( $tax_query ) {
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                if ( isset( $tax_query['relation'] ) && strtoupper( $tax_query['relation'] ) == 'OR' ) {
-                       $this->relation = 'OR';
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         if ( isset( $tax_query['relation'] ) ) {
+                       $this->relation = $this->sanitize_relation( $tax_query['relation'] );
</ins><span class="cx" style="display: block; padding: 0 10px">                 } else {
</span><span class="cx" style="display: block; padding: 0 10px">                        $this->relation = 'AND';
</span><span class="cx" style="display: block; padding: 0 10px">                }
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -749,7 +749,7 @@
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">                foreach ( $queries as $key => $query ) {
</span><span class="cx" style="display: block; padding: 0 10px">                        if ( 'relation' === $key ) {
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                                $cleaned_query['relation'] = $query;
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                         $cleaned_query['relation'] = $this->sanitize_relation( $query );
</ins><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">                        // First-order clause.
</span><span class="cx" style="display: block; padding: 0 10px">                        } else if ( self::is_first_order_clause( $query ) ) {
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -786,6 +786,11 @@
</span><span class="cx" style="display: block; padding: 0 10px">                                $cleaned_subquery = $this->sanitize_query( $query );
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">                                if ( ! empty( $cleaned_subquery ) ) {
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                                        // All queries with children must have a relation.
+                                       if ( ! isset( $cleaned_subquery['relation'] ) ) {
+                                               $cleaned_subquery['relation'] = 'AND';
+                                       }
+
</ins><span class="cx" style="display: block; padding: 0 10px">                                         $cleaned_query[] = $cleaned_subquery;
</span><span class="cx" style="display: block; padding: 0 10px">                                }
</span><span class="cx" style="display: block; padding: 0 10px">                        }
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -795,6 +800,23 @@
</span><span class="cx" style="display: block; padding: 0 10px">        }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">        /**
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         * Sanitize a 'relation' operator.
+        *
+        * @since 4.1.0
+        * @access public
+        *
+        * @param string $relation Raw relation key from the query argument.
+        * @return Sanitized relation ('AND' or 'OR').
+        */
+       public function sanitize_relation( $relation ) {
+               if ( 'OR' === strtoupper( $relation ) ) {
+                       return 'OR';
+               } else {
+                       return 'AND';
+               }
+       }
+
+       /**
</ins><span class="cx" style="display: block; padding: 0 10px">          * Determine whether a clause is first-order.
</span><span class="cx" style="display: block; padding: 0 10px">         *
</span><span class="cx" style="display: block; padding: 0 10px">         * A "first-order" clause is one that contains any of the first-order
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -852,7 +874,12 @@
</span><span class="cx" style="display: block; padding: 0 10px">         * }
</span><span class="cx" style="display: block; padding: 0 10px">         */
</span><span class="cx" style="display: block; padding: 0 10px">        protected function get_sql_clauses() {
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                $sql = $this->get_sql_for_query( $this->queries );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         /*
+                * $queries are passed by reference to get_sql_for_query() for recursion.
+                * To keep $this->queries unaltered, pass a copy.
+                */
+               $queries = $this->queries;
+               $sql = $this->get_sql_for_query( $queries );
</ins><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">                if ( ! empty( $sql['where'] ) ) {
</span><span class="cx" style="display: block; padding: 0 10px">                        $sql['where'] = ' AND ' . $sql['where'];
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -880,7 +907,7 @@
</span><span class="cx" style="display: block; padding: 0 10px">         *     @type string $where SQL fragment to append to the main WHERE clause.
</span><span class="cx" style="display: block; padding: 0 10px">         * }
</span><span class="cx" style="display: block; padding: 0 10px">         */
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-        protected function get_sql_for_query( $query, $depth = 0 ) {
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ protected function get_sql_for_query( &$query, $depth = 0 ) {
</ins><span class="cx" style="display: block; padding: 0 10px">                 $sql_chunks = array(
</span><span class="cx" style="display: block; padding: 0 10px">                        'join'  => array(),
</span><span class="cx" style="display: block; padding: 0 10px">                        'where' => array(),
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -896,7 +923,7 @@
</span><span class="cx" style="display: block; padding: 0 10px">                        $indent .= "  ";
</span><span class="cx" style="display: block; padding: 0 10px">                }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                foreach ( $query as $key => $clause ) {
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         foreach ( $query as $key => &$clause ) {
</ins><span class="cx" style="display: block; padding: 0 10px">                         if ( 'relation' === $key ) {
</span><span class="cx" style="display: block; padding: 0 10px">                                $relation = $query['relation'];
</span><span class="cx" style="display: block; padding: 0 10px">                        } else if ( is_array( $clause ) ) {
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -961,7 +988,7 @@
</span><span class="cx" style="display: block; padding: 0 10px">         *     @type string $where SQL fragment to append to the main WHERE clause.
</span><span class="cx" style="display: block; padding: 0 10px">         * }
</span><span class="cx" style="display: block; padding: 0 10px">         */
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-        public function get_sql_for_clause( $clause, $parent_query ) {
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ public function get_sql_for_clause( &$clause, $parent_query ) {
</ins><span class="cx" style="display: block; padding: 0 10px">                 global $wpdb;
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">                $sql = array(
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -988,14 +1015,27 @@
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">                        $terms = implode( ',', $terms );
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                        $i = count( $this->table_aliases );
-                       $alias = $i ? 'tt' . $i : $wpdb->term_relationships;
-                       $this->table_aliases[] = $alias;
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                 /*
+                        * Before creating another table join, see if this clause has a
+                        * sibling with an existing join that can be shared.
+                        */
+                       $alias = $this->find_compatible_table_alias( $clause, $parent_query );
+                       if ( false === $alias ) {
+                               $i = count( $this->table_aliases );
+                               $alias = $i ? 'tt' . $i : $wpdb->term_relationships;
</ins><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                        $join .= " INNER JOIN $wpdb->term_relationships";
-                       $join .= $i ? " AS $alias" : '';
-                       $join .= " ON ($this->primary_table.$this->primary_id_column = $alias.object_id)";
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                         // Store the alias as part of a flat array to build future iterators.
+                               $this->table_aliases[] = $alias;
</ins><span class="cx" style="display: block; padding: 0 10px"> 
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                                // Store the alias with this clause, so later siblings can use it.
+                               $clause['alias'] = $alias;
+
+                               $join .= " INNER JOIN $wpdb->term_relationships";
+                               $join .= $i ? " AS $alias" : '';
+                               $join .= " ON ($this->primary_table.$this->primary_id_column = $alias.object_id)";
+                       }
+
+
</ins><span class="cx" style="display: block; padding: 0 10px">                         $where = "$alias.term_taxonomy_id $operator ($terms)";
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">                } elseif ( 'NOT IN' == $operator ) {
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -1047,7 +1087,58 @@
</span><span class="cx" style="display: block; padding: 0 10px">                return $sql;
</span><span class="cx" style="display: block; padding: 0 10px">        }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+        /**
+        * Identify an existing table alias that is compatible with the current query clause.
+        *
+        * We avoid unnecessary table joins by allowing each clause to look for
+        * an existing table alias that is compatible with the query that it
+        * needs to perform. An existing alias is compatible if (a) it is a
+        * sibling of $clause (ie, it's under the scope of the same relation),
+        * and (b) the combination of operator and relation between the clauses
+        * allows for a shared table join. In the case of WP_Tax_Query, this
+        * only applies to IN clauses that are connected by the relation OR.
+        *
+        * @since 4.1.0
+        * @access protected
+        *
+        * @param  array       $clause       Query clause.
+        * @param  array       $parent_query Parent query of $clause.
+        * @return string|bool Table alias if found, otherwise false.
+        */
+       protected function find_compatible_table_alias( $clause, $parent_query ) {
+               $alias = false;
</ins><span class="cx" style="display: block; padding: 0 10px"> 
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                // Sanity check. Only IN queries use the JOIN syntax .
+               if ( ! isset( $clause['operator'] ) || 'IN' !== $clause['operator'] ) {
+                       return $alias;
+               }
+
+               // Since we're only checking IN queries, we're only concerned with OR relations.
+               if ( ! isset( $parent_query['relation'] ) || 'OR' !== $parent_query['relation'] ) {
+                       return $alias;
+               }
+
+               $compatible_operators = array( 'IN' );
+
+               foreach ( $parent_query as $sibling ) {
+                       if ( ! is_array( $sibling ) || ! $this->is_first_order_clause( $sibling ) ) {
+                               continue;
+                       }
+
+                       if ( empty( $sibling['alias'] ) || empty( $sibling['operator'] ) ) {
+                               continue;
+                       }
+
+                       // The sibling must both have compatible operator to share its alias.
+                       if ( in_array( strtoupper( $sibling['operator'] ), $compatible_operators ) ) {
+                               $alias = $sibling['alias'];
+                               break;
+                       }
+               }
+
+               return $alias;
+       }
+
</ins><span class="cx" style="display: block; padding: 0 10px">         /**
</span><span class="cx" style="display: block; padding: 0 10px">         * Validates a single query.
</span><span class="cx" style="display: block; padding: 0 10px">         *
</span></span></pre></div>
<a id="trunktestsphpunittestspostqueryphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/tests/phpunit/tests/post/query.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/tests/phpunit/tests/post/query.php  2014-10-15 15:42:45 UTC (rev 29901)
+++ trunk/tests/phpunit/tests/post/query.php    2014-10-15 16:39:19 UTC (rev 29902)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -1376,7 +1376,53 @@
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">        /**
</span><span class="cx" style="display: block; padding: 0 10px">         * @group taxonomy
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         * @ticket 18105
</ins><span class="cx" style="display: block; padding: 0 10px">          */
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+        public function test_tax_query_single_query_multiple_queries_operator_not_in() {
+               $t1 = $this->factory->term->create( array(
+                       'taxonomy' => 'category',
+                       'slug' => 'foo',
+                       'name' => 'Foo',
+               ) );
+               $t2 = $this->factory->term->create( array(
+                       'taxonomy' => 'category',
+                       'slug' => 'bar',
+                       'name' => 'Bar',
+               ) );
+               $p1 = $this->factory->post->create();
+               $p2 = $this->factory->post->create();
+               $p3 = $this->factory->post->create();
+
+               wp_set_post_terms( $p1, $t1, 'category' );
+               wp_set_post_terms( $p2, $t2, 'category' );
+
+               $q = new WP_Query( array(
+                       'fields' => 'ids',
+                       'update_post_meta_cache' => false,
+                       'update_post_term_cache' => false,
+                       'tax_query' => array(
+                               'relation' => 'AND',
+                               array(
+                                       'taxonomy' => 'category',
+                                       'terms' => array( 'foo' ),
+                                       'field' => 'slug',
+                                       'operator' => 'NOT IN',
+                               ),
+                               array(
+                                       'taxonomy' => 'category',
+                                       'terms' => array( 'bar' ),
+                                       'field' => 'slug',
+                                       'operator' => 'NOT IN',
+                               ),
+                       ),
+               ) );
+
+               $this->assertEquals( array( $p3 ), $q->posts );
+       }
+
+       /**
+        * @group taxonomy
+        */
</ins><span class="cx" style="display: block; padding: 0 10px">         public function test_tax_query_single_query_multiple_terms_operator_and() {
</span><span class="cx" style="display: block; padding: 0 10px">                $t1 = $this->factory->term->create( array(
</span><span class="cx" style="display: block; padding: 0 10px">                        'taxonomy' => 'category',
</span></span></pre></div>
<a id="trunktestsphpunitteststermqueryphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/tests/phpunit/tests/term/query.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/tests/phpunit/tests/term/query.php  2014-10-15 15:42:45 UTC (rev 29901)
+++ trunk/tests/phpunit/tests/term/query.php    2014-10-15 16:39:19 UTC (rev 29902)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -218,4 +218,138 @@
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">                $this->assertTrue( is_wp_error( $tq->queries[0] ) );
</span><span class="cx" style="display: block; padding: 0 10px">        }
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+
+       /**
+        * @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' );
+       }
+
</ins><span class="cx" style="display: block; padding: 0 10px"> }
</span></span></pre>
</div>
</div>

</body>
</html>