My apologies in advance for this confusing report. I am hoping someone will recognize the SQL below and can tell me what module is or might be creating it.

I am a programmer (mostly perl but I have written a couple of simple Drupal modules for unique customer needs and submitted a couple of Drupal module PostgreSQL patches) and very familiar with SQL. We are running Drupal 7.22 with PostgreSQL 9.1 on both a dev site and a production site which both show this same issue.

This is from the PostgreSQL logs. I do not see anything in the Drupal report of recent log entries.

CDT ERROR: for SELECT DISTINCT, ORDER BY expressions must appear in select list at character 363

CDT STATEMENT: SELECT DISTINCT nc.nid FROM node_comment_statistics nc INNER JOIN node_access na ON na.nid = nc.nid LEFT JOIN node n ON nc.nid = n.nid WHERE (na.grant_view >= 1 AND ((na.gid = 0 AND na.realm = 'all') OR (na.gid = 0 AND na.realm = 'domain_site') OR (na.gid = 0 AND na.realm = 'domain_id'))) AND (n.type <> 'webform_report') AND ( nc.comment_count > 0 )ORDER BY nc.last_comment_timestamp DESC LIMIT 10 OFFSET 0

This should be an easy, easy fix...BUT what is making me crazy is that I can not find the module that is generating this SQL. I have recursively grepped in the (debian) /usr/share/drupal7/ directory for "node_comment_statistics" and "last_comment_timestamp" and examined every file that contains that table or field name. As far as I have been able to find, no Drupal7 module or file in drupal7/includes is generating this SQL either statically or dynamically using the d7 API.

So, does anyone recognize this SQL and can tell me where it might be coming from?

Thank you.

Comments

neRok’s picture

Category: bug » support

Im not an SQL expert, but it seems that statement is comparing node_comment_statistics with node_access, sorting by last_comment_timestamp, and filtering by a few things including node type webform_report.

This sounds like a view of webform_report nodes to me, sorted by last comment date (with applicable node access checks). Chances are you are using the Webform Report module?! It could also be an admin-view of webform_reports?

Andy Dorman’s picture

Thanks, we are indeed using Webform Report. I will look in that direction.

Pancho’s picture

Project: Drupal core » Webform Report
Version: 7.22 » 7.x-1.0
Component: database system » Code
Category: support » bug

Tentatively moving this one to Webform Report's queue and recategorizing it as bug.

jimbullington’s picture

Webform Report does not issue any such query. It looks like some sort of node access query to me... Look at _node_query_node_access_alter() in node.module...

Andy Dorman’s picture

JimBullington is correct. I said we use Webform Report, but as it turns out we use Webform Report on an older D6 site, not on any of the D7 sites.

However, I do not believe this is coming from the node module...I searched through the code in the D7 node module and there is only one instance where that module querys the node_comment_statistics table... to get the count of comments.

$comments = db_query('SELECT comment_count FROM {node_comment_statistics} WHERE nid = :nid', array('nid' => $node->nid))->fetchField();

At this point I am thinking this must be a dynamically generated query from a module like views that pulls the table names and parameters from the db. That would explain why my searches through the code do not show any query that includes node_comment_statistics aliased to 'nc' (everyone aliases it to 'ncs').

I have been digging through Views with no luck so far. I was just hoping someone might recognize the purpose of this query and that would help pinpoint where I need to focus working on a fix.

Pancho’s picture

Project: Webform Report » Drupal core
Version: 7.x-1.0 » 7.x-dev
Component: Code » other
Status: Active » Postponed (maintainer needs more info)

Moving back to the Drupal core queue.
Now, if you say you don't have Webform Report installed on this site, it should be quite easy. Remove the uninstalled Webform Report module from the file system if it is around. Then just do a grep for 'webform_report', and it should give you very few matches, maybe even just the one you're looking for...

neRok’s picture

Still sounds like a view with dud settings to me. I would go through each view (views containing 'content', as compared to comments or users).

Andy Dorman’s picture

Thank you Pancho and neRok for your thoughts.

And this is where it starts getting weird. I made sure webform_report was not in our D7 modules directory. Then I grepped for webform_report recursively in both drupal7/modules and drupal7/includes and that text does not exist anywhere in the code as far as egrep is concerned.

So the term 'webform_report' MUST be coming from a db query field and the only dynamic query generator I know of that we use is Views.

I have already done a lot of looking in views, with no luck so far. But I will continue looking.

I was also thinking, is this something that Drupal's db abstraction layer should look for or possibly even fix? I mean, it IS a straightforward rule that ALL fields in a ORDER BY must also be in the SELECT list, no?

Pancho’s picture

Then I grepped for webform_report recursively in both drupal7/modules and drupal7/includes and that text does not exist anywhere in the code as far as egrep is concerned.

Just to make sure we didn't miss something easy, before it's getting more difficult: Did you also grep 'drupal7/sites/all/modules'? A full grep -r 'webform_report' drupal7/* would be best.
Secondly, did you check your views? Finally, even if dynamically built - 'webform_report' must be somewhere. It can't just appear out of the nothing... :)

Andy Dorman’s picture

Good points Pancho and I agree with you.

We are running Debian, so I expanded the grep to look at everything in the /usr/share/drupal7/* dir, including and specifically /usr/share/drupal7/sites/*. Nothing about webform_report anywhere.

I also just finished talking with our site editor that builds all our dynamic views. He agrees that a view that specifically excludes the node type webform_report would stand out, especially since we do not have D7 webform_report installed. He does not recall ever excluding webform_report in any view....but he is going back to take a look.

The sites in question (FanMail.com and CollegeEducation411.com) are new enough that they are not very big yet. So there is a fairly small number of views to examine. He is checking them all and so far has not seen webform_report mentioned anywhere.

Now, we DO use webform on both of these sites. It seems unlikely to me that a module developer outside of webform is going to go to the trouble of writing a view that excludes the webform_report node type. So I am now looking at every query that webform does to see if any bear any resemblance to our problem SQL query. FWIW, webform does only one DISTINCT query which obviously does not match and nowhere do they try to orderBy anything resembling a timestamp.

neRok’s picture

Do a search of the database.