-
Notifications
You must be signed in to change notification settings - Fork 57
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
Fixes #110 and #107 #111
Open
mpgreg
wants to merge
5
commits into
astronomer:main
Choose a base branch
from
mpgreg:main
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
Fixes #110 and #107 #111
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
f07cb03
fixes #100
3719604
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] be4dfad
Merge branch 'astronomer:main' into main
mpgreg 87a42e2
fixes #110 and #107
a5a7955
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 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
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -112,6 +112,8 @@ class GreatExpectationsOperator(BaseOperator): | |||||
:type return_json_dict: bool | ||||||
:param use_open_lineage: If True (default), creates an OpenLineage action if an OpenLineage environment is found | ||||||
:type use_open_lineage: bool | ||||||
:param database: If provided, overwrites the default database provided by the connection | ||||||
:type database: Optional[str] | ||||||
:param schema: If provided, overwrites the default schema provided by the connection | ||||||
:type schema: Optional[str] | ||||||
""" | ||||||
|
@@ -148,6 +150,7 @@ def __init__( | |||||
return_json_dict: bool = False, | ||||||
use_open_lineage: bool = True, | ||||||
schema: Optional[str] = None, | ||||||
database: Optional[str] = None, | ||||||
*args, | ||||||
**kwargs, | ||||||
) -> None: | ||||||
|
@@ -175,6 +178,7 @@ def __init__( | |||||
self.datasource: Optional[Datasource] = None | ||||||
self.batch_request: Optional[BatchRequestBase] = None | ||||||
self.schema = schema | ||||||
self.database = database | ||||||
self.kwargs = kwargs | ||||||
|
||||||
if self.is_dataframe and self.query_to_validate: | ||||||
|
@@ -227,14 +231,31 @@ def __init__( | |||||
if isinstance(self.checkpoint_config, CheckpointConfig): | ||||||
self.checkpoint_config = deep_filter_properties_iterable(properties=self.checkpoint_config.to_dict()) | ||||||
|
||||||
# If a schema is passed as part of the data_asset_name, use that schema | ||||||
if self.data_asset_name and "." in self.data_asset_name: | ||||||
# Assume data_asset_name is in the form "SCHEMA.TABLE" | ||||||
# Schema parameter always takes priority | ||||||
# If a schema and db are passed as part of the data_asset_name, use that schema/db | ||||||
if self.data_asset_name: | ||||||
# Check if data_asset_name is in the form "SCHEMA.TABLE" or "DATABASE.TABLE.SCHEMA" | ||||||
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. typo here
Suggested change
|
||||||
# Database and Schema parameters always takes priority | ||||||
asset_list = self.data_asset_name.split(".") | ||||||
self.schema = self.schema or asset_list[0] | ||||||
|
||||||
# Update data_asset_name to be only the table | ||||||
self.data_asset_name = asset_list[1] | ||||||
self.data_asset_name = asset_list.pop(-1) | ||||||
|
||||||
if asset_list: | ||||||
schema_name = asset_list.pop(-1) | ||||||
if self.schema and schema_name: | ||||||
print("Using Operator argument for 'schema' instead of schema in fully-qualified table name.") | ||||||
self.schema = self.schema or schema_name | ||||||
|
||||||
if asset_list: | ||||||
database_name = asset_list.pop(-1) | ||||||
if self.database and database_name: | ||||||
print("Using Operator argument for 'database' instead of database in fully-qualified table name.") | ||||||
self.database = self.database or database_name | ||||||
|
||||||
if asset_list: | ||||||
raise AirflowException( | ||||||
"Parameter data_asset_name must be specified as TABLE, SCHEMA.TABLE or DATABASE.SCHEMA.TABLE." | ||||||
) | ||||||
|
||||||
def make_connection_configuration(self) -> Dict[str, str]: | ||||||
"""Builds connection strings based off existing Airflow connections. Only supports necessary extras.""" | ||||||
|
@@ -243,15 +264,14 @@ def make_connection_configuration(self) -> Dict[str, str]: | |||||
raise ValueError(f"Connections does not exist in Airflow for conn_id: {self.conn_id}") | ||||||
self.schema = self.schema or self.conn.schema | ||||||
conn_type = self.conn.conn_type | ||||||
if conn_type in ("redshift", "postgres", "mysql", "mssql"): | ||||||
odbc_connector = "" | ||||||
if conn_type in ("redshift", "postgres"): | ||||||
odbc_connector = "postgresql+psycopg2" | ||||||
elif conn_type == "mysql": | ||||||
odbc_connector = "mysql" | ||||||
else: | ||||||
odbc_connector = "mssql+pyodbc" | ||||||
uri_string = f"{odbc_connector}://{self.conn.login}:{self.conn.password}@{self.conn.host}:{self.conn.port}/{self.schema}" # noqa | ||||||
|
||||||
# Postgres uses database instead of schema. User must pass database and schema via Operator args. | ||||||
if conn_type in ("redshift", "postgres"): | ||||||
uri_string = f"postgresql+psycopg2://{self.conn.login}:{self.conn.password}@{self.conn.host}:{self.conn.port}/{self.database}?options=-csearch_path%3D{self.schema}" # noqa | ||||||
elif conn_type == "mysql": | ||||||
uri_string = f"mysql://{self.conn.login}:{self.conn.password}@{self.conn.host}:{self.conn.port}/{self.schema}" # noqa | ||||||
elif conn_type == "mssql": | ||||||
uri_string = f"mssql+pyodbc://{self.conn.login}:{self.conn.password}@{self.conn.host}:{self.conn.port}/{self.schema}" # noqa | ||||||
elif conn_type == "snowflake": | ||||||
try: | ||||||
return self.build_snowflake_connection_config_from_hook() | ||||||
|
@@ -316,9 +336,10 @@ def build_snowflake_connection_config_from_hook(self) -> Dict[str, str]: | |||||
|
||||||
hook = SnowflakeHook(snowflake_conn_id=self.conn_id) | ||||||
|
||||||
# Support the operator overriding the schema | ||||||
# Support the operator overriding the schema and database | ||||||
# which is necessary for temp tables. | ||||||
hook.schema = self.schema or hook.schema | ||||||
hook.database = self.database or hook.database | ||||||
|
||||||
conn = hook.get_connection(self.conn_id) | ||||||
engine = hook.get_sqlalchemy_engine() | ||||||
|
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.
Can we not get these values from the connection extras?
I feel in general the connection should be self sufficient and exposing these parameters in the operators is separating related things(to the connection) out. Anything that is provided in the extras can override the values for database/schema, etc, no?
WDYT?
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.
From the docs https://airflow.apache.org/docs/apache-airflow-providers-postgres/stable/connections/postgres.html it doesn't look like database is a valid extra.
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.
You could get the database from
self.conn.schema
. The Airflow has database name in the web UI, but it's saved as 'schema` column.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 find this whole database == schema and schema == database thing with the postgres provider to be an antipattern which is confusing for developers. One reason for this override is to clear that up. Also it may be desireable to set a different db/schema for a particular set of expectations rather than use the same for all.