-
Notifications
You must be signed in to change notification settings - Fork 17.2k
fix(mcp): ASCII chart crashes with NaN when dataset contains null values #39916
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
aminghadersohi
wants to merge
7
commits into
apache:master
Choose a base branch
from
aminghadersohi:fix/mcp-ascii-chart-nan
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+205
−13
Open
Changes from 4 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
393906c
fix(mcp): guard against NaN in ASCII chart bar/line renderers
aminghadersohi fb4ccc9
test(mcp): fix label assertion in NaN bar chart test
aminghadersohi 436e7ae
test(mcp): strengthen all-NaN fallback assertion in bar chart test
aminghadersohi 32192df
test(mcp): strengthen NaN bar chart test assertions
aminghadersohi 429e14a
test(mcp): assert horizontal layout in NaN bar chart skipping test
aminghadersohi f80e3b3
fix(mcp): exclude booleans from numeric extraction in ASCII chart ren…
aminghadersohi a560f84
fix(mcp): apply consistent NaN+bool guards to scatter extractor and s…
aminghadersohi File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
109 changes: 109 additions & 0 deletions
109
tests/unit_tests/mcp_service/chart/test_ascii_charts.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,109 @@ | ||
| # Licensed to the Apache Software Foundation (ASF) under one | ||
| # or more contributor license agreements. See the NOTICE file | ||
| # distributed with this work for additional information | ||
| # regarding copyright ownership. The ASF licenses this file | ||
| # to you under the Apache License, Version 2.0 (the | ||
| # "License"); you may not use this file except in compliance | ||
| # with the License. You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, | ||
| # software distributed under the License is distributed on an | ||
| # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| # KIND, either express or implied. See the License for the | ||
| # specific language governing permissions and limitations | ||
| # under the License. | ||
|
|
||
| """Unit tests for ascii_charts.py NaN/null value handling.""" | ||
|
|
||
| from superset.mcp_service.chart.ascii_charts import generate_ascii_chart | ||
|
|
||
|
|
||
| def test_bar_chart_with_null_values_does_not_raise() -> None: | ||
| """Bar chart renderer must not crash when dataset rows contain NaN values.""" | ||
| data = [ | ||
| {"category": "A", "value": 10.0}, | ||
| {"category": "B", "value": float("nan")}, | ||
| {"category": "C", "value": 30.0}, | ||
| ] | ||
| result = generate_ascii_chart(data, "bar") | ||
| assert isinstance(result, str) | ||
| assert len(result) > 0 | ||
|
|
||
|
|
||
| def test_bar_chart_with_all_null_values_returns_fallback() -> None: | ||
| """Bar chart with no valid numeric rows should return the no-data fallback.""" | ||
| data = [ | ||
| {"category": "A", "value": float("nan")}, | ||
| {"category": "B", "value": float("nan")}, | ||
| ] | ||
| result = generate_ascii_chart(data, "bar") | ||
| assert isinstance(result, str) | ||
| assert result == "No numeric data found for bar chart" | ||
| assert "█" not in result # No bars should be rendered in the fallback path | ||
|
|
||
|
|
||
| def test_line_chart_with_null_values_does_not_raise() -> None: | ||
| """Line chart renderer must not crash when dataset rows contain NaN values.""" | ||
| data = [ | ||
| {"date": "2024-01", "sales": 100.0}, | ||
| {"date": "2024-02", "sales": float("nan")}, | ||
| {"date": "2024-03", "sales": 300.0}, | ||
| ] | ||
| result = generate_ascii_chart(data, "line") | ||
| assert isinstance(result, str) | ||
| assert len(result) > 0 | ||
|
|
||
|
|
||
| def test_horizontal_bar_chart_nan_rows_are_skipped() -> None: | ||
| """NaN rows must be silently skipped; valid rows render normally.""" | ||
| # Use labels longer than 8 chars to force horizontal layout, where full | ||
| # label text is preserved (vertical layout truncates to 3 chars). | ||
| data = [ | ||
| {"label": "Alpha Category", "amount": 50.0}, | ||
| {"label": "Beta Category", "amount": float("nan")}, | ||
| {"label": "Gamma Category", "amount": 150.0}, | ||
| ] | ||
| result = generate_ascii_chart(data, "bar") | ||
|
aminghadersohi marked this conversation as resolved.
|
||
| # Both valid labels must appear; the NaN row (Beta) must be absent | ||
| assert "Alpha" in result | ||
| assert "Gamma" in result | ||
| assert "Beta" not in result | ||
|
|
||
|
|
||
| def test_column_chart_with_null_values_does_not_raise() -> None: | ||
| """Column (vertical bar) chart must not crash on NaN values.""" | ||
| data = [ | ||
| {"x": "Q1", "y": 10.0}, | ||
| {"x": "Q2", "y": float("nan")}, | ||
| {"x": "Q3", "y": 30.0}, | ||
| {"x": "Q4", "y": 40.0}, | ||
| ] | ||
| result = generate_ascii_chart(data, "column") | ||
| assert isinstance(result, str) | ||
| assert len(result) > 0 | ||
|
|
||
|
|
||
| def test_timeseries_bar_with_null_values_does_not_raise() -> None: | ||
| """echarts_timeseries_bar chart type must not crash on NaN values.""" | ||
| data = [ | ||
| {"ts": "2024-01-01", "count": 5.0}, | ||
| {"ts": "2024-01-02", "count": float("nan")}, | ||
| {"ts": "2024-01-03", "count": 15.0}, | ||
| ] | ||
| result = generate_ascii_chart(data, "echarts_timeseries_bar") | ||
| assert isinstance(result, str) | ||
| assert len(result) > 0 | ||
|
|
||
|
|
||
| def test_chart_with_none_values_does_not_raise() -> None: | ||
| """None (SQL NULL) values should be treated identically to NaN.""" | ||
| data = [ | ||
| {"name": "X", "metric": 100.0}, | ||
| {"name": "Y", "metric": None}, | ||
| {"name": "Z", "metric": 200.0}, | ||
| ] | ||
| result = generate_ascii_chart(data, "bar") | ||
| assert isinstance(result, str) | ||
| assert len(result) > 0 | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.