-
Notifications
You must be signed in to change notification settings - Fork 16.6k
fix(results): handle unnamed result columns safely . #37570
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
Arunodoy18
wants to merge
1
commit into
apache:master
Choose a base branch
from
Arunodoy18:fix-mssql-unnamed-result-columns
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.
Open
Changes from all commits
Commits
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
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -99,6 +99,31 @@ def convert_to_string(value: Any) -> str: | |||||||||||||||||||||||||||||||||
| return str(value) | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| def normalize_column_name(value: Any, index: int) -> str: | ||||||||||||||||||||||||||||||||||
| """ | ||||||||||||||||||||||||||||||||||
| Normalize a column name from the cursor description. | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| Some databases (e.g., MSSQL) return empty strings for unnamed columns | ||||||||||||||||||||||||||||||||||
| (e.g., SELECT COUNT(*) without an alias). This function ensures every | ||||||||||||||||||||||||||||||||||
| column has a valid, non-empty name by generating a positional fallback | ||||||||||||||||||||||||||||||||||
| name when needed. | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| :param value: The column name from cursor.description (can be str, bytes, None, etc.) | ||||||||||||||||||||||||||||||||||
| :param index: The 0-based column position, used to generate fallback names | ||||||||||||||||||||||||||||||||||
| :return: A non-empty string column name | ||||||||||||||||||||||||||||||||||
| """ | ||||||||||||||||||||||||||||||||||
| if value is None: | ||||||||||||||||||||||||||||||||||
| return f"_col{index}" | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| name = convert_to_string(value) | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| # Handle empty or whitespace-only names | ||||||||||||||||||||||||||||||||||
| if not name or not name.strip(): | ||||||||||||||||||||||||||||||||||
| return f"_col{index}" | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| return name | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| class SupersetResultSet: | ||||||||||||||||||||||||||||||||||
| def __init__( # pylint: disable=too-many-locals # noqa: C901 | ||||||||||||||||||||||||||||||||||
| self, | ||||||||||||||||||||||||||||||||||
|
|
@@ -116,8 +141,13 @@ def __init__( # pylint: disable=too-many-locals # noqa: C901 | |||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| if cursor_description: | ||||||||||||||||||||||||||||||||||
| # get deduped list of column names | ||||||||||||||||||||||||||||||||||
| # Use normalize_column_name to handle None/empty names from databases | ||||||||||||||||||||||||||||||||||
| # like MSSQL that return empty strings for unnamed columns | ||||||||||||||||||||||||||||||||||
| column_names = dedup( | ||||||||||||||||||||||||||||||||||
| [convert_to_string(col[0]) for col in cursor_description] | ||||||||||||||||||||||||||||||||||
| [ | ||||||||||||||||||||||||||||||||||
| normalize_column_name(col[0], idx) | ||||||||||||||||||||||||||||||||||
| for idx, col in enumerate(cursor_description) | ||||||||||||||||||||||||||||||||||
| ] | ||||||||||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||||||||||
|
Comment on lines
146
to
151
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Suggestion: Accessing Severity Level: Major
|
||||||||||||||||||||||||||||||||||
| column_names = dedup( | |
| [convert_to_string(col[0]) for col in cursor_description] | |
| [ | |
| normalize_column_name(col[0], idx) | |
| for idx, col in enumerate(cursor_description) | |
| ] | |
| ) | |
| column_names_values: list[str] = [] | |
| for idx, col in enumerate(cursor_description): | |
| try: | |
| name_value = col[0] | |
| except Exception: | |
| # description entry missing or not indexable; treat as unnamed | |
| name_value = None | |
| column_names_values.append(normalize_column_name(name_value, idx)) | |
| column_names = dedup(column_names_values) |
Steps of Reproduction ✅
1. In a Python REPL or unit test import the class: open `superset/result_set.py` and
locate `SupersetResultSet.__init__` at `superset/result_set.py:128` (constructor start).
2. Construct a malformed cursor description and invoke the constructor directly:
- Create `cursor_description = [None]` or `cursor_description = [123]` in the test.
- Call `SupersetResultSet(data=[], cursor_description=cursor_description,
db_engine_spec=BaseEngineSpec)` (the call site is the `__init__` at
`superset/result_set.py:128`).
3. When the list comprehension at `superset/result_set.py:146-150` executes, it tries to
evaluate `col[0]` (the call to `normalize_column_name(col[0], idx)` is at
`superset/result_set.py:148`), which raises:
- `TypeError: 'NoneType' object is not subscriptable` for `None`, or
- `TypeError: 'int' object is not subscriptable` for `123`.
4. Observe the exception bubbles out of `SupersetResultSet.__init__` and prevents
result-set construction; this reproduces the failure caused by unguarded `col[0]` access.
- Note: This is reproducible without other parts of Superset by directly calling the
constructor described above.Prompt for AI Agent 🤖
This is a comment left during a code review.
**Path:** superset/result_set.py
**Line:** 146:151
**Comment:**
*Possible Bug: Accessing `col[0]` without validating that `col` is a sequence (or non-None) can raise TypeError/IndexError if a cursor description entry is None or not indexable; iterate and safely extract the 0th element before calling `normalize_column_name`.
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.
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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Suggestion: Decoding column names can raise UnicodeDecodeError when
convert_to_stringtries to decode non-UTF-8 bytes; catch decoding errors and fallback to a safe decode (errors='replace') or str conversion so the normalization step never raises. [possible bug]Severity Level: Major⚠️
Steps of Reproduction ✅
Prompt for AI Agent 🤖