refactor (datatypes): add from_str constructor#7465
Open
OutSquareCapital wants to merge 2 commits intotobymao:mainfrom
Open
refactor (datatypes): add from_str constructor#7465OutSquareCapital wants to merge 2 commits intotobymao:mainfrom
from_str constructor#7465OutSquareCapital wants to merge 2 commits intotobymao:mainfrom
Conversation
…method to use it
…`str` with certainty, and replaced it by `from_str` constructor
OutSquareCapital
commented
Apr 7, 2026
Contributor
Author
There was a problem hiding this comment.
Here we were passing a dialect, even tough kind is statically known to be a DType instance. So this parameter had no effect at runtime. better to directly use DType.into_expr()
Collaborator
@OutSquareCapital did you benchmark this? Can you provide some actual numbers to understand the impact of this change? Cover both paths where the inputs are strings and paths where inputs are non-strings to measure the impact. |
georgesittas
reviewed
Apr 7, 2026
sqlglot/expressions/datatypes.py
Outdated
| raise | ||
| return cls.from_str(dtype, dialect, udt, **kwargs) | ||
| elif isinstance(dtype, DType): | ||
| data_type_exp = DataType(this=dtype) |
Collaborator
There was a problem hiding this comment.
Why was this section until L373 refactored?
This file contains hidden or 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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
This PR add a new
DatatType.from_str()classmethod constructor, and modify call sites to use it when we know astrwill be used.This brings the following benefit:
isinstancecheckbuildis more readablebuildwhenDType.into_expr()could be directly used instead. Now fixedSelffor return types andclsinstead ofDataType(class name agnostic).Note
I still hesitate between
from_strandparsefor the method name.