简体   繁体   English

从子选择执行 SELECT * 查询时,BigQuery 是否保证列顺序?

[英]Does BigQuery guarantee column order when performing SELECT * query from subselect?

Does BigQuery guarantee column order when performing SELECT * FROM subquery?执行SELECT * FROM子查询时,BigQuery 是否保证列顺序?
For example, given table t with columns a , b , c , d if I'd execute a query:例如,如果我执行查询,给定表t的列abcd

SELECT * EXCEPT (a) FROM (SELECT a, b, c, d FROM t)

Will the columns in the result always have the same order ( b , c , d in this case)?结果中的列是否始终具有相同的顺序(在这种情况下为bcd )?

I am going to answer "yes";我要回答“是”; however, it is not documented to do so.但是,没有记录这样做。 The only guarantee (according to the documentation) is:唯一的保证(根据文档)是:

SELECT * , often referred to as select star, produces one output column for each column that is visible after executing the full query. SELECT * ,通常称为 select star,为执行完整查询后可见的每一列生成一个输出列。

This does NOT explicitly state that the columns are in the order they are defined -- ie first by the order of references in the FROM clause and then by the ordering within each reference.这并没有明确说明列是按它们定义的顺序排列的——即首先按FROM子句中引用的顺序,然后按每个引用中的顺序。 I'm not even sure if the SQL standard specifies the ordering (although I have a vague recollection that the 92 standard might have).我什至不确定 SQL 标准是否指定了顺序(尽管我模糊地记得 92 标准可能有)。

That said, I have never seen any database not produce the columns in the specified order.也就是说,我从未见过任何数据库不按指定顺序生成列。 Plus, databases (in general) have to keep the ordering of the column to support INSERT .另外,数据库(通常)必须保持列的顺序以支持INSERT You can see it yourself in INFORMATION_SCHEMA.COLUMNS.ORDINAL_POSITION .您可以在INFORMATION_SCHEMA.COLUMNS.ORDINAL_POSITION中自行查看。 And, the REPLACE functionality (to replace a column expression in the SELECT list) would make less sense if the ordering were not guaranteed.而且,如果不能保证排序,则REPLACE功能(替换SELECT列表中的列表达式)将变得毫无意义。

I might argue that you are "pretty safe" assuming that the ordering is the same.假设顺序相同,我可能会争辩说您“非常安全”。 And that we should lobby Google to fix the documentation to make this clear.并且我们应该游说谷歌修复文档以明确这一点。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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