Ujjwaljain16 commented on code in PR #38227:
URL: https://github.com/apache/superset/pull/38227#discussion_r2907136791
##########
superset/common/query_object.py:
##########
@@ -420,10 +421,39 @@ def cache_key(self, **extra: Any) -> str: # noqa: C901
the use-provided inputs to bounds, which may be time-relative (as in
"5 days ago" or "now").
"""
- # Cast to dict[str, Any] for mutation operations
- cache_dict: dict[str, Any] = dict(self.to_dict())
+ # Use deepcopy to prevent mutation of original objects (nested
metrics/columns)
+ cache_dict: dict[str, Any] = copy.deepcopy(self.to_dict()) # type:
ignore[arg-type]
cache_dict.update(extra)
+ def _normalize_sql(value: Any) -> Any:
+ if isinstance(value, str):
+ return value.replace("\r\n", "\n").strip()
+ return value
+
+ # Normalize SQL expressions to ensure deterministic cache keys
+ for metric in cache_dict.get("metrics") or []:
+ if isinstance(metric, dict) and metric.get("expressionType") ==
"SQL":
+ metric["sqlExpression"] =
_normalize_sql(metric.get("sqlExpression"))
+
+ for column in cache_dict.get("columns") or []:
+ if isinstance(column, dict) and column.get("expressionType") ==
"SQL":
+ column["sqlExpression"] =
_normalize_sql(column.get("sqlExpression"))
+
+ for order in cache_dict.get("orderby") or []:
+ if (
+ isinstance(order, (list, tuple))
+ and len(order) > 0
+ and isinstance(order[0], dict)
+ and order[0].get("expressionType") == "SQL"
+ ):
+ order[0]["sqlExpression"] = _normalize_sql(
+ order[0].get("sqlExpression")
+ )
+
Review Comment:
Done
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]