Google bigquery BQ脚本-声明:使用现有表的模式类型

Google bigquery BQ脚本-声明:使用现有表的模式类型,google-bigquery,Google Bigquery,使用新的BQ脚本功能,是否可以使用现有表的类型声明变量?我们的表有很大的模式,显式是不可行的 文档中没有显示这一点,因此可能还不可能 假设persons是一个现有表 DECLARE aPerson (persons.schema) 一种伪代码类型,其中我希望单个变量属于该模式,甚至是一个数组 DECLARE somePersons ARRAY<(persons.schema)> 继承现有表的模式的变量,或在数组示例中继承一组现有表的模式 更新了ELLIOTT,在示例中添加了更多内

使用新的BQ脚本功能,是否可以使用现有表的类型声明变量?我们的表有很大的模式,显式是不可行的

文档中没有显示这一点,因此可能还不可能

假设persons是一个现有表

DECLARE aPerson (persons.schema)
一种伪代码类型,其中我希望单个变量属于该模式,甚至是一个数组

DECLARE somePersons ARRAY<(persons.schema)>
继承现有表的模式的变量,或在数组示例中继承一组现有表的模式

更新了ELLIOTT,在示例中添加了更多内容

基本上,给定一方最多有3个特定于系统的“配置文件”

其思想是生成一个单一版本,其中每个系统概要文件都根据每个被调用的存储过程的一些内部规则进行合并

我得到了一些错误,这些错误与没有声明我想要成为表类型master\u party\u profile\u changed\u parties的变量类型有关

下面的代码希望不会太混乱

DECLARE changedPartyIds ARRAY<STRING>;
DECLARE numChangedParties, i INT64 DEFAULT 0;

-- below is a workaround (Elliott B from GCP) as you cant declare a variable of a type using an existing tables schema, this is a suggested workaround where the schema needs to come from 'master_party_profile_changed_parties' table
-- create a small temp array and can use this later in select projection.

CREATE TEMP TABLE PartyProfileRawRefdataRow AS
  SELECT t
  FROM `datset.master_party_profile_changed_parties_small` AS t
  LIMIT 1;

-- fetch the changed party ids - they will be changed if any of their profiles have changes since last run date
-- we need to process all changed parties profiles (1 Party -> (*) PartyProfiles) to generate the single 'master' view

SET changedPartyIds = (
  SELECT
    ARRAY_AGG(partyId order by partyId)
  FROM
    `sdv-analytics-wt-uat.sdv_bi_derived_events.master_party_profile_changed_parties_small`
);

-- lets process each party in turn now, looking at their profiles, with a specific order of precedence for each master field.
-- while there are parties with changed profiles still to process

WHILE i <= ARRAY_LENGTH(changedPartyIds) DO
  -- for each party, set system1Profile - might be null.  
  -- ERROR ELLIOTT: need to declare careProfile as a type of my source table?
  SET system1Profile = (
    SELECT 
      (SELECT t FROM PartyProfileRawRefdataRow AS t).*
    FROM
      inputProfiles ip
    WHERE
      partyId = changedPartyIds[OFFSET(i)]
  );
  -- for this party, now set system2Profile - might be null. 
  -- ERROR ELLIOTT: need to declare system1Profile as a type?

  SET system2Profile = (
    SELECT 
      (SELECT t FROM PartyProfileRawRefdataRow AS t).*
    FROM
      inputProfiles ip
    WHERE
      partyId = changedPartyIds[OFFSET(i)]
  );

  -- for this party, set a system3 profile - might be null.  
  -- ERROR ELLIOTT: need to declare system3 as a type?

  SET system3Profile = (
    SELECT 
      (SELECT t FROM PartyProfileRawRefdataRow AS t).*
    FROM
      inputProfiles ip
    WHERE
      partyId = changedPartyIds[OFFSET(i)]
  );

  -- ERROR ELLIOTT: need to declare masterProfile fort SP (INOUT) as a type too?  The StoredProc will merge addresses from first 3 according to rules and populate masterProfile sub attribute...tahts just detail, main issue is cannot declare the variables in the first place?

  CALL MasterProfile_mergeAddresses(system1Profile, system2Profile, system3Profile, masterProfile);

  -- Finally, insert the newly derived masterProfile into an actual table.
  INSERT INTO `sdv-analytics-wt-uat.sdv_bi_derived_events.master_party_profile_generation_output_master_profile` VALUES (
    masterProfile
  );

  -- increment counter and move onto next party
  SET i = i + 1; 
END WHILE;

不支持使用表的行类型声明变量。但是,您可以,如果您这样做,请将其链接到此处。同时,您可以使用一行创建一个临时表,然后将其作为变量处理。作为草图:

创建临时表PersonRow作为 选择t 从dataset.persons作为t 限值1; 选择x、y、z,从PersonRow中选择t作为t* 来自dataset.other_表;
我刚刚提交了感谢Elliott并注意到了临时解决方案。我们目前使用Terraform来提供视图,是否也能够从Terraform管理脚本和存储过程代码?仅在“保存”脚本时,它们是否会像其他保存的查询一样保存为“查询”,仅保存UI中保存视图选项的查询?如果您可以将这些资源保存到特定的数据集(如视图)并具有分层分离/权限等,那就太好了。目前,将整个查询保存到平面结构并不理想。真是太感谢你了!嗯-试一下这个,选择x,y,z部分意味着我需要列出确切的变量数你的例子表明宽度为3?我们在测试中使用的表有3000列宽…实际上你并不是说,x,y,z来自其他表…我会继续玩这个是的,这个例子中的x,y,z来自不同的表。可以通过创建存储过程来保存脚本。对于Terraform,有人提交了这个请求:试图向exmaple添加一些额外的上下文/内容。我需要此复杂表类型的4个变量“master\u party\u profile\u changed\u parties\u small”,验证失败,说明我需要声明此复杂表类型的system1Profile、system2Profile、system3Profile和masterProfile……感谢您的帮助!干杯:在我看来,这比工程设计更极端,可以用基于场景的方式来完成。在任何情况下——就像今天一样——都没有办法像您试图做的那样声明未知类型table/struct的变量——即使在Elliott在他的回答中明确说明了这一点——也没有必要对我如此消极。也许是设计过度我故意忽略了很多逻辑,也许我没有完全遵循Elliott推荐的这项新功能的完整使用选项-我们并没有你那么好…我只是想展示更多的真实例子,同时了解这一切…谢谢你,如果它看起来是这样的话-它根本不打算对你产生负面影响。只是提供了我所看到的反馈。在我看来,你怎么知道你很可能是在错误的方向上呢?当然,这是基于几十年的现实生活的例子。很明显,我可能是错的,但无论如何,请不要把这件事放在心上,我只是想帮忙