简体   繁体   中英

SELECT … LIMIT 1 query results in more than one row?

I noticed that LIMIT queries will return more than the expected number of rows when they are executed against tables that contain nested or repeated data. For example, the following query run against the persons sample data set from the developer guide produces the following results:

% bq query 'SELECT fullName, children.name FROM [persons.person] LIMIT 1'   
+----------+---------------+
| fullName | children_name |
+----------+---------------+
| John Doe | Jane          |
| John Doe | John          |
+----------+---------------+

It looks like BQL is applying the LIMIT operator before flattening the results as opposed to the other way around (which I think would make more sense).

Is this a bug in the BQL implementation or is this the expected behavior? If this is the expected behavior can someone please provide an explanation for why this makes sense?

This is expected given the way BigQuery flattens query results. When you run the query, the LIMIT 1 applies to the repeated record. Then the results get flattened in the output, and you get two rows. A workaround is to use an explicit flatten operation. For example:

SELECT fullName, children.name 
FROM (FLATTEN([persons.person], children.name) LIMIT 1

This will return only a single row.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM