Index: includes/database/select.inc
===================================================================
RCS file: /cvs/drupal/drupal/includes/database/select.inc,v
retrieving revision 1.45
diff -u -r1.45 select.inc
--- includes/database/select.inc	8 Aug 2010 02:18:53 -0000	1.45
+++ includes/database/select.inc	19 Aug 2010 01:07:36 -0000
@@ -1125,7 +1125,21 @@
       }
       drupal_alter($hooks, $query);
     }
-    return $this->prepared = TRUE;
+
+    $this->prepared = TRUE;
+
+    // Now also prepare any sub-queries.
+    foreach ($this->tables as $table) {
+      if ($table['table'] instanceof SelectQueryInterface) {
+        $table['table']->preExecute();
+      }
+    }
+
+    foreach ($this->union as $union) {
+      $union['query']->preExecute();
+    }
+
+    return $this;
   }
 
   public function execute() {
Index: modules/simpletest/tests/database_test.test
===================================================================
RCS file: /cvs/drupal/drupal/modules/simpletest/tests/database_test.test,v
retrieving revision 1.98
diff -u -r1.98 database_test.test
--- modules/simpletest/tests/database_test.test	8 Aug 2010 02:18:53 -0000	1.98
+++ modules/simpletest/tests/database_test.test	19 Aug 2010 01:07:37 -0000
@@ -2029,6 +2029,72 @@
     $this->assertEqual($crowded_job->job, $crowded_job->otherjob, t('Correctly joined same table twice.'));
     $this->assertNotEqual($crowded_job->name, $crowded_job->othername, t('Correctly joined same table twice.'));
   }
+
+}
+
+/**
+ * Test more complex select statements, part 2.
+ */
+class DatabaseSelectComplexTestCase2 extends DatabaseTestCase {
+
+  public static function getInfo() {
+    return array(
+      'name' => 'Select tests, complex 2',
+      'description' => 'Test the Select query builder with even more complex queries.',
+      'group' => 'Database',
+    );
+  }
+
+  function setUp() {
+    DrupalWebTestCase::setUp('database_test', 'node_access_test');
+
+    $schema['test'] = drupal_get_schema('test');
+    $schema['test_people'] = drupal_get_schema('test_people');
+    $schema['test_one_blob'] = drupal_get_schema('test_one_blob');
+    $schema['test_two_blobs'] = drupal_get_schema('test_two_blobs');
+    $schema['test_task'] = drupal_get_schema('test_task');
+
+    $this->installTables($schema);
+
+    $this->addSampleData();
+  }
+
+  /**
+   * Test that we can join on a query.
+   */
+  function testJoinSubquery() {
+    $acct = $this->drupalCreateUser(array('access content'));
+    $this->drupalLogin($acct);
+
+    $query = db_select('test_task', 'tt', array('target' => 'slave'));
+    $query->addExpression('tt.pid + 1', 'abc');
+    $query->condition('priority', 1, '>');
+    $query->condition('priority', 100, '<');
+
+    $subquery = db_select('test', 'tp');
+    $subquery->join('test_one_blob', 'tpb', 'tp.id = tpb.id');
+    $subquery->join('node', 'n', 'tp.id = n.nid');
+    $subquery->addTag('node_access');
+    $subquery->addMetaData('account', $acct);
+    $subquery->addField('tp', 'id');
+    $subquery->condition('age', 5, '>');
+    $subquery->condition('age', 500, '<');
+
+    $query->leftJoin($subquery, 'sq', 'tt.pid = sq.id');
+    $query->join('test_one_blob', 'tb3', 'tt.pid = tb3.id');
+
+    // Construct the query string.
+    // This is the same sequence that SelectQuery::execute() goes through.
+    $query->preExecute();
+    $query->getArguments();
+    $str = (string) $query;
+
+    // Verify that the string only has one copy of condition placeholder 0.
+    debug($str);
+    $pos = strpos($str, 'db_condition_placeholder_0', 0);
+    $pos2 = strpos($str, 'db_condition_placeholder_0', $pos + 1);
+    $this->assertFalse($pos2, "Condition placeholder is not repeated");
+  }
 }
 
 class DatabaseSelectPagerDefaultTestCase extends DatabaseTestCase {
