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

Feat(schema): add method to check if column exists #2381

Merged
merged 3 commits into from
Oct 6, 2023

Conversation

georgesittas
Copy link
Collaborator

@georgesittas georgesittas commented Oct 6, 2023

Thought something like this might be an improvement to the API

Copy link
Collaborator

@barakalon barakalon left a comment

Choose a reason for hiding this comment

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

I'm assuming this is intended to be used somewhere in sqlmesh?

If this is intended to be used anywhere within the optimizer, it needs to be on the abstract Schema class.

@georgesittas
Copy link
Collaborator Author

I'm assuming this is intended to be used somewhere in sqlmesh?

If this is intended to be used anywhere within the optimizer, it needs to be on the abstract Schema class.

It started out that way, but I'm not sure if I'm going to use it there after all. I just made the PR regardless because it seemed like a useful utility, since the only alternative I think would be to use get_column_type which doesn't provide a clear signal about the column missing:

>>> from sqlglot.schema import MappingSchema
>>> s = MappingSchema({"x": {"y": "int"}})
>>> import sqlglot
>>> s.get_column_type("x", sqlglot.exp.column("y"))
(DATATYPE this: Type.INT, nested: False, prefix: False)
>>> s.get_column_type("x", sqlglot.exp.column("f"))
(DATATYPE this: Type.UNKNOWN)

@barakalon
Copy link
Collaborator

I guess we can't just do "y" in s.column_names("x") because of normalization?

And still, if you think this will be a useful utility, we probably want it on the Schema interface. Unless you enforce a MappingSchema is used in sqlmesh?

@georgesittas
Copy link
Collaborator Author

I guess we can't just do "y" in s.column_names("x") because of normalization?

I think so, because the schema may or may not contain normalized names (not necessarily related to internal schema normalize attribute), which is why I started off with this interface instead of __contains__, i.e. to allow for extra flexibility.

And still, if you think this will be a useful utility, we probably want it on the Schema interface. Unless you enforce a MappingSchema is used in sqlmesh?

Happy to add this to the Schema interface, it seems like a general-purpose schema utility anyway. The only reason I didn't do it from the start was to avoid breaking custom schema implementations in client code, since no one's currently using this interface anyway.

@barakalon
Copy link
Collaborator

Maybe we should ditch ABC in favor of regular ol' raise NotImplementedError. Or perhaps we can just add a simple, default implementation that doesn't normalize and uses column_names.

@tobymao
Copy link
Owner

tobymao commented Oct 6, 2023

what about @barakalon comment on schema vs mapping schema?

@georgesittas
Copy link
Collaborator Author

what about @barakalon comment on schema vs mapping schema?

Just added a simple implementation in the base class here c92017c, @barakalon is this what you had in mind?

column: exp.Column,
column: exp.Column | str,
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

The previous type hint was more restrictive than needed, we already supported strings in the implementation, e.g. as shown in this test: https://github.com/tobymao/sqlglot/blob/main/tests/test_schema.py#L167

Copy link
Collaborator

@barakalon barakalon left a comment

Choose a reason for hiding this comment

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

perfect 👍

@georgesittas georgesittas merged commit f4f8366 into main Oct 6, 2023
@georgesittas georgesittas deleted the jo/schema_has_column branch October 6, 2023 21:30
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.

3 participants