I marked this as 4.7, but I think this is still a problem in HEAD.

We have been struggling in views for several months with recurring problems of queries using from_unixtime that break when run through db_rewrite_sql. Earl suggested I try to get it down to the simplest possible reproducable use case and I have finally broken it down into a simple query using core tables that will always break:

<?php
$query = "SELECT * FROM {node} node LEFT JOIN {comments} comments ON node.nid = comments.nid WHERE from_UNIXTIME(node.created)";
$primary_table = 'node';
$primary_field = 'nid';

$query = db_rewrite_sql($query, $primary_table, $primary_field,  array());
?>

The above query will return as:

SELECT * FROM {node} node LEFT JOIN {comments} comments ON DISTINCT(node.nid) = comments.nid WHERE from_UNIXTIME(node.created)

Which is, of course, invalid sql. The pattern I finally came up with that will break is a join of any two fields with a where clause that uses FROM_UNIXTIME. That's it. Remove the join and it works fine. Remove the FROM_UNIXTIME and it works fine.

I found numerous issues related to problems with FROM_UNIXTIME and an interesting one was http://drupal.org/node/28577 which suggested that maybe the 'FROM' in FROM_UNIXTIME was getting confused with the 'FROM' part of the query. That particular issue seems to be resolved, but it still might be a clue to the problem.

Other examples of reports of this problem:

http://drupal.org/node/49157
http://drupal.org/node/67927
http://drupal.org/node/76806
http://drupal.org/node/78027

CommentFileSizeAuthor
#4 from_unixtime.patch788 byteschx
Support from Acquia helps fund testing for Drupal Acquia logo

Comments

merlinofchaos’s picture

The issue in 28577 fixed it, but the whole thing was later rewritten and I think the FROM part is once again the culprit.

RayZ’s picture

Haven't tested, but this is likely fixed by the patch at http://drupal.org/node/80200.

RayZ’s picture

Oops ... sorry, that was specific to pager_query(). Nevermind.

chx’s picture

Status: Active » Needs review
FileSize
788 bytes

Thanks for the tip, the same idea is applicable here: let's look for a space after FROM.

KarenS’s picture

Status: Needs review » Reviewed & tested by the community

I can confirm this works in my test case.

Dries’s picture

Priority: Critical » Normal

from_unixtime is a MySQL-ism and shouldn't be used to begin with. It results in code that is incompatible with PostgreSQL (and with Drupal core in general).

Tempted to mark this "won't fix".

webchick’s picture

Dries: That's true about the MySQL-ism but it's still a buggy regex. If ANSI SQL ever decided to introduce a function called FROMAGE() that turned values into cheese, the same problem would happen. ;)

killes@www.drop.org’s picture

postgres users could easily define a function that is also named from_unixtime. IIRC Drupal used to define a pgsql IF function. Or was it GREATER?

drumm’s picture

Status: Reviewed & tested by the community » Fixed

Since this actually has nothing to do with FROM_UNIXTIME, it is any instance of 'FROM', this is okay. Still don't use FROM_UNIXTIME().

Committed to HEAD.

killes@www.drop.org’s picture

also to 4.7

Anonymous’s picture

Status: Fixed » Closed (fixed)