Development

#3362 ([PATCH] Non-MySQL RDBMS cannot mix implicit and explicit JOIN types)

You must first sign up to be able to contribute.

Ticket #3362 (closed defect: invalid)

Opened 3 months ago

Last modified 2 weeks ago

[PATCH] Non-MySQL RDBMS cannot mix implicit and explicit JOIN types

Reported by: caeies Assigned to: fabien
Priority: major Milestone: 1.0.17
Component: model Version: 1.0.16
Keywords: Cc:
Qualification: Unreviewed

Description

Hi all,

We are trying to use the sfSimpleForum in our app, but we got this problem :

 [wrapped: Could not execute query [Native Error: ERROR: invalid reference to FROM-clause entry for table "sf_simple_forum_forum" at character 903 HINT: There is an entry for table "sf_simple_forum_forum", but it cannot be referenced from this part of the query.] [User Info: SELECT sf_simple_forum_forum.ID, sf_simple_forum_forum.NAME, sf_simple_forum_forum.DESCRIPTION, sf_simple_forum_forum.RANK, sf_simple_forum_forum.CATEGORY_ID, sf_simple_forum_forum.CREATED_AT, sf_simple_forum_forum.UPDATED_AT, sf_simple_forum_forum.STRIPPED_NAME, sf_simple_forum_forum.LATEST_POST_ID, sf_simple_forum_forum.NB_POSTS, sf_simple_forum_forum.NB_TOPICS, sf_simple_forum_category.ID, sf_simple_forum_category.NAME, sf_simple_forum_category.STRIPPED_NAME, sf_simple_forum_category.DESCRIPTION, sf_simple_forum_category.RANK, sf_simple_forum_category.CREATED_AT, sf_simple_forum_post.ID, sf_simple_forum_post.TITLE, sf_simple_forum_post.CONTENT, sf_simple_forum_post.TOPIC_ID, sf_simple_forum_post.USER_ID, sf_simple_forum_post.CREATED_AT, sf_simple_forum_post.FORUM_ID, sf_simple_forum_post.AUTHOR_NAME FROM sf_simple_forum_forum, sf_simple_forum_category LEFT JOIN sf_simple_forum_post ON (sf_simple_forum_forum.LATEST_POST_ID=sf_simple_forum_post.ID) WHERE sf_simple_forum_forum.CATEGORY_ID=sf_simple_forum_category.ID AND sf_simple_forum_forum.CATEGORY_ID=sf_simple_forum_category.ID ORDER BY sf_simple_forum_category.RANK ASC,sf_simple_forum_forum.RANK ASC]]

The database is pgsql 8.2.

After some googling, I think that the problem could be not in sfSimpleForum, but in the propel code ???

I find something perhaps usefull : http://archives.postgresql.org/pgsql-general/2006-09/msg01139.php

We can help if needed.

Regards,

Caeies

PS: there's a missing version tag (1.0.13, which is our version).

Attachments

BasePeer.patch (1.1 kB) - added by vanchuck on 05/22/08 05:26:25.

Change History

04/17/08 19:01:50 changed by caeies

Hi all,

Just do a quick test, changing from :

sf_simple_forum_forum, sf_simple_forum_category LEFT JOIN sf_simple_forum_post

to

sf_simple_forum_forum CROSS JOIN sf_simple_forum_category LEFT JOIN sf_simple_forum_post

is working, when using the cli ...

So looks more like a propel problems ?

regards

Caeies.

04/18/08 14:40:48 changed by FabianLange

  • version changed from 1.0.12 to 1.0.13.
  • milestone deleted.

04/18/08 14:59:03 changed by caeies

  • summary changed from sfSimpleForumPlugin complains when used with pgsql 8.2 to sfSimpleForumPlugin complains when used with pgsql 8.2 / propel error ?.

Hi all,

I have made the following change to propel :

--- lib/vendor/propel/util/BasePeer.php (revision 8506)
+++ lib/vendor/propel/util/BasePeer.php (working copy)
@@ -832,7 +832,7 @@
                $sql =  "SELECT "
                                .($selectModifiers ? implode(" ", $selectModifiers) . " " : "")
                                .implode(", ", $selectClause)
-                               ." FROM ". ( (!empty($joinClause) && count($fromClause) > 1 && (substr(get_class($db), 0, 7) == 'DBMySQL')) ? "(" . implode(", ", $fromClause) . ")" : implode(", ", $fromClause) )
+                               ." FROM ". ( (!empty($joinClause) && count($fromClause) > 1 && (substr(get_class($db), 0, 7) == 'DBMySQL')) ? "(" . implode(", ", $fromClause) . ")" : implode(" CROSS JOIN ", $fromClause) )
                                                                .($joinClause ? ' ' . implode(' ', $joinClause) : '')
                                .($whereClause ? " WHERE ".implode(" AND ", $whereClause) : "")
                                .($groupByClause ? " GROUP BY ".implode(",", $groupByClause) : "")

It's now working, but 1) I fear to have side effects even if it doesn't looks like in my apps; 2) That won't work with some others DB ... and I got no idea on how do to this properly ...

Regards,

Caeies.

05/20/08 22:49:28 changed by vanchuck

I often come across this problem with PostgreSQL... and I haven't understood why before, but I've now looked into it further....

The problem is in combining the default implicit join (using the comma) with an explicit join, such as "LEFT JOIN". In both MySQL5.0.12+ and PgSQL8.0+ (and probably earlier versions), the explicit JOIN types take precedence over the implicit comma joins, as is the SQL2003 standard. This means that:

SELECT * FROM sf_simple_forum_forum, sf_simple_forum_category LEFT JOIN sf_simple_forum_post 
  ON (sf_simple_forum_forum.LATEST_POST_ID=sf_simple_forum_post.ID) 
  WHERE sf_simple_forum_forum.CATEGORY_ID=sf_simple_forum_category.ID

is actually executed as:

SELECT * FROM sf_simple_forum_forum, (sf_simple_forum_category LEFT JOIN sf_simple_forum_post 
  ON (sf_simple_forum_forum.LATEST_POST_ID=sf_simple_forum_post.ID))
  WHERE sf_simple_forum_forum.CATEGORY_ID=sf_simple_forum_category.ID

Therefore, since the 'ON' clause only operates on the tables being joined, the ON clause fails because the first column is not accessible in the second join, which the ON statement is part of.

The proper query should look like:

SELECT * FROM sf_simple_forum_forum JOIN sf_simple_forum_category LEFT JOIN sf_simple_forum_post 
  WHERE sf_simple_forum_forum.LATEST_POST_ID=sf_simple_forum_post.ID 
  AND sf_simple_forum_forum.CATEGORY_ID=sf_simple_forum_category.ID

In standard SQL, CROSS JOIN join conditions are specified with 'WHERE'. But it seems both PgSQL and MySQL support using CROSS JOIN with an ON condition.

The solution for this error in the plugin should modify the query that is causing the problem within the forum. It should be able to be fixed by simply changing the query to explicitly use Criteria::INNER_JOIN instead of the default implicit (comma) join.

Some background on related issues:

  1. A change to fix this precedence issue for MySQL was made in [1336], by adding parenthesis.
  2. It was discovered that this broke PgSQL queries entirely (see #557), so was again updated in [1457].
  3. The issue was raised in Propel here.
  4. That latest change was incorporated into Propel 1.2 Here, and now exists pretty much the same in Propel 1.3

Despite those changes, a fix to the precedence issue for non-MySQL RDBMS has not been developed.

I would like to see Caeies' patch tested on MySQL (different versions) to see if that works. If CROSS JOIN works as a perfect drop-in replacement for the implicit join on all RDBMS, then I see no reason not to use it. Then we could get rid of the RDBMS-specific hack that is in there now, as well.

I can confirm CROSS JOIN works in PgSQL (I'm running version 8.1.11)

05/20/08 23:16:34 changed by vanchuck

  • owner changed from francois to fabien.
  • version changed from 1.0.13 to 1.0.16.
  • component changed from sfSimpleForumPlugin to model.
  • summary changed from sfSimpleForumPlugin complains when used with pgsql 8.2 / propel error ? to [PATCH] Non-MySQL RDBMS cannot mix implicit and explicit JOIN types.

I can now also confirm CROSS JOIN works in MySQL 3.23, 4.1, and 5.0

I have therefore attached a patch which gets rid of the MySQL-specific hack and incorporates the CROSS JOIN syntax as the default join type when there is an implicit join along with an explicit one.

I'm changing this to a model defect, as it's more general than just an issue with the forum plugin.

05/22/08 05:26:25 changed by vanchuck

  • attachment BasePeer.patch added.

05/22/08 07:41:17 changed by dwhittle

Should this be filed with the Propel trac instead? propel.phpdb.org

05/22/08 10:43:54 changed by vanchuck

Yes, I think it should be filed with propel as well...

I guess the question is, how would this change get incorporated into SF...

I'm sure it can get fixed in Propel 1.3, but what about SF1.0.x users that rely on Propel 1.2? I'm not sure this patch would get incorporated into the 1.2 Propel codebase...

05/23/08 02:58:21 changed by hozn

I applied a modified version of the CROSS JOIN patch to Propel 1.3 branch. Hopefully this won't have adverse affects. This can be tracked in Propel here: http://propel.phpdb.org/trac/ticket/283

Thanks!

06/23/08 05:15:55 changed by dwhittle

  • status changed from new to closed.
  • resolution set to invalid.
  • milestone set to 1.0.17.

This is fix in Propel 1.3, as this is a bug in propel. Please file and resolve at propel trac: propel.phpdb.org, when it is fixed in 1.2 we will update.