Skip to content
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

Splink 4 dialect agnostic blocking rules #1890

Merged
merged 25 commits into from
Jan 29, 2024
Merged

Splink 4 dialect agnostic blocking rules #1890

merged 25 commits into from
Jan 29, 2024

Conversation

RobinL
Copy link
Member

@RobinL RobinL commented Jan 25, 2024

In this PR, I have tried to mirror the work on ComparisonLevelCreator, but for blocking rules.

Hence the user passes BlockingRuleCreator objects into the settings dictionary. These are only turned into actual BlockingRules via blocking_rule_to_obj once the dialect is known via the DB API.

Example script
import splink.comparison_library as cl
from splink.blocking_rule_library import And, CustomRule, block_on
from splink.database_api import DuckDBAPI
from splink.datasets import splink_datasets
from splink.linker import Linker

df = splink_datasets.fake_1000
df.head()

db_api = DuckDBAPI()


settings = {
    "link_type": "dedupe_only",
    "comparisons": [
        cl.ExactMatch("first_name"),
        cl.ExactMatch("surname"),
        cl.ExactMatch("dob"),
        cl.ExactMatch("city"),
    ],
    "blocking_rules_to_generate_predictions": [
        block_on("first_name", "surname"),
        And(
            CustomRule("l.dob = r.dob"),
            CustomRule("l.city = r.city", salting_partitions=10),
            block_on("email", salting_partitions=4),
        ),
    ],
    "retain_matching_columns": True,
    "retain_intermediate_calculation_columns": True,
}


linker = Linker(df, settings, db_api)

linker.estimate_parameters_using_expectation_maximisation(block_on("first_name"))

linker.predict().as_pandas_dataframe()

@RobinL RobinL changed the base branch from master to splink4_dev January 25, 2024 21:25
@RobinL
Copy link
Member Author

RobinL commented Jan 27, 2024

To do next:

Split up creator file and library file

@RobinL RobinL changed the title (WIP) Splink 4 dialect agnostic blocking rules Splink 4 dialect agnostic blocking rules Jan 28, 2024

self.base_dialect_str = base_dialect_str

def create_sql(self, sql_dialect: SplinkDialect) -> str:
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've copied this across rather than attempt to re-use the code from the CustomLevel

That was deliberate, to reduce indirection, but i don't feel particularly strongly about this

_clause = "AND"


class Or(_Merge):
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I left Or in to get tests passing, but i think there's an argument for dropping support for it: it's very rare you want to use an OR in a blocking rule because it usually results in a full cartesian product of the input dataframe being created

@RobinL RobinL requested a review from ADBond January 28, 2024 14:52
Copy link
Contributor

@ADBond ADBond left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great, this works nicely and fits in well with the rest of the stuff we've been doing. Very nice being able to write stuff like block_on(ColumnExpression("surname").substr(1, 4))

Comment on lines +155 to +159
def block_on(
*col_names_or_exprs: Union[str, ColumnExpression],
salting_partitions=None,
arrays_to_explode=None,
) -> BlockingRuleCreator:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder if it might be helpful to give an error here if people use the old syntax, given that this is possibly a more subtle change compared to other API tweaks?
For instance currently if you use block_on(["first_name", "surname"]) you will hit AttributeError: 'NoneType' object has no attribute 'sql_dialect' at some point once the linker is doing its thing, so it might be handy to nudge users in the right direction rather than them trying to unpick that

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point - done

@RobinL RobinL merged commit ce42b25 into splink4_dev Jan 29, 2024
6 checks passed
@RobinL RobinL deleted the block_on branch January 29, 2024 14:37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants