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

[BUGFIX] Set application query param for SnowflakeDatasource #9863

Merged
merged 29 commits into from May 14, 2024

Conversation

roblim
Copy link
Member

@roblim roblim commented May 1, 2024

  • Set application query param for SnowflakeDatasource when creating engine. This is to ensure GX gets attribution for the Snowflake Partner program

  • This was attempted previously by setting the SF_PARTNER env var, which would typically be picked up by the Snowflake connecter to set the application query param. However, since GX uses snowflake-sqlalchemy to connect to Snowflake, the env var gets superseded, with attribution going to snowflake-sqlalchemy.

  • PR title is prefixed with one of: [BUGFIX], [FEATURE], [DOCS], [MAINTENANCE], [CONTRIB]

  • Code is linted - run invoke lint (uses ruff format + ruff check)

  • Appropriate tests and docs have been updated

For more information about contributing, see Contribute.

After you submit your PR, keep the page open and monitor the statuses of the various checks made by our continuous integration process at the bottom of the page. Please fix any issues that come up and reach out on Slack if you need help. Thanks for contributing!

roblim added 3 commits May 1, 2024 12:55
- This ends up being a no-op, since env var ends up being superceded by snowflake-sqlalchemy
@roblim roblim requested a review from jcampbell May 1, 2024 20:14
@roblim roblim self-assigned this May 1, 2024
@roblim roblim changed the title Set application query param for SnowflakeDatasource [BUGFIX] Set application query param for SnowflakeDatasource May 1, 2024
self._engine = sa.create_engine(connection_string, **kwargs)
self._engine = sa.create_engine(
connection_string,
connect_args={"application": snowflake_partner_application},
Copy link
Member

Choose a reason for hiding this comment

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

Just eyeballing this: does this conflict if the user has connect_args in their kwargs?

Copy link
Member Author

Choose a reason for hiding this comment

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

Ah great catch! Yeah, would probably break. Will update to merge if present.

Copy link
Member Author

Choose a reason for hiding this comment

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

Or actually, just noticed a test that suggests providing both str and connect args should yield ValueError. Lemme dig deeper and confirm expected behavior.

Copy link
Member Author

Choose a reason for hiding this comment

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

Changed impl to add query param to url instead of using connect_args, mostly to allow for easier testing (once an engine is instantiated, there's no way to really check connect_args without actually connecting to Snowflake).

Comment on lines +103 to +105
_ = SnowflakeDatasource(
name="my_sf_ds", connection_string=connection_string, **connect_args
)
Copy link
Member Author

Choose a reason for hiding this comment

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

This test wasn't working properly. It was passing only because same error was being thrown for missing name, and when this was fixed, passing because improper url was also throwing same error.

When test was fixed, it revealed that validation of connection_string XOR connection args wasn't happening.

Comment on lines 129 to 134

if values.get("connection_string") and connection_details:
raise ValueError(
"Cannot provide both a connection string or a combination of account, user, and password."
)

Copy link
Member Author

Choose a reason for hiding this comment

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

test_conflicting_connection_string_and_args_raises_error was broken. After fixing, found that XOR check wasn't happening, so added this.

@Kilo59 - I assume we still wanted this XOR validation since we still had the above test, and there was a validator called _check_xor_input_args (but XOR check wasn't actually happening). LMK if you meant to remove XOR condition.

@pydantic.root_validator
def _check_xor_input_args(cls, values: dict) -> dict:
@pydantic.validator("connection_string")
def _check_connection_string(
Copy link
Member Author

Choose a reason for hiding this comment

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

Renamed since it wasn't actually doing XOR check and was just validating connection_string.

@roblim roblim requested a review from Kilo59 May 3, 2024 08:14
@roblim roblim marked this pull request as ready for review May 3, 2024 08:14
**connection_string
application=snowflake_partner_application,
**connection_string,
**kwargs,
Copy link
Member Author

Choose a reason for hiding this comment

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

@Kilo59 - I noticed **kwargs was missing here. Assumed we still wanted it since we're still passing **kwargs to engine for str connection_string, and we were passing **kwargs here before. Is this correct?

Copy link
Member

Choose a reason for hiding this comment

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

I think the kwargs are passed elsewhere.

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 thought they might be too, but they end up getting filtered out in _get_connect_args, since kwargs is defined in parent class:

    def _get_connect_args(self) -> dict[str, str | bool]:
        excluded_fields: set[str] = set(SQLDatasource.__fields__.keys())
        # dump as json dict to force serialization of things like AnyUrl
        return self._json_dict(exclude=excluded_fields, exclude_none=True)

@Kilo59 Kilo59 requested a review from cdkini May 3, 2024 19:27
Comment on lines 220 to 222
snowflake_partner_application = "great_expectations_oss"
else:
snowflake_partner_application = "great_expectations_oss"
Copy link
Member

Choose a reason for hiding this comment

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

Can we make these constants?

Copy link
Member

Choose a reason for hiding this comment

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

And is there a way to decouple Cloud logic here? I don't love leaking this logic here but we might need to given our current architecture

Copy link
Member Author

Choose a reason for hiding this comment

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

Sure. Added constants in 3f3dd3c.

WRT second point, I don't think there's a cleaner way to decouple cloud logic, but I did factor it out into a separate util method so at least it's less exposed in the public method. 10066e7

@roblim roblim requested a review from cdkini May 7, 2024 00:33
@roblim roblim added this pull request to the merge queue May 7, 2024
@github-merge-queue github-merge-queue bot removed this pull request from the merge queue due to failed status checks May 7, 2024
@roblim roblim enabled auto-merge May 14, 2024 06:05
@roblim roblim added this pull request to the merge queue May 14, 2024
Merged via the queue into 0.18.x with commit 55c9d0d May 14, 2024
59 checks passed
@roblim roblim deleted the set-sf-partner-by-context-type branch May 14, 2024 06:49
roblim added a commit that referenced this pull request May 14, 2024
# Conflicts:
#	docs/docusaurus/versioned_docs/version-0.18/snippets/redshift_python_example.py
#	great_expectations/datasource/fluent/snowflake_datasource.py
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.

None yet

4 participants