A very small patch, but be careful of the unit test. It bites. :-) See the docblock. I can also see us leaving out the unit test for this one since it logically cannot be perfect.

Note that the implementation here is NOT optimal. It's not supposed to be the most performant, just the most stable. We can look into DB-specific optimizations post-freeze, as those won't require an API change.

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

Damien Tournoud’s picture

Status: Needs review » Needs work

The logic looks sound, but there are lot of coding style issues in there.

Crell’s picture

Status: Needs work » Needs review
FileSize
4.53 KB

Lots? I found only one tab. You'll have to be more specific because I don't see any others.

Crell’s picture

Issue tags: +API change

Tagging.

Crell’s picture

FileSize
4.53 KB

Minor comment and whitespace updates per review from DamZ.

Damien Tournoud’s picture

Status: Needs review » Reviewed & tested by the community

Yay.

chx’s picture

Status: Reviewed & tested by the community » Needs work

This can not get into core like this, we need to add a big red ugly warning that this does not scale to big resultsets. http://jan.kneschke.de/projects/mysql/order-by-rand/ however the scalable version will be very MySQL specific and thus is contrib. And that's fine. But I want my warning :)

Crell’s picture

Status: Needs work » Reviewed & tested by the community
FileSize
4.81 KB

Ask and ye shall receive. No code changes so back to RTBC.

Crell’s picture

FileSize
4.95 KB

Slightly improved comment.

chx’s picture

I am OK with this.

webchick’s picture

Status: Reviewed & tested by the community » Needs work
Issue tags: +Needs documentation

Hm. I'm a little nervous about this test. We'll have to see if it starts affecting testbot with other patches in the queue.

Otherwise this looks well-documented, and adds in a missing feature from DBTNG. Committed to HEAD.

This needs to be documented in the upgrade docs.

David_Rothstein’s picture

Status: Needs work » Needs review
FileSize
5.17 KB

Eh, with my luck, all my patches are going to be in the 1% that randomly fails :) Also, if I'm looking at the numbers right, I think the probability of this test failing is actually higher than 1%...

I'd like to suggest that perhaps we aren't even testing the correct thing here. We need to test the Drupal database layer, but this test is essentially also trying to assert that MySQL has a good random number generator. I don't think we need or want to go down that road (the test is barely scratching the surface of that, anyway).

The attached patch takes a different approach. It basically just asserts that when you run the query twice, you get the same results in a different order. If the same exact select query gives a differently ordered result each time, it's hard to imagine any possible way that could happen without having triggered the database's randomization process.

Furthermore, this also tests that the random ordering is actually an "ordering" (i.e., that there are no repeats), which the previous test did not do.

The chance of this test failing by accident is effectively zero, so I'd consider it a big improvement :)

Crell’s picture

Status: Needs review » Reviewed & tested by the community

Yeah, I do like the test in #11 better.

Crell’s picture

Category: feature » task
Issue tags: -Needs documentation, -API change

Documentation has been added, too, and this is no longer an API change just test improvement.

Dries’s picture

Status: Reviewed & tested by the community » Fixed

Committed to CVS HEAD. Thanks!

chx’s picture

Category: task » bug
Status: Fixed » Active

$this->assertNotEqual($randomized_ids, $ordered_ids, t('A query with random ordering returns an unordered set of IDs.'));

Huh? Why would a randomization be always different from the ordering-by-id result? If it's random then it can be anything. I agree the chances of two randomizations being the same are sufficiently low, however it can happen theoretically (if it's independent and random...)

Crell’s picture

Status: Active » Fixed

The previous test was not deterministically perfect either. The new one is, arguably, simpler. The odds of it returning a false negative are easy to calculate, and really really low. (1 in 52!, I believe). I don't know off hand what the false negative odds were on the previous test.

David_Rothstein’s picture

Yeah, to put that number in perspective, the Sun is going to burn out long, long, long before this test ever randomly fails. I think we're OK :)

I don't remember the exact number, but the odds of the previous version failing were something around 1%, which was one of the motivations for changing it. 1% is pretty high - it would hit us all the time.

Also, don't forget that we call $this->randomName() hundreds of times in the tests. That could cause failures too, if it stumbled upon something that caused a conflict. That one actually might happen before the Sun burns out... although not much before :)

Crell’s picture

No, we could have a false negative later today. The odds of that are 1 in 52!. We could also have a false negative tomorrow. The odds of that are also 1 in 52!. The odds of both happening are 1 in 52!*52!. Miniscule, but still possible.

David_Rothstein’s picture

Let's put it this way - I'm willing to bet a very large sum of money that the Sun will burn out first ... Of course, it would be difficult to collect on that bet :)

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.