You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@superset.apache.org by "SEPURI-SAI-KRISHNA (via GitHub)" <gi...@apache.org> on 2026/10/17 05:32:42 UTC

[PR] fix(export): quote XLSX formulas with leading whitespace [superset]

SEPURI-SAI-KRISHNA opened a new pull request, #45407:
URL: https://github.com/apache/superset/pull/45407

   ### SUMMARY
   
   Fixes #45406.
   
   The chart XLSX formula guard in `superset/utils/excel.py` checked `value[0]`, so
   leading whitespace let a formula through unquoted. `" =1+1"`, `"\t=1+1"` and
   `"  @SUM(A1)"` were written as live formulas. The streaming writer
   (`excel_streaming.py`) and the CSV writer already `lstrip()` before checking,
   and the comment on the shared `FORMULA_PREFIXES` constant says both export paths
   agree, which was not the case.
   
   The fix strips before inspecting the first character, mirroring the streaming
   writer. The CSV-only extras (`|`, `%`, doubled-quote, negative-number handling)
   are left alone, so the diff is only the one inconsistency.
   
   ### BEFORE/AFTER SCREENSHOTS OR ANIMATED GIF
   
   Before (`df_to_excel` then `pd.read_excel`):
   
   ```
   ' =1+1'       -> ' =1+1'      (live formula)
   '\t=1+1'      -> '\t=1+1'     (live formula)
   '  @SUM(A1)'  -> '  @SUM(A1)' (live formula)
   ```
   
   After:
   
   ```
   ' =1+1'       -> "' =1+1"      (neutralised text)
   '\t=1+1'      -> "'\t=1+1"     (neutralised text)
   '  @SUM(A1)'  -> "'  @SUM(A1)" (neutralised text)
   ```
   
   ### TESTING INSTRUCTIONS
   
   ```
   pytest tests/unit_tests/utils/excel_tests.py
   ```
   
   New `test_quote_formulas_with_leading_whitespace` fails on the old guard and
   passes with the fix; the existing `test_quote_formulas` still passes.
   
   ### ADDITIONAL INFORMATION
   
   - [x] Has associated issue: #45406
   - [ ] 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
   


-- 
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: notifications-unsubscribe@superset.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@superset.apache.org
For additional commands, e-mail: notifications-help@superset.apache.org


Re: [PR] fix(export): quote XLSX formulas with leading whitespace [superset]

Posted by "codeant-ai-for-open-source[bot] (via GitHub)" <gi...@apache.org>.
codeant-ai-for-open-source[bot] commented on PR #45407:
URL: https://github.com/apache/superset/pull/45407#issuecomment-6230272668

   ## CodeAnt PR Risk: Low Risk
   
   - The PR appears safe to merge; it quotes formula-like values even when whitespace precedes the formula prefix.
   - The focused test covers spaces, tabs, and newlines while confirming ordinary text remains unchanged.
   
   **Assessed commit:** `118b603ddb72`


-- 
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: notifications-unsubscribe@superset.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@superset.apache.org
For additional commands, e-mail: notifications-help@superset.apache.org


Re: [PR] fix(export): quote XLSX formulas with leading whitespace [superset]

Posted by "sadpandajoe (via GitHub)" <gi...@apache.org>.
sadpandajoe commented on code in PR #45407:
URL: https://github.com/apache/superset/pull/45407#discussion_r4367727119


##########
superset/utils/excel.py:
##########
@@ -49,12 +49,30 @@
 
 
 def _quote_formula(value: Any) -> Any:
-    """Prefix a string with a quote when it would parse as a formula."""
-    return (
-        f"'{value}"
-        if isinstance(value, str) and len(value) and value[0] in FORMULA_PREFIXES
-        else value
-    )
+    """Prefix a string with a quote when it would parse as a formula.
+
+    A string whose first character is ``=`` is stored as a formula cell by the
+    writer, so quoting it is what keeps a formula out of the workbook. Dropping
+    the quote would put one in.
+
+    Leading whitespace is stripped before the prefix is read, so ``" =cmd"`` is
+    quoted like ``"=cmd"``. That part is for consistency across the export paths
+    rather than to stop the workbook evaluating it: XlsxWriter builds a formula
+    cell only from a string whose first character is ``=``, so a value with

Review Comment:
   This sentence says XlsxWriter builds a formula cell "only from a string whose first character is `=`", and the next line says anything in front of the prefix becomes a plain string. XlsxWriter also treats a string that starts with `{=` and ends with `}` as an array formula, so `{=1+1}` still lands in the workbook as a formula cell and is not covered by `_quote_formula` or its docstring. A future reader could take the docstring as proof that the first-character check is exhaustive. Could the wording be narrowed to what this change covers, leading whitespace before the prefix?



-- 
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: notifications-unsubscribe@superset.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@superset.apache.org
For additional commands, e-mail: notifications-help@superset.apache.org


Re: [PR] fix(export): quote XLSX formulas with leading whitespace [superset]

Posted by "SEPURI-SAI-KRISHNA (via GitHub)" <gi...@apache.org>.
SEPURI-SAI-KRISHNA commented on code in PR #45407:
URL: https://github.com/apache/superset/pull/45407#discussion_r4358381840


##########
superset/utils/excel.py:
##########
@@ -48,12 +48,18 @@
 
 
 def _quote_formula(value: Any) -> Any:
-    """Prefix a string with a quote when it would parse as a formula."""
-    return (
-        f"'{value}"
-        if isinstance(value, str) and len(value) and value[0] in FORMULA_PREFIXES
-        else value
-    )
+    """Prefix a string with a quote when it would parse as a formula.
+
+    Leading whitespace is ignored when detecting a formula, because spreadsheet
+    apps still evaluate a cell whose formula prefix is preceded by spaces or
+    tabs (e.g. ``" =cmd"`` or ``"\t=cmd"``). This matches the streaming writer
+    in ``superset.utils.excel_streaming``, which the comment on
+    ``FORMULA_PREFIXES`` says both export paths agree with.
+    """
+    if not isinstance(value, str):
+        return value
+    stripped = value.lstrip()
+    return f"'{value}" if stripped and stripped[0] in FORMULA_PREFIXES else value

Review Comment:
   You are right, and I checked it rather than argue it. Writing the cell types out with no quoting at all:
   
   ```
   '=1+1'       -> FORMULA cell
   ' =1+1'      -> string cell
   '\t=1+1'     -> string cell
   '+1+1'       -> string cell
   '@SUM(A1)'   -> string cell
   '  - North'  -> string cell
   ```
   
   So the answer to your question is no: there is no case where the leading-whitespace form is evaluated in the XLSX output. XlsxWriter converts only a string whose first character is `=`, which the old guard already caught. The streaming writer never produces a formula at all, since it goes through `write_string`. My docstring claimed these values were evaluated, and that was wrong. I have replaced it.
   
   Your `"  - North"` point is also right, and it is worth being exact about where it already applies:
   
   ```
   value         CSV            streaming      xlsx before    xlsx after
   ' =1+1'       "' =1+1"       "' =1+1"       ' =1+1'        "' =1+1"
   '  @SUM(A1)'  "'  @SUM(A1)"  "'  @SUM(A1)"  '  @SUM(A1)'   "'  @SUM(A1)"
   '  - North'   "'  - North"   "'  - North"   '  - North'    "'  - North"
   ```
   
   `_starts_like_spreadsheet_formula` in `csv.py` strips, and `_quote_if_formula` in `excel_streaming.py` strips, so an indented label already exports with an apostrophe from both of those paths. That is what @rusackas was pointing at: the tradeoff is not new, this brings the third path in line with the other two.
   
   `excel_streaming._quote_if_formula` carried the same sentence, and since the description points at that function I corrected both docstrings rather than leave one copy of the claim behind. That writer is further from evaluating anything than this one: every string goes out through `write_string`, so it never produces a formula cell at all.
   
   So I have kept the change and rewritten the justification to what it actually is, a consistency fix across the export paths, with the quote defending against how a downstream viewer or a re-import reads the text rather than against anything XlsxWriter does. The description now says the same and carries the table.
   
   If you would still rather the XLSX path not strip, the honest version of that is to stop all three stripping, since leaving this one alone puts the inconsistency back. I did not take that on here because it would widen a one-line fix into a change to the CSV export's behaviour.
   
   One thing I noticed while measuring and did not touch: `csv.py` has `_is_negative_number`, so `-5` exports bare there, while both XLSX paths quote it as `'-5`. That divergence predates this PR and is the opposite direction to the one being fixed. Say the word if you want it in scope and I will open it separately.
   



-- 
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: notifications-unsubscribe@superset.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@superset.apache.org
For additional commands, e-mail: notifications-help@superset.apache.org


Re: [PR] fix(export): quote XLSX formulas with leading whitespace [superset]

Posted by "SEPURI-SAI-KRISHNA (via GitHub)" <gi...@apache.org>.
SEPURI-SAI-KRISHNA commented on code in PR #45407:
URL: https://github.com/apache/superset/pull/45407#discussion_r4367643022


##########
superset/utils/excel_streaming.py:
##########
@@ -58,9 +58,12 @@ def _quote_if_formula(text: str) -> str:
     Prefix formula-like text with an apostrophe so spreadsheet apps treat it as
     literal text (defense against formula injection).
 
-    Leading whitespace is ignored when detecting a formula, because spreadsheet
-    apps still evaluate a cell whose formula prefix is preceded by spaces or
-    tabs (e.g. ``" =cmd"`` or ``"\\t=cmd"``).
+    Leading whitespace is stripped before the prefix is read, so ``" =cmd"`` is
+    quoted like ``"=cmd"``. Every string goes out through ``write_string``, so

Review Comment:
   You are right, and the sentence was wrong rather than imprecise. Measured with
   the quote removed, `write_row` stores `=1+1` as `<f>1+1</f>`. `write_string` is
   used only for the truncation notice and the informational sheet, not for headers
   or data rows.
   
   Reworded to say `write_row` types each cell from its value, so the quote is what
   keeps a formula out of the workbook. The whitespace half is kept separate: the
   prefix is read at the first character only, so `" =cmd"` is a plain string cell
   either way, and quoting it is for cross-path consistency and for how a
   downstream reader parses the text.
   
   I wrote that same sentence into my reply on the `excel.py` thread as well, so
   that comment is wrong where it says this writer never builds a formula cell.
   The rest of it stands, including the cell-type table.
   
   `excel.py` had the inverse gap: accurate, but it never said a bare `=` becomes a
   formula cell there either, so a reader could still take the whole guard as
   cosmetic. Measured through `df_to_excel`, without the guard `=1+1` leaves the
   string table and the sheet gains a formula cell. Added that sentence there too.
   
   The claim appears once more in `excel_streaming_tests.py:129`, from #41133
   rather than this branch. Correcting it in three places and leaving a fourth in
   the same subsystem's tests seemed worse than touching one extra file.
   



-- 
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: notifications-unsubscribe@superset.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@superset.apache.org
For additional commands, e-mail: notifications-help@superset.apache.org


Re: [PR] fix(export): quote XLSX formulas with leading whitespace [superset]

Posted by "netlify[bot] (via GitHub)" <gi...@apache.org>.
netlify[bot] commented on PR #45407:
URL: https://github.com/apache/superset/pull/45407#issuecomment-6230242624

   ### <span aria-hidden="true">✅</span> Deploy Preview for *superset-docs-preview* ready!
   
   
   |  Name | Link |
   |:-:|------------------------|
   |<span aria-hidden="true">🔨</span> Latest commit | 118b603ddb728f2398fb2ff0a4ee24b7a8a3ae46 |
   |<span aria-hidden="true">🔍</span> Latest deploy log | https://app.netlify.com/projects/superset-docs-preview/deploys/6ad3087815f717c676b0e953 |
   |<span aria-hidden="true">😎</span> Deploy Preview | [https://deploy-preview-45407--superset-docs-preview.netlify.app](https://deploy-preview-45407--superset-docs-preview.netlify.app) |
   |<span aria-hidden="true">📱</span> Preview on mobile | <details><summary> Toggle QR Code... </summary><br /><br />![QR Code](https://app.netlify.com/qr-code/eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJ1cmwiOiJodHRwczovL2RlcGxveS1wcmV2aWV3LTQ1NDA3LS1zdXBlcnNldC1kb2NzLXByZXZpZXcubmV0bGlmeS5hcHAifQ.4-e_UT86JMLpivoxRko9IKTueuBi5C4j2uruA29YH-0)<br /><br />_Use your smartphone camera to open QR code link._</details> |
   |<span aria-hidden="true">🤖</span> Make changes | [Run an agent on this branch](https://app.netlify.com/projects/superset-docs-preview/agent-runs#DcpLCoAgFADAu7x1kiJqdYOO8fKTgqVYkRHdPXezmBdySVs-YQLoYCm4a9_sQu1rPCpxqWxXRLJeWAy5fTjtkVHblo3NMT2zaV2i4XRQAxNOMaWlkgu1o-Dw_Q) |
   ---
   <!-- [superset-docs-preview Preview](https://deploy-preview-45407--superset-docs-preview.netlify.app) -->
   _To edit notification comments on pull requests, go to your [Netlify project configuration](https://app.netlify.com/projects/superset-docs-preview/configuration/notifications#deploy-notifications)._


-- 
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: notifications-unsubscribe@superset.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@superset.apache.org
For additional commands, e-mail: notifications-help@superset.apache.org


Re: [PR] fix(export): quote XLSX formulas with leading whitespace [superset]

Posted by "sadpandajoe (via GitHub)" <gi...@apache.org>.
sadpandajoe commented on code in PR #45407:
URL: https://github.com/apache/superset/pull/45407#discussion_r4345868316


##########
tests/unit_tests/utils/excel_tests.py:
##########
@@ -80,6 +80,28 @@ def test_quote_formulas() -> None:
     ]
 
 
+def test_quote_formulas_with_leading_whitespace() -> None:
+    """
+    Formulas are quoted even when a space, tab, or newline comes first.
+
+    Spreadsheet apps still evaluate a cell whose formula prefix is preceded by
+    whitespace, so the guard must strip before inspecting the first character.
+    The streaming writer (``excel_streaming``) and the CSV writer already do
+    this; this keeps the chart XLSX path in line.
+    """
+    df = pd.DataFrame(
+        {"formula": [" =1+1", "\t=1+1", "\n=1+1", "  @SUM(A1)", "normal"]}

Review Comment:
   None of these inputs makes `lstrip()` return an empty string, so the `stripped and` guard in `_quote_formula` is untested. If that guard were dropped, this test would still pass but exporting a whitespace-only cell like `" \t\n"` would raise `IndexError` on `stripped[0]`. Could you add a direct `quote_formulas` assertion that `["", " \t\n"]` come back unchanged (not through an Excel round-trip, since blank cells read back as missing)?



-- 
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: notifications-unsubscribe@superset.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@superset.apache.org
For additional commands, e-mail: notifications-help@superset.apache.org


Re: [PR] fix(export): quote XLSX formulas with leading whitespace [superset]

Posted by "SEPURI-SAI-KRISHNA (via GitHub)" <gi...@apache.org>.
SEPURI-SAI-KRISHNA commented on code in PR #45407:
URL: https://github.com/apache/superset/pull/45407#discussion_r4356728192


##########
tests/unit_tests/utils/excel_tests.py:
##########
@@ -80,6 +80,28 @@ def test_quote_formulas() -> None:
     ]
 
 
+def test_quote_formulas_with_leading_whitespace() -> None:
+    """
+    Formulas are quoted even when a space, tab, or newline comes first.
+
+    Spreadsheet apps still evaluate a cell whose formula prefix is preceded by
+    whitespace, so the guard must strip before inspecting the first character.
+    The streaming writer (``excel_streaming``) and the CSV writer already do
+    this; this keeps the chart XLSX path in line.
+    """
+    df = pd.DataFrame(
+        {"formula": [" =1+1", "\t=1+1", "\n=1+1", "  @SUM(A1)", "normal"]}

Review Comment:
   Confirmed, and the guard is doing more work than it was before this PR. The old `len(value)` tested the original string, which was enough while the first character was read off that same string. `lstrip()` can empty a string that was not empty, so an input master handled fine now reaches the subscript empty:
   
   ```
   value      master              this PR, no guard
   ""         ""                  IndexError: string index out of range
   " \t\n"    " \t\n"             IndexError: string index out of range
   "   "      "   "               IndexError: string index out of range
   ```
   
   So the fix introduced the reachable crash and the guard is what prevents it, which makes it exactly the thing that needed a test. A warehouse returning a blank or whitespace-only cell is ordinary.
   
   Added `test_quote_formulas_leaves_blank_and_whitespace_only_cells_alone`, asserting `""`, `" \t\n"` and `"   "` come back unchanged. It calls `quote_formulas` directly as you suggested, since a blank cell read back out of a sheet comes through as a missing value and would hide the result.
   
   I also ran the rest of the changed function the same way. Four conditions, all now pinned:
   
   ```
   drop the stripped guard   -> fails the new test
   drop lstrip               -> fails test_quote_formulas_with_leading_whitespace
   drop the isinstance guard -> fails 10 tests
   revert to master's guard  -> fails test_quote_formulas_with_leading_whitespace
   ```
   
   The description now records the guard change too, which it had not.
   



-- 
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: notifications-unsubscribe@superset.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@superset.apache.org
For additional commands, e-mail: notifications-help@superset.apache.org


Re: [PR] fix(export): quote XLSX formulas with leading whitespace [superset]

Posted by "SEPURI-SAI-KRISHNA (via GitHub)" <gi...@apache.org>.
SEPURI-SAI-KRISHNA commented on code in PR #45407:
URL: https://github.com/apache/superset/pull/45407#discussion_r4371488399


##########
superset/utils/excel.py:
##########
@@ -49,12 +49,30 @@
 
 
 def _quote_formula(value: Any) -> Any:
-    """Prefix a string with a quote when it would parse as a formula."""
-    return (
-        f"'{value}"
-        if isinstance(value, str) and len(value) and value[0] in FORMULA_PREFIXES
-        else value
-    )
+    """Prefix a string with a quote when it would parse as a formula.
+
+    A string whose first character is ``=`` is stored as a formula cell by the
+    writer, so quoting it is what keeps a formula out of the workbook. Dropping
+    the quote would put one in.
+
+    Leading whitespace is stripped before the prefix is read, so ``" =cmd"`` is
+    quoted like ``"=cmd"``. That part is for consistency across the export paths
+    rather than to stop the workbook evaluating it: XlsxWriter builds a formula
+    cell only from a string whose first character is ``=``, so a value with

Review Comment:
   Good catch, and the sentence was false rather than merely broad. Narrowed it.
   
   Measured through `write_row` on xlsxwriter 3.2.9, `{=cmd()}` is stored as
   `<f t="array">cmd()</f>`. The `strings_to_formulas` option does not help: the
   array branch in `_write_token_as_string` is not gated on it, so with
   `strings_to_formulas: False` a bare `=1+1` becomes a plain string while
   `{=cmd()}` is still a formula. It needs both the `{=` prefix and the `}`
   suffix, `{1+1}` stays a string, and leading whitespace defeats it like it
   defeats `=`. All three guards pass it through unchanged, so it reaches the
   sheet from `df_to_excel` and the streaming writer alike.
   
   The docstrings now say only what the change relies on, that whitespace in front
   of the prefix stops the writer recognising a formula at all, and say outright
   that the set of unprefixed strings it treats as formulas is wider than
   `FORMULA_PREFIXES`, naming the `{=...}` form as not covered here.
   
   The same overstatement was in four more places, in `excel_streaming.py`,
   `excel_tests.py`, `excel_streaming_tests.py` and the PR description itself. All
   five are corrected.
   
   Keeping the guard fix out of this PR, since it is a behaviour change and this
   one is documentation. Filing it as its own issue and PR, credited to you.
   



-- 
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: notifications-unsubscribe@superset.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@superset.apache.org
For additional commands, e-mail: notifications-help@superset.apache.org


Re: [PR] fix(export): quote XLSX formulas with leading whitespace [superset]

Posted by "SEPURI-SAI-KRISHNA (via GitHub)" <gi...@apache.org>.
SEPURI-SAI-KRISHNA commented on code in PR #45407:
URL: https://github.com/apache/superset/pull/45407#discussion_r4385078443


##########
superset/utils/excel.py:
##########
@@ -49,12 +49,36 @@
 
 
 def _quote_formula(value: Any) -> Any:
-    """Prefix a string with a quote when it would parse as a formula."""
-    return (
-        f"'{value}"
-        if isinstance(value, str) and len(value) and value[0] in FORMULA_PREFIXES
-        else value
-    )
+    """Prefix a string with a quote when it would parse as a formula.
+
+    A string whose first character is ``=`` is stored as a formula cell by the
+    writer, so quoting it is what keeps a formula out of the workbook. Dropping
+    the quote would put one in.
+
+    Leading whitespace is stripped before the prefix is read, so ``" =cmd"`` is
+    quoted like ``"=cmd"``. That part is for consistency across the export paths
+    rather than to stop the workbook evaluating it: whitespace in front of the
+    prefix stops XlsxWriter recognising a formula at all, so the value is
+    written as a plain string cell either way. The CSV writer
+    (``_starts_like_spreadsheet_formula``) and the streaming writer both strip
+    before reading the prefix, so without this the same cell exports quoted
+    from two of the three paths and bare from this one. What the quote guards
+    against there is how a downstream viewer or a re-import reads the text, not
+    how XlsxWriter stored it.
+
+    Which *unprefixed* strings the writer treats as formulas is a wider
+    question than ``FORMULA_PREFIXES``, and this guard does not answer it: a
+    string wrapped in ``{=`` and ``}`` is stored as an array formula, which
+    neither the quote nor the ``strings_to_formulas`` workbook option prevents.

Review Comment:
   You are right, and I had measured this and then written the opposite. Through
   `write_row`, `{=1+1}` stores as `<f t="array" ref="A1">1+1</f>` and `'{=1+1}`
   stores as a plain string, with `strings_to_formulas: False` changing neither.
   
   Your point about reaching for something heavier made me fix the vagueness as
   well, not just the false half. `_write_token_as_string` reaches
   `_write_formula` from exactly two branches: `startswith("=")`, gated on
   `strings_to_formulas`, and `startswith("{=") and endswith("}")`, gated on
   nothing. So the set is bounded, the quote is the right tool, and extending
   `FORMULA_PREFIXES` is the whole of the follow-up. The paragraph says that now
   instead of calling it a wider open question.
   
   Checked the other three places and the description; this sentence was the only
   one making the claim.
   



-- 
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: notifications-unsubscribe@superset.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@superset.apache.org
For additional commands, e-mail: notifications-help@superset.apache.org


Re: [PR] fix(export): quote XLSX formulas with leading whitespace [superset]

Posted by "SEPURI-SAI-KRISHNA (via GitHub)" <gi...@apache.org>.
SEPURI-SAI-KRISHNA commented on PR #45407:
URL: https://github.com/apache/superset/pull/45407#issuecomment-6386308958

   @rusackas you were holding on sadpandajoe's open thread being answered by me
   rather than by you. That answer went up on the 23rd, in the `excel.py` thread:
   the measured cell types say no, there is no case where the leading-whitespace
   form is evaluated in the XLSX output, and the `"  - North"` apostrophe already
   ships today from both the CSV and the streaming paths, so the tradeoff is not
   new. Your read of it was right.
   
   One correction to that answer, which @sadpandajoe caught since: I justified it
   partly by saying the streaming writer never builds a formula cell because every
   string goes through `write_string`. That is wrong. Headers and rows go through
   `write_row`, which types each cell from its value, so a bare `=` does become a
   formula cell there, and the quote is what stops it. The whitespace half of the
   argument is unchanged, and that is the half this PR turns on.
   


-- 
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: notifications-unsubscribe@superset.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@superset.apache.org
For additional commands, e-mail: notifications-help@superset.apache.org


Re: [PR] fix(export): quote XLSX formulas with leading whitespace [superset]

Posted by "sadpandajoe (via GitHub)" <gi...@apache.org>.
sadpandajoe commented on code in PR #45407:
URL: https://github.com/apache/superset/pull/45407#discussion_r4371649835


##########
superset/utils/excel.py:
##########
@@ -49,12 +49,36 @@
 
 
 def _quote_formula(value: Any) -> Any:
-    """Prefix a string with a quote when it would parse as a formula."""
-    return (
-        f"'{value}"
-        if isinstance(value, str) and len(value) and value[0] in FORMULA_PREFIXES
-        else value
-    )
+    """Prefix a string with a quote when it would parse as a formula.
+
+    A string whose first character is ``=`` is stored as a formula cell by the
+    writer, so quoting it is what keeps a formula out of the workbook. Dropping
+    the quote would put one in.
+
+    Leading whitespace is stripped before the prefix is read, so ``" =cmd"`` is
+    quoted like ``"=cmd"``. That part is for consistency across the export paths
+    rather than to stop the workbook evaluating it: whitespace in front of the
+    prefix stops XlsxWriter recognising a formula at all, so the value is
+    written as a plain string cell either way. The CSV writer
+    (``_starts_like_spreadsheet_formula``) and the streaming writer both strip
+    before reading the prefix, so without this the same cell exports quoted
+    from two of the three paths and bare from this one. What the quote guards
+    against there is how a downstream viewer or a re-import reads the text, not
+    how XlsxWriter stored it.
+
+    Which *unprefixed* strings the writer treats as formulas is a wider
+    question than ``FORMULA_PREFIXES``, and this guard does not answer it: a
+    string wrapped in ``{=`` and ``}`` is stored as an array formula, which
+    neither the quote nor the ``strings_to_formulas`` workbook option prevents.

Review Comment:
   This says the quote does not prevent an array formula, but it does: through `write_row`, `"{=1+1}"` is stored as an `ArrayFormula` cell while `"'{=1+1}"` is stored as a plain string. What is missing is that `FORMULA_PREFIXES` never routes a `{=...}` string to the quote, not that quoting cannot stop it. A maintainer reading this could conclude the quote technique is the wrong tool and reach for something heavier when the follow-up fix is to extend the prefix check. Could you reword it to say the guard does not quote that form?



-- 
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: notifications-unsubscribe@superset.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@superset.apache.org
For additional commands, e-mail: notifications-help@superset.apache.org


Re: [PR] fix(export): quote XLSX formulas with leading whitespace [superset]

Posted by "sadpandajoe (via GitHub)" <gi...@apache.org>.
sadpandajoe commented on code in PR #45407:
URL: https://github.com/apache/superset/pull/45407#discussion_r4320255562


##########
superset/utils/excel.py:
##########
@@ -48,12 +48,18 @@
 
 
 def _quote_formula(value: Any) -> Any:
-    """Prefix a string with a quote when it would parse as a formula."""
-    return (
-        f"'{value}"
-        if isinstance(value, str) and len(value) and value[0] in FORMULA_PREFIXES
-        else value
-    )
+    """Prefix a string with a quote when it would parse as a formula.
+
+    Leading whitespace is ignored when detecting a formula, because spreadsheet
+    apps still evaluate a cell whose formula prefix is preceded by spaces or
+    tabs (e.g. ``" =cmd"`` or ``"\t=cmd"``). This matches the streaming writer
+    in ``superset.utils.excel_streaming``, which the comment on
+    ``FORMULA_PREFIXES`` says both export paths agree with.
+    """
+    if not isinstance(value, str):
+        return value
+    stripped = value.lstrip()
+    return f"'{value}" if stripped and stripped[0] in FORMULA_PREFIXES else value

Review Comment:
   This also changes benign values: a dataset string like `"  - North"` (indented list-style label) used to export unchanged and will now land in the cell as `'  - North`, with a visible apostrophe. The XLSX path writes through XlsxWriter, which only treats a string as a formula when it starts with `=`, so a whitespace-prefixed value was already stored as a plain string cell rather than a live formula. Is there a case where the leading-whitespace form is actually evaluated in the XLSX output? If not, should this stay as it was and leave the whitespace handling to the CSV/streaming writers?



-- 
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: notifications-unsubscribe@superset.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@superset.apache.org
For additional commands, e-mail: notifications-help@superset.apache.org


Re: [PR] fix(export): quote XLSX formulas with leading whitespace [superset]

Posted by "sadpandajoe (via GitHub) via notifications" <no...@superset.apache.org>.
sadpandajoe merged PR #45407:
URL: https://github.com/apache/superset/pull/45407


-- 
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: notifications-unsubscribe@superset.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@superset.apache.org
For additional commands, e-mail: notifications-help@superset.apache.org


Re: [PR] fix(export): quote XLSX formulas with leading whitespace [superset]

Posted by "sadpandajoe (via GitHub)" <gi...@apache.org>.
sadpandajoe commented on code in PR #45407:
URL: https://github.com/apache/superset/pull/45407#discussion_r4364160785


##########
superset/utils/excel_streaming.py:
##########
@@ -58,9 +58,12 @@ def _quote_if_formula(text: str) -> str:
     Prefix formula-like text with an apostrophe so spreadsheet apps treat it as
     literal text (defense against formula injection).
 
-    Leading whitespace is ignored when detecting a formula, because spreadsheet
-    apps still evaluate a cell whose formula prefix is preceded by spaces or
-    tabs (e.g. ``" =cmd"`` or ``"\\t=cmd"``).
+    Leading whitespace is stripped before the prefix is read, so ``" =cmd"`` is
+    quoted like ``"=cmd"``. Every string goes out through ``write_string``, so

Review Comment:
   The new text says every string goes out through `write_string`, but headers and data rows are written with `worksheet.write_row` (lines 190 and 202), which does type detection, so a string starting with `=` would become a formula cell if it were not quoted first. As written, a future reader could conclude the quote here is optional and drop it. Could this say the quote is what keeps `write_row` from creating formula cells, rather than claiming the writer never produces one?



##########
tests/unit_tests/utils/excel_tests.py:
##########
@@ -80,6 +80,47 @@ def test_quote_formulas() -> None:
     ]
 
 
+def test_quote_formulas_with_leading_whitespace() -> None:
+    """
+    Formulas are quoted even when a space, tab, or newline comes first.
+
+    Spreadsheet apps still evaluate a cell whose formula prefix is preceded by

Review Comment:
   This docstring still says spreadsheet apps evaluate a cell whose formula prefix follows whitespace, which is the claim the source docstrings were just corrected to drop: XlsxWriter writes these as plain string cells either way. A reader of this test would take it as a guard against live formula execution rather than an export-consistency check. Could the wording match the corrected `_quote_formula` docstring?



-- 
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: notifications-unsubscribe@superset.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@superset.apache.org
For additional commands, e-mail: notifications-help@superset.apache.org


Re: [PR] fix(export): quote XLSX formulas with leading whitespace [superset]

Posted by "SEPURI-SAI-KRISHNA (via GitHub)" <gi...@apache.org>.
SEPURI-SAI-KRISHNA commented on code in PR #45407:
URL: https://github.com/apache/superset/pull/45407#discussion_r4367644095


##########
tests/unit_tests/utils/excel_tests.py:
##########
@@ -80,6 +80,47 @@ def test_quote_formulas() -> None:
     ]
 
 
+def test_quote_formulas_with_leading_whitespace() -> None:
+    """
+    Formulas are quoted even when a space, tab, or newline comes first.
+
+    Spreadsheet apps still evaluate a cell whose formula prefix is preceded by

Review Comment:
   Agreed, that docstring was the claim the source docstrings had just dropped.
   
   Reworded to match: XlsxWriter reads the formula prefix at the first character
   only, so a value with whitespace in front of it is a plain string cell whether
   or not it is quoted, which makes this an export-consistency check rather than a
   guard against evaluation. The CSV writer and the streaming writer both strip
   before reading the prefix, so without this the same cell goes out quoted from
   two of the three paths and bare from this one.
   



-- 
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: notifications-unsubscribe@superset.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@superset.apache.org
For additional commands, e-mail: notifications-help@superset.apache.org


Re: [PR] fix(export): quote XLSX formulas with leading whitespace [superset]

Posted by "codecov[bot] (via GitHub)" <gi...@apache.org>.
codecov[bot] commented on PR #45407:
URL: https://github.com/apache/superset/pull/45407#issuecomment-6230341695

   ## [Codecov](https://app.codecov.io/gh/apache/superset/pull/45407?dropdown=coverage&src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache) Report
   :x: Patch coverage is `0%` with `4 lines` in your changes missing coverage. Please review.
   :white_check_mark: Project coverage is 64.90%. Comparing base ([`ad63b3e`](https://app.codecov.io/gh/apache/superset/commit/ad63b3eb7aba0e9410756c48a1ef6c448c254ecb?dropdown=coverage&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache)) to head ([`118b603`](https://app.codecov.io/gh/apache/superset/commit/118b603ddb728f2398fb2ff0a4ee24b7a8a3ae46?dropdown=coverage&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache)).
   :warning: Report is 12 commits behind head on master.
   
   | [Files with missing lines](https://app.codecov.io/gh/apache/superset/pull/45407?dropdown=coverage&src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache) | Patch % | Lines |
   |---|---|---|
   | [superset/utils/excel.py](https://app.codecov.io/gh/apache/superset/pull/45407?src=pr&el=tree&filepath=superset%2Futils%2Fexcel.py&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache#diff-c3VwZXJzZXQvdXRpbHMvZXhjZWwucHk=) | 0.00% | [4 Missing :warning: ](https://app.codecov.io/gh/apache/superset/pull/45407?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache) |
   > :exclamation:  There is a different number of reports uploaded between BASE (ad63b3e) and HEAD (118b603). Click for more details.
   > 
   > <details><summary>HEAD has 4 uploads less than BASE</summary>
   >
   >| Flag | BASE (ad63b3e) | HEAD (118b603) |
   >|------|------|------|
   >|python|7|3|
   ></details>
   
   <details><summary>Additional details and impacted files</summary>
   
   
   
   ```diff
   @@             Coverage Diff             @@
   ##           master   #45407       +/-   ##
   ===========================================
   - Coverage   83.49%   64.90%   -18.59%     
   ===========================================
     Files        3047     3048        +1     
     Lines      199516   199587       +71     
     Branches    46552    46552               
   ===========================================
   - Hits       166589   129547    -37042     
   - Misses      29701    67109    +37408     
   + Partials     3226     2931      -295     
   ```
   
   | [Flag](https://app.codecov.io/gh/apache/superset/pull/45407/flags?src=pr&el=flags&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache) | Coverage Δ | |
   |---|---|---|
   | [hive](https://app.codecov.io/gh/apache/superset/pull/45407/flags?src=pr&el=flag&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache) | `34.98% <0.00%> (+0.01%)` | :arrow_up: |
   | [mysql](https://app.codecov.io/gh/apache/superset/pull/45407/flags?src=pr&el=flag&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache) | `?` | |
   | [postgres](https://app.codecov.io/gh/apache/superset/pull/45407/flags?src=pr&el=flag&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache) | `?` | |
   | [presto](https://app.codecov.io/gh/apache/superset/pull/45407/flags?src=pr&el=flag&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache) | `36.73% <0.00%> (+0.01%)` | :arrow_up: |
   | [python](https://app.codecov.io/gh/apache/superset/pull/45407/flags?src=pr&el=flag&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache) | `52.81% <0.00%> (-34.20%)` | :arrow_down: |
   | [sqlite](https://app.codecov.io/gh/apache/superset/pull/45407/flags?src=pr&el=flag&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache) | `52.70% <0.00%> (+0.01%)` | :arrow_up: |
   | [unit](https://app.codecov.io/gh/apache/superset/pull/45407/flags?src=pr&el=flag&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache) | `?` | |
   
   Flags with carried forward coverage won't be shown. [Click here](https://docs.codecov.io/docs/carryforward-flags?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache#carryforward-flags-in-the-pull-request-comment) to find out more.
   </details>
   
   [:umbrella: View full report in Codecov by Harness](https://app.codecov.io/gh/apache/superset/pull/45407?dropdown=coverage&src=pr&el=continue&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache).   
   :loudspeaker: Have feedback on the report? [Share it here](https://about.codecov.io/codecov-pr-comment-feedback/?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache).
   <details><summary> :rocket: New features to boost your workflow: </summary>
   
   - :snowflake: [Test Analytics](https://docs.codecov.com/docs/test-analytics): Detect flaky tests, report on failures, and find test suite problems.
   - :package: [JS Bundle Analysis](https://docs.codecov.com/docs/javascript-bundle-analysis): Save yourself from yourself by tracking and limiting bundle sizes in JS merges.
   </details>


-- 
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: notifications-unsubscribe@superset.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@superset.apache.org
For additional commands, e-mail: notifications-help@superset.apache.org


Re: [PR] fix(export): quote XLSX formulas with leading whitespace [superset]

Posted by "SEPURI-SAI-KRISHNA (via GitHub) via notifications" <no...@superset.apache.org>.
SEPURI-SAI-KRISHNA commented on PR #45407:
URL: https://github.com/apache/superset/pull/45407#issuecomment-6607441073

   @sadpandajoe the one failure is not from this diff.
   
   `test-postgres` was 3148 passed, 1 failed, and the failure is
   `datasets/api_tests.py::test_get_dataset_render_jinja`, which asserts a fixed
   order for the `columns` list while `SqlaTable.columns` has no `order_by`
   (`superset/connectors/sqla/models.py:1887`), so the order is whatever Postgres
   hands back. `test-postgres-required` passed on #45859, #45855, #45876 and
   #45795 in the same window. This PR touches `superset/utils/excel.py`,
   `superset/utils/excel_streaming.py` and their two unit test files.
   
   Merged master to get a fresh run.
   


-- 
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: notifications-unsubscribe@superset.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@superset.apache.org
For additional commands, e-mail: notifications-help@superset.apache.org