BQ脚本-声明:使用现有表的架构类型

时间:2019-10-21 12:39:13

标签: google-bigquery

使用新的BQ脚本功能,是否可以使用现有表的类型声明变量?我们的表具有较大的架构,并且无法正常工作

文档中没有任何内容显示出来,因此可能尚无法实现。

假设人员是现有表格。...

DECLARE aPerson (persons.schema)

伪代码的类型,我希望单个变量属于该模式,甚至是一个数组

DECLARE somePersons ARRAY<(persons.schema)>

一个变量,它继承了现有表的模式,或者在数组示例中是一组表。

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

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

想法是生成此版本的单个版本,其中每个系统配置文件根据某些规则(每个被调用的存储过程的内部)进行合并

我遇到与未声明变量类型为表类型(master_party_profile_changed_pa​​rties)有关的错误。

下面的代码希望它不要太混乱!

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;

1 个答案:

答案 0 :(得分:2)

不支持使用表的行类型声明变量。不过,您可以submit a feature request,如果需要,请在此处链接。同时,您可以创建一个只有一行的临时表,然后在以后将其视为变量。作为草图:

var results= query.List<>();