-
Notifications
You must be signed in to change notification settings - Fork 769
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
Conversation
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'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 >>> 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) |
I guess we can't just do 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? |
I think so, because the schema may or may not contain normalized names (not necessarily related to internal schema
Happy to add this to the |
Maybe we should ditch ABC in favor of regular ol' |
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, |
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.
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
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.
perfect 👍
Thought something like this might be an improvement to the API