Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding an ORDER BY statement to a query without flattening results leads to "Cannot query the cross product of repeated fields"

Query: 
    "SELECT * FROM [table] ORDER BY id DESC LIMIT 10"
    AllowLargeResults = true
    FlattenResults = false

table schema:
    [
        {
            "name": "id",
            "type": "STRING",
            "mode": "NULLABLE"
        },
        {
            "name": "repeated_field_1",
            "type": "STRING",
            "mode": "REPEATED"
        },
        {
            "name": "repeated_field_2",
            "type": "STRING",
            "mode": "REPEATED"
        }
    ]

The query "SELECT * FROM [table] LIMIT 10" works just fine. I get this error when I add an order by clause, even though the order by does not mention either repeated field.

Is there any way to make this work?

like image 296
Dylan Douglas Avatar asked Nov 26 '25 17:11

Dylan Douglas


1 Answers

The ORDER BY clause causes BigQuery to automatically flatten the output of a query, causing your query to attempt to generate a cross product of repeated_field_1 and repeated_field_2.

If you don't care about preserving the repeatedness of the fields, you could explicitly FLATTEN both of them, which will cause your query to generate the cross product that the original query is complaining about.

SELECT *
FROM FLATTEN(FLATTEN([table], repeated_field_1), repeated_field_2)
ORDER BY id DESC
LIMIT 10

Other than that, I don't have a good workaround for your query to both ORDER BY and also output repeated fields.

See also: BigQuery flattens result when selecting into table with GROUP BY even with “noflatten_results” flag on

like image 89
Danny Kitt Avatar answered Nov 30 '25 01:11

Danny Kitt



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!