-
Notifications
You must be signed in to change notification settings - Fork 44
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
get all composite key candidates function added #58
Open
souvik-databricks
wants to merge
1
commit into
main
Choose a base branch
from
feature/get_all_key_combos
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.
Open
Changes from all commits
Commits
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 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
This file contains 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 |
---|---|---|
|
@@ -3,7 +3,7 @@ | |
|
||
from delta import DeltaTable | ||
import pyspark | ||
from pyspark.sql.functions import count, col, row_number, md5, concat_ws | ||
from pyspark.sql.functions import count, col, row_number, md5, concat_ws, collect_set, size, lit | ||
from pyspark.sql.window import Window | ||
from pyspark.sql.dataframe import DataFrame | ||
|
||
|
@@ -384,6 +384,33 @@ def find_composite_key_candidates( | |
return list(df_col_excluded.select(*c).columns) | ||
|
||
|
||
def find_all_composite_key_combos( | ||
df: Union[DeltaTable, DataFrame], exclude_cols: List[str] = None | ||
): | ||
if type(df) == DeltaTable: | ||
df = df.toDF() | ||
if exclude_cols is None: | ||
exclude_cols = [] | ||
df_col_excluded = df.drop(*exclude_cols) | ||
col_select_condition = df_col_excluded.distinct().count() | ||
initcols = df_col_excluded.columns | ||
for i in range(len(initcols)+1): | ||
for c in list(combinations(initcols, i+2)): | ||
df_col_excluded = df_col_excluded.withColumn(','.join(c), concat_ws(',', *c)) | ||
finalcols = df_col_excluded.columns | ||
exprs = [size(collect_set(x)).alias(x) for x in finalcols] | ||
df_col_excluded = df_col_excluded \ | ||
.withColumn("column_combos ->", lit("distinct_row_counts ->")) \ | ||
.groupBy("column_combos ->") \ | ||
.agg(*exprs) | ||
columns = [ | ||
column for column in df_col_excluded.columns if | ||
df_col_excluded.select(column).collect()[0][0] == col_select_condition | ||
] | ||
df_col_excluded.select("column_combos ->", *columns).show(truncate=False) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @souvik-databricks we can remove that show, right? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes we can remove the show |
||
return columns | ||
|
||
|
||
def with_md5_cols( | ||
df: Union[DeltaTable, DataFrame], | ||
cols: List[str], | ||
|
This file contains 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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@souvik-databricks why does it need to distinct? If .distinct().count() < .count(), there is no unique combination, or?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@robertkossendey
.distinct()
is required because if your data has duplicates in them and then thecollect_set()
inline 401
will matchup and we have only unique combination of values for the columns.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@souvik-databricks still struggling to understand, could you add a test with a completely duplicate line? Because that should return no combination at all right?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@robertkossendey So in that scenario if you have a case of duplicate record then that would indicate that from the columns that you have included to search in, you don't have valid composite key combos. You would have to include more columns to act as part of the composite key.
If in the whole dataset you have a full exact duplicate record then it won't make any sense to keep it as that's then redundant information.
And I will add a test with duplicate record with the explanation of the output for that test case in the readme.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@souvik-databricks but isn't that the information that there is no valid key candidate in the provided dataset?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I was OOO for last week. @robertkossendey I am back now, I will resume the work now.