Merge "Database: Fix degenerate parenthesized joins"
authorjenkins-bot <jenkins-bot@gerrit.wikimedia.org>
Thu, 30 Nov 2017 04:30:28 +0000 (04:30 +0000)
committerGerrit Code Review <gerrit@wikimedia.org>
Thu, 30 Nov 2017 04:30:28 +0000 (04:30 +0000)
includes/libs/rdbms/database/Database.php
tests/phpunit/includes/libs/rdbms/database/DatabaseTest.php

index 5edf3fd..e10746c 100644 (file)
@@ -2030,9 +2030,19 @@ abstract class Database implements IDatabase, IMaintainableDatabase, LoggerAware
 
                        if ( is_array( $table ) ) {
                                // A parenthesized group
-                               $joinedTable = '('
-                                       . $this->tableNamesWithIndexClauseOrJOIN( $table, $use_index, $ignore_index, $join_conds )
-                                       . ')';
+                               if ( count( $table ) > 1 ) {
+                                       $joinedTable = '('
+                                               . $this->tableNamesWithIndexClauseOrJOIN( $table, $use_index, $ignore_index, $join_conds )
+                                               . ')';
+                               } else {
+                                       // Degenerate case
+                                       $innerTable = reset( $table );
+                                       $innerAlias = key( $table );
+                                       $joinedTable = $this->tableNameWithAlias(
+                                               $innerTable,
+                                               is_string( $innerAlias ) ? $innerAlias : $innerTable
+                                       );
+                               }
                        } else {
                                $joinedTable = $this->tableNameWithAlias( $table, $alias );
                        }
index ee7ad2f..7933f19 100644 (file)
@@ -120,6 +120,13 @@ class DatabaseTest extends PHPUnit_Framework_TestCase {
                                ],
                                'table1 LEFT JOIN (table2 JOIN table3 ON ((t2_id = t3_id))) ON ((t1_id = t2_id))'
                        ],
+                       'join with degenerate parenthesized group' => [
+                               [ 'table1', 'n' => [ 't2' => 'table2' ] ],
+                               [
+                                       'n' => [ 'LEFT JOIN', 't1_id = t2_id' ],
+                               ],
+                               'table1 LEFT JOIN table2 t2 ON ((t1_id = t2_id))'
+                       ],
                ];
        }