-
Notifications
You must be signed in to change notification settings - Fork 14
Add history mode guide #49
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
fivetran-abdulsalam
wants to merge
14
commits into
main
Choose a base branch
from
add-history-guide
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from 1 commit
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
e56d843
added history mode guide
fivetran-abdulsalam 34e7146
doc grammar update
fivetran-abdulsalam 1a5084f
Update how-to-handle-history-mode-files.md
5tran-alexil 20139d3
incorporate tech writer changes
fivetran-abdulsalam 93746f2
small refactor:
fivetran-abdulsalam c92095e
more refactor
fivetran-abdulsalam 747a5e4
changed grammar
fivetran-abdulsalam 62deeb8
changed insert to upserted
fivetran-abdulsalam c336946
incorporate more comments
fivetran-abdulsalam fd405e8
Apply suggestions from code review
5tran-alexil 42112f4
Update how-to-handle-history-mode-batch-files.md
5tran-alexil df9968d
removed deleted_column
fivetran-abdulsalam 92deeb6
Apply suggestions from code review
5tran-alexil 20a582f
Merge branch 'main' into add-history-guide
fivetran-abdulsalam File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
|
fivetran-abdulsalam marked this conversation as resolved.
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,220 @@ | ||
| #### What is History Mode | ||
|
fivetran-abdulsalam marked this conversation as resolved.
Outdated
|
||
|
|
||
| History mode allows us to capture every version of each record processed by the fivetran connectors. | ||
|
fivetran-abdulsalam marked this conversation as resolved.
Outdated
|
||
| In order to keep all versions of the record, we have introduced three new system columns for tables with history mode enabled. | ||
|
fivetran-abdulsalam marked this conversation as resolved.
Outdated
|
||
|
|
||
|
|
||
| Column | Type | Description | ||
| --- | --- | --- | ||
| _fivetran_active | Boolean | TRUE if it is the currently active record. FALSE if it is a historical version of the record. Only one version of the record can be TRUE. | ||
| _fivatran_start | TimeStamp | The time when the record was first created or modified in the source. | ||
|
fivetran-abdulsalam marked this conversation as resolved.
Outdated
|
||
| _fivetran_end | TimeStamp | The value for this column depends on whether the record is active. If the record is not active(`_fivetran_active`=FALSE), then `_fivetran_end` value will be `_fivetran_start` of the next version of the record minus 1 millisecond. If the record is deleted, then the value will be the same as the timestamp of delete operation. If the record is active(`_fivetran_active`=TRUE), then `_fivetran_end` is the max allowed value that we can set for a TIMESTAMP column. | ||
|
fivetran-abdulsalam marked this conversation as resolved.
Outdated
|
||
|
|
||
|
|
||
| #### Points to remember in history mode | ||
|
fivetran-abdulsalam marked this conversation as resolved.
Outdated
|
||
|
|
||
| - In WriterBatchRequest we pass a new optional field HistoryMode which specifies if current connector mode is history mode or not. In this HistoryMode field, we pass `deleted_column` column name which we need to modify only if it is present in destination. | ||
|
fivetran-abdulsalam marked this conversation as resolved.
Outdated
|
||
| - If the existing table is not empty then in the batch file we also send a boolean column `_fivetran_earliest`. Suppose in an `upsert` we got multiple versions of the same record in a flush, then we set the `_fivetran_earliest` to `TRUE` for the record which have the earliest `_fivetran_start` and rest of the versions will have `_fivetran_earliest` as FALSE. | ||
|
fivetran-abdulsalam marked this conversation as resolved.
Outdated
|
||
| - For each Replace, Update and Delete batch files, DELETE the existing records from destination table if `_fivetran_start` of destination table is greater than or equal to `_fivetran_start` of batch file(Refer Replace example 1 and example 2). | ||
|
fivetran-abdulsalam marked this conversation as resolved.
Outdated
|
||
|
|
||
| Note: This `_fivetran_earliest` column should never be added in the destination table. We introduced this column to easily identify the earliest record and can be used to optimize data loads query. | ||
|
fivetran-abdulsalam marked this conversation as resolved.
Outdated
|
||
| Below is an example of `replaqce_file` | ||
|
fivetran-abdulsalam marked this conversation as resolved.
Outdated
|
||
|
|
||
| Id(PK) | COL1 | _fivetran_start(PK) | _fivetran_end | _fivetran_active | _fivetran_earliest | ||
| ---|---------|---------------------| --- |------------------| --- | ||
| 1 | abc | T1 | T2-1 | FALSE | TRUE | ||
| 2 | xyz | T1 | TMAX | TRUE | TRUE | ||
| 1 | pqr | T2 | T3-1 | FALSE | FALSE | ||
| 1 | def | T3 | TMAX | TRUE | FALSE | ||
|
|
||
| #### How to Handle Replaces, Updates and Deletes | ||
|
fivetran-abdulsalam marked this conversation as resolved.
Outdated
|
||
|
|
||
| ##### Replace | ||
|
fivetran-abdulsalam marked this conversation as resolved.
Outdated
|
||
|
|
||
| ###### Example 1: | ||
|
fivetran-abdulsalam marked this conversation as resolved.
Outdated
|
||
|
|
||
| When `_fivetran_start` of destination table is less than `_fivetran_start` of batch file. | ||
|
fivetran-abdulsalam marked this conversation as resolved.
Outdated
|
||
| Suppose the existing Table in destination is as below: | ||
|
|
||
| Id(PK) | COL1 | COL2 | _fivetran_start(PK) | _fivetran_end | _fivetran_active | _fivetran_synced | ||
| --- |------|----| --- | --- | --- | --- | ||
| 1 | abc | 1 |T1 | T2-1 | FALSE | T100 | ||
| 1 | pqr | 2 | T2 | TMAX | TRUE | T101 | ||
| 2 | mno | 3 | T2 | TMAX | TRUE | T103 | ||
|
|
||
| At source new records are added: | ||
|
fivetran-abdulsalam marked this conversation as resolved.
Outdated
|
||
|
|
||
| Id(PK) | COL1 | COL2 | Timestamp | Type | ||
| --- | --- | --- |-----------| --- | ||
| 1 | def |1 | T3 | Inserted | ||
| 1 | ghi | 1 | T4 | Inserted | ||
|
|
||
| Replace batch file will be: | ||
|
fivetran-abdulsalam marked this conversation as resolved.
Outdated
|
||
|
|
||
| Id(PK) | COL1 | COL2 | _fivetran_start(PK) | _fivetran_end | _fivetran_active | _fivatran_earliest | _fivetran_synced | ||
| --- |------|-------|---------------------| --- | --- | --- | --- | ||
| 1 | def | 1 | T3 | T4-1 | FALSE | TRUE | T104 | ||
| 1 | ghi | 1| T4 | TMAX | TRUE | FALSE | T105 | ||
|
|
||
|
|
||
| Final Destination Table will be: | ||
|
fivetran-abdulsalam marked this conversation as resolved.
Outdated
|
||
|
|
||
| Id(PK) | COL1 | COL2 | _fivetran_start(PK) | _fivetran_end | _fivetran_active | _fivetran_synced | ||
| --- |---|--------|---------------------| --- |------------------| --- | ||
| 1 | abc | 1 | T1 | T2-1 | FALSE | T100 | ||
| 1 | pqr | 2 | T2 | T3-1 | FALSE | T101 | ||
| 2 | mno | 3 | T3 | TMAX | TRUE | T103 | ||
| 1 | def | 1 |T3 | T4-1 | FALSE | T104 | ||
| 1 | ghi | 1 | T4 | TMAX | TRUE | T105 | ||
|
|
||
| **Explanation:** | ||
| - We got new records for id = 1. | ||
| - Check for corresponding earliest record(`_fivetran_earliest` as TRUE), DELETE the existing records from destination table if `_fivetran_start` of destination table is greater than or equal to `_fivetran_start` of batch file(In above example no) | ||
|
fivetran-abdulsalam marked this conversation as resolved.
Outdated
|
||
| - `_fivetran_end` of the active record in destination table is set to `_fivatran_start`-1 of the `_fivatran_earliest` record of batch file. | ||
|
fivetran-abdulsalam marked this conversation as resolved.
Outdated
|
||
| - Set `_fivetran_active` for above updated record to FALSE and `deleted_column`(if present in destination table) to TRUE | ||
|
fivetran-abdulsalam marked this conversation as resolved.
Outdated
|
||
| - New records are inserted AS IS excluding `_fivetran_earliest` column in destination table. | ||
|
fivetran-abdulsalam marked this conversation as resolved.
Outdated
|
||
|
|
||
| ###### Example 2 | ||
|
fivetran-abdulsalam marked this conversation as resolved.
Outdated
|
||
|
|
||
| When `_fivetran_start` of destination table is greater than or equal to `_fivetran_start` of batch file. | ||
|
fivetran-abdulsalam marked this conversation as resolved.
Outdated
|
||
| Suppose the existing Table in destination is as below: | ||
|
fivetran-abdulsalam marked this conversation as resolved.
Outdated
|
||
|
|
||
| Id(PK) | COL1 | COL2 | _fivetran_start(PK) | _fivetran_end | _fivetran_active | _fivetran_synced | ||
| --- |---|--------|---------------------| --- |------------------| --- | ||
| 1 | xyz | 4 | T1 | T3-1 | FALSE | T100 | ||
| 1 | abc | 1 | T3 | T4-1 | FALSE | T100 | ||
| 1 | pqr | 2 | T4 | TMAX | TRUE | T101 | ||
| 2 | mno | 3 | T4 | TMAX | TRUE | T103 | ||
|
|
||
| At source new records are added: | ||
|
fivetran-abdulsalam marked this conversation as resolved.
Outdated
|
||
|
|
||
| Id(PK) | COL1 | COL2 | Timestamp | Type | ||
| --- | --- | --- | --- | --- | ||
| 1 | ghi | 1 | T2 | Inserted | ||
|
|
||
|
|
||
|
|
||
| Replace batch file will be: | ||
|
fivetran-abdulsalam marked this conversation as resolved.
Outdated
|
||
|
|
||
| Id(PK) | COL1 | COL2 | _fivetran_start(PK) | _fivetran_end | _fivetran_active | _fivatran_earliest | _fivetran_synced | ||
| --- | --- | --- | --- | --- | --- | --- | --- | ||
| 1 | ghi | 1 | T2 | TMAX | TRUE | TRUE | T104 | ||
|
|
||
| Final Destination table will be: | ||
|
|
||
| Id(PK) | COL1 | COL2 | _fivetran_start(PK) | _fivetran_end | _fivetran_active | _fivetran_synced | ||
| --- | --- | --- | --- | --- | --- | --- | ||
| 1 | ghi | 1 | T2 | TMAX | TRUE | T104 | ||
| 1 | xyz | 4 | T1 | T3-1 | FALSE | T100 | ||
| 2 | mno | 3 | T4 | TMAX | TRUE | T103 | ||
|
|
||
| **Explanation:** | ||
| We got new records for id = 1. | ||
| - Check for corresponding earliest record(`_fivetran_earliest` TRUE), DELETE the existing records from destination table if `_fivetran_start` of destination table is greater than or equal to `_fivetran_start` of batch file(in above example yes, so deleted id = 1 with _fivetran_start = T3 and T4) | ||
| - `_fivetran_end` of the active record in destination table is set to `_fivatran_start`-1 of the `_fivatran_earliest` record of batch file. | ||
| - Set `_fivetran_active` for above updated record to FALSE and `deleted_column`(if present in destination table) to TRUE | ||
| - New records are inserted AS IS excluding `_fivetran_earliest` column in destination table. | ||
|
|
||
| ##### Updates | ||
|
fivetran-abdulsalam marked this conversation as resolved.
Outdated
|
||
|
|
||
| Suppose the existing Table in destination is: | ||
|
|
||
| Id(PK) | COL1 | COL2 | _fivetran_start(PK) | _fivetran_end | _fivetran_active | _fivetran_synced | ||
| --- | --- | --- | --- | --- | --- | --- | ||
| 1 | abc | 1 | T1 | T2-1 | FALSE | T100 | ||
| 1 | pqr | 2 | T2 | TMAX | TRUE | T101 | ||
| 2 | mno | 3 | T2 | TMAX | TRUE | T103 | ||
|
|
||
|
|
||
| At source records with Id = 1 is updated: | ||
|
|
||
| Id(PK) | COL1 | Timestamp | Type | ||
| --- | --- | --- | --- | ||
| 1 | xyz | T3 | Updated | ||
|
|
||
|
|
||
|
|
||
| And record with id = 2 is updated as: | ||
|
fivetran-abdulsalam marked this conversation as resolved.
Outdated
|
||
|
|
||
| Id(PK) | COL2 | Timestamp | Type | ||
| --- | --- | --- | --- | ||
| 2 | 1000 | T4 | Updated | ||
|
|
||
| And record with Id = 1 is again updated as | ||
|
|
||
| Id(PK) | COL1 | Timestamp | Type | ||
| --- | --- | --- | --- | ||
| 1 | def | T5 | Updated | ||
|
|
||
|
|
||
|
|
||
| Update batch file will be: | ||
|
|
||
|
|
||
| Id(PK) | COL1 | COL2 | _fivetran_start(PK) | _fivetran_end | _fivetran_active | _fivatran_earliest | _fivatran_synced | ||
| --- | --- | --- | --- | --- | --- | --- | --- | ||
| 1 | xyz | | T3| T5-1 | FALSE | TRUE | T107 | ||
| 2 | | 1000 | T4 | TMAX | TRUE | TRUE | T108 | ||
| 1 | def | | T5 | TMAX | TRUE | FALSE | T109 | ||
|
|
||
|
|
||
| Final Destination Table will be: | ||
|
|
||
| Id(PK) | COL1 | COL2 | _fivetran_start(PK) | _fivetran_end | _fivetran_active | _fivetran_synced | ||
| --- | --- | --- | --- | --- | --- | --- | ||
| 1 | abc | 1 | T1 | T2-1 | FALSE | T100 | ||
| 1 | pqr | 2 | T2 | T3-1 | FALSE | T101 | ||
| 2 | mno | 3 | T2 | T4-1 | FALSE | T103 | ||
| 1 | def | 2 | T5 | TMAX | TRUE | T109 | ||
| 1 | xyz | 2 | T3 | T5-1 | FALSE | T107 | ||
| 2 | mno | 1000 | T4 | TMAX | TRUE | T108 | ||
|
|
||
|
|
||
|
|
||
| **Explanation:** | ||
| - In batch file we got records with id = 1 and id = 2. | ||
| - We set other columns(non updated columns) to the values of the active records. In above case for id = 2, we didn’t get COL1 value, so we set COL1 to “mno”(COL1 value of the active record) | ||
| - _fivetran_end of the active record in destination table is set to _fivatran_start-1 of the _fivatran_earliest record of batch file | ||
| - Set _fivetran_active for above updated record to FALSE and deleted_column(if present in destination table) to TRUE | ||
| - Other columns are set AS IS from the batch file in the destination table except _fivetran_earliest column. | ||
|
|
||
|
|
||
| ##### Deletes | ||
|
fivetran-abdulsalam marked this conversation as resolved.
Outdated
|
||
|
|
||
| Existing Table in destination: | ||
|
|
||
| Id(PK) | COL1 | COL2 | _fivetran_start(PK) | _fivetran_end | _fivetran_active | _fivetran_synced | ||
| --- | --- | --- | --- | --- | --- | --- | ||
| 1 | abc | 1 | T1 | T2-1 | FALSE | T100 | ||
| 1 | pqr | 2 | T2 | TMAX | TRUE | T101 | ||
| 2 | mno | 3 | T2 | TMAX | TRUE | T103 | ||
|
|
||
|
|
||
|
|
||
| At source a record is deleted: | ||
|
|
||
|
|
||
| Id(PK) | Timestamp | Type | ||
| --- | --- | --- | ||
| 1 | T3 | Deleted | ||
|
|
||
|
|
||
| Delete batch file will be: | ||
|
|
||
| Id(PK) | _fivetran_start(PK) | _fivetran_end | _fivetran_active | _fivetran_earliest | _fivetran_synced | ||
| --- | --- |---------------|------| --- | --- | ||
| 1 | | T3-1 | | TRUE | T104 | ||
|
|
||
|
|
||
| Final Destination Table will be: | ||
|
|
||
| Id(PK) | COL1 | COL2 | _fivetran_start(PK) | _fivetran_end | _fivetran_active | _fivetran_synced | ||
| --- | --- | --- | --- | --- |------------------| --- | ||
| 1 | abc | 1 | T1 | T2-1 | FALSE | T100 | ||
| 1 | pqr | 2 | T2 | T3-1 | FALSE | T101 | ||
| 2 | mno | 3 | T2 | TMAX | TRUE | T103 | ||
|
|
||
| **Explanation:** | ||
| Set `_fivetran_active` to FALSE for the active record and set `_fivetran_end` = T3-1 and `deleted_column`(if present in destination) to TRUE | ||
|
|
||
|
|
||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.