You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
CREATE TEMP FUNCTION rejection_rules(arr ANY TYPE) AS (
4
+
(SELECT [
5
+
STRUCT( "REJECTION_RULE" as mdmStage,
6
+
"O campo businesspartner_id é inválido." as mdmErrorMsg,
7
+
((record.businesspartner_id is Null) or (record.businesspartner_id = '')) as mdmActive)
8
+
,
9
+
STRUCT( "REJECTION_RULE" as mdmStage,
10
+
"O campo erp_id é inválido." as mdmErrorMsg,
11
+
((record.erp_id is null) or (record.erp_id = '')) as mdmActive)
12
+
,
13
+
STRUCT( "RELATIONSHIP_CONSTRAINT" as mdmStage,
14
+
"O campo _orgid não existe na ST mapping." as mdmErrorMsg,
15
+
((record._orgid is Null) or (record._orgid = '')) as mdmActive)
16
+
,
17
+
STRUCT( "REJECTION_RULE" as mdmStage,
18
+
"O campo businesspartnergroup_id é inválido." as mdmErrorMsg,
19
+
((record.field1 is Null) or (record.field1 = '')) as mdmActive)
20
+
,
21
+
STRUCT( "REJECTION_RULE" as mdmStage,
22
+
"O campo alias é inválido." as mdmErrorMsg,
23
+
((record.alias is Null) or (record.alias = '')) as mdmActive)
24
+
,
25
+
STRUCT( "REJECTION_RULE" as mdmStage,
26
+
"O campo deleted é inválido." as mdmErrorMsg,
27
+
(record.deleted is Null) as mdmActive)
28
+
] FROM UNNEST(arr) AS record)
29
+
);
30
+
31
+
with sa2_mdbusinesspartner as (
32
+
SELECT
33
+
stg.mdmTenantId as stgTenantId,
34
+
stg.a2_cgc as _orgid,
35
+
--`labs-app-mdm-production.a_techfin`.hash(stg.a2_cod, 'mdbusinesspartner_supplier') as businesspartner_id,
36
+
`labs-app-mdm-production.a_techfin`.hash(stg.protheus_pk, "mdbusinesspartner_supplier") As businesspartner_id,--Mantem compativel a PK, com a que esta sendo geradda no DM antigo
37
+
stg.A2_NREDUZ as alias,
38
+
`labs-app-mdm-production.a_techfin`.hash(stg.a2_cod, 'mdaddress_supplier') as addres_id,
39
+
`labs-app-mdm-production.a_techfin`.hash(stg.a2_cod, 'mdbusinesspartnergroup_supplier') as businesspartnergroup_id,
40
+
stg.a2_cod as protheus_id,
41
+
false as deleted,
42
+
stg.a2_cod as erp_id
43
+
--metadata-v2--
44
+
from (select * except(ranking) from (select row_number() over (partition by mdmTenantId, a2_cod, a2_cgc ORDER BY mdmCounterForEntity DESC, mdmLastUpdated DESC) ranking, * from stg_protheus_carol_sa2
45
+
--timestamp-- WHERE _ingestionDatetime > SAFE.DATETIME(TIMESTAMP_MICROS(SAFE_CAST({{start_from}} AS INT64)))
46
+
) where ranking = 1 ) as stg
47
+
),
48
+
mdbusinesspartner as (
49
+
select * from sa2_mdbusinesspartner
50
+
)
51
+
52
+
SELECT *,
53
+
(EXISTS(SELECT 1 FROM UNNEST(mdmErrors) WHERE mdmActive = true)) AS mdmDeleted
54
+
FROM
55
+
(
56
+
select dm.* except(alias, stgTenantId),
57
+
REGEXP_REPLACE(dm.alias, r'([^\p{ASCII}]+)', '') AS alias,
CREATE TEMP FUNCTION rejection_rules(arr ANY TYPE) AS (
4
+
(SELECT [
5
+
STRUCT( "REJECTION_RULE" as mdmStage,
6
+
"O campo uuid é inválido." as mdmErrorMsg,
7
+
(record.uuid is Null) as mdmActive)
8
+
] FROM UNNEST(arr) AS record)
9
+
);
10
+
11
+
with company_fndorganization as (
12
+
SELECT
13
+
stg.mdmTenantId as stgTenantId,
14
+
stg.uuid as uuid,
15
+
stg.description as name,
16
+
stg.description as alias,
17
+
org.federalid as federalid,
18
+
stg.mdmCounterForEntity as _counter,
19
+
`labs-app-mdm-production.a_techfin`.mdmIdStaging(stg.mdmConnectorId, 'company_organization', CONCAT('{"emitente":{"path":"', lower(path), '"}}')) as mdmStagingRecord
20
+
--metadataNoId-v2--
21
+
from (select * except(ranking) from (select row_number() over (partition by mdmTenantId, mdmId ORDER BY mdmCounterForEntity DESC, mdmLastUpdated DESC) ranking, * from stg_protheus_carol_company
22
+
--timestamp-- WHERE _ingestionDatetime > SAFE.DATETIME(TIMESTAMP_MICROS(SAFE_CAST({{start_from}} AS INT64)))
23
+
) where ranking = 1 ) as stg
24
+
left join (select * except(ranking) from (select *, row_number() over (partition by mdmTenantId, uuid ORDER BY mdmCounterForEntity DESC, mdmLastUpdated DESC) ranking from stg_protheus_carol_organization) where ranking = 1) as org
25
+
on org.mdmTenantId = stg.mdmTenantId
26
+
and org.uuid = stg.uuid
27
+
),
28
+
organization_fndorganization as (
29
+
SELECT
30
+
stg.mdmTenantId as stgTenantId,
31
+
stg.uuid as uuid,
32
+
stg.name as name,
33
+
stg.alias as alias,
34
+
stg.federalid as federalid,
35
+
stg.mdmCounterForEntity as _counter,
36
+
`labs-app-mdm-production.a_techfin`.mdmIdStaging(stg.mdmConnectorId, 'organization_map', CONCAT('{"pk":{"uuid":"', lower(stg.uuid), '"}}')) as mdmStagingRecord
37
+
--metadataNoId-v2--
38
+
from (select * except(ranking) from (select row_number() over (partition by mdmTenantId, mdmId ORDER BY mdmCounterForEntity DESC, mdmLastUpdated DESC) ranking, * from stg_protheus_carol_organization
39
+
--timestamp-- WHERE _ingestionDatetime > SAFE.DATETIME(TIMESTAMP_MICROS(SAFE_CAST({{start_from}} AS INT64)))
40
+
) where ranking = 1 ) as stg
41
+
),
42
+
organization as (
43
+
select *
44
+
from company_fndorganization stg
45
+
union all
46
+
select *
47
+
from organization_fndorganization stg
48
+
)
49
+
50
+
SELECT *,
51
+
(EXISTS(SELECT 1 FROM UNNEST(mdmErrors) WHERE mdmActive = true)) AS mdmDeleted
(case length(trim(coalesce(dm.name,""))) -- adding a default value instead of rejecting it. Environment tenant70ead8d42e8111eba8f40a586461440e got a lot of rejections because of this validation.
57
+
when 0 then "<em branco>"
58
+
else dm.name
59
+
end) as name,
60
+
(case length(trim(coalesce(dm.alias,""))) -- adding a default value instead of rejecting it. Environment tenant70ead8d42e8111eba8f40a586461440e got a lot of rejections because of this validation.
61
+
when 0 then "<em branco>"
62
+
else dm.alias
63
+
end) as alias,
64
+
rejection_rules(ARRAY((SELECT AS STRUCT dm.uuid))) AS mdmErrors
65
+
from (select * except(ranking) from (select row_number() over (partition by stgTenantId, uuid ORDER BY _counter DESC) ranking, * from organization) where ranking = 1 ) dm
0 commit comments