Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
* warning: pg_query() [function.pg-query]: Query failed: ERREUR: clauses ORDER BY multiples non autorisées in /home/html/test/includes/database.pgsql.inc on line 139.
* user warning: query: (SELECT thread FROM comments WHERE nid = 63743 AND status = 0 ORDER BY timestamp DESC LIMIT 136) ORDER BY thread DESC LIMIT 1 in /home/html/test/modules/comment/comment.module on line 366.
* warning: pg_query() [function.pg-query]: Query failed: ERREUR: clauses ORDER BY multiples non autorisées in /home/html/test/includes/database.pgsql.inc on line 139.
* user warning: query: (SELECT thread FROM comments WHERE nid = 67329 AND status = 0 ORDER BY timestamp DESC LIMIT 56) ORDER BY thread DESC LIMIT 1 in /home/html/test/modules/comment/comment.module on line 366.
* warning: pg_query() [function.pg-query]: Query failed: ERREUR: clauses ORDER BY multiples non autorisées in /home/html/test/includes/database.pgsql.inc on line 139.
* user warning: query: (SELECT thread FROM comments WHERE nid = 49805 AND status = 0 ORDER BY timestamp DESC LIMIT 326) ORDER BY thread DESC LIMIT 1 in /home/html/test/modules/comment/comment.module on line 366.
* warning: pg_query() [function.pg-query]: Query failed: ERREUR: clauses ORDER BY multiples non autorisées in /home/html/test/includes/database.pgsql.inc on line 139.
* user warning: query: (SELECT thread FROM comments WHERE nid = 66944 AND status = 0 ORDER BY timestamp DESC LIMIT 125) ORDER BY thread DESC LIMIT 1 in /home/html/test/modules/comment/comment.module on line 366.
* warning: pg_query() [function.pg-query]: Query failed: ERREUR: clauses ORDER BY multiples non autorisées in /home/html/test/includes/database.pgsql.inc on line 139.
* user warning: query: (SELECT thread FROM comments WHERE nid = 58110 AND status = 0 ORDER BY timestamp DESC LIMIT 14) ORDER BY thread DESC LIMIT 1 in /home/html/test/modules/comment/comment.module on line 366.
* warning: pg_query() [function.pg-query]: Query failed: ERREUR: clauses ORDER BY multiples non autorisées in /home/html/test/includes/database.pgsql.inc on line 139.
* user warning: query: (SELECT thread FROM comments WHERE nid = 66801 AND status = 0 ORDER BY timestamp DESC LIMIT 165) ORDER BY thread DESC LIMIT 1 in /home/html/test/modules/comment/comment.module on line 366.
* warning: pg_query() [function.pg-query]: Query failed: ERREUR: clauses ORDER BY multiples non autorisées in /home/html/test/includes/database.pgsql.inc on line 139.
* user warning: query: (SELECT thread FROM comments WHERE nid = 67321 AND status = 0 ORDER BY timestamp DESC LIMIT 58) ORDER BY thread DESC LIMIT 1 in /home/html/test/modules/comment/comment.module on line 366.
<code>
<code>$result = db_query('(SELECT thread FROM {comments} WHERE nid = %d AND status = 0 ORDER BY timestamp DESC LIMIT %d) ORDER BY thread DESC LIMIT 1', $node->nid, $new_replies);
Should be:
$result = db_query('SELECT thread FROM {comments} WHERE cid IN (SELECT cid FROM comments WHERE nid = %d AND status = 0 ORDER BY timestamp DESC LIMIT %d) ORDER BY thread DESC LIMIT 1', $node->nid, $new_replies);
The same applies for:
$result = db_query('(SELECT thread FROM {comments} WHERE nid = %d AND status = 0 ORDER BY timestamp DESC LIMIT %d) ORDER BY SUBSTRING(thread, 1, (LENGTH(thread) - 1)) LIMIT 1', $node->nid, $new_replies);
Comment | File | Size | Author |
---|---|---|---|
#47 | comment_pg_d6.patch | 1.96 KB | andypost |
#42 | comment_pg_d6.patch | 1.96 KB | andypost |
#30 | comment_pg.patch | 1.07 KB | andypost |
#27 | drupal-comment-module-order-by.diff | 1.68 KB | Shiny |
Comments
Comment #1
grub3 CreditAttribution: grub3 commentedSEcond query should be:
$result = db_query('(SELECT thread FROM {comments} WHERE cid IN (SELECT cid FROM {comments} WHERE nid = %d AND status = 0 ORDER BY timestamp DESC LIMIT %d) ORDER BY SUBSTRING(thread, 1, (LENGTH(thread) - 1)) LIMIT 1', $node->nid, $new_replies);
Comment #2
grub3 CreditAttribution: grub3 commentedUpdate to critical as all forum running under PostgreSQL should have the same problem.
Comment #3
Damien Tournoud CreditAttribution: Damien Tournoud commentedCould you point us to where this is actually documented?
Comment #4
grub3 CreditAttribution: grub3 commentedI don't know where this is documented. Actually the message displays "clauses ORDER BY multiples non autorisées", which means "multiple ORDER BY not authorized".
The query I provided is a traditional nested query. I can be executed on any database. MySQL query analyser should rewrite the query as a traditional nested query with some kind of IN. Therefore there is no degradation using the SQL query I provided.
If MySQL has some kind of SQL parser, try looking how MySQL rewrites the query.
Comment #5
grub3 CreditAttribution: grub3 commentedThe idea behind this fix is that when you write this MySQLism, MySQL is going to rewrite the query inside the engine. It will surely not execute like that. So it is better to write the correct SQL directly and there is no time execution loss.
Comment #6
Damien Tournoud CreditAttribution: Damien Tournoud commented@jmpoure: you can really not call that query an "MySQLism", especially if you can't point us to where it is documented that you can't have an ORDER in a FROM-based subquery.
Comment #7
grub3 CreditAttribution: grub3 commentedA database should always know how it should run joins.
(SELECT thread FROM comments WHERE nid = 63743 AND status = 0 ORDER BY timestamp DESC LIMIT 136) ORDER BY thread DESC LIMIT 1
is not a standard query. I can be rewritten internaly in MySQL as a subquery or a join.
To know whether this is a standard query, you can access the analyser in the database and read how the query is rewritten.
MySQL will not execute(SELECT thread FROM comments WHERE nid = 63743 AND status = 0 ORDER BY timestamp DESC LIMIT 136) ORDER BY thread DESC LIMIT 1 directlty. The parser will rewrite it and you wron't see it.
So it is better to write a standard query where you see the JOIN or the SUBQUERY. Anyway PostgreSQL is picky and will not accept this SQL clause, probably because it is not standard. PostgreSQL hackers usualy don't accept non-standard queries. It could take them 5 minutes to modify PostgreSQL core, but if it is not standard, they don't accept it. If you know FFmpeg, the same applies for Video and Audio codecs. If it is not standard, even a simple hack, core hackers do not accept something non-compliant with standards. This is not my fault.
Using MySQL is like using Windows 95. You always run into problems that you don't know how to analyse because you don't see what is "inside".
If this is standard SQL, I apologize in advance, but I doubt.
Comment #8
jaydub CreditAttribution: jaydub commentedAll comments regarding standard or non-standard aside, the fact that the query fails should be of primary concern. I can verify that the query fails in PostgreSQL. I tried the suggested alternative query in the parent issue and while that -does- work in PostgreSQL, it does not work in MySQL 5:
Comment #9
jaydub CreditAttribution: jaydub commentedHow about this?
That at least didn't throw an error both in MySQL and PostgreSQL.
Comment #10
brianV CreditAttribution: brianV commentedAccording to PostgreSQL's documentation,
So it appears that multiple ORDER BY statements are legitimate.
Comment #11
brianV CreditAttribution: brianV commentedJay,
You should work off the CVS code. The current CVS for this statement is:
Actually, can you test if this CVS code gives the same issues under PostgreSQL?
Comment #12
grub3 CreditAttribution: grub3 commentedSorry, I feel stupid. It added this code to my SVN drupal installation but don't know in which situation I should test it. What kind of comments does it display? Sorry, I feel lost and stupid.
Comment #13
Damien Tournoud CreditAttribution: Damien Tournoud commentedIn fact, it seems like this is just a syntax issue:
... is an implicit FROM subquery. The syntax is apparently valid on MySQL, but not on PostgreSQL, so we should write:
Comment #14
Norbert Poellmann CreditAttribution: Norbert Poellmann commentedNo, I don't agree to multiple ORDER BY statements in postgres.
http://www.postgresql.org/docs/8.3/interactive/sql-select.html
talks about The ORDER BY clause (not clauses) and the syntax shows only
one (optional) ORDER BY clause per SELECT statement.:
That means: you can have one or more
(indicated by [, ...]), but only one
Instead of:
(SELECT thread FROM comments WHERE nid = 63743 AND status = 0 ORDER BY timestamp DESC LIMIT 136) ORDER BY thread DESC LIMIT 1 ;
it should simply say:
SELECT thread FROM comments WHERE nid = 63743 AND status = 0
ORDER BY timestamp DESC , thread DESC LIMIT 1 ;
Comment #15
Damien Tournoud CreditAttribution: Damien Tournoud commented@Norbert: see my comment #13, it's nothing more then a syntax issue: MySQL read the query as an implicit sub-query on FROM, which is what we want. Making the subquery explicit should solve the issue.
No, those are not the same queries (see the difference in the LIMIT clauses).
Comment #16
Damien Tournoud CreditAttribution: Damien Tournoud commentedBumping to D7 to quick fix that there (and add a test for that...), before backporting.
Comment #17
Damien Tournoud CreditAttribution: Damien Tournoud commented#237509: PostgreSQL 8.3 error: multiple ORDER BY clauses in comment.module was a duplicate.
Comment #18
Alex_Tutubalin CreditAttribution: Alex_Tutubalin commentedFolks!
(i'm author of bugreport and patch #237509 which is duplicate to this bug)
As I can see
1. PosgreSQL does not supports multiple ORDER BY
2. MySQL does not supports LIMIT within subquery
3. comment.module really needs two ORDER BYs (first select N latest comments, than select highest thread id in them)
The only way is to use
if ( $GLOBALS['db_type']=='pgsql')
{
pgsql query
}
else
{ mysql query.
So, patch is trivial, just combine my patch from http://drupal.org/node/462270 to these ugly if-s
Comment #19
Damien Tournoud CreditAttribution: Damien Tournoud commented@Alex Tutubalin:
MySQL does support LIMIT inside sub-queries (of course... the current code, that is an implicit subquery works). The only thing left to do is to roll a patch according to my suggestion in #13.
Comment #20
Alex_Tutubalin CreditAttribution: Alex_Tutubalin commented@Damien
This is different queries.
First one selects Top N threads by timestamp, than maximum thread ID in these N top-timestamp;
Second one will effectively selects latest (by timestamp).
Again, my solution is
- Left MySQL query as is (assuming that two order-by-s is doing right thing)
- Make special PgSQL query (subselect with Order-by/Limit)
Select between two queries at PHP level by GLOBALS[pg_type]
This should work, althought not beautiful solution.
Comment #21
Damien Tournoud CreditAttribution: Damien Tournoud commented@Alex_Tutubalin: Please read carefully. This query is what we want and it should work the same on both PostgreSQL and MySQL:
Comment #22
Alex_Tutubalin CreditAttribution: Alex_Tutubalin commentedHave you check it on MySQL ?
Also, on PgSQL 8.3 you should use slightly different syntax
SELECT column FROM (SELECT ....) AS column ORDER BY... LIMIT...
Comment #23
Damien Tournoud CreditAttribution: Damien Tournoud commentedSELECT thread FROM (SELECT thread FROM {comments} WHERE nid = %d AND status = 0 ORDER BY timestamp DESC LIMIT %d) AS last_threads ORDER BY thread DESC LIMIT 1
works perfectly on MySQL.
Comment #24
Alex_Tutubalin CreditAttribution: Alex_Tutubalin commentedSo, my patch from http://drupal.org/node/462270 should work.
Comment #25
andyposttag for http://geek.joshwaihi.com/content/drupal-code-sprints-postgresql
Comment #26
Josh Waihi CreditAttribution: Josh Waihi commentedtagging properly
Comment #27
Shiny CreditAttribution: Shiny commentedattaching patch by Alex_Tutubalin, which i am testing at the Postres Code Sprint
Comment #29
Shiny CreditAttribution: Shiny commentedSo far i can't replicate this error - i've tried in D6 and D7
Can someone please add the steps to make this error occur? Then i can verify it is fixed by the patch.
(otherwise the patch looks very sane and safe for D6 - needs re-rolling for D7).
Comment #30
andypost#27 was for drupal6
Now patch for D7
But suppose reply for comments are broken in d7
Comment #32
deekayen CreditAttribution: deekayen commentedmisbehaving bot
Comment #33
andypostBot passed all tests
Comment #34
Damien Tournoud CreditAttribution: Damien Tournoud commentedMakes total sense, and the bot is happy.
Comment #35
Damien Tournoud CreditAttribution: Damien Tournoud commentedThere have been a lot of back and forth in this issue, so here is some clarification for the core committers.
The issue here is that we have a query that has an invalid syntax:
It is an implicit FROM-based subquery (we first get the threads of the latest comments, then we order them by thread).
MySQL happily deals with this query, even if it is invalid, but PostgreSQL ignore the parenthesis and return a cryptic "Multiple ORDER BY clauses are not allowed).
The patch simply transforms this query into:
Comment #36
andypostI post new issue related to comment ordering #529374: Reply on comment is broken
Maybe this one should be fixed first
Comment #38
andypostLet's re-test
Comment #39
andypostLooks like bot was broken so rtbc
Comment #40
webchickCommitted to HEAD. Thanks!
Comment #41
Damien Tournoud CreditAttribution: Damien Tournoud commentedWe need to backport this.
Comment #42
andypostReroll #27
Comment #43
grub3 CreditAttribution: grub3 commentedThanks. Will review shorly as I am back from holidays. Sorry for the delay.
Comment #44
grub3 CreditAttribution: grub3 commentedThe patch works as expected. I tested on my D6 forums.
Hope this can be applied ASAP.
It seems straightforward and will not break anything.
Thanks!
Comment #45
grub3 CreditAttribution: grub3 commentedPlease inform us when it is committed to CVS.
Comment #46
Dave ReidExtra space in the SQL.
@jmpoure: Please read http://drupal.org/node/317.
Comment #47
andypostSo here reroll without extra space
Comment #48
andypostAnd status change... sorry
Comment #49
grub3 CreditAttribution: grub3 commentedThank you Andy and Damien.
In Guidelines for writing MySQL and PostgreSQL compliant SQL
read MySQLism: avoid nested ORDER BY, use nested queries
This describes the SQL-99 standard and how to rewrite nested ORDER BY.
So you can apply the patch safely.
The query will execute very well under PostgreSQL and MySQL.
We hope that the quide will improve Drupal conformance to SQL-99.
Comment #50
andypostNeed another review to rtbc this!
Comment #51
lambic CreditAttribution: lambic commentedlooks good
Comment #52
Gábor HojtsyWho tested this on an actual Drupal 6 setup on MySQL? I'm seeing people tested the standalone query which was formed into the patch and people also tested the patch on PostgreSQL, but nobody tested the patch on MySQL, right?
Comment #53
Dave ReidI don't get the same result between the new and old queries. New query also didn't pass the SQL-99 validator (http://developer.mimer.com/validator/parser99/index.tml)
Comment #54
andypost@Dave I found no difference betwen old and new query on mysql 5.0.51a (debian). Please provide more info about your env
Comment #55
bellHead CreditAttribution: bellHead commentedI've applied the patch to a D6 on both Postgres and mysql. The Postgres instance worked after the patch and not before. Mysql instance works either way.
The two queries return exactly the same results against mysql on a 50000 node generated test set with up to 150 comments per node and a random new_replies between 3 and 10.
The validator at mimer doesn't seem to like the ORDER BY or LIMIT in the subquery. I can't find anything in the spec forbidding them though.
Comment #56
Gábor HojtsyThanks, committed to Drupal 6 as well.