Ujjwaljain16 commented on issue #37114:
URL: https://github.com/apache/superset/issues/37114#issuecomment-3954181425

   **Proposed Fix**
   
   I investigated the cache key mismatch when `GLOBAL_ASYNC_QUERIES` is enabled 
and confirmed that formatting differences in ad-hoc SQL expressions can produce 
different `QueryObject.cache_key()` values between the web process and Celery 
workers.
   
   While `where` and `having` clauses are sanitized during `validate()`, ad-hoc 
SQL expressions in the following fields were not normalized prior to hashing:
   
   * `metrics` (adhoc SQL)
   * `columns` (adhoc SQL)
   * `orderby` (adhoc SQL)
   
   Differences such as:
   
   * `\r\n` vs `\n`
   * leading/trailing whitespace
   
   result in different `to_dict()` outputs and therefore different cache keys, 
which can lead to HTTP 422 errors in async mode.
   
   ---
   
   **Proposed Change**
   
   I implemented a minimal and safe normalization step inside 
`QueryObject.cache_key()`:
   
   * Use `copy.deepcopy(self.to_dict())` to avoid mutating the original object.
   * Normalize ad-hoc SQL expressions by:
   
     * Converting `\r\n` → `\n`
     * Stripping leading/trailing whitespace
   * Apply this only when `expressionType == "SQL"` (defensive guard).
   * Do not render Jinja templates or alter execution lifecycle.
   
   This keeps the change strictly within the hashing layer and avoids 
introducing request/context dependency.
   
   ---
   
   **Verification**
   
   * Reproduced mismatch with CRLF vs LF and whitespace differences.
   * Added unit tests covering:
   
     * CRLF/LF parity
     * Mixed newline cases
     * Whitespace differences
     * Defensive `orderby` structures
     * No mutation guarantee
   * Confirmed async queries no longer produce 422 due to formatting 
differences.
   
   If this approach looks acceptable, I’ll open a PR with the changes and 
accompanying tests.
   
   


-- 
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]

Reply via email to