|
| 1 | +/* From Github Copilot |
| 2 | +The script begins with a Common Table Expression (CTE) named |
| 3 | +cte that selects the owner_id, created_on, change_timestamp, |
| 4 | +and resource_access fields from the aclsnapshots table. |
| 5 | +The resource_access field is parsed as JSON and aliased as acl. |
| 6 | +The CTE only includes rows where the owner_id is 8016650. |
| 7 | +
|
| 8 | +The main SELECT statement then extracts data from this CTE. |
| 9 | +It selects the owner_id, created_on, and value fields directly. |
| 10 | +It also uses the COALESCE function to select the first non-null |
| 11 | +value from a list of possible fields in the value JSON object |
| 12 | +for accesstype and principalid. This is done to handle potential |
| 13 | +variations in the field names in the JSON object. |
| 14 | +
|
| 15 | +The LATERAL FLATTEN function is used to transform the acl JSON |
| 16 | +object into a set of rows. The outer=>TRUE argument means that |
| 17 | +if acl is an empty array or null, a single row with nulls in the |
| 18 | +columns produced by FLATTEN is returned. This is joined with the |
| 19 | +CTE to produce the final result set. |
| 20 | +*/ |
| 21 | +with cte as ( |
| 22 | + select |
| 23 | + owner_id, |
| 24 | + created_on, |
| 25 | + change_timestamp, |
| 26 | + parse_json(resource_access) as acl |
| 27 | + from |
| 28 | + synapse_data_warehouse.synapse_raw.aclsnapshots |
| 29 | + where |
| 30 | + owner_id = 8016650 |
| 31 | +) |
| 32 | + |
| 33 | +select |
| 34 | + owner_id, --noqa: RF02 |
| 35 | + created_on, --noqa: RF02 |
| 36 | + value, --noqa: RF02 |
| 37 | + coalesce( |
| 38 | + value:"accesstype"::variant, --noqa: RF02 |
| 39 | + value:"accessType"::variant, --noqa: RF02 |
| 40 | + value:"accesstype#1"::variant, --noqa: RF02 |
| 41 | + value:"accesstype#2"::variant, --noqa: RF02 |
| 42 | + value:"accesstype#3"::variant --noqa: RF02 |
| 43 | + ) as accesstype, |
| 44 | + coalesce( |
| 45 | + value:"principalId"::number, --noqa: RF02 |
| 46 | + value:"principalid"::number, --noqa: RF02 |
| 47 | + value:"principalid#1"::number --noqa: RF02 |
| 48 | + ) as principalid |
| 49 | + |
| 50 | +from |
| 51 | + cte, |
| 52 | + lateral flatten(acl, outer => TRUE); |
0 commit comments