Say you have the following table in BigQuery:
A = user1 | 0 0 |
user2 | 0 3 |
user3 | 4 0 |
After a cross join, you have
dist = |user1 user2 0 0 , 0 3 | #comma is just showing user val seperation
|user1 user3 0 0 , 4 0 |
|user2 user3 0 3 , 4 0 |
How can you perform row aggregation in BigQuery to compute pairwise aggregation across rows. As a typical use case, you could compute the euclidean distance between the two users. I want to compute the following metric between the two users:
sum(min(user1_row[i], user2_row[i]) / abs(user1_row[i] - user2_row[i]))
summed over all i for each pair of users.
For example in Python you would simply:
for i in np.arange(row_length/2)]):
dist.append([user1, user2, np.sum(min(r1[i], r2[i]) / abs(r1[i] - r2[i]))])
To start with the ugly way: you could flatten out the math into the query. That is, turn
for i in ... sum(min(...)/abs(...))
into SQL operating over each of the fields. Note thatMIN
andSUM
are aggregate functions that you won't want to use. Instead use+
for SUM andIF(a < b, a, b)
forMIN
.ABS(a, b)
looks likeIF(a < b, b-a, a-b)
. If you were just computing the Euclidian distance, you could doThe nicer way is User-Defined Functions, and create the vectors as repeated values. You can then write a
DISTANCE()
function that performs your computation over the two arrays from the left and the right side of the cross join. If you're not in the UDF beta program and would like to join, please contact google cloud support.Finally, if you change your schema from
{user:string, field1:float, field2:float, field3:float,...}
to{user:string, fields:[field:float]}
You could then flatten the field with position and do the cross join on that. As in:
If you save this as a view, call it "dataset1.flat_view"
Then you can do your join:
This will give you one row each for each pair of users and each field matching field. You can save that as the view "dataset1.joined_view".
Finally, you can do your aggregations:
Since you want this:
it would look like: