96
96
{# Getting the column names for all additional columns #}
97
97
{%- set derived_column_names = datavault4dbt .extract_column_names (derived_columns) - %}
98
98
{%- set hashed_column_names = datavault4dbt .extract_column_names (hashed_columns) - %}
99
- {%- set prejoined_column_names = datavault4dbt .extract_column_names (prejoined_columns) - %}
99
+ {%- set prejoined_column_names = datavault4dbt .extract_prejoin_column_names (prejoined_columns) - %}
100
100
{%- set missing_column_names = datavault4dbt .extract_column_names (missing_columns) - %}
101
101
{%- set exclude_column_names = derived_column_names + hashed_column_names + prejoined_column_names + missing_column_names + ldts_rsrc_input_column_names %}
102
102
{%- set source_and_derived_column_names = (all_source_columns + derived_column_names) | unique | list - %}
185
185
{# Setting the ldts default datatype #}
186
186
{% set ldts_default_dtype = datavault4dbt .timestamp_default_dtype () %}
187
187
188
+ {{ datavault4dbt .prepend_generated_by () }}
189
+
188
190
WITH
189
191
190
192
{# Selecting everything that we need from the source relation. #}
@@ -256,26 +258,51 @@ missing_columns AS (
256
258
),
257
259
{%- endif - %}
258
260
261
+
259
262
{%- if datavault4dbt .is_something (prejoined_columns) %}
260
263
{# Prejoining Business Keys of other source objects for Link purposes #}
261
264
prejoined_columns AS (
262
265
263
266
SELECT
264
267
{% if final_columns_to_select | length > 0 - %}
265
268
{{ datavault4dbt .print_list (datavault4dbt .prefix (columns= datavault4dbt .escape_column_names (final_columns_to_select), prefix_str= ' lcte' ).split(' ,' )) }}
266
- {% endif %}
267
- {%- for col, vals in prejoined_columns .items () - %}
268
- ,pj_{{loop .index }}.{{ vals[' bk' ] }} AS {{ col }}
269
- {% endfor - %}
269
+ {%- endif - %}
270
+
271
+ {# Iterate over each prejoin, doing logic checks and generating the select-statements #}
272
+ {%- for prejoin in prejoined_columns - %}
273
+ {%- set prejoin_alias = ' pj_' + loop .index |string - %}
274
+
275
+ {# If extract_columns and/or aliases are passed as string convert them to a list so they can be used as iterators later #}
276
+ {%- if not datavault4dbt .is_list (prejoin[' extract_columns' ])- %}
277
+ {%- do prejoin .update ({' extract_columns' : [prejoin[' extract_columns' ]]}) - %}
278
+ {%- endif - %}
279
+ {%- if not datavault4dbt .is_list (prejoin[' aliases' ]) and datavault4dbt .is_something (prejoin[' aliases' ]) - %}
280
+ {%- do prejoin .update ({' aliases' : [prejoin[' aliases' ]]}) - %}
281
+ {%- endif - %}
282
+
283
+ {# If passed, make sure there are as many aliases as there are extract_columns, ensuring a 1:1 mapping #}
284
+ {%- if datavault4dbt .is_something (prejoin[' aliases' ]) - %}
285
+ {%- if not prejoin[' aliases' ]|length == prejoin[' extract_columns' ]|length - %}
286
+ {%- do exceptions .raise_compiler_error (" Prejoin aliases must have the same length as extract_columns. Got "
287
+ ~ prejoin[' extract_columns' ]|length ~ " extract_column(s) and " ~ prejoin[' aliases' ]|length ~ " aliase(s)." ) - %}
288
+ {%- endif - %}
289
+ {%- endif - %}
290
+
291
+ {# Generate the columns for the SELECT-statement #}
292
+ {%- for column in prejoin[' extract_columns' ] %}
293
+ ,{{ prejoin_alias }}.{{ column }} {% if datavault4dbt .is_something (prejoin[' aliases' ]) - %} AS {{ prejoin[' aliases' ][loop .index0 ] }} {% endif - %}
294
+ {%- endfor - %}
295
+ {%- endfor %}
270
296
271
297
FROM {{ last_cte }} lcte
272
298
273
- {% for col, vals in prejoined_columns .items () %}
299
+ {# Iterate over prejoins and generate the join-statements #}
300
+ {%- for prejoin in prejoined_columns - %}
274
301
275
- {%- if ' src_name ' in vals . keys () or ' src_table ' in vals .keys () - %}
276
- {%- set relation = source(vals[ ' src_name ' ]|string, vals[ ' src_table ' ]) - %}
277
- {%- elif ' ref_model ' in vals .keys () - %}
278
- {%- set relation = ref(vals[ ' ref_model ' ]) - %}
302
+ {%- if ' ref_model ' in prejoin .keys () - %}
303
+ {% set relation = ref(prejoin[ ' ref_model ' ]) - %}
304
+ {%- elif ' src_name ' in prejoin . keys () and ' src_table ' in prejoin .keys () - %}
305
+ {%- set relation = source(prejoin[ ' src_name ' ]|string, prejoin[ ' src_table ' ]) - %}
279
306
{%- else - %}
280
307
{%- set error_message - %}
281
308
Prejoin error: Invalid target entity definition. Allowed are:
@@ -296,28 +323,25 @@ prejoined_columns AS (
296
323
ref_column_name: join_columns_in_ref_model
297
324
298
325
Got:
299
- {{ col }}: {{ vals }}
326
+ {{ prejoin }}
300
327
{%- endset - %}
301
328
302
329
{%- do exceptions .raise_compiler_error (error_message) - %}
303
330
{%- endif - %}
304
331
305
- {# This sets a default value for the operator that connects multiple joining conditions. Only when it is not set by user. #}
306
- {%- if ' operator' not in vals .keys () - %}
332
+ {%- if ' operator' not in prejoin .keys () - %}
307
333
{%- set operator = ' AND' - %}
308
334
{%- else - %}
309
- {%- set operator = vals [' operator' ] - %}
335
+ {%- set operator = prejoin [' operator' ] - %}
310
336
{%- endif - %}
311
-
312
- {%- set prejoin_alias = ' pj_' + loop .index |string - %}
313
-
314
- left join {{ relation }} as {{ prejoin_alias }}
315
- on {{ datavault4dbt .multikey (columns= vals[' this_column_name' ], prefix= [' lcte' , prejoin_alias], condition= ' =' , operator= operator, right_columns= vals[' ref_column_name' ]) }}
316
-
317
- {% endfor %}
337
+ {%- set prejoin_alias = ' pj_' + loop .index |string %}
338
+
339
+ left join {{ relation }} as {{ prejoin_alias }}
340
+ on {{ datavault4dbt .multikey (columns= prejoin[' this_column_name' ], prefix= [' lcte' , prejoin_alias], condition= ' =' , operator= operator, right_columns= prejoin[' ref_column_name' ]) }}
341
+ {%- endfor - %}
318
342
319
343
{% set last_cte = " prejoined_columns" - %}
320
- {%- set final_columns_to_select = final_columns_to_select + prejoined_column_names %}
344
+ {%- set final_columns_to_select = final_columns_to_select + prejoined_column_names - %}
321
345
),
322
346
{%- endif - %}
323
347
@@ -444,65 +468,61 @@ unknown_values AS (
444
468
445
469
SELECT
446
470
447
- {{ datavault4dbt .string_to_timestamp (timestamp_format, beginning_of_all_times) }} as {{ load_datetime_col_name }},
448
- ' {{ unknown_value_rsrc }}' as {{ record_source_col_name }}
471
+ {{ datavault4dbt .string_to_timestamp (timestamp_format, beginning_of_all_times) }} as {{ load_datetime_col_name }}
472
+ , ' {{ unknown_value_rsrc }}' as {{ record_source_col_name }}
449
473
450
- {%- if columns_without_excluded_columns is defined and columns_without_excluded_columns| length > 0 - %},
474
+ {%- if columns_without_excluded_columns is defined and columns_without_excluded_columns| length > 0 - %}
451
475
{# Generating Ghost Records for all source columns, except the ldts, rsrc & edwSequence column #}
452
476
{%- for column in columns_without_excluded_columns %}
453
- {{ datavault4dbt .ghost_record_per_datatype (column_name= column .name , datatype= column .dtype , ghost_record_type= ' unknown' ) }}
454
- {%- if not loop .last %},{% endif - %}
477
+ ,{{ datavault4dbt .ghost_record_per_datatype (column_name= column .name , datatype= column .dtype , ghost_record_type= ' unknown' ) }}
455
478
{%- endfor - %}
456
479
457
480
{%- endif - %}
458
481
459
- {%- if datavault4dbt .is_something (missing_columns) - %},
482
+ {%- if datavault4dbt .is_something (missing_columns) - %}
460
483
{# Additionally generating ghost record for missing columns #}
461
484
{%- for col, dtype in missing_columns .items () %}
462
- {{ datavault4dbt .ghost_record_per_datatype (column_name= col, datatype= dtype, ghost_record_type= ' unknown' ) }}
463
- {%- if not loop .last %},{% endif - %}
485
+ ,{{- datavault4dbt .ghost_record_per_datatype (column_name= col, datatype= dtype, ghost_record_type= ' unknown' ) }}
464
486
{%- endfor - %}
465
487
{%- endif - %}
466
488
467
- {%- if datavault4dbt .is_something (prejoined_columns) - %},
468
- {# Additionally generating ghost records for the prejoined attributes#}
469
- {% for col, vals in prejoined_columns . items () %}
489
+ {%- if datavault4dbt .is_something (prejoined_columns) - %}
490
+ {# Additionally generating ghost records for the prejoined attributes #}
491
+ {%- for prejoin in prejoined_columns - %}
470
492
471
- {%- if ' src_name ' in vals . keys () or ' src_table ' in vals .keys () - %}
472
- {%- set relation = source(vals[ ' src_name ' ]|string, vals[ ' src_table ' ]) - %}
473
- {%- elif ' ref_model ' in vals .keys () - %}
474
- {%- set relation = ref(vals[ ' ref_model ' ]) - %}
493
+ {%- if ' ref_model ' in prejoin .keys () - %}
494
+ {%- set relation = ref(prejoin[ ' ref_model ' ]) - %}
495
+ {%- elif ' src_name ' in prejoin . keys () and ' src_table ' in prejoin .keys () - %}
496
+ {%- set relation = source(prejoin[ ' src_name ' ]|string, prejoin[ ' src_table ' ]) - %}
475
497
{%- endif - %}
476
498
477
499
{%- set pj_relation_columns = adapter .get_columns_in_relation ( relation ) - %}
478
- {{ log(' pj_relation_columns: ' ~ pj_relation_columns, false ) }}
479
-
480
- {% for column in pj_relation_columns - %}
481
-
482
- {% if column .name |lower == vals[' bk' ]|lower - %}
483
- {{ log(' column found? yes, for column :' ~ column .name , false) }}
484
- {{ datavault4dbt .ghost_record_per_datatype (column_name= column .name , datatype= column .dtype , ghost_record_type= ' unknown' , alias= col) }}
500
+ {{ log(' pj_relation_columns for ' ~relation~' : ' ~ pj_relation_columns, false ) }}
501
+
502
+ {%- for column in pj_relation_columns - %}
503
+ {%- if column .name |lower in prejoin[' extract_columns' ]|map(' lower' ) - %}
504
+ {%- set prejoin_extract_cols_lower = prejoin[' extract_columns' ]|map(' lower' )|list - %}
505
+ {%- set prejoin_col_index = prejoin_extract_cols_lower .index (column .name |lower ) - %}
506
+ {{ log(' column found? yes, for column: ' ~ column .name , false) }}
507
+ ,{{ datavault4dbt .ghost_record_per_datatype (column_name= column .name , datatype= column .dtype , ghost_record_type= ' unknown' , alias= prejoin[' aliases' ][prejoin_col_index]) }}
485
508
{%- endif - %}
486
509
487
510
{%- endfor - %}
488
- {%- if not loop .last %},{% endif %}
489
511
{% endfor - %}
490
512
{%- endif %}
491
513
492
- {%- if datavault4dbt .is_something (derived_columns) - %},
493
- {# Additionally generating Ghost Records for Derived Columns #}
494
- {%- for column_name, properties in derived_columns_with_datatypes_DICT .items () %}
495
- {{ datavault4dbt .ghost_record_per_datatype (column_name= column_name, datatype= properties .datatype , ghost_record_type= ' unknown' ) }}
496
- {%- if not loop .last %},{% endif - %}
514
+ {%- if datavault4dbt .is_something (derived_columns) - %}
515
+ {# Additionally generating Ghost Records for Derived Columns #}
516
+ {% for column_name, properties in derived_columns_with_datatypes_DICT .items () %}
517
+ ,{{ datavault4dbt .ghost_record_per_datatype (column_name= column_name, datatype= properties .datatype , ghost_record_type= ' unknown' ) }}
497
518
{%- endfor - %}
498
519
499
520
{%- endif - %}
500
521
501
- {%- if datavault4dbt .is_something (processed_hash_columns) - %},
522
+ {%- if datavault4dbt .is_something (processed_hash_columns) - %}
502
523
503
524
{%- for hash_column in processed_hash_columns %}
504
- CAST({{ datavault4dbt .as_constant (column_str= unknown_key) }} as {{ hash_dtype }}) as {{ hash_column }}
505
- {%- if not loop .last %},{% endif %}
525
+ ,CAST({{ datavault4dbt .as_constant (column_str= unknown_key) }} as {{ hash_dtype }}) as {{ hash_column }}
506
526
{%- endfor - %}
507
527
508
528
{%- endif - %}
@@ -514,62 +534,61 @@ error_values AS (
514
534
515
535
SELECT
516
536
517
- {{ datavault4dbt .string_to_timestamp (timestamp_format , end_of_all_times) }} as {{ load_datetime_col_name }},
518
- ' {{ error_value_rsrc }}' as {{ record_source_col_name }}
537
+ {{ datavault4dbt .string_to_timestamp (timestamp_format , end_of_all_times) }} as {{ load_datetime_col_name }}
538
+ , ' {{ error_value_rsrc }}' as {{ record_source_col_name }}
519
539
520
- {%- if columns_without_excluded_columns is defined and columns_without_excluded_columns| length > 0 - %},
540
+ {%- if columns_without_excluded_columns is defined and columns_without_excluded_columns| length > 0 - %}
521
541
{# Generating Ghost Records for Source Columns #}
522
542
{%- for column in columns_without_excluded_columns %}
523
- {{ datavault4dbt .ghost_record_per_datatype (column_name= column .name , datatype= column .dtype , ghost_record_type= ' error' ) }}
524
- {%- if not loop .last %},{% endif - %}
543
+ ,{{ datavault4dbt .ghost_record_per_datatype (column_name= column .name , datatype= column .dtype , ghost_record_type= ' error' ) }}
525
544
{%- endfor - %}
526
545
527
546
{%- endif - %}
528
547
529
- {%- if datavault4dbt .is_something (missing_columns) - %},
548
+ {%- if datavault4dbt .is_something (missing_columns) - %}
530
549
{# Additionally generating ghost record for Missing columns #}
531
550
{%- for col, dtype in missing_columns .items () %}
532
- {{ datavault4dbt .ghost_record_per_datatype (column_name= col, datatype= dtype, ghost_record_type= ' error' ) }}
533
- {%- if not loop .last %},{% endif - %}
551
+ ,{{ datavault4dbt .ghost_record_per_datatype (column_name= col, datatype= dtype, ghost_record_type= ' error' ) }}
534
552
{%- endfor - %}
535
553
{%- endif - %}
536
554
537
- {%- if datavault4dbt .is_something (prejoined_columns) - %},
538
- {# Additionally generating ghost records for the prejoined attributes #}
539
- {%- for col, vals in prejoined_columns . items () %}
555
+ {%- if datavault4dbt .is_something (prejoined_columns) - %}
556
+ {# Additionally generating ghost records for the prejoined attributes#}
557
+ {% for prejoin in prejoined_columns %}
540
558
541
- {%- if ' src_name ' in vals . keys () or ' src_table ' in vals .keys () - %}
542
- {%- set relation = source(vals[ ' src_name ' ]|string, vals[ ' src_table ' ]) - %}
543
- {%- elif ' ref_model ' in vals .keys () - %}
544
- {%- set relation = ref(vals[ ' ref_model ' ]) - %}
559
+ {%- if ' ref_model ' in prejoin .keys () - %}
560
+ {% set relation = ref(prejoin[ ' ref_model ' ]) - %}
561
+ {%- elif ' src_name ' in prejoin . keys () and ' src_table ' in prejoin .keys () - %}
562
+ {%- set relation = source(prejoin[ ' src_name ' ]|string, prejoin[ ' src_table ' ]) - %}
545
563
{%- endif - %}
546
564
547
565
{%- set pj_relation_columns = adapter .get_columns_in_relation ( relation ) - %}
566
+ {{- log(' pj_relation_columns for ' ~relation~' : ' ~ pj_relation_columns, false ) - }}
548
567
549
568
{% for column in pj_relation_columns - %}
550
- {% if column .name |lower == vals[' bk' ]|lower - %}
551
- {{ datavault4dbt .ghost_record_per_datatype (column_name= column .name , datatype= column .dtype , ghost_record_type= ' error' , alias= col) - }}
569
+ {%- if column .name |lower in prejoin[' extract_columns' ]|map(' lower' ) - %}
570
+ {%- set prejoin_extract_cols_lower = prejoin[' extract_columns' ]|map(' lower' )|list - %}
571
+ {%- set prejoin_col_index = prejoin_extract_cols_lower .index (column .name |lower ) - %}
572
+ {{ log(' column found? yes, for column: ' ~ column .name , false) }}
573
+ ,{{ datavault4dbt .ghost_record_per_datatype (column_name= column .name , datatype= column .dtype , ghost_record_type= ' error' , alias= prejoin[' aliases' ][prejoin_col_index]) }}
552
574
{%- endif - %}
575
+
553
576
{%- endfor - %}
554
- {%- if not loop .last - %},{%- endif %}
555
577
{% endfor - %}
578
+ {%- endif %}
556
579
557
- {%- endif - %}
558
-
559
- {%- if datavault4dbt .is_something (derived_columns) %},
580
+ {%- if datavault4dbt .is_something (derived_columns) %}
560
581
{# Additionally generating Ghost Records for Derived Columns #}
561
582
{%- for column_name, properties in derived_columns_with_datatypes_DICT .items () %}
562
- {{ datavault4dbt .ghost_record_per_datatype (column_name= column_name, datatype= properties .datatype , ghost_record_type= ' error' ) }}
563
- {%- if not loop .last %},{% endif %}
583
+ ,{{ datavault4dbt .ghost_record_per_datatype (column_name= column_name, datatype= properties .datatype , ghost_record_type= ' error' ) }}
564
584
{%- endfor - %}
565
585
566
586
{%- endif - %}
567
587
568
- {%- if datavault4dbt .is_something (processed_hash_columns) - %},
588
+ {%- if datavault4dbt .is_something (processed_hash_columns) - %}
569
589
570
590
{%- for hash_column in processed_hash_columns %}
571
- CAST({{ datavault4dbt .as_constant (column_str= error_key) }} as {{ hash_dtype }}) as {{ hash_column }}
572
- {%- if not loop .last %},{% endif %}
591
+ ,CAST({{ datavault4dbt .as_constant (column_str= error_key) }} as {{ hash_dtype }}) as {{ hash_column }}
573
592
{%- endfor - %}
574
593
575
594
{%- endif - %}
0 commit comments