sushu0 opened a new pull request, #45467:
URL: https://github.com/apache/superset/pull/45467

   ### SUMMARY
   
   Fixes #45466.
   
   Percentage and ratio `compare` post-processing raises Decimal division 
exceptions when a raw SQL DECIMAL column has a zero baseline. The result is 
converted to float only after the division has already raised. Raw-column 
queries preserve these Decimal values through `SqlaTable.normalize_df`; the 
usual UI metric path normally coerces metrics earlier.
   
   Use a local Decimal context for percentage/ratio arithmetic so division by 
zero and 0/0 produce the same infinity/NaN results as the existing float path. 
Keep division before float conversion to preserve finite ratios for very large 
or small Decimal operands. Absolute differences and the caller's Decimal 
context remain unchanged. Add regression coverage for plain/MultiIndex columns, 
the actual normalization/post-processing dispatch, extreme operand ranges, and 
context restoration; document the behavior in the API reference.
   
   ### BEFORE/AFTER SCREENSHOTS OR ANIMATED GIF
   
   Not applicable; backend post-processing only.
   
   Before: comparing Decimal `10 / 0` raises `decimal.DivisionByZero`; `0 / 0` 
raises `decimal.InvalidOperation`.
   
   After: comparison completes with the existing float-path infinity/NaN 
behavior. Both `1e500 / 5e499` and `1e-500 / 5e-501` still produce ratio `2.0` 
and percentage change `1.0`.
   
   ### TESTING INSTRUCTIONS
   
   - Before the fix, all 5 initial Decimal regressions failed, including a raw 
`QueryObject` passed through the real `SqlaTable.normalize_df` and 
`exec_post_processing` methods.
   - After the fix: `python -m pytest tests/unit_tests/pandas_postprocessing 
tests/unit_tests/queries/query_object_test.py 
--ignore=tests/unit_tests/pandas_postprocessing/test_prophet.py -q` — **232 
passed**. The unrelated optional Prophet dependency is not installed locally.
   - Changed-file Ruff, `python -m py_compile`, and direct Pylint with the 
project plugin passed. The Pylint pre-commit hook also passed after adding the 
existing Bash executable to PATH.
   - Ran the staged-file pre-commit checks. Native Windows mypy reports 3 
pre-existing `signal.SIGALRM`/`signal.alarm` errors in untouched 
`superset/utils/core.py`. Running the same hook's mypy and type stubs with 
`--platform linux --check-untyped-defs` passed for both changed Python files. 
Other applicable hooks passed.
   
   For a database-backed check, use a raw-column chart-data query over SQL 
DECIMAL columns `current` and `previous`, with a zero in `previous`, and apply 
`compare` using `compare_type: "ratio"` or `"percentage"`. The request should 
complete like the equivalent float input. The local tests exercise actual 
project code, not an extracted copy; no live database, HTTP endpoint or SQL Lab 
browser verification is claimed.
   
   ### ADDITIONAL INFORMATION
   
   - [x] Has associated issue: #45466
   - [ ] Required feature flags:
   - [ ] Changes UI
   - [ ] Includes DB Migration (follow approval process in 
[SIP-59](https://github.com/apache/superset/issues/13351))
     - [ ] Migration is atomic, supports rollback & is backwards-compatible
     - [ ] Confirm DB migration upgrade and downgrade tested
     - [ ] Runtime estimates and downtime expectations provided
   - [ ] Introduces new feature or API
   - [ ] Removes existing feature or API
   
   AI assistance disclosure: OpenAI Codex assisted with investigation, 
implementation, test preparation, automated review and local validation. This 
PR does not claim prior human review or human-executed validation of this 
change.
   


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