|
| 1 | +{# |
| 2 | + Copyright (c) 2024, Oracle and/or its affiliates. |
| 3 | + |
| 4 | + Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + you may not use this file except in compliance with the License. |
| 6 | + You may obtain a copy of the License at |
| 7 | + |
| 8 | + https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + |
| 10 | + Unless required by applicable law or agreed to in writing, software |
| 11 | + distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + See the License for the specific language governing permissions and |
| 14 | + limitations under the License. |
| 15 | +#} |
| 16 | + |
| 17 | +{# |
| 18 | + Legacy hash function ORA_HASH is known to have collisions in large datasets |
| 19 | + causing errors in snapshot merge statement. Please check the below Github |
| 20 | + issues: |
| 21 | + |
| 22 | + https://github.com/oracle/dbt-oracle/issues/52 |
| 23 | + https://github.com/oracle/dbt-oracle/issues/102 |
| 24 | + |
| 25 | +This hash function is used in the marcro oracle__snapshot_hash_arguments |
| 26 | + |
| 27 | +dbt-oracle 1.9 will switch to a stronger hash function - SHA256. Changing the |
| 28 | +hash function will invalidate existing snapshots.These helper macros will |
| 29 | +ensure a smoother transition to dbt-oracle 1.9. |
| 30 | + |
| 31 | +It is recommended for teams to switch to SHA256 hash function before |
| 32 | +dbt-oracle 1.9 using a 2-step process: |
| 33 | +1. Create a macro oracle__snapshot_hash_arguments(args) in your dbt project |
| 34 | + Copy paste the contents of macro |
| 35 | + oracle__snapshot_standard_hash_arguments(args) shown below. This will become |
| 36 | + the default from dbt-oracle 1.9 |
| 37 | + |
| 38 | +2. Run the following operation on your snapshot table |
| 39 | + |
| 40 | +dbt --debug run-operation update_legacy_dbt_scd_id \ |
| 41 | + --args '{snapshot_table: PROMOTION_COSTS_SNAPSHOT, cols: ["promo_id", "dbt_updated_at"]}' |
| 42 | + |
| 43 | +#} |
| 44 | + |
| 45 | +{% macro oracle__snapshot_standard_hash_arguments(args) -%} |
| 46 | + STANDARD_HASH({%- for arg in args -%} |
| 47 | + coalesce(cast({{ arg }} as varchar(4000) ), '') |
| 48 | + {% if not loop.last %} || '|' || {% endif %} |
| 49 | + {%- endfor -%}, 'SHA256') |
| 50 | +{%- endmacro %} |
| 51 | + |
| 52 | + |
| 53 | +{% macro update_legacy_dbt_scd_id(snapshot_table, cols) -%} |
| 54 | + |
| 55 | + {%- call statement('update_legacy_dbt_scd_id_dtype') -%} |
| 56 | + BEGIN |
| 57 | + UPDATE {{ snapshot_table }} SET DBT_SCD_ID = NULL; |
| 58 | + COMMIT; |
| 59 | + EXECUTE IMMEDIATE 'ALTER TABLE {{ snapshot_table }} MODIFY (dbt_scd_id RAW(32))'; |
| 60 | + END; |
| 61 | + {%- endcall -%} |
| 62 | + |
| 63 | + {%- call statement('update_legacy_dbt_scd_id') -%} |
| 64 | + BEGIN |
| 65 | + UPDATE {{ snapshot_table }} |
| 66 | + SET dbt_scd_id = {{ oracle__snapshot_standard_hash_arguments(cols) }}; |
| 67 | + COMMIT; |
| 68 | + END; |
| 69 | + {%- endcall -%} |
| 70 | +{%- endmacro %} |
0 commit comments