For anyone who is curious, the weighted sampling logic is the SQL translation of Algorithm A from Efraimidis and Spirakis's 2006 paper [1]:
Algorithm A
Input: A population V of weighted items
Output: A WRS of size n
1: For each v[i] in V, u[i] = random(0, 1) and k[i] = u^(1/w[i])
2: Select the n items with the largest keys k[i] as a WRS
Instead of the more direct tanslation of `ORDER BY POW(RANDOM(), (1/weight)) DESC`, we use the convenient fact that logarithm is a monotone transform over the positive reals to take the log of the ORDER BY argument without changing the sort order:
ORDER BY POW(RANDOM(), (1/weight)) DESC
=> ORDER BY LN(POW(RANDOM(), (1/weight))) DESC -- LN preserves order.
=> ORDER BY LN(RANDOM()) / weight DESC -- LN(x^a) = LN(x) * a.
=> ORDER BY -LN(RANDOM()) / weight -- Order by `x DESC` is same as by `-x`.
[1] Pavlos S Efraimidis and Paul G Spirakis. Weighted random sampling with a reservoir. Information Processing Letters, 97(5):181–185, 2006