Skip to content

fix(post-processing): flatten every index level - #45263

Open
AliIbnBotir wants to merge 3 commits into
apache:masterfrom
AliIbnBotir:fix/flatten-multiindex
Open

AliIbnBotir wants to merge 3 commits into
apache:masterfrom
AliIbnBotir:fix/flatten-multiindex

Conversation

@AliIbnBotir

@AliIbnBotir AliIbnBotir commented Oct 14, 2026 •

Copy link
Copy Markdown

SUMMARY

flatten post-processing reset only the first level of the dataframe index
(df.reset_index(level=0)). After a pivot with two or more index columns, the
remaining index levels were never turned back into columns, so they were lost
when the result was serialized. flatten now resets every index level. A
single-level index behaves as before.

BEFORE/AFTER SCREENSHOTS OR ANIMATED GIF

N/A (backend only)

TESTING INSTRUCTIONS

  • pytest tests/unit_tests/pandas_postprocessing/test_flatten.py
  • New test test_flat_should_reset_every_index_level pivots on two index
    columns and checks that both come back as columns after flatten.

ADDITIONAL INFORMATION

flatten reset only the first level of a MultiIndex, so a pivot with
several index columns lost the others when the result was serialized.
A single-level index is reset as before.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
if reset_index and not isinstance(df.index, pd.RangeIndex):
df = df.reset_index(level=0)
# every level of a MultiIndex, e.g. a pivot with several index columns
df = df.reset_index()

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggestion: When a later index level shares a name with a data column or another level, reset_index() raises ValueError, failing query post-processing.

Assessment: 🟠 Major · 🔁 Occurrence: Rarely · 🏷️ Logic error

Use CodeAnt Skill Fix in Cursor Fix in VSCode Claude

Prompt for AI Agent 🤖
This is a comment left during a code review.

**Path:** superset/utils/pandas_postprocessing/flatten.py
**Line:** 105:105
**Comment:**
	*Logic Error: When a later index level shares a name with a data column or another level, `reset_index()` raises `ValueError`, failing query post-processing.

Validate the correctness of the flagged issue. If correct, How can I resolve this? If you propose a fix, implement it and please make it concise.
Once fix is implemented, also check other comments on the same PR, and ask user if the user wants to fix the rest of the comments as well. if said yes, then fetch all the comments validate the correctness and implement a minimal fix
👍 | 👎

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks. This collision isn’t new: the previous reset_index(level=0) already raises the same ValueError when level 0 shares a name with a column. This PR applies the same behaviour to every level instead of silently dropping levels 1..n. Happy to add explicit handling if maintainers prefer, but it seems out of scope for this fix.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

✅ Customized review instruction saved!

Instruction:

Do not flag reset_index() name-collision failures in flatten; level-0 collisions already had this behavior, and extending the reset to all levels is intentional in this change.

Applied to:

  • superset/utils/pandas_postprocessing/flatten.py

💡 To manage or update this instruction, visit: CodeAnt AI Settings

@codeant-ai-for-open-source

codeant-ai-for-open-source Bot commented Oct 14, 2026 •

Copy link
Copy Markdown
Contributor

CodeAnt  CodeAnt PR Risk

🟡  Medium Risk

  • The PR needs attention before merging.
  • Resetting every index level can now fail when a later level name collides with a data column or another level, breaking query post-processing.

Assessed commit: c52015581c6d

@codecov

codecov Bot commented Oct 14, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 84.70%. Comparing base (a8c13c6) to head (c520155).

Additional details and impacted files
@@             Coverage Diff             @@
##           master   #45263       +/-   ##
===========================================
+ Coverage   65.62%   84.70%   +19.08%     
===========================================
  Files        3069     3069               
  Lines      205661   205649       -12     
  Branches    48336    48335        -1     
===========================================
+ Hits       134956   174200    +39244     
+ Misses      67624    28156    -39468     
- Partials     3081     3293      +212     
Flag Coverage Δ
hive 34.51% <0.00%> (ø)
mysql 52.58% <100.00%> (+<0.01%) ⬆️
postgres 52.59% <100.00%> (-0.01%) ⬇️
presto 36.24% <0.00%> (ø)
python 87.75% <100.00%> (+34.89%) ⬆️
sqlite 52.35% <100.00%> (ø)
unit 82.48% <100.00%> (?)

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@EnxDev EnxDev left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me. With a single-level index reset_index() does exactly what reset_index(level=0) did, so the only behavior change is the MultiIndex case from #45261, and the new test pins that down.

The built-in charts all pivot on [xAxisLabel] only, so this really only shows up for API callers that pivot on several index columns. I agree with your read on the name-collision thread: level 0 could already collide the same way, and raising beats quietly dropping columns.

The red check-openapi-spec-drift looks unrelated, nothing here touches the API schema.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

flatten post-processing drops index columns when the index has several levels

3 participants