Change Tracking Utilities¶
ChangeTrackingMode ¶
Bases: str, Enum
Accepted values for change_tracking_mode.
CURRENT_ONLY– keep exactly one current row per key (Type 1 semantics).TRACK_HISTORY– create new versions + close previous ones (Type 2 semantics).
ChangeTrackingWriteBuilder ¶
ChangeTrackingWriteBuilder(df: DataFrame)
Thin wrapper that applies change-tracking semantics via df.write.change_tracking.
Source code in src/spark_fuse/utils/change_tracking.py
137 138 139 140 | |
table ¶
table(name: str, verbose: bool = False, **options: Any) -> None
Write to target table with change tracking semantics.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
Target table name or Delta path. |
required |
verbose
|
bool
|
When True, log operational details at INFO level. |
False
|
**options
|
Any
|
Additional change tracking options. |
{}
|
Source code in src/spark_fuse/utils/change_tracking.py
163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 | |
apply_change_tracking ¶
apply_change_tracking(spark: SparkSession, source_df: DataFrame, target: str, *, change_tracking_mode: Union[ChangeTrackingMode, str, int], verbose: bool = False, **kwargs: Any) -> None
Unified entry point for change-tracking writes.
change_tracking_mode accepts:
- :class:ChangeTrackingMode
- "current_only" / "track_history"
- 1 / 2 (handy when passing options via strings)
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
spark
|
SparkSession
|
SparkSession used to read/write Delta tables. |
required |
source_df
|
DataFrame
|
DataFrame containing incoming records. |
required |
target
|
str
|
Unity Catalog table name or Delta path. |
required |
change_tracking_mode
|
Union[ChangeTrackingMode, str, int]
|
The change tracking mode to use. |
required |
verbose
|
bool
|
When |
False
|
**kwargs
|
Any
|
Additional arguments passed to the underlying upsert function. |
{}
|
Source code in src/spark_fuse/utils/change_tracking.py
834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 | |
apply_change_tracking_from_options ¶
apply_change_tracking_from_options(spark: SparkSession, source_df: DataFrame, target: str, *, options: Mapping[str, Any], verbose: bool = False) -> None
Route the write based on df.write-style options.
Used by :class:ChangeTrackingWriteBuilder and data-frame accessors.
Expected keys
change_tracking_mode: accepts1/2orcurrent_only/track_history.- Strategy options via
current_only_options/track_history_optionsor the genericchange_tracking_optionskey.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
spark
|
SparkSession
|
SparkSession used to read/write Delta tables. |
required |
source_df
|
DataFrame
|
DataFrame containing incoming records. |
required |
target
|
str
|
Unity Catalog table name or Delta path. |
required |
options
|
Mapping[str, Any]
|
Options mapping containing change_tracking_mode and optional kwargs. |
required |
verbose
|
bool
|
When True, log operational details at INFO level. |
False
|
Source code in src/spark_fuse/utils/change_tracking.py
234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 | |
change_tracking_writer ¶
change_tracking_writer(df: DataFrame) -> ChangeTrackingWriteBuilder
Return a builder that mirrors DataFrameWriter but routes to change-tracking helpers.
Source code in src/spark_fuse/utils/change_tracking.py
183 184 185 186 | |
current_only_upsert ¶
current_only_upsert(spark: SparkSession, source_df: DataFrame, target: str, *, business_keys: Sequence[str], tracked_columns: Optional[Iterable[str]] = None, dedupe_keys: Optional[Sequence[str]] = None, order_by: Optional[Sequence[str]] = None, hash_col: str = 'row_hash', null_key_policy: str = 'error', create_if_not_exists: bool = True, allow_schema_evolution: bool = False, verbose: bool = False) -> None
Implement :class:ChangeTrackingMode.CURRENT_ONLY.
Keeps exactly one active row per business_keys combination by:
- De-duplicating within the incoming batch using
dedupe_keys/order_by. - Hashing the tracked columns to detect changes.
- Running a Delta
MERGEthat updates only when the row changed and inserts when missing.
When allow_schema_evolution is True we temporarily enable Delta auto-merge so new source
columns are added to the target on demand.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
spark
|
SparkSession
|
SparkSession used to read/write Delta tables. |
required |
source_df
|
DataFrame
|
DataFrame containing incoming records. |
required |
target
|
str
|
Unity Catalog table name or Delta path. |
required |
business_keys
|
Sequence[str]
|
Columns that uniquely identify an entity (merge condition). |
required |
tracked_columns
|
Optional[Iterable[str]]
|
Columns whose changes trigger an update. Defaults to all non-key columns. |
None
|
dedupe_keys
|
Optional[Sequence[str]]
|
Columns used to de-duplicate input before merge. Defaults to |
None
|
order_by
|
Optional[Sequence[str]]
|
Columns used to choose the most recent record per |
None
|
hash_col
|
str
|
Name of the hash column used to detect row changes. |
'row_hash'
|
null_key_policy
|
str
|
Policy for null business keys. Either |
'error'
|
create_if_not_exists
|
bool
|
When |
True
|
allow_schema_evolution
|
bool
|
When |
False
|
verbose
|
bool
|
When |
False
|
Source code in src/spark_fuse/utils/change_tracking.py
464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 | |
enable_change_tracking_accessors ¶
enable_change_tracking_accessors(*, force: bool = False) -> None
Attach .change_tracking helpers to DataFrame/DataFrameWriter/DataStreamWriter.
Once enabled (this module runs it on import), df.change_tracking,
df.write.change_tracking, and df.writeStream.change_tracking are available.
Batch accessors expose :class:ChangeTrackingWriteBuilder; the streaming accessor exposes
:class:~spark_fuse.utils.change_tracking_streaming.StreamingChangeTrackingBuilder.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
force
|
bool
|
When |
False
|
Source code in src/spark_fuse/utils/change_tracking.py
189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 | |
track_history_upsert ¶
track_history_upsert(spark: SparkSession, source_df: DataFrame, target: str, *, business_keys: Sequence[str], tracked_columns: Optional[Iterable[str]] = None, dedupe_keys: Optional[Sequence[str]] = None, order_by: Optional[Sequence[str]] = None, effective_col: str = 'effective_start_ts', expiry_col: str = 'effective_end_ts', current_col: str = 'is_current', version_col: str = 'version', hash_col: str = 'row_hash', load_ts_expr: Optional[Union[str, Column]] = None, default_expiry_value: Optional[Union[str, Column]] = None, null_key_policy: str = 'error', create_if_not_exists: bool = True, allow_schema_evolution: bool = False, verbose: bool = False) -> None
Implement :class:ChangeTrackingMode.TRACK_HISTORY.
Two-step algorithm
1) Close currently active target rows when incoming record for same key has a different row hash. 2) Insert new active rows for new keys or changed keys with incremented version.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
spark
|
SparkSession
|
SparkSession used to read/write Delta tables. |
required |
source_df
|
DataFrame
|
DataFrame containing incoming records. Can include duplicates; these are
de-duplicated by |
required |
target
|
str
|
Unity Catalog table name (for example, |
required |
business_keys
|
Sequence[str]
|
Columns that uniquely identify an entity (merge condition). |
required |
tracked_columns
|
Optional[Iterable[str]]
|
Columns whose changes trigger a new version. Defaults to all non-key, non-metadata columns. |
None
|
dedupe_keys
|
Optional[Sequence[str]]
|
Columns used to de-duplicate input before merge. Defaults to |
None
|
order_by
|
Optional[Sequence[str]]
|
Columns used to choose the most recent record per |
None
|
effective_col
|
str
|
Name of the effective-start timestamp column in the target dataset. |
'effective_start_ts'
|
expiry_col
|
str
|
Name of the effective-end timestamp column in the target dataset. |
'effective_end_ts'
|
current_col
|
str
|
Name of the boolean "is current" column in the target dataset. |
'is_current'
|
version_col
|
str
|
Name of the version column in the target dataset. |
'version'
|
hash_col
|
str
|
Name of the hash column used to detect row changes. |
'row_hash'
|
load_ts_expr
|
Optional[Union[str, Column]]
|
PySpark Column or SQL expression string that provides the effective-start
Timestamp to use for |
None
|
default_expiry_value
|
Optional[Union[str, Column]]
|
Value to use for |
None
|
null_key_policy
|
str
|
Policy for null business keys in |
'error'
|
create_if_not_exists
|
bool
|
When |
True
|
allow_schema_evolution
|
bool
|
When |
False
|
verbose
|
bool
|
When |
False
|
Source code in src/spark_fuse/utils/change_tracking.py
627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 | |