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

Add BigQuery Column and Table Check Operators #26368

Merged
merged 4 commits into from
Sep 21, 2022

Conversation

denimalpaca
Copy link
Contributor

Add two new operators based on the SQLColumnCheckOperator and SQLTableCheckOperator that also provide job_ids so results of the queries can be pulled and parsed, and so OpenLineage can parse datasets and provide lineage information.

closes: #26367

@boring-cyborg boring-cyborg bot added area:providers provider:google Google (including GCP) related issues labels Sep 13, 2022
@denimalpaca denimalpaca force-pushed the add_bigquery_common_check_operators branch from ba66b90 to 8e76bae Compare September 14, 2022 13:34
@denimalpaca denimalpaca force-pushed the add_bigquery_common_check_operators branch 2 times, most recently from d31eacc to 6870aef Compare September 16, 2022 16:22
@potiuk potiuk force-pushed the add_bigquery_common_check_operators branch from 6870aef to 4a5203e Compare September 19, 2022 11:23
@potiuk
Copy link
Member

potiuk commented Sep 19, 2022

Rebased it to rebuild it @denimalpaca - if you can see if ti works and ping me if it does, that would be awesome

@denimalpaca
Copy link
Contributor Author

Rebased it to rebuild it @denimalpaca - if you can see if ti works and ping me if it does, that would be awesome

@potiuk looks like it's all green -- and already behind again

@denimalpaca denimalpaca force-pushed the add_bigquery_common_check_operators branch from 4a5203e to 2113530 Compare September 19, 2022 20:45
Add two new operators based on the SQLColumnCheckOperator and
SQLTableCheckOperator that also provide job_ids so results
of the queries can be pulled and parsed, and so OpenLineage
can parse datasets and provide lineage information.
@denimalpaca denimalpaca force-pushed the add_bigquery_common_check_operators branch from 2113530 to 90f217d Compare September 20, 2022 14:02
@potiuk potiuk merged commit c4256ca into apache:main Sep 21, 2022
failed_tests = []
for column in self.column_mapping:
checks = [*self.column_mapping[column]]
checks_sql = ",".join([self.column_checks[check].replace("column", column) for check in checks])
Copy link
Contributor

Choose a reason for hiding this comment

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

I know that "self.column_checks" is part of the parent class but I see from git blame that you're the author. So I would recommend replacing the following strings:

"SUM(CASE WHEN column IS NULL THEN 1 ELSE 0 END) AS column_null_check"

with a format string template:

"SUM(CASE WHEN {column} IS NULL THEN 1 ELSE 0 END) AS {column}_null_check"

it can be later used with check.format(column=column) instead of replacing the sub string.

hook = self.get_db_hook()
checks_sql = " UNION ALL ".join(
[
self.sql_check_template.replace("check_statement", value["check_statement"])
Copy link
Contributor

Choose a reason for hiding this comment

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

It will not work with your test data:

checks={"row_count_check": {"check_statement": {"COUNT(*) = 4"}}},

check_statement is set. Did you mean:

checks={"row_count_check": {"check_statement": "COUNT(*) = 4"}}

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ah, that was a typo on my part in the test case. Yes, it should be the latter.

@bhirsz
Copy link
Contributor

bhirsz commented Sep 27, 2022

Hi, thank you for the contribution. I noticed it when our system tests started to fail - I left a comment where the possible issue is. But I also see that there are no unit test for those two new operators.. . There could be other issues as well that could be detected by unit tests. Would it be possible for you to add them? @denimalpaca

@potiuk Is it possible to revert this change? This code doesn't have tests and may contain other issues.

dag_id=self.dag_id,
task_id=self.task_id,
logical_date=context["logical_date"],
configuration=self.configuration,
Copy link
Contributor

Choose a reason for hiding this comment

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

the self.configuration does not exist (or I can't see it). But I see that bigquery allows to pass job_id as empty string. In that case (check other operators) the following method will be invoked to set job_id: _custom_job_id . So you probably don't need to use generate_job_id .

records.columns = records.columns.str.lower()
self.log.info("Record:\n%s", records)

for row in records.iterrows():
Copy link
Contributor

@bhirsz bhirsz Sep 27, 2022

Choose a reason for hiding this comment

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

I don't know what type record is, but you could try to unpack it for more clarity instead of row[1]:

for _, columns in records.iterrows():
    check = columns.get("check_name")
...

@denimalpaca
Copy link
Contributor Author

@bhirsz I've opened a new PR to address your concerns, and we can add some tests in there as well.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area:providers provider:google Google (including GCP) related issues
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add SQLColumnCheck and SQLTableCheck Operators for BigQuery
3 participants