feat: removing dup logic in sqla/models.py and models/helpers.py - #34177
Conversation
There was a problem hiding this comment.
I've completed my review and didn't find any issues.
Files scanned
| File Path | Reviewed |
|---|---|
| superset/connectors/sqla/models.py | ✅ |
| superset/models/helpers.py | ✅ |
Explore our documentation to understand the languages and file types we support and the files we ignore.
Check out our docs on how you can make Korbit work best for you and your team.
| def get_query_str(self, query_obj: QueryObjectDict) -> str: | ||
| """Returns a query as a string using ExploreMixin implementation""" | ||
| return ExploreMixin.get_query_str(self, query_obj) | ||
|
|
||
| def text(self, clause: str) -> TextClause: | ||
| """Returns a text clause using ExploreMixin implementation""" | ||
| return ExploreMixin.text(self, clause) |
There was a problem hiding this comment.
@betodealmeida these two methods are a bit of a hack, where from my understanding the inheritance scheme is non linear in some specific cases, so we need to cherry pick some methods to skip a level of inheritance.
PR still achieves the goal of de-duping logic, but doesn't fully fix the inheritance scheme and related patchwork ...
| ) | ||
| return ob | ||
|
|
||
| def _normalize_prequery_result_type( |
There was a problem hiding this comment.
The parent method seems more complete, handling cases where column is not a dict. This is one of the major problems with code duplication, things get fixed in one place but not another. :(
There was a problem hiding this comment.
Confirmed on slack, this comment vouches for deleting this method as opposed to the other one that's slightly different.
Huge props to Claude Code for running this whole analysis and fixing everything here.
Remove duplicate code between SqlaTable and ExploreMixin classes
This commit eliminates 7 duplicate methods totaling ~146 lines of code that were
introduced by PR #20281 in July 2022. The duplication occurred when @hughhhh
created the
ExploreMixinclass to enable Query objects to be visualized in Explore,but instead of properly refactoring shared functionality, he copy-pasted methods
from SqlaTable into the new mixin.
Root Cause Analysis
The code duplication was traced to commit e5e8867
where ExploreMixin was created with methods copied from SqlaTable:
Method Resolution Order Impact
SqlaTable inherits from (Model, BaseDatasource, ExploreMixin), meaning:
Methods Removed from SqlaTable
Exact duplicates:
filter_values_handler(60 lines) - Static method for handling filter values_apply_cte(12 lines) - Static method for appending CTEs to SQLmake_orderby_compatible(28 lines) - Instance method for ORDER BY compatibilitytext(2 lines) - Instance method for creating TextClause objectsget_query_str(4 lines) - Instance method for generating query stringsDivergent methods consolidated:
_normalize_prequery_result_type(35 lines) - Improved to use ExploreMixin'smore robust version with better error handling for missing columns
changed_by_name(5 lines) - Improved to use AuditMixinNullable's more secureversion with HTML escaping instead of plain string conversion
Technical Improvements
Beyond eliminating duplication, this refactoring improves the codebase by:
.get()method instead of direct dict access for column lookupsTesting
All existing unit tests pass (8/8) and critical integration tests for the
normalize_prequery_result_type functionality have been verified. The inheritance
hierarchy ensures SqlaTable now properly inherits the more robust implementations
from ExploreMixin and AuditMixinNullable.
Fixes: Code duplication introduced in PR #20281 (feat: Visualize SqlLab.Query model data in Explore)