dlt-utils-lib 0.8.0__tar.gz → 0.8.2__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,3 +1,3 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: dlt_utils_lib
3
- Version: 0.8.0
3
+ Version: 0.8.2
@@ -44,3 +44,22 @@ def update_cdc_timestamp(df: DataFrame, time_diff_threshold: int) -> DataFrame:
44
44
  ).otherwise(col('cdc_timestamp'))
45
45
  )
46
46
  return df
47
+
48
+
49
+ def adjust_sequence_operation(df: DataFrame, uniq_columns: list) -> DataFrame:
50
+ window_spec = Window.partitionBy(uniq_columns).orderBy("cdc_timestamp")
51
+
52
+ df_with_lag = df.withColumn("prev_op", lag("Op").over(window_spec)) \
53
+ .withColumn("prev_cdc_timestamp", lag("cdc_timestamp").over(window_spec))
54
+
55
+ window_spec_all = Window.partitionBy(uniq_columns)
56
+ df_with_max_u_timestamp = df_with_lag.withColumn(
57
+ "min_u_cdc_timestamp", spark_min(when(col("Op") == "U", col("cdc_timestamp"))).over(window_spec_all)
58
+ )
59
+
60
+ df_adjusted = df_with_max_u_timestamp.withColumn(
61
+ "cdc_timestamp",
62
+ when((col("Op") == "I") & (col("min_u_cdc_timestamp").isNotNull()), col("min_u_cdc_timestamp") -F.expr("interval 1 milliseconds")).otherwise(col("cdc_timestamp"))
63
+ )
64
+ df_final = df_adjusted.drop("prev_op", "prev_cdc_timestamp", "min_u_cdc_timestamp")
65
+ return df_final
@@ -1,8 +1,8 @@
1
1
  from pyspark.sql.functions import col, expr
2
- from .dlt_transformations import update_cdc_timestamp, apply_partitions
2
+ from .dlt_transformations import update_cdc_timestamp, apply_partitions, adjust_sequence_operation
3
3
 
4
4
  # Define the transformation function dynamically
5
- def create_bronze_table_definition(spark, dlt, table_name: str, files_path: str, file_format: str, partitions: dict, schema_exclude_columns: list, time_diff_history_cdc_timestamp: int = 30):
5
+ def create_bronze_table_definition(spark, dlt, table_name: str, files_path: str, file_format: str, partitions: dict, schema_exclude_columns: list, keys: list, time_diff_history_cdc_timestamp: int = 30):
6
6
  @dlt.table(
7
7
  name=f"bronze_{table_name}",
8
8
  comment="This is the bronze table.",
@@ -20,6 +20,7 @@ def create_bronze_table_definition(spark, dlt, table_name: str, files_path: str,
20
20
  .load(files_path) \
21
21
  .withColumn('cdc_timestamp', col('cdc_timestamp').cast('timestamp')) \
22
22
  .transform(lambda df: update_cdc_timestamp(df, time_diff_history_cdc_timestamp)) \
23
+ .transform(lambda df: adjust_sequence_operation(df, keys)) \
23
24
  .transform(lambda df: apply_partitions(df, partitions))
24
25
 
25
26
  return transform_cdc_to_bronze
@@ -1,3 +1,3 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: dlt-utils-lib
3
- Version: 0.8.0
3
+ Version: 0.8.2
@@ -2,7 +2,7 @@ from setuptools import setup, find_packages
2
2
 
3
3
  setup(
4
4
  name='dlt_utils_lib',
5
- version='0.8.0',
5
+ version='0.8.2',
6
6
  packages=find_packages(),
7
7
  install_requires=[
8
8
  'pyspark',
File without changes