Skip to content

Conversation

@sadpandajoe
Copy link
Member

SUMMARY

Root Cause

MySQL boolean fields were incorrectly displaying as numeric icons with raw 0/1 values instead of readable True/False text in SQL Lab and Explore views. The issue had two layers:

  1. Type Mapping: TINYINT(1) columns were mapped to GenericDataType.NUMERIC instead of GenericDataType.BOOLEAN
  2. Value Conversion Bug: bool("0") and bool(b"0") return True in Python instead of the expected False

Solution

Two-layer fix targeting MySQL's TINYINT(1) boolean representation:

  1. Schema-level: Added precise pattern matching for TINYINT(1) → GenericDataType.BOOLEAN
  2. Runtime-level: Enhanced fetch_data() with normalization logic for strings/bytes/Decimal before boolean conversion

Changes

  • superset/db_engine_specs/mysql.py: Added boolean type mapping and conversion logic
  • tests/unit_tests/db_engine_specs/test_mysql.py: Comprehensive test coverage for edge cases

BEFORE/AFTER SCREENSHOTS OR ANIMATED GIF

TESTING INSTRUCTIONS

  • ✅ All existing MySQL tests pass
  • ✅ New parametrized tests cover string/bytes/Decimal conversion scenarios
  • ✅ Preserves behavior for non-boolean TINYINT variants (TINYINT(2), etc.)
  • ✅ End-to-end pipeline validation with pandas boolean inference

ADDITIONAL INFORMATION

  • Has associated issue: Fixes: Boolean fields not rendered correctly for MySQL in Superset 4.1.3 #35166
  • Required feature flags:
  • Changes UI
  • Includes DB Migration (follow approval process in SIP-59)
    • 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

sadpandajoe and others added 2 commits September 24, 2025 15:20
Fixes MySQL TINYINT(1)/BOOLEAN columns displaying as numeric icons with 0/1 values instead of boolean representation with True/False in SQL Lab and Explore views.

## Changes

### Schema Mapping (mysql.py)
- Add precise column_type_mappings for TINYINT(1), BOOLEAN, and BOOL patterns
- Map to GenericDataType.BOOLEAN for proper metadata inspection
- Ensures dataset schemas show boolean icons for actual boolean types only

### Runtime Conversion (mysql.py)
- Implement fetch_data override with ultra-precise boolean detection
- Convert 0/1 integers to True/False for TINYINT(1) columns
- Use multiple reliable markers: FIELD_TYPE.TINY + display_size=1 OR SQLAlchemy type string
- Extract _is_boolean_column helper method for clean detection logic
- Enables pandas boolean dtype inference via extract_dataframe_dtypes

### Testing (test_mysql.py)
- Add boolean type test cases to existing parametrized tests
- Test TINYINT(1), BOOLEAN, BOOL → boolean mapping
- Test TINYINT, TINYINT(2+) → numeric mapping (preserved behavior)

## Technical Details

MySQL stores BOOLEAN as TINYINT(1) but returns 0/1 integers instead of Python booleans. This two-layer solution:
1. Maps TINYINT(1) metadata to GenericDataType.BOOLEAN for schema inspection
2. Converts query result values 0/1 → True/False for proper pandas inference

The detection uses explicit width 1 or positive SQLAlchemy type string markers to avoid mis-converting broader TINYINT columns.

Fixes: #35166

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <[email protected]>
…ersion

- Fix critical bug where bool('0') and bool(b'0') returned True instead of False
- Add proper type normalization for strings, bytes, and Decimal values
- Convert string/bytes/Decimal to int before applying bool() for accurate MySQL boolean conversion
- Maintains existing behavior for integer values while fixing edge cases
- Addresses feedback on conversion logic in mysql.py:323-330

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <[email protected]>
@dosubot dosubot bot added the data:connect:mysql Related to MySQL label Sep 25, 2025
Copy link

@korbit-ai korbit-ai bot left a comment

Choose a reason for hiding this comment

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

Review by Korbit AI

Korbit automatically attempts to detect when you fix issues in new commits.
Category Issue Status
Functionality Unreliable boolean column detection across drivers ▹ view 🧠 Not in standard
Files scanned
File Path Reviewed
superset/db_engine_specs/mysql.py

Explore our documentation to understand the languages and file types we support and the files we ignore.

Check out our docs on how you can make Korbit work best for you and your team.

Loving Korbit!? Share us on LinkedIn Reddit and X

Comment on lines +289 to +292
# Check SQLAlchemy type string (some drivers provide it at index 4)
if len(col_desc) > 4 and isinstance(col_desc[4], str):
sqla_type_str = col_desc[4].lower()
return any(marker in sqla_type_str for marker in ["bool", "tinyint(1)"])

This comment was marked as resolved.

@sadpandajoe sadpandajoe changed the title fix(mysql): render TINYINT(1) columns as boolean in SQL Lab and charts fix(mysql): render TINYINT(1) columns as boolean in SQL Lab and charts Sep 25, 2025
value = int(value)
elif isinstance(value, Decimal):
value = int(value)
new_row[col_idx] = bool(value)
Copy link
Member

Choose a reason for hiding this comment

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

Copy link
Member

Choose a reason for hiding this comment

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

Unless @betodealmeida can think of a better way

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.

Boolean fields not rendered correctly for MySQL in Superset 4.1.3

2 participants