-
Notifications
You must be signed in to change notification settings - Fork 167
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
BlockingRule: Clarify name of sql property #1700
Merged
Merged
Changes from 10 commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
5a7188e
rename sql property
RobinL 64556ae
type hinting
RobinL 189e1e5
fixes and typing
RobinL fed4a55
lint
RobinL 087987a
improve property and method names
RobinL e1e8495
fix bug
RobinL e6404f1
fix ipynb
RobinL b488167
fix tests
RobinL fe17c8e
fix tests
RobinL 82c0d40
fix tests
RobinL c83ea66
check blocking_rule_sql is a string
RobinL 2bcad9b
Merge branch 'master' into refactor_blockingrule_rename_sql_property
RobinL 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
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 sqlglot import parse_one | ||
from sqlglot.expressions import Join, Column | ||
from sqlglot.optimizer.eliminate_joins import join_condition | ||
from typing import TYPE_CHECKING, Union | ||
from typing import TYPE_CHECKING, List | ||
import logging | ||
|
||
from .misc import ensure_is_list | ||
|
@@ -40,15 +40,15 @@ def blocking_rule_to_obj(br): | |
class BlockingRule: | ||
def __init__( | ||
self, | ||
blocking_rule: BlockingRule | dict | str, | ||
blocking_rule_sql: BlockingRule | dict | str, | ||
salting_partitions=1, | ||
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. Might as well add the 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. Good opint. I'll add it to the next PR, which changes this a bit anyway |
||
sqlglot_dialect: str = None, | ||
): | ||
if sqlglot_dialect: | ||
self._sql_dialect = sqlglot_dialect | ||
|
||
self.blocking_rule = blocking_rule | ||
self.preceding_rules = [] | ||
self.blocking_rule_sql = blocking_rule_sql | ||
self.preceding_rules: List[BlockingRule] = [] | ||
self.sqlglot_dialect = sqlglot_dialect | ||
self.salting_partitions = salting_partitions | ||
|
||
|
@@ -60,40 +60,45 @@ def sql_dialect(self): | |
def match_key(self): | ||
return len(self.preceding_rules) | ||
|
||
@property | ||
def sql(self): | ||
# Wrapper to reveal the underlying SQL | ||
return self.blocking_rule | ||
|
||
def add_preceding_rules(self, rules): | ||
rules = ensure_is_list(rules) | ||
self.preceding_rules = rules | ||
|
||
@property | ||
def and_not_preceding_rules_sql(self): | ||
if not self.preceding_rules: | ||
return "" | ||
def exclude_pairs_generated_by_this_rule_sql(self): | ||
"""A SQL string specifying how to exclude the results | ||
of THIS blocking rule from subseqent blocking statements, | ||
so that subsequent statements do not produce duplicate pairs | ||
""" | ||
|
||
# Note the coalesce function is important here - otherwise | ||
# you filter out any records with nulls in the previous rules | ||
# meaning these comparisons get lost | ||
return f"coalesce(({self.blocking_rule_sql}),false)" | ||
|
||
@property | ||
def exclude_pairs_generated_by_all_preceding_rules_sql(self): | ||
"""A SQL string that excludes the results of ALL previous blocking rules from | ||
the pairwise comparisons generated. | ||
""" | ||
if not self.preceding_rules: | ||
return "" | ||
or_clauses = [ | ||
f"coalesce(({r.blocking_rule}), false)" for r in self.preceding_rules | ||
br.exclude_pairs_generated_by_this_rule_sql() for br in self.preceding_rules | ||
] | ||
previous_rules = " OR ".join(or_clauses) | ||
return f"AND NOT ({previous_rules})" | ||
|
||
@property | ||
def salted_blocking_rules(self): | ||
if self.salting_partitions == 1: | ||
yield self.blocking_rule | ||
yield self.blocking_rule_sql | ||
else: | ||
for n in range(self.salting_partitions): | ||
yield f"{self.blocking_rule} and ceiling(l.__splink_salt * {self.salting_partitions}) = {n+1}" # noqa: E501 | ||
yield f"{self.blocking_rule_sql} and ceiling(l.__splink_salt * {self.salting_partitions}) = {n+1}" # noqa: E501 | ||
|
||
@property | ||
def _parsed_join_condition(self): | ||
br = self.blocking_rule | ||
br = self.blocking_rule_sql | ||
return parse_one("INNER JOIN r", into=Join).on( | ||
br, dialect=self.sqlglot_dialect | ||
) # using sqlglot==11.4.1 | ||
|
@@ -147,7 +152,7 @@ def as_dict(self): | |
"The minimal representation of the blocking rule" | ||
output = {} | ||
|
||
output["blocking_rule"] = self.blocking_rule | ||
output["blocking_rule"] = self.blocking_rule_sql | ||
output["sql_dialect"] = self.sql_dialect | ||
|
||
if self.salting_partitions > 1 and self.sql_dialect == "spark": | ||
|
@@ -157,7 +162,7 @@ def as_dict(self): | |
|
||
def _as_completed_dict(self): | ||
if not self.salting_partitions > 1 and self.sql_dialect == "spark": | ||
return self.blocking_rule | ||
return self.blocking_rule_sql | ||
else: | ||
return self.as_dict() | ||
|
||
|
@@ -166,7 +171,7 @@ def descr(self): | |
return "Custom" if not hasattr(self, "_description") else self._description | ||
|
||
def _abbreviated_sql(self, cutoff=75): | ||
sql = self.blocking_rule | ||
sql = self.blocking_rule_sql | ||
return (sql[:cutoff] + "...") if len(sql) > cutoff else sql | ||
|
||
def __repr__(self): | ||
|
@@ -312,7 +317,7 @@ def block_using_rules_sqls(linker: Linker): | |
if apply_salt: | ||
salted_blocking_rules = br.salted_blocking_rules | ||
else: | ||
salted_blocking_rules = [br.blocking_rule] | ||
salted_blocking_rules = [br.blocking_rule_sql] | ||
|
||
for salted_br in salted_blocking_rules: | ||
sql = f""" | ||
|
@@ -324,7 +329,7 @@ def block_using_rules_sqls(linker: Linker): | |
inner join {linker._input_tablename_r} as r | ||
on | ||
({salted_br}) | ||
{br.and_not_preceding_rules_sql} | ||
{br.exclude_pairs_generated_by_all_preceding_rules_sql} | ||
{where_condition} | ||
""" | ||
|
||
|
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
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
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
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
Oops, something went wrong.
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.
I don't think we should allow this to take as an input any of those types - i think this should be string only
I've just pushed a commit to enforce this temporariliy to check that tests still pass