DatabaseSelectPagerDefaultTestCase::testInnerPagerQuery is supposed to "confirm that a pager query with inner pager query returns valid results". The test is probably broken because the feature is:

    $query = db_select('test', 't')
      ->extend('PagerDefault');
      ->fields('t', array('age'))
      ->orderBy('age')
      ->limit(5);

    $outer_query = db_select($query);
    $outer_query->addField('subquery', 'age');

    echo $outer_query->execute()->getQueryString();

Returns:

SELECT subquery.age AS age FROM (SELECT t.age AS age FROM simpletest635915test t ORDER BY age ASC) subquery

Comments

berdir’s picture

The test validates currently something slightly different, bug #3 from the initial posting in #467984: DBTNG bugfixes to be exact. Basically, that means that it failed completely before my fix, because the code did not recognise the "PagerDefault" class as a SelectQuery as it used "instanceof SelectQuery" instead of "instanceof SelectQueryInterface".

Obviously, this is broken too and once there is a bugfix, the test can be easily expanded to test for this bug too. I'll try to look into this.

berdir’s picture

StatusFileSize
new999 bytes

Ok, I found the bug and fixed the test, but I don't have a solution yet.

The problematic part is this part:

      // If the table is a subquery, compile it and integrate it into this query.
      if ($table['table'] instanceof SelectQueryInterface) {
        $table_string = '(' . (string)$table['table'] . ')';

However, PagerDefault does add its stuff in an overidden execute() method. By casting the query directly to a string, we never call execute().

I'm not sure how to fix this in a way that avoid similiar bugs in the future. Just changing PagerDefault might not be enough. The first thing that comes to my mind is making th execute() method final and try to find other ways to add the stuff to our query. That would also make all that complicated stuff to avoid calling things multiple times unecessary. But I'm not sure if we can do this at this point.

berdir’s picture

Status: Active » Needs review

Status: Needs review » Needs work

The last submitted patch, fixed_the_test_for_now.patch, failed testing.

damien tournoud’s picture

I don't believe there is any way to properly support that. There are basically two types of query extenders: those that do something nasty on execute(), those that don't. I suggest we just find a way to make sure that we raise an exception at one point if one tries to use an extender that has to do something nasty on execute() in a subquery.

berdir’s picture

Status: Needs review » Needs work
StatusFileSize
new4.84 KB

Do we even need that nasty execute stuff?

The pager test passes now, let's see what the other tests are saying...

berdir’s picture

Status: Needs work » Needs review
damien tournoud’s picture

Yes we need that nasty execute stuff.

The only function that can have a side effect is execute(). The others are supposed to be passive functions only.

berdir’s picture

Status: Needs work » Needs review
StatusFileSize
new7.23 KB

Ok, discussed this with DamZ...

- We separate query extenders into good and bad. This is done by adding a new hasExecute() method to SelectQueryExtender which is called recursively and defaults to FALSE. if an extenders needs execute() to work correctly he is supposed to override that method and return TRUE.

- We need such a method and can't rely on reflection because of the recursion. $query->extend('SearchQuery')->extend('AGoodExtender') will not have an overriden execute() method in the top-level, but can't be used as a subquery because of the Search extender.

- SeachQuery is definitly a nasty query, TableSort is not.

- We are not yet sure if PagerDefault can be converted to a good query but I think it can because of the reasons outlined above (can be overriden/called multiple times and only range() is called, no irreversible action happens)

Let's see if the tests pass now. Thoughts on the general approach?

The comments and tests can probably be improved...

Status: Needs review » Needs work

The last submitted patch, nasty_or_good.patch, failed testing.

berdir’s picture

Status: Needs work » Needs review
StatusFileSize
new6.7 KB

Diffed against a wrong version...

berdir’s picture

StatusFileSize
new6.7 KB

Well, that happens if you write patches in vi and don't even do a basic php syntax test.. ;)

Status: Needs review » Needs work

The last submitted patch, nasty_or_good3.patch, failed testing.

berdir’s picture

Status: Needs work » Needs review
StatusFileSize
new8 KB

another try...

- Added a check so that we only call hasExecute(() on extended queries
- Overriden the countQuery() method in SearchQuery so that we don't need to do it in node_search_submit() because that's not possible with the new exception anymore.

sun’s picture

Status: Needs review » Needs work
+++ includes/database/database.inc
@@ -1673,6 +1673,12 @@ class FieldsOverlapException extends Exception {}
+ * Exception thrown if an extended query that overrides execute() is used as a
+ * subquery.

Exception thrown if an extended query overriding execute() is used as subquery.

+++ includes/database/select.inc
@@ -605,6 +605,23 @@ class SelectQueryExtender implements SelectQueryInterface {
+   * Extenders that override the execute() method are supposed to return TRUE.

This summary doesn't map to the function body -- at least it rather sounds like a advanced description, not a summary about what this function is doing.

+++ includes/database/select.inc
@@ -605,6 +605,23 @@ class SelectQueryExtender implements SelectQueryInterface {
+   * ¶
+   * Only extended queries that don't are supported as subqueries, others can't
+   * be handled because subqueries are casted to a string without calling
+   * execute().

I tried to rephrase this sentence, but wasn't able to understand it.

Also, trailing white-space here.

+++ includes/pager.inc
@@ -149,7 +107,33 @@ class PagerDefault extends SelectQueryExtender {
   public function limit($limit = 10) {
...
+    ¶

Trailing white-space here.

+++ modules/search/search.extender.inc
@@ -442,4 +442,31 @@ class SearchQuery extends SelectQueryExtender {
+  public function countQuery() {
...
+    // Zero-out existing fields.
+    $fields =& $count->getFields();
+    $fields = array();

wow. Not sure what this construct is actually doing, but a short comment would be helpful, since this looks quite weird.

Powered by Dreditor.

berdir’s picture

Status: Needs work » Needs review
StatusFileSize
new9.1 KB

Thanks for the review, this was more a proof of concept so the comments were bad :)

Really need to point Crell to this....

Anyway, here is a re-roll with the comments updated. Is it more clear now? The Zero-out stuff already existed, I just copied that to SearchQuery.

Crell’s picture

Um. I'm still kinda confused. :-)

Can someone explain to me 1) What exactly the issue is here and 2) Why we need an extra method? Can't we use reflection to determine if a class overrides execute()?

berdir’s picture

1) We do not call execute() on a SelectQueryInterface object when using it as a subquery of another query. This means that extended queries which override execute() and do stuff in there don't work as expected. (That we are not calling the alter hook of subqueries is a similiar bug which is addressed in another issue).

2) I'm happy to be proven otherwise, but I don't think so. Since we are using the Decorator-Pattern, there could be two different decorators and only the inner overrides execute() (Example: A -> B -> C, B overrides execute()). I don't think we can reliably detect this with reflection. The method allows us to bubble up until either a decorator returns TRUE or we reach the SelectQuery object.

Crell’s picture

Title: DatabaseSelectPagerDefaultTestCase::testInnerPagerQuery broken » Extenders and subqueries don't play nice

1) OK, I think I follow now. When would we actually be doing that, though? I cannot think of a case where we'd want a tablesort or pager query as a subquery. What's wrong with documenting "dude, seriously, don't do that, it's dumb"?

2) What about checking with this: http://us2.php.net/manual/en/reflectionmethod.getdeclaringclass.php

Also changing title so have a space in it somewhere so it doesn't screw up the issue listing page... :-)

moshe weitzman’s picture

Priority: Critical » Normal

Based on last comment, lets downgrade this. If anyone disagrees, feel free to return to critical with a justification

webchick’s picture

This issue is rated as critical #791270: SearchQuery exact search conditions not added to CountQuery. Berdir recommends fixing this instead. Crell, does that give you the use case you're looking for on this, or..?

berdir’s picture

See my comment in the other issue, the part that is relevant for the linked issue above is only a necessary side effect of the changes proposed in this patch, I only suggest to move these changes to the other issue since they help to simplify the usage and avoid bugs for modules using SearchQuery.

berdir’s picture

#16: nasty_or_good5.patch queued for re-testing.

Status: Needs review » Needs work

The last submitted patch, nasty_or_good5.patch, failed testing.

Crell’s picture

Version: 7.x-dev » 8.x-dev

I don't think this can be fixed in the D7 cycle, if at all. The linked issue apparently got fixed already.

agileware’s picture

Does this issue also cover the case of a query with a subquery in a condition which is then used with the pager. Because that also doesn't work.

Version: 8.0.x-dev » 8.1.x-dev

Drupal 8.0.6 was released on April 6 and is the final bugfix release for the Drupal 8.0.x series. Drupal 8.0.x will not receive any further development aside from security fixes. Drupal 8.1.0-rc1 is now available and sites should prepare to update to 8.1.0.

Bug reports should be targeted against the 8.1.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.2.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.1.x-dev » 8.2.x-dev

Drupal 8.1.9 was released on September 7 and is the final bugfix release for the Drupal 8.1.x series. Drupal 8.1.x will not receive any further development aside from security fixes. Drupal 8.2.0-rc1 is now available and sites should prepare to upgrade to 8.2.0.

Bug reports should be targeted against the 8.2.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.3.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.2.x-dev » 8.3.x-dev

Drupal 8.2.6 was released on February 1, 2017 and is the final full bugfix release for the Drupal 8.2.x series. Drupal 8.2.x will not receive any further development aside from critical and security fixes. Sites should prepare to update to 8.3.0 on April 5, 2017. (Drupal 8.3.0-alpha1 is available for testing.)

Bug reports should be targeted against the 8.3.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.4.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.3.x-dev » 8.4.x-dev

Drupal 8.3.6 was released on August 2, 2017 and is the final full bugfix release for the Drupal 8.3.x series. Drupal 8.3.x will not receive any further development aside from critical and security fixes. Sites should prepare to update to 8.4.0 on October 4, 2017. (Drupal 8.4.0-alpha1 is available for testing.)

Bug reports should be targeted against the 8.4.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.5.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.4.x-dev » 8.5.x-dev

Drupal 8.4.4 was released on January 3, 2018 and is the final full bugfix release for the Drupal 8.4.x series. Drupal 8.4.x will not receive any further development aside from critical and security fixes. Sites should prepare to update to 8.5.0 on March 7, 2018. (Drupal 8.5.0-alpha1 is available for testing.)

Bug reports should be targeted against the 8.5.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.6.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.5.x-dev » 8.6.x-dev

Drupal 8.5.6 was released on August 1, 2018 and is the final bugfix release for the Drupal 8.5.x series. Drupal 8.5.x will not receive any further development aside from security fixes. Sites should prepare to update to 8.6.0 on September 5, 2018. (Drupal 8.6.0-rc1 is available for testing.)

Bug reports should be targeted against the 8.6.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.7.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

andypost’s picture

Version: 8.8.x-dev » 8.9.x-dev

Drupal 8.8.0-alpha1 will be released the week of October 14th, 2019, which means new developments and disruptive changes should now be targeted against the 8.9.x-dev branch. (Any changes to 8.9.x will also be committed to 9.0.x in preparation for Drupal 9’s release, but some changes like significant feature additions will be deferred to 9.1.x.). For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

Version: 8.9.x-dev » 9.1.x-dev

Drupal 8.9.0-beta1 was released on March 20, 2020. 8.9.x is the final, long-term support (LTS) minor release of Drupal 8, which means new developments and disruptive changes should now be targeted against the 9.1.x-dev branch. For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

Version: 9.1.x-dev » 9.2.x-dev

Drupal 9.1.0-alpha1 will be released the week of October 19, 2020, which means new developments and disruptive changes should now be targeted for the 9.2.x-dev branch. For more information see the Drupal 9 minor version schedule and the Allowed changes during the Drupal 9 release cycle.

Version: 9.2.x-dev » 9.3.x-dev

Drupal 9.2.0-alpha1 will be released the week of May 3, 2021, which means new developments and disruptive changes should now be targeted for the 9.3.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.3.x-dev » 9.4.x-dev

Drupal 9.3.0-rc1 was released on November 26, 2021, which means new developments and disruptive changes should now be targeted for the 9.4.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.4.x-dev » 9.5.x-dev

Drupal 9.4.0-alpha1 was released on May 6, 2022, which means new developments and disruptive changes should now be targeted for the 9.5.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.5.x-dev » 10.1.x-dev

Drupal 9.5.0-beta2 and Drupal 10.0.0-beta2 were released on September 29, 2022, which means new developments and disruptive changes should now be targeted for the 10.1.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 10.1.x-dev » 11.x-dev

Drupal core is moving towards using a “main” branch. As an interim step, a new 11.x branch has been opened, as Drupal.org infrastructure cannot currently fully support a branch named main. New developments and disruptive changes should now be targeted for the 11.x branch, which currently accepts only minor-version allowed changes. For more information, see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 11.x-dev » main

Drupal core is now using the main branch as the primary development branch. New developments and disruptive changes should now be targeted to the main branch.

Read more in the announcement.

luke.stewart’s picture

Status: Needs work » Postponed (maintainer needs more info)
Issue tags: +Bug Smash Initiative

Currently the least recently updated core issue that is active or needs work.

Apart from an issue summary update this was last engaged with a long long long time ago.

Given this I'm going to mark this as PMNMI.

If it's still a problem that should be address please set it back to needs work.

Otherwise I think this could be closed in 3 months.