[BugFix] Make SQLAlchemy reflection dataclasses hashable#71302
Closed
VijayShekhawat7 wants to merge 1 commit intoStarRocks:mainfrom
Closed
[BugFix] Make SQLAlchemy reflection dataclasses hashable#71302VijayShekhawat7 wants to merge 1 commit intoStarRocks:mainfrom
VijayShekhawat7 wants to merge 1 commit intoStarRocks:mainfrom
Conversation
ab07555 to
e2080b5
Compare
The ReflectedTableKeyInfo, ReflectedPartitionInfo, ReflectedDistributionInfo, and ReflectedRefreshInfo dataclasses were not hashable, which caused TypeError when SQLAlchemy's reflection cache tried to use them as dict keys. Three changes: 1. Add custom __hash__ methods to ReflectedTableKeyInfo and ReflectedDistributionInfo that convert list-typed `columns` fields to tuples before hashing, since List[str] is not hashable. ReflectedRefreshInfo and ReflectedPartitionInfo keep unsafe_hash=True as all their fields are already hashable types. 2. Refactor __str__ methods to use local variables instead of mutating self attributes, preventing hash instability after str() calls. 3. Remove dead variable `type_str` in ReflectedPartitionInfo.__str__. Fixes StarRocks#70733 Signed-off-by: Vijay Shekhawat <vijayshekhawat1995@gmail.com> Made-with: Cursor
e2080b5 to
a3b776d
Compare
Author
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.
Why I'm doing:
Fixes #70733
The
ReflectedTableKeyInfo,ReflectedPartitionInfo,ReflectedDistributionInfo, andReflectedRefreshInfodataclasses in the StarRocks Python client lack__hash__implementations. SQLAlchemy's reflection cache uses these as dict keys, causingTypeError: unhashable typeat runtime.What I'm doing:
Three changes to make all four dataclasses safely hashable:
Custom
__hash__for list-column classes:ReflectedTableKeyInfoandReflectedDistributionInfohave acolumnsfield typedOptional[Union[List[str], str]]. Python'sunsafe_hash=Truecannot handleList[str](unhashable type). These classes now define explicit__hash__methods that convert list-typedcolumnsto tuples before hashing.ReflectedRefreshInfoandReflectedPartitionInfokeepunsafe_hash=Truesince all their fields are already hashable types (strings).Non-mutating
__str__methods: The original__str__methods onReflectedTableKeyInfo,ReflectedPartitionInfo, andReflectedDistributionInfomutatedself.typeandself.columnsin-place (e.g.,self.type = self.type.upper()). With hashing enabled, this would cause hash instability — the hash changes after callingstr(). Refactored all__str__methods to use local variables instead.Dead code removal: Removed unused
type_strvariable inReflectedPartitionInfo.__str__.What type of PR is this:
Does this PR entail a change in behavior?
If yes, please specify the type of change:
Checklist:
Bugfix cherry-pick branch check: