aminghadersohi commented on code in PR #43771:
URL: https://github.com/apache/superset/pull/43771#discussion_r3972014690


##########
superset/mcp_service/chart/query_result.py:
##########
@@ -15,82 +15,1521 @@
 # specific language governing permissions and limitations
 # under the License.
 
-"""Helpers for interpreting ChartDataCommand result envelopes."""
+"""Canonicalize and validate ``ChartDataCommand`` result envelopes."""
 
 import math
+import time as system_time
+from bisect import bisect_right
 from collections.abc import Mapping
+from dataclasses import dataclass
+from datetime import date, datetime, time, timedelta, timezone
+from decimal import Decimal
+from enum import Enum
 from typing import Any
+from uuid import UUID
+from zoneinfo import ZoneInfo
 
+import numpy as np
+import pandas as pd
+import pytz
+from dateutil import tz as dateutil_tz, zoneinfo as dateutil_zoneinfo
+from pydantic import BaseModel
+from pydantic_core import to_json
+
+from superset.common.chart_data import ChartDataResultFormat
+from superset.common.db_query_status import QueryStatus
+from superset.constants import CACHE_DISABLED_TIMEOUT
 from superset.mcp_service.chart.schemas import ChartError
+from superset.utils.core import (
+    ExtraFiltersReasonType,
+    ExtraFiltersTimeColumnType,
+    GenericDataType,
+)
 
 FAILED_QUERY_STATUSES = frozenset(
     {"error", "failed", "stopped", "timed_out", "cancelled", "canceled"}
 )
 
+# These are aggregate envelope limits, not per-query allowances. In particular,
+# splitting a result across the maximum number of queries must not multiply the
+# permitted rows, nodes, or encoded bytes.
+MAX_QUERY_RESULTS = 32
+MAX_QUERY_RESULT_ROWS_PER_QUERY = 50_000
+MAX_QUERY_RESULT_ROWS = 100_000
+MAX_QUERY_RESULT_COLUMNS = 4_096
+MAX_QUERY_RESULT_VALUES = 2_500_000
+MAX_QUERY_RESULT_VALUE_BYTES = 16 * 1024 * 1024
+MAX_QUERY_RESULT_METADATA_BYTES = 1024 * 1024
+MAX_QUERY_RESULT_METADATA_ITEMS = 32_768
+MAX_RESULT_VALUE_ITEMS = 4_096
+MAX_RESULT_VALUE_DEPTH = 32
+MAX_RESULT_STRING_LENGTH = 65_536
+MAX_RESULT_KEY_LENGTH = 4_096
+MAX_RESULT_INTEGER_BITS = 4_096
+MAX_RESULT_INTEGER_DIGITS = 1_234
+MAX_RESULT_DECIMAL_DIGITS = 1_024
+MAX_RESULT_DECIMAL_MAGNITUDE = 4_096
+MAX_RESULT_DECIMAL_STORAGE = 2_048
+MAX_QUERY_RESULT_ROWCOUNT = 2**63 - 1
+MAX_QUERY_RESULT_CACHE_TIMEOUT = 2**31 - 1
+MAX_QUERY_RESULT_TIMESTAMP_LENGTH = 64
+
+_ERROR_KEYS = ("error", "errors", "error_message", "message", "detail")
+_MAX_ERROR_TEXT_BYTES = 2_000
+_TRUSTED_TIMEZONE_TYPES = (timezone, ZoneInfo)
+_SAFE_RESULT_ENUM_TYPES = frozenset(
+    {
+        ChartDataResultFormat,
+        QueryStatus,
+        ExtraFiltersReasonType,
+        ExtraFiltersTimeColumnType,
+        GenericDataType,
+    }
+)
+_RESULT_FORMAT_VALUES = frozenset(
+    object.__getattribute__(member, "_value_") for member in 
ChartDataResultFormat
+)
+_COLTYPE_VALUES = frozenset(
+    object.__getattribute__(member, "_value_") for member in GenericDataType
+)
+_NUMPY_INTEGER_TYPES = frozenset(
+    type(value)
+    for value in (
+        np.int8(0),
+        np.int16(0),
+        np.int32(0),
+        np.int64(0),
+        np.uint8(0),
+        np.uint16(0),
+        np.uint32(0),
+        np.uint64(0),
+    )
+)
+_NUMPY_FLOAT_TYPES = frozenset(
+    type(value)
+    for value in (np.float16(0), np.float32(0), np.float64(0), 
np.longdouble(0))
+)
+_PANDAS_NAT_TYPE = type(pd.NaT)
+_PANDAS_NA_TYPE = type(pd.NA)
+_PANDAS_PERIOD_TYPE = type(pd.Period("2000-01", freq="M"))
+_PANDAS_INTERVAL_TYPE = type(pd.Interval(0, 1))
+_DATEUTIL_FIXED_TIMEZONE_TYPES = frozenset(
+    {type(dateutil_tz.tzoffset(None, 0)), type(dateutil_tz.tzutc())}
+)
+_DATEUTIL_NAMED_TIMEZONE_TYPES = frozenset(
+    {dateutil_tz.tzfile, dateutil_zoneinfo.tzfile}
+)
+_DATEUTIL_TTINFO_TYPE = type(
+    object.__getattribute__(dateutil_tz.gettz("UTC"), 
"__dict__")["_ttinfo_std"]
+)
+_DATEUTIL_LOCAL_TIMEZONE_TYPE = type(dateutil_tz.tzlocal())
+_PYTZ_FIXED_TIMEZONE_TYPES = frozenset({type(pytz.FixedOffset(1))})
+_MAX_DATEUTIL_TRANSITIONS = 4_096
+_MAX_DATEUTIL_TTINFOS = 256
+_MAX_DATEUTIL_TRANSITION_MAGNITUDE = 10**12
+
+
+@dataclass
+class _ResultBudget:
+    """Aggregate counters shared by every query and metadata value."""
+
+    rows: int = 0
+    values: int = 0
+    json_bytes: int = 0
+    metadata_items: int = 0
+    metadata_bytes: int = 0
+
+
+@dataclass(frozen=True)
+class _DateutilTimezoneState:
+    """Hook-free subset of a validated exact dateutil tzfile transition 
table."""
 
-def _query_error_text(value: Any) -> str | None:
-    """Convert a bounded query error payload into a useful message."""
-    if value is None or value is False:
+    transitions: tuple[int, ...]
+    transition_offsets: tuple[int, ...]
+    standard_offset: int
+    before_offset: int | None
+
+
+def _invalid_result(message: str) -> ChartError:
+    return ChartError(
+        error=f"Chart query returned {message}.",
+        error_type="InvalidQueryResult",
+    )
+
+
+def _invalid_metadata(label: str) -> ChartError:
+    return ChartError(
+        error=f"{label} returned hostile or malformed metadata.",
+        error_type="InvalidQueryResult",
+    )
+
+
+def _safe_enum_value(value: Any, expected: frozenset[type[Any]]) -> Any | None:
+    """Read trusted enum storage without invoking public conversion hooks."""
+    if type(value) not in expected or type(value) not in 
_SAFE_RESULT_ENUM_TYPES:
         return None
-    if isinstance(value, Mapping):
-        for key in ("error", "error_message", "message", "detail"):
-            if text := _query_error_text(value.get(key)):
-                return text
+    return object.__getattribute__(value, "_value_")
+
+
+def _bounded_utf8_length(value: str, maximum: int) -> int | None:
+    """Return the exact UTF-8 size while bounding pre-encoding work."""
+    if str.__len__(value) > maximum:
         return None
-    if isinstance(value, (list, tuple)):
-        parts = [text for item in value if (text := _query_error_text(item))]
-        return "; ".join(parts[:3]) or None
-    text = str(value)
-    return text[:2000] if text else None
+    try:
+        encoded = str.encode(value, "utf-8", errors="strict")
+    except UnicodeEncodeError:
+        return None
+    size = bytes.__len__(encoded)
+    return size if size <= maximum else None
 
 
-def _failure_for_query_payload(
-    payload: Mapping[str, Any], label: str
-) -> ChartError | None:
-    """Extract one failure from a top-level or per-query payload."""
+def _json_string_size(value: str, maximum: int) -> int | None:
+    """Return compact UTF-8 JSON string size without serializing the value."""
+    raw_size = _bounded_utf8_length(value, maximum)
+    if raw_size is None:
+        return None
+    escaped_size = raw_size + 2
+    for character in value:
+        codepoint = ord(character)
+        if character in {'"', "\\", "\b", "\t", "\n", "\f", "\r"}:
+            escaped_size += 1
+        elif codepoint < 0x20:
+            escaped_size += 5
+    return escaped_size
+
+
+def _integer_json_size(value: int) -> int:
+    """Return exact decimal JSON size without rendering the bounded integer."""
+    magnitude = -value if value < 0 else value
+    if magnitude == 0:
+        digits = 1
+    else:
+        bits = int.bit_length(magnitude)
+        digits = ((bits - 1) * 30103) // 100000 + 1
+        if magnitude >= 10**digits:
+            digits += 1
+    return digits + (value < 0)
+
+
+def _container_json_syntax_size(item_count: int, *, mapping: bool) -> int:
+    """Return braces/brackets plus compact separators and mapping colons."""
+    if item_count == 0:
+        return 2
+    return 2 + item_count - 1 + (item_count if mapping else 0)
+
+
+def _normalized_scalar_json_size(value: Any) -> int:
+    """Return exact compact JSON size for a normalized scalar."""
+    value_type = type(value)
+    if value is None:
+        return 4
+    if value_type is bool:
+        return 4 if value else 5
+    if value_type is str:
+        size = _json_string_size(value, MAX_RESULT_STRING_LENGTH)
+        assert size is not None
+        return size
+    if value_type is int:
+        return _integer_json_size(value)
+    if value_type is float:
+        return len(float.__repr__(value))
+    if value_type is Decimal:
+        # Pydantic serializes Decimal values as JSON strings so their exact
+        # finite value survives the wire projection without binary rounding.
+        text = Decimal.__str__(value)
+        size = _json_string_size(text, MAX_RESULT_STRING_LENGTH)
+        assert size is not None
+        return size
+    raise AssertionError("result scalar was not normalized")
+
+
+def _pydantic_scalar_json_size(value: Any) -> int:
+    """Return the scalar size emitted by Pydantic's JSON serializer."""
+    if type(value) is float:
+        # pydantic-core uses the shortest exponent (``1e-7``), while Python's
+        # repr retains a leading zero (``1e-07``).
+        return len(to_json(value))
+    return _normalized_scalar_json_size(value)
+
+
+def _charge_json_bytes(
+    budget: _ResultBudget, size: int, *, metadata: bool = False
+) -> str | None:
+    budget.json_bytes += size
+    if budget.json_bytes > MAX_QUERY_RESULT_VALUE_BYTES:
+        return "too many aggregate JSON bytes"
+    if metadata:
+        budget.metadata_bytes += size
+        if budget.metadata_bytes > MAX_QUERY_RESULT_METADATA_BYTES:
+            return "too many aggregate metadata JSON bytes"
+    return None
+
+
+def _charge_value(budget: _ResultBudget, *, metadata: bool = False) -> str | 
None:
+    budget.values += 1
+    if budget.values > MAX_QUERY_RESULT_VALUES:
+        return "too many aggregate values"
+    if metadata:
+        budget.metadata_items += 1
+        if budget.metadata_items > MAX_QUERY_RESULT_METADATA_ITEMS:
+            return "too many aggregate metadata values"
+    return None
+
+
+def _charge_text(
+    value: str,
+    budget: _ResultBudget,
+    *,
+    key: bool = False,
+    metadata: bool = False,
+) -> str | None:
+    maximum = (
+        MAX_RESULT_KEY_LENGTH
+        if key
+        else MAX_QUERY_RESULT_METADATA_BYTES
+        if metadata
+        else MAX_RESULT_STRING_LENGTH
+    )
+    size = _json_string_size(value, maximum)
+    if size is None:
+        return "an invalid or oversized object key" if key else "invalid text 
data"
+    return _charge_json_bytes(budget, size, metadata=metadata)
+
+
+def _integer_failure(value: int) -> str | None:
+    bits = int.bit_length(value)
+    if bits > MAX_RESULT_INTEGER_BITS:
+        return "an oversized integer"
+    digits = 1 if bits == 0 else ((bits - 1) * 30103) // 100000 + 1
+    if digits > MAX_RESULT_INTEGER_DIGITS:
+        return "an oversized integer"
+    return None
+
+
+def _decimal_failure(value: Decimal) -> str | None:
+    if Decimal.__sizeof__(value) > MAX_RESULT_DECIMAL_STORAGE:
+        return "an oversized Decimal"
+    if not Decimal.is_finite(value):
+        return "a non-finite Decimal"
+    parts = Decimal.as_tuple(value)
+    if tuple.__len__(parts.digits) > MAX_RESULT_DECIMAL_DIGITS:
+        return "an oversized Decimal"
+    exponent = parts.exponent
+    if type(exponent) is not int or abs(exponent) > 
MAX_RESULT_DECIMAL_MAGNITUDE:
+        return "an oversized Decimal"
+    return None
+
+
+def _type_mro(value_type: type[Any]) -> tuple[type[Any], ...]:
+    """Read a concrete type's MRO without consulting metaclass overrides."""
+    try:
+        mro = type.__getattribute__(value_type, "__mro__")
+    except (AttributeError, TypeError):  # pragma: no cover - defensive 
metaclass
+        return ()
+    return mro if type(mro) is tuple else ()
+
+
+def _timezone_name_without_hooks(tzinfo: Any) -> str | None:  # noqa: C901
+    """Read common pytz/dateutil zone state without dispatching timezone 
hooks."""
+    value_mro = _type_mro(type(tzinfo))
+    if any(base is pytz.tzinfo.BaseTzInfo for base in value_mro):
+        for base in value_mro:
+            try:
+                namespace = type.__getattribute__(base, "__dict__")
+            except (AttributeError, TypeError):  # pragma: no cover
+                continue
+            zone = namespace.get("zone")
+            if type(zone) is str and _bounded_utf8_length(zone, 256) is not 
None:
+                try:
+                    canonical = pytz.timezone(zone)
+                except (KeyError, ValueError):
+                    return None
+                # Generated pytz types are trusted; arbitrary subclasses that
+                # inherit their internal fields are not.
+                return zone if type(canonical) is type(tzinfo) else None
+
+    if type(tzinfo) not in _DATEUTIL_NAMED_TIMEZONE_TYPES:
+        return None
+
+    try:
+        namespace = object.__getattribute__(tzinfo, "__dict__")
+    except (AttributeError, TypeError):
+        return None
+    if type(namespace) is not dict:
+        return None
+    filename = dict.get(namespace, "_filename")
+    if type(filename) is not str or _bounded_utf8_length(filename, 4_096) is 
None:
+        return None
+    marker = "/zoneinfo/"
+    if (offset := str.find(filename, marker)) >= 0:
+        name = str.__getitem__(filename, slice(offset + len(marker), None))
+    elif not str.startswith(filename, "/") and str.find(filename, "\\") < 0:
+        name = filename
+    else:
+        return None
+    parts = str.split(name, "/")
+    if not parts or any(part in {"", ".", ".."} for part in parts):
+        return None
+    return name if _bounded_utf8_length(name, 256) is not None else None
+
+
+def _object_namespace(value: Any) -> dict[str, Any] | None:
+    """Read exact instance storage without descriptor dispatch."""
+    try:
+        namespace = object.__getattribute__(value, "__dict__")
+    except (AttributeError, TypeError):
+        return None
+    return namespace if type(namespace) is dict else None
+
+
+def _dateutil_ttinfo_offset_without_hooks(value: Any) -> int | None:
+    """Validate one exact dateutil transition record and return its offset."""
+    if type(value) is not _DATEUTIL_TTINFO_TYPE:
+        return None
+    try:
+        offset = object.__getattribute__(value, "offset")
+        delta = object.__getattribute__(value, "delta")
+        isdst = object.__getattribute__(value, "isdst")
+        abbreviation = object.__getattribute__(value, "abbr")
+        is_standard = object.__getattribute__(value, "isstd")
+        is_gmt = object.__getattribute__(value, "isgmt")
+        dst_offset = object.__getattribute__(value, "dstoffset")
+    except (AttributeError, TypeError):
+        return None
+    if type(offset) is not int or not -86_400 < offset < 86_400:
+        return None
+    if type(delta) is not timedelta or delta != timedelta(seconds=offset):
+        return None
+    if type(isdst) is not int or isdst not in {0, 1}:
+        return None
+    if abbreviation is not None and (
+        type(abbreviation) is not str or _bounded_utf8_length(abbreviation, 
256) is None
+    ):
+        return None
+    if type(is_standard) is not bool or type(is_gmt) is not bool:
+        return None
+    if type(dst_offset) is not timedelta:
+        return None
+    if not -timedelta(days=1) < dst_offset < timedelta(days=1):
+        return None
+    return offset
+
+
+def _dateutil_named_state_without_hooks(  # noqa: C901
+    tzinfo: Any,
+) -> _DateutilTimezoneState | None:
+    """Validate bounded exact dateutil tzfile state without timezone hooks."""
+    if type(tzinfo) not in _DATEUTIL_NAMED_TIMEZONE_TYPES:
+        return None
+    if _timezone_name_without_hooks(tzinfo) is None:
+        return None
+    namespace = _object_namespace(tzinfo)
+    if namespace is None:
+        return None
+    transitions = dict.get(namespace, "_trans_list")
+    utc_transitions = dict.get(namespace, "_trans_list_utc")
+    transition_info = dict.get(namespace, "_trans_idx")
+    info_list = dict.get(namespace, "_ttinfo_list")
+    standard_info = dict.get(namespace, "_ttinfo_std")
+    before_info = dict.get(namespace, "_ttinfo_before")
+    first_info = dict.get(namespace, "_ttinfo_first")
+    if (
+        type(transitions) is not tuple
+        or type(utc_transitions) is not tuple
+        or type(transition_info) is not tuple
+        or type(info_list) is not list
+        or tuple.__len__(transitions) > _MAX_DATEUTIL_TRANSITIONS
+        or tuple.__len__(utc_transitions) != tuple.__len__(transitions)
+        or tuple.__len__(transition_info) != tuple.__len__(transitions)
+        or list.__len__(info_list) == 0
+        or list.__len__(info_list) > _MAX_DATEUTIL_TTINFOS
+    ):
+        return None
+
+    previous_transition: int | None = None
+    previous_utc_transition: int | None = None
+    for index in range(tuple.__len__(transitions)):
+        transition = tuple.__getitem__(transitions, index)
+        utc_transition = tuple.__getitem__(utc_transitions, index)
+        if (
+            type(transition) is not int
+            or type(utc_transition) is not int
+            or abs(transition) > _MAX_DATEUTIL_TRANSITION_MAGNITUDE
+            or abs(utc_transition) > _MAX_DATEUTIL_TRANSITION_MAGNITUDE
+            or (previous_transition is not None and transition <= 
previous_transition)
+            or (
+                previous_utc_transition is not None
+                and utc_transition <= previous_utc_transition
+            )
+        ):
+            return None
+        previous_transition = transition
+        previous_utc_transition = utc_transition
+
+    known_info_ids: set[int] = set()
+    for index in range(list.__len__(info_list)):
+        info = list.__getitem__(info_list, index)
+        if _dateutil_ttinfo_offset_without_hooks(info) is None:
+            return None
+        known_info_ids.add(id(info))
+    for info in (standard_info, before_info, first_info):
+        if info is not None and id(info) not in known_info_ids:
+            return None
+    for index in range(tuple.__len__(transition_info)):
+        if id(tuple.__getitem__(transition_info, index)) not in known_info_ids:
+            return None
+    if not transitions:
+        if (
+            standard_info is not list.__getitem__(info_list, 0)
+            or first_info is not standard_info
+            or before_info is not None
+        ):
+            return None
+    else:
+        expected_standard = None
+        expected_dst = None
+        for index in range(tuple.__len__(transition_info) - 1, -1, -1):
+            info = tuple.__getitem__(transition_info, index)
+            is_dst = object.__getattribute__(info, "isdst")
+            if expected_standard is None and not is_dst:
+                expected_standard = info
+            elif expected_dst is None and is_dst:
+                expected_dst = info
+            if expected_standard is not None and expected_dst is not None:
+                break
+        if expected_standard is None:
+            expected_standard = expected_dst
+        expected_before = None
+        for index in range(list.__len__(info_list)):
+            info = list.__getitem__(info_list, index)
+            if not object.__getattribute__(info, "isdst"):
+                expected_before = info
+                break
+        if expected_before is None:
+            expected_before = list.__getitem__(info_list, 0)
+        if standard_info is not expected_standard or before_info is not 
expected_before:
+            return None
+    standard_offset = _dateutil_ttinfo_offset_without_hooks(standard_info)
+    if standard_offset is None:
+        return None
+    before_offset = (
+        _dateutil_ttinfo_offset_without_hooks(before_info)
+        if before_info is not None
+        else None
+    )
+    transition_offsets: list[int] = []
+    previous_offset: int | None = None
+    previous_base_offset: int | None = None
+    previous_is_dst: int | None = None
+    previous_dst_offset = 0
+    for index in range(tuple.__len__(transition_info)):
+        info = tuple.__getitem__(transition_info, index)
+        if id(info) not in known_info_ids:
+            return None
+        offset = _dateutil_ttinfo_offset_without_hooks(info)
+        if offset is None:
+            return None
+        is_dst = object.__getattribute__(info, "isdst")
+        dst_offset_seconds = 0
+        if previous_is_dst is not None and is_dst:
+            if not previous_is_dst:
+                assert previous_offset is not None
+                dst_offset_seconds = offset - previous_offset
+            if not dst_offset_seconds and previous_dst_offset:
+                dst_offset_seconds = previous_dst_offset
+            previous_dst_offset = dst_offset_seconds
+        base_offset = offset - dst_offset_seconds
+        adjustment = base_offset
+        if (
+            previous_base_offset is not None
+            and base_offset != previous_base_offset
+            and is_dst != previous_is_dst
+        ):
+            adjustment = previous_base_offset
+        if (
+            tuple.__getitem__(transitions, index)
+            != tuple.__getitem__(utc_transitions, index) + adjustment
+        ):
+            return None
+        transition_offsets.append(offset)
+        previous_offset = offset
+        previous_base_offset = base_offset
+        previous_is_dst = is_dst
+    if transitions and before_offset is None:
+        return None
+    return _DateutilTimezoneState(
+        transitions=transitions,
+        transition_offsets=tuple(transition_offsets),
+        standard_offset=standard_offset,
+        before_offset=before_offset,
+    )
+
+
+def _dateutil_named_offset_without_hooks(  # noqa: C901
+    value: datetime, tzinfo: Any
+) -> timezone | None:
+    """Preserve dateutil's source-selected wall offset from validated state."""
+    state = _dateutil_named_state_without_hooks(tzinfo)
+    if state is None:
+        return None
+    epoch_ordinal = date.toordinal(date(1970, 1, 1))
+    wall_timestamp = (
+        (datetime.toordinal(value) - epoch_ordinal) * 86_400
+        + value.hour * 3_600
+        + value.minute * 60
+        + value.second
+    )
+    transitions = state.transitions
+    selected_offset: int | None
+    if not transitions:
+        selected_offset = state.standard_offset
+    else:
+        index = bisect_right(transitions, wall_timestamp) - 1
+
+        def offset_at(transition_index: int | None) -> int | None:
+            if transition_index is None or transition_index + 1 >= 
len(transitions):
+                return state.standard_offset
+            if transition_index < 0:
+                return state.before_offset
+            return state.transition_offsets[transition_index]
+
+        if index > 0:
+            selected_offset = offset_at(index)
+            previous_offset = offset_at(index - 1)
+            if selected_offset is None or previous_offset is None:
+                return None
+            is_ambiguous = wall_timestamp < transitions[index] + (
+                previous_offset - selected_offset
+            )
+            if not value.fold and is_ambiguous:
+                index -= 1
+        selected_offset = offset_at(index)
+        if selected_offset is None:
+            return None
+    try:
+        return timezone(timedelta(seconds=selected_offset))
+    except (OverflowError, ValueError):
+        return None
+
+
+def _pytz_named_offset_without_hooks(tzinfo: Any) -> timezone | None:
+    """Return a localized pytz zone's stored offset without calling hooks."""
+    if _timezone_name_without_hooks(tzinfo) is None:
+        return None
+    namespace = _object_namespace(tzinfo)
+    offset = dict.get(namespace, "_utcoffset") if namespace is not None else 
None
+    if offset is None:
+        # Static pytz zones store their fixed offset on the generated class.
+        class_namespace = type.__getattribute__(type(tzinfo), "__dict__")
+        offset = class_namespace.get("_utcoffset")
+    if type(offset) is not timedelta:
+        return None
+    try:
+        return timezone(offset)
+    except ValueError:
+        return None
+
+
+def _dateutil_local_offset_without_hooks(
+    value: datetime, tzinfo: Any
+) -> timezone | None:
+    """Select a dateutil local offset using builtin system-time data."""
+    if type(tzinfo) is not _DATEUTIL_LOCAL_TIMEZONE_TYPE:
+        return None
+    namespace = _object_namespace(tzinfo)
+    if namespace is None:
+        return None
+    standard_offset = dict.get(namespace, "_std_offset")
+    daylight_offset = dict.get(namespace, "_dst_offset")
+    has_daylight = dict.get(namespace, "_hasdst")
+    if (
+        type(standard_offset) is not timedelta
+        or type(daylight_offset) is not timedelta
+        or type(has_daylight) is not bool
+    ):
+        return None
+    selected_offset = standard_offset
+    if has_daylight:
+        epoch = datetime(1970, 1, 1)
+        naive = datetime(
+            value.year,
+            value.month,
+            value.day,
+            value.hour,
+            value.minute,
+            value.second,
+            value.microsecond,
+        )
+        timestamp = (naive - epoch).total_seconds()
+        try:
+            is_daylight = bool(
+                system_time.localtime(timestamp + 
system_time.timezone).tm_isdst
+            )
+            daylight_saved = daylight_offset - standard_offset
+            previous_is_daylight = bool(
+                system_time.localtime(
+                    timestamp
+                    - timedelta.total_seconds(daylight_saved)
+                    + system_time.timezone
+                ).tm_isdst
+            )
+        except (OverflowError, OSError, ValueError):
+            return None
+        if not is_daylight and is_daylight != previous_is_daylight:
+            is_daylight = not bool(value.fold)
+        selected_offset = daylight_offset if is_daylight else standard_offset
+    try:
+        return timezone(selected_offset)
+    except ValueError:
+        return None
+
+
+def _canonical_timezone(tzinfo: Any) -> timezone | ZoneInfo | None:  # noqa: 
C901
+    if any(type(tzinfo) is trusted for trusted in _TRUSTED_TIMEZONE_TYPES):
+        return tzinfo
+    if tzinfo is pytz.UTC:
+        return timezone.utc
+    if type(tzinfo) in _DATEUTIL_FIXED_TIMEZONE_TYPES:
+        try:
+            namespace = object.__getattribute__(tzinfo, "__dict__")
+        except (AttributeError, TypeError):
+            return timezone.utc if type(tzinfo) is type(dateutil_tz.tzutc()) 
else None
+        if type(namespace) is not dict:
+            return None
+        offset = dict.get(namespace, "_offset")
+        if type(offset) is not timedelta:
+            return timezone.utc if type(tzinfo) is type(dateutil_tz.tzutc()) 
else None
+        if abs(offset) >= timedelta(days=1):
+            return None
+        return timezone(offset)
+    if type(tzinfo) in _PYTZ_FIXED_TIMEZONE_TYPES:
+        try:
+            namespace = object.__getattribute__(tzinfo, "__dict__")
+        except (AttributeError, TypeError):
+            return None
+        if type(namespace) is not dict:
+            return None
+        minutes = dict.get(namespace, "_minutes")
+        if type(minutes) is not int or not -1_440 < minutes < 1_440:
+            return None
+        return timezone(timedelta(minutes=minutes))
+    return None
+
+
+def _timestamp_offset_without_hooks(value: pd.Timestamp) -> timezone | None:
+    """Recover a timestamp's stored wall-clock offset without timezone 
hooks."""
+    multipliers = {"s": 1_000_000_000, "ms": 1_000_000, "us": 1_000, "ns": 1}
+    multiplier = multipliers.get(value.unit)
+    if multiplier is None:
+        return None
+    try:
+        instant_ns = int(value.asm8.view("i8")) * multiplier
+        epoch_ordinal = date.toordinal(date(1970, 1, 1))
+        wall_ns = (
+            (
+                (datetime.toordinal(value) - epoch_ordinal) * 86_400
+                + value.hour * 3600
+                + value.minute * 60
+                + value.second
+            )
+            * 1_000_000_000
+            + value.microsecond * 1000
+            + value.nanosecond
+        )
+        offset_ns = wall_ns - instant_ns
+        if offset_ns % 1000:
+            return None
+        return timezone(timedelta(microseconds=offset_ns // 1000))
+    except (OverflowError, TypeError, ValueError):
+        return None
+
+
+def _canonical_datetime(value: datetime) -> tuple[str | None, str | None]:
+    """Serialize an exact datetime through trusted timezone state only."""
+    tzinfo = value.tzinfo
+    canonical_value = value
+    if tzinfo is not None and not any(
+        type(tzinfo) is trusted for trusted in _TRUSTED_TIMEZONE_TYPES
+    ):
+        canonical_tz = (
+            _pytz_named_offset_without_hooks(tzinfo)
+            or _dateutil_local_offset_without_hooks(value, tzinfo)
+            or _dateutil_named_offset_without_hooks(value, tzinfo)
+            or _canonical_timezone(tzinfo)
+        )
+        if canonical_tz is None:
+            return None, "a datetime with an unsupported timezone"
+        canonical_value = datetime(
+            value.year,
+            value.month,
+            value.day,
+            value.hour,
+            value.minute,
+            value.second,
+            value.microsecond,
+            tzinfo=canonical_tz,
+            fold=value.fold,
+        )
+    try:
+        return datetime.isoformat(canonical_value), None
+    except (OverflowError, TypeError, ValueError):
+        return None, "an invalid datetime"
+
+
+def _canonical_time(value: time) -> tuple[str | None, str | None]:
+    """Serialize an exact time through trusted timezone state only."""
+    tzinfo = value.tzinfo
+    canonical_value = value
+    if tzinfo is not None and not any(
+        type(tzinfo) is trusted for trusted in _TRUSTED_TIMEZONE_TYPES
+    ):
+        if _dateutil_named_state_without_hooks(tzinfo) is not None:
+            canonical_value = time(
+                value.hour,
+                value.minute,
+                value.second,
+                value.microsecond,
+                fold=value.fold,
+            )
+        else:
+            canonical_tz = _canonical_timezone(tzinfo)
+            if canonical_tz is None and type(tzinfo) is 
_DATEUTIL_LOCAL_TIMEZONE_TYPE:
+                namespace = _object_namespace(tzinfo)
+                if namespace is not None and dict.get(namespace, "_hasdst") is 
False:
+                    offset = dict.get(namespace, "_std_offset")
+                    if type(offset) is timedelta:
+                        try:
+                            canonical_tz = timezone(offset)
+                        except ValueError:
+                            canonical_tz = None
+            if canonical_tz is None:
+                return None, "a time with an unsupported timezone"
+            canonical_value = time(
+                value.hour,
+                value.minute,
+                value.second,
+                value.microsecond,
+                tzinfo=canonical_tz,
+                fold=value.fold,
+            )
+    try:
+        return time.isoformat(canonical_value), None
+    except (OverflowError, TypeError, ValueError):
+        return None, "an invalid time"
+
+
+def _canonical_timestamp(value: pd.Timestamp) -> tuple[str | None, str | None]:
+    """Preserve a trusted timestamp's instant, offset, nanoseconds, and 
fold."""
+    try:
+        tzinfo = value.tzinfo
+        if tzinfo is not None and not any(
+            type(tzinfo) is trusted for trusted in _TRUSTED_TIMEZONE_TYPES
+        ):
+            supported_timezone = (
+                _canonical_timezone(tzinfo) is not None
+                or _pytz_named_offset_without_hooks(tzinfo) is not None
+                or type(tzinfo) is _DATEUTIL_LOCAL_TIMEZONE_TYPE
+                or _dateutil_named_state_without_hooks(tzinfo) is not None
+            )
+            if not supported_timezone:
+                return None, "a timestamp with an unsupported timezone"
+            canonical_tz = _timestamp_offset_without_hooks(value)
+            if canonical_tz is None:
+                return None, "an invalid timestamp"
+            raw_value = value.asm8.view("i8")
+            value = pd.Timestamp(raw_value, unit=value.unit, 
tz="UTC").tz_convert(
+                canonical_tz
+            )
+        return pd.Timestamp.isoformat(value), None
+    except (KeyError, OverflowError, TypeError, ValueError):
+        return None, "an invalid timestamp"
+
+
+def _normalize_scalar(value: Any) -> tuple[Any, str | None]:  # noqa: C901
+    """Convert one exact trusted producer scalar to a JSON-safe scalar."""
+    value_type = type(value)
+    if value is None or value_type is bool or value_type is str:
+        return value, None
+    if value_type is int:
+        return value, _integer_failure(value)
+    if value_type is float:
+        if math.isnan(value):
+            return None, None
+        return (value, None) if math.isfinite(value) else (None, "a non-finite 
number")
+    if value_type is Decimal:
+        return value, _decimal_failure(value)
+    if value_type is datetime:
+        return _canonical_datetime(value)
+    if value_type is time:
+        return _canonical_time(value)
+    if value_type is date:
+        return date.isoformat(value), None
+    if value_type is timedelta:
+        try:
+            return pd.Timedelta(value).isoformat(), None
+        except (OverflowError, TypeError, ValueError):
+            return None, "an invalid duration"
+    if value_type is UUID:
+        return UUID.__str__(value), None
+
+    if value_type is _PANDAS_NAT_TYPE or value_type is _PANDAS_NA_TYPE:
+        return None, None
+    if value_type is pd.Timestamp:
+        return _canonical_timestamp(value)
+    if value_type is pd.Timedelta:
+        if pd.isna(value):
+            return None, None
+        try:
+            return pd.Timedelta.isoformat(value), None
+        except (OverflowError, TypeError, ValueError):
+            return None, "an invalid pandas duration"
+    if value_type is _PANDAS_PERIOD_TYPE or value_type is 
_PANDAS_INTERVAL_TYPE:
+        # These concrete immutable pandas extension scalars are trusted. Exact
+        # type checks deliberately exclude subclasses with conversion hooks.
+        try:
+            normalized_text = str(value)
+        except (OverflowError, TypeError, ValueError):
+            return None, "an invalid pandas scalar"
+        if _bounded_utf8_length(normalized_text, MAX_RESULT_STRING_LENGTH) is 
None:
+            return None, "an oversized pandas scalar"
+        return normalized_text, None
+    if value_type in _NUMPY_INTEGER_TYPES:
+        normalized = int(value)
+        return normalized, _integer_failure(normalized)
+    if value_type in _NUMPY_FLOAT_TYPES:
+        normalized_float = float(value)
+        if math.isnan(normalized_float):
+            return None, None
+        return (
+            (normalized_float, None)
+            if math.isfinite(normalized_float)
+            else (None, "a non-finite NumPy number")
+        )
+    if value_type is np.bool_:
+        return bool(value), None
+    if value_type is np.str_:
+        return str(value), None
+    if value_type is np.datetime64:
+        if np.isnat(value):
+            return None, None
+        try:
+            return _canonical_timestamp(pd.Timestamp(value))
+        except (OverflowError, TypeError, ValueError):
+            return None, "an invalid NumPy timestamp"
+    if value_type is np.timedelta64:
+        if np.isnat(value):
+            return None, None
+        try:
+            return pd.Timedelta(value).isoformat(), None
+        except (OverflowError, TypeError, ValueError):
+            return None, "an invalid NumPy duration"
+    return None, "an unsupported or subclassed value"
+
+
+def _normalize_value(  # noqa: C901
+    value: Any,
+    budget: _ResultBudget,
+    *,
+    enum_types: frozenset[type[Any]] = frozenset(),
+    metadata: bool = False,
+) -> tuple[Any, str | None]:
+    """Iteratively normalize one bounded exact-container value tree."""
+    stack: list[
+        tuple[Any, list[Any] | dict[str, Any] | None, int | str | None, int, 
bool]
+    ] = [(value, None, None, 0, False)]
+    active_containers: set[int] = set()
+    root = value
+
+    while stack:
+        item, parent, slot, depth, leaving = stack.pop()
+        if leaving:
+            active_containers.remove(id(item))
+            continue
+        if depth > MAX_RESULT_VALUE_DEPTH:
+            return None, "excessively nested data"
+        if reason := _charge_value(budget, metadata=metadata):
+            return None, reason
+
+        if type(item) is list:
+            identity = id(item)
+            if identity in active_containers:
+                return None, "cyclic containers"
+            active_containers.add(identity)
+            width = list.__len__(item)
+            if width > MAX_RESULT_VALUE_ITEMS:
+                return None, "an oversized array"
+            if reason := _charge_json_bytes(
+                budget,
+                _container_json_syntax_size(width, mapping=False),
+                metadata=metadata,
+            ):
+                return None, reason
+            stack.append((item, None, None, depth, True))
+            stack.extend(
+                (list.__getitem__(item, index), item, index, depth + 1, False)
+                for index in range(width - 1, -1, -1)
+            )
+            continue
+
+        if type(item) is dict:
+            identity = id(item)
+            if identity in active_containers:
+                return None, "cyclic containers"
+            active_containers.add(identity)
+            width = dict.__len__(item)
+            if width > MAX_RESULT_VALUE_ITEMS:
+                return None, "an oversized object"
+            if reason := _charge_json_bytes(
+                budget,
+                _container_json_syntax_size(width, mapping=True),
+                metadata=metadata,
+            ):
+                return None, reason
+            children: list[tuple[Any, dict[str, Any], str, int, bool]] = []
+            for key, child in dict.items(item):
+                if type(key) is not str:
+                    return None, "a non-string object key"
+                if reason := _charge_text(key, budget, key=True, 
metadata=metadata):
+                    return None, reason
+                children.append((child, item, key, depth + 1, False))
+            stack.append((item, None, None, depth, True))
+            stack.extend(reversed(children))
+            continue
+
+        source_item = item
+        if type(item) in enum_types:
+            normalized = _safe_enum_value(item, enum_types)
+            if normalized is None:
+                return None, "an unsupported enum"
+            item = normalized
+        elif any(base is Enum for base in _type_mro(type(item))):
+            return None, "an enum outside its expected metadata slot"
+
+        normalized, reason = _normalize_scalar(item)
+        if reason is not None:
+            return None, reason
+        max_string_bytes = (
+            MAX_QUERY_RESULT_METADATA_BYTES if metadata else 
MAX_RESULT_STRING_LENGTH
+        )
+        if type(normalized) is str:
+            scalar_size = _json_string_size(normalized, max_string_bytes)
+            if scalar_size is None:
+                return None, "invalid text data"
+        else:
+            scalar_size = _normalized_scalar_json_size(normalized)
+        if reason := _charge_json_bytes(
+            budget,
+            scalar_size,
+            metadata=metadata,
+        ):
+            return None, reason
+        if parent is None:
+            root = normalized
+        elif normalized is not source_item:
+            if type(parent) is list:
+                assert type(slot) is int
+                list.__setitem__(parent, slot, normalized)
+            else:
+                assert type(parent) is dict
+                assert type(slot) is str
+                dict.__setitem__(parent, slot, normalized)
+    return root, None
+
+
+def _normalize_metadata_value(
+    payload: dict[str, Any], key: str, budget: _ResultBudget
+) -> str | None:
+    enum_slots: dict[str, frozenset[type[Any]]] = {
+        "status": frozenset({QueryStatus}),
+        "result_format": frozenset({ChartDataResultFormat}),
+        "coltypes": frozenset({GenericDataType}),
+        "applied_filters": frozenset({ExtraFiltersTimeColumnType}),
+        "rejected_filters": frozenset(
+            {ExtraFiltersReasonType, ExtraFiltersTimeColumnType}
+        ),
+    }
+    value = dict.__getitem__(payload, key)
+    normalized, reason = _normalize_value(
+        value,
+        budget,
+        enum_types=enum_slots.get(key, frozenset()),
+        metadata=True,
+    )
+    if reason is None and normalized is not value:
+        dict.__setitem__(payload, key, normalized)
+    return reason
+
+
+def _error_text(value: Any) -> str | None:
+    """Extract a bounded error from an already validated primitive tree."""
+    stack: list[Any] = [value]
+    parts: list[str] = []
+    used = 0
+    while stack and len(parts) < 3 and used < _MAX_ERROR_TEXT_BYTES:
+        item = stack.pop()
+        if type(item) is dict:
+            stack.extend(
+                reversed(
+                    [dict.__getitem__(item, key) for key in _ERROR_KEYS if key 
in item]
+                )
+            )
+            continue
+        if type(item) is list:
+            stack.extend(
+                list.__getitem__(item, index)
+                for index in range(list.__len__(item) - 1, -1, -1)
+            )
+            continue
+        if item is None or item is False:
+            continue
+        if type(item) is str:
+            remaining = _MAX_ERROR_TEXT_BYTES - used - (2 if parts else 0)
+            encoded = str.encode(item, "utf-8")[:remaining]
+            text = bytes.decode(encoded, "utf-8", errors="ignore")
+            if text:
+                parts.append(text)
+                used += bytes.__len__(encoded) + (2 if len(parts) > 1 else 0)
+    return "; ".join(parts) or None
+
+
+def _failure_for_payload(payload: dict[str, Any], label: str) -> ChartError | 
None:
     for key in ("error", "errors", "error_message"):
-        if message := _query_error_text(payload.get(key)):
+        if key in payload and (message := 
_error_text(dict.__getitem__(payload, key))):
             return ChartError(
                 error=f"{label} failed: {message}", error_type="QueryError"
             )
 
-    raw_status = payload.get("status")
-    status = str(getattr(raw_status, "value", raw_status) or "")
+    raw_status = dict.get(payload, "status")
+    status = raw_status if type(raw_status) is str else ""
     normalized_status = status.strip().casefold().replace("-", "_").replace(" 
", "_")
     if normalized_status in FAILED_QUERY_STATUSES:
         message = (
-            _query_error_text(payload.get("message"))
-            or _query_error_text(payload.get("error_message"))
+            _error_text(dict.get(payload, "message"))
+            or _error_text(dict.get(payload, "error_message"))
             or normalized_status
         )
         return ChartError(error=f"{label} failed: {message}", 
error_type="QueryError")
-    if payload.get("success") is False:
-        message = _query_error_text(payload.get("message")) or "request failed"
+    if dict.get(payload, "success") is False:
+        message = _error_text(dict.get(payload, "message")) or "request failed"
         return ChartError(error=f"{label} failed: {message}", 
error_type="QueryError")
+    if raw_status is None and "data" not in payload and "queries" not in 
payload:
+        if message := _error_text(dict.get(payload, "message")):
+            return ChartError(
+                error=f"{label} failed: {message}", error_type="QueryError"
+            )
+    return None
+
+
+def _metadata_shape_error(  # noqa: C901
+    payload: dict[str, Any], label: str, budget: _ResultBudget
+) -> ChartError | None:
+    if "success" in payload and type(dict.__getitem__(payload, "success")) is 
not bool:
+        return _invalid_metadata(label)
+    if "status" in payload and type(dict.__getitem__(payload, "status")) is 
not str:
+        return _invalid_metadata(label)
     if (
-        raw_status is None
-        and "data" not in payload
-        and "queries" not in payload
-        and (message := _query_error_text(payload.get("message")))
+        "result_format" in payload
+        and dict.__getitem__(payload, "result_format") not in 
_RESULT_FORMAT_VALUES
     ):
-        return ChartError(error=f"{label} failed: {message}", 
error_type="QueryError")
+        return _invalid_metadata(label)
+    for count_key in ("rowcount", "sql_rowcount", "total_rows"):
+        if count_key in payload:
+            count = dict.__getitem__(payload, count_key)
+            if count is not None and not (
+                type(count) is int and 0 <= count <= MAX_QUERY_RESULT_ROWCOUNT
+            ):
+                return _invalid_metadata(label)
+    if "is_cached" in payload:
+        cached = dict.__getitem__(payload, "is_cached")
+        if cached is None:
+            if reason := _charge_json_bytes(
+                budget, 1, metadata=True
+            ):  # ``false`` vs ``null``
+                return _invalid_result(reason)
+            dict.__setitem__(payload, "is_cached", False)
+        elif type(cached) is not bool:
+            return _invalid_metadata(label)
+    if "cache_timeout" in payload:
+        timeout = dict.__getitem__(payload, "cache_timeout")
+        if timeout is not None and not (
+            type(timeout) is int
+            and (
+                timeout == CACHE_DISABLED_TIMEOUT
+                or 0 <= timeout <= MAX_QUERY_RESULT_CACHE_TIMEOUT
+            )
+        ):
+            return _invalid_metadata(label)
+    if "cache_key" in payload:
+        cache_key = dict.__getitem__(payload, "cache_key")
+        if cache_key is not None and (type(cache_key) is not str or not 
cache_key):
+            return _invalid_metadata(label)
+    for timestamp_key in ("cached_dttm", "cache_dttm", "queried_dttm"):
+        if timestamp_key not in payload:
+            continue
+        timestamp = dict.__getitem__(payload, timestamp_key)
+        if timestamp is None:
+            continue
+        if (
+            type(timestamp) is not str
+            or not timestamp
+            or len(timestamp) > MAX_QUERY_RESULT_TIMESTAMP_LENGTH
+        ):
+            return _invalid_metadata(label)
+        normalized = f"{timestamp[:-1]}+00:00" if timestamp.endswith("Z") else 
timestamp
+        try:
+            parsed = datetime.fromisoformat(normalized)
+        except ValueError:
+            return _invalid_metadata(label)
+        if parsed.tzinfo is None or parsed.utcoffset() != timedelta(0):

Review Comment:
   Addressed: naive cache timestamps are interpreted as UTC and canonicalized, 
while non-UTC aware timestamps remain rejected. The cache metadata regression 
now covers a naive `cache_dttm`; MyPy and Ruff pass.



##########
superset/mcp_service/chart/tool/get_chart_data.py:
##########
@@ -1115,56 +1224,22 @@ async def _query_from_form_data(  # noqa: C901
                 error_type="NoData",
             )
 
-        columns = []
-        for col_name in raw_columns:
-            sample_values = [
-                row.get(col_name) for row in data[:3] if row.get(col_name) is 
not None
-            ]
-            data_type = "string"
-            if sample_values and all(
-                isinstance(v, (int, float)) for v in sample_values
-            ):
-                data_type = "numeric"
-            columns.append(
-                DataColumn(
-                    name=col_name,
-                    display_name=col_name.replace("_", " ").title(),
-                    data_type=data_type,
-                    sample_values=sample_values[:3],
-                    null_count=sum(1 for row in data if row.get(col_name) is 
None),
-                    unique_count=len({str(row.get(col_name)) for row in data}),
-                )
-            )
+        columns = _build_data_columns(
+            data, raw_columns, query_result.get("coltypes", [])
+        )
 
         cache_status = get_cache_status_from_result(
             query_result, force_refresh=request.force_refresh
         )
 
         chart_name = form_data.get("slice_name", "Unsaved chart")
-        if request.format in {"csv", "excel"}:
-            from superset.models.slice import Slice
-
-            # A transient chart supplies export metadata without saving 
anything.
-            chart = Slice(id=0, slice_name=chart_name, viz_type=viz_type)
-            export = (
-                _export_data_as_csv
-                if request.format == "csv"
-                else _export_data_as_excel
-            )
-            return export(
-                chart,
-                data[: request.limit] if request.limit else data,
-                raw_columns,
-                cache_status,
-                PerformanceMetadata(query_duration_ms=0, 
cache_status="fresh_query"),
-            )
         summary = (
             f"Unsaved chart ({viz_type}). "
             f"Contains {len(data)} rows across {len(raw_columns)} columns."
         )
 
         await ctx.report_progress(4, 4, "Building response")
-        return ChartData(
+        response = ChartData(

Review Comment:
   Addressed: the unsaved form-data path now branches to the existing CSV/Excel 
exporters before constructing JSON ChartData. The unsaved Decimal matrix now 
includes CSV and verifies the export payload; MyPy and Ruff pass.



##########
superset/mcp_service/app.py:
##########
@@ -395,8 +395,8 @@ def get_default_instructions(
 - chart_type="table": Data table for detailed views
 - chart_type="table", viz_type="ag-grid-table": Interactive AG Grid table
 - chart_type="pie": Pie chart for proportional data (set donut=True for donut)
-- chart_type="gauge": Gauge/dial for one numeric metric, optionally grouped
-  into up to 10 dials (native viz_type is "gauge_chart")
+- chart_type="sunburst": Hierarchical part-to-whole chart (hierarchy + metric

Review Comment:
   Addressed: Gauge remains listed alongside Sunburst in the MCP capability 
catalog; its existing registry/display-name behavior is preserved. 
Gauge/Sunburst shared tests remain in the exact-head unit suite.



##########
superset/mcp_service/chart/chart_utils.py:
##########
@@ -1249,6 +1336,659 @@ def map_gauge_config(config: GaugeChartConfig) -> 
Dict[str, Any]:
     return form_data
 
 
+def map_sunburst_config(config: SunburstChartConfig) -> Dict[str, Any]:
+    """Map typed Sunburst config to the ECharts ``sunburst_v2`` form_data.
+
+    The frontend control panel stores hierarchy levels under ``columns`` and
+    metrics under singular ``metric`` / ``secondary_metric`` keys.  Its
+    buildQuery adds primary-metric descending ordering when ``sort_by_metric``
+    is enabled; server-side query builders mirror that transform separately.
+    """
+    form_data: Dict[str, Any] = {
+        "viz_type": "sunburst_v2",
+        "columns": [dimension.name for dimension in config.hierarchy],
+        "metric": create_metric_object(config.metric),
+        "sort_by_metric": config.sort_by_metric,
+        "row_limit": config.row_limit,
+        "show_labels": config.show_labels,
+        "show_labels_threshold": config.show_labels_threshold,
+        "show_total": config.show_total,
+        "show_null_values": config.show_null_values,
+        "label_type": config.label_type,
+        "number_format": config.number_format,
+        "date_format": config.date_format,
+    }
+    if config.secondary_metric is not None:
+        form_data["secondary_metric"] = 
create_metric_object(config.secondary_metric)
+    if config.color_scheme is not None:
+        form_data["color_scheme"] = config.color_scheme
+    if config.linear_color_scheme is not None:
+        form_data["linear_color_scheme"] = config.linear_color_scheme
+    if config.time_range is not None:
+        form_data["time_range"] = config.time_range
+    if config.temporal_column is not None:
+        form_data["granularity_sqla"] = config.temporal_column
+    if config.time_grain is not None:
+        form_data["time_grain_sqla"] = config.time_grain
+
+    _copy_sunburst_native_envelope(form_data, config)
+
+    add_currency_format(form_data, config.currency_format)
+    _add_adhoc_filters(form_data, config.filters)
+    return form_data
+
+
+# Sunburst fields with explicit omission/clear semantics. Mapper defaults must
+# not overwrite same-viz state when the typed field was omitted, while explicit
+# clears must also beat the shared preservation registry on cross-viz updates.
+# Required query roles (hierarchy and metric) are deliberately absent: a full
+# replacement always updates them.
+_SUNBURST_UPDATE_FIELD_KEYS: dict[str, str] = {
+    "time_range": "time_range",
+    "time_grain": "time_grain_sqla",
+    "temporal_column": "granularity_sqla",
+    "sort_by_metric": "sort_by_metric",
+    "row_limit": "row_limit",
+    "color_scheme": "color_scheme",
+    "linear_color_scheme": "linear_color_scheme",
+    "show_labels": "show_labels",
+    "show_labels_threshold": "show_labels_threshold",
+    "show_total": "show_total",
+    "show_null_values": "show_null_values",
+    "label_type": "label_type",
+    "number_format": "number_format",
+    "date_format": "date_format",
+    "currency_format": "currency_format",
+    "extra_form_data": "extra_form_data",
+    "url_params": "url_params",
+    "standardized_form_data": "standardizedFormData",
+}
+
+
+# Presentation controls emitted sparsely by chart mappers need three-way update
+# semantics: omitted preserves saved native state, an explicit value replaces
+# it, and explicit ``None``/``False`` clears a truthy saved value when the 
mapper
+# has no canonical false/null representation.  Query roles are intentionally
+# absent: a replacement config always owns those through the plugin contract.
+# Paths below also cover nested axis/legend models so an omitted nested 
property
+# is not mistaken for an explicit clear of the whole control.
+_MODELED_UPDATE_CONTROL_PATHS: dict[str, dict[str, tuple[tuple[str, ...], 
...]]] = {
+    "PieChartConfig": {
+        "color_scheme": (("color_scheme",),),
+        "show_labels": (("show_labels",),),
+        "show_legend": (("show_legend",),),
+        "legendOrientation": (("legend_orientation",),),
+        "label_type": (("label_type",),),
+        "number_format": (("number_format",),),
+        "date_format": (("date_format",),),
+        "sort_by_metric": (("sort_by_metric",),),
+        "row_limit": (("row_limit",),),
+        "donut": (("donut",),),
+        "show_total": (("show_total",),),
+        "labels_outside": (("labels_outside",),),
+        "outerRadius": (("outer_radius",),),
+        "innerRadius": (("inner_radius",),),
+        "currency_format": (("currency_format",),),
+    },
+    "TableChartConfig": {
+        "row_limit": (("row_limit",),),
+        "color_scheme": (("color_scheme",),),
+        "column_config": (("column_config",),),
+    },
+    "XYChartConfig": {
+        "row_limit": (("row_limit",),),
+        "series_limit": (("series_limit",),),
+        "stack": (("stacked",),),
+        "orientation": (("orientation",),),
+        "x_axis_title": (("x_axis", "title"),),
+        "x_axis_format": (("x_axis", "format"),),
+        "y_axis_title": (("y_axis", "title"),),
+        "y_axis_format": (("y_axis", "format"),),
+        "y_axis_scale": (("y_axis", "scale"),),
+        "show_legend": (("legend", "show"),),
+        "legendOrientation": (("legend", "position"), ("legend_orientation",)),
+        "x_axis_time_format": (("x_axis_time_format",),),
+        "show_value": (("show_value",),),
+        "currency_format": (("currency_format",),),
+        "color_scheme": (("color_scheme",),),
+    },
+    "HistogramChartConfig": {
+        "bins": (("bins",),),
+        "normalize": (("normalize",),),
+        "cumulative": (("cumulative",),),
+        "row_limit": (("row_limit",),),
+    },
+    "BoxPlotChartConfig": {
+        "whiskerOptions": (
+            ("whisker_type",),
+            ("percentile_low",),
+            ("percentile_high",),
+        ),
+        "row_limit": (("row_limit",),),
+        "number_format": (("number_format",),),
+        "date_format": (("date_format",),),
+    },
+    "WaterfallChartConfig": {
+        "show_total": (("show_total",),),
+        "show_legend": (("show_legend",),),
+        "increase_label": (("increase_label",),),
+        "decrease_label": (("decrease_label",),),
+        "total_label": (("total_label",),),
+        "x_axis_time_format": (("x_axis_time_format",),),
+        "y_axis_format": (("y_axis_format",),),
+        "currency_format": (("currency_format",),),
+        "row_limit": (("row_limit",),),
+    },
+    "BigNumberChartConfig": {
+        "subheader": (("subheader",),),
+        "y_axis_format": (("y_axis_format",),),
+        "time_format": (("time_format",),),
+        "currency_format": (("currency_format",),),
+        "color_scheme": (("color_scheme",),),
+        "start_y_axis_at_zero": (("start_y_axis_at_zero",),),
+        "compare_lag": (("compare_lag",),),
+        "aggregation": (("aggregation",),),
+    },
+    "HandlebarsChartConfig": {
+        "row_limit": (("row_limit",),),
+        "order_desc": (("order_desc",),),
+        "styleTemplate": (("style_template",),),
+    },
+    "PivotTableChartConfig": {
+        "aggregateFunction": (("aggregate_function",),),
+        "rowTotals": (("show_row_totals",),),
+        "colTotals": (("show_column_totals",),),
+        "transposePivot": (("transpose",),),
+        "combineMetric": (("combine_metric",),),
+        "valueFormat": (("value_format",),),
+        "date_format": (("date_format",),),
+        "currency_format": (("currency_format",),),
+        "row_limit": (("row_limit",),),
+    },
+    "InteractivePivotChartConfig": {
+        "order_desc": (("sort_descending",),),
+        "row_limit": (("row_limit",),),
+        "rowGroupCounts": (("show_row_group_counts",),),
+        "rowTotals": (("show_row_totals",),),
+        "colTotals": (("show_column_totals",),),
+        "colSubTotals": (("show_column_subtotals",),),
+        "valueFormat": (("value_format",),),
+        "date_format": (("date_format",),),
+        "currency_format": (("currency_format",),),
+        "colOrder": (("column_sort",),),
+        "allow_render_html": (("allow_render_html",),),
+        "expand_pivot_groups": (("expand_pivot_groups",),),
+        "time_compare": (("comparison_period",),),
+        "comparison_type": (("comparison_type",),),
+    },
+    "MixedTimeseriesChartConfig": {
+        "seriesType": (("primary_kind",),),
+        "area": (("primary_kind",),),
+        "seriesTypeB": (("secondary_kind",),),
+        "areaB": (("secondary_kind",),),
+        "show_legend": (("show_legend",),),
+        "legendOrientation": (("legend_orientation",),),
+        "show_value": (("show_value",),),
+        "color_scheme": (("color_scheme",),),
+        "currency_format": (("currency_format",),),
+        "currency_format_secondary": (("currency_format_secondary",),),
+        "xAxisTitle": (("x_axis", "title"),),
+        "x_axis_time_format": (("x_axis", "format"),),
+        "yAxisTitle": (("y_axis", "title"),),
+        "y_axis_format": (("y_axis", "format"),),
+        "logAxis": (("y_axis", "scale"),),
+        "yAxisTitleSecondary": (("y_axis_secondary", "title"),),
+        "y_axis_format_secondary": (("y_axis_secondary", "format"),),
+        "logAxisSecondary": (("y_axis_secondary", "scale"),),
+        "row_limit": (("row_limit",),),
+    },
+}
+
+
+def _model_path_was_set(config: Any, path: tuple[str, ...]) -> bool:
+    """Return whether every component of a Pydantic model path was supplied."""
+    current = config
+    for field_name in path:
+        if field_name not in getattr(current, "model_fields_set", set()):
+            return False
+        current = getattr(current, field_name, None)
+        if current is None:
+            # An explicit null parent clears all of its mapped descendants.
+            return True
+    return True
+
+
+def _apply_modeled_update_semantics(
+    existing_form_data: Mapping[str, Any],
+    new_form_data: Dict[str, Any],
+    config: Any,
+) -> set[str]:
+    """Preserve truly omitted modeled controls and return explicit clears."""
+    explicit_clears: set[str] = set()
+    controls = _MODELED_UPDATE_CONTROL_PATHS.get(type(config).__name__, {})
+    for form_key, paths in controls.items():
+        if any(_model_path_was_set(config, path) for path in paths):
+            if form_key not in new_form_data:
+                explicit_clears.add(form_key)
+            continue
+        if form_key in existing_form_data:
+            new_form_data[form_key] = existing_form_data[form_key]
+        else:
+            new_form_data.pop(form_key, None)
+    return explicit_clears
+
+
+_TEMPORAL_FORM_DATA_KEYS = frozenset(
+    {
+        "granularity",
+        "granularity_sqla",
+        "since",
+        "time_grain",
+        "time_grain_sqla",
+        "time_range",
+        "until",
+    }
+)
+
+
+def _is_temporal_filter(filter_: Any) -> bool:
+    """Return whether a native, adhoc, or legacy filter carries a time 
range."""
+    return isinstance(filter_, dict) and (
+        filter_.get("operator") == FilterOperator.TEMPORAL_RANGE.value
+        or filter_.get("op") == FilterOperator.TEMPORAL_RANGE.value
+        or filter_.get("col") in {"__time_col", "__time_grain", "__time_range"}
+    )
+
+
+def _without_temporal_filters(value: Any) -> Any:
+    """Copy a filter list without temporal predicates, preserving other 
shapes."""
+    if not isinstance(value, list):
+        return value
+    return [filter_ for filter_ in value if not _is_temporal_filter(filter_)]
+
+
+def _scrub_temporal_form_data(form_data: Mapping[str, Any]) -> Dict[str, Any]:
+    """Remove every source capable of reconstructing explicitly cleared time 
state."""
+    scrubbed = dict(form_data)
+    for key in _TEMPORAL_FORM_DATA_KEYS:
+        scrubbed.pop(key, None)
+    scrubbed.pop(MCP_DASHBOARD_TIME_FILTER_SUBJECT, None)
+
+    for key in ("adhoc_filters", "extra_filters", "filters"):
+        if key in scrubbed:
+            scrubbed[key] = _without_temporal_filters(scrubbed[key])
+
+    extra_form_data = scrubbed.get("extra_form_data")
+    if isinstance(extra_form_data, dict):
+        cleaned_extra = dict(extra_form_data)
+        for key in _TEMPORAL_FORM_DATA_KEYS:
+            cleaned_extra.pop(key, None)
+        for key in ("adhoc_filters", "extra_filters", "filters"):
+            if key in cleaned_extra:
+                cleaned_extra[key] = 
_without_temporal_filters(cleaned_extra[key])
+        scrubbed["extra_form_data"] = cleaned_extra
+    elif extra_form_data is None:
+        scrubbed.pop("extra_form_data", None)
+    return scrubbed
+
+
+# One bounded registry owns state that may survive a form-data replacement.
+# Query roles and plugin-specific controls are deliberately absent. This keeps
+# cross-viz transitions preview/save-safe without chart-by-chart allowlists 
that
+# can drift as new plugins are registered.
+FORM_DATA_UPDATE_PRESERVE_KEYS: dict[str, frozenset[str]] = {
+    "envelope": frozenset(
+        {
+            "dashboardId",
+            "dashboards",
+            "datasource",
+            "extra_form_data",
+            "slice_id",
+            "slice_name",
+            "standardizedFormData",
+            "url_params",
+        }
+    ),
+    "presentation": frozenset(
+        {
+            "color_scheme",
+            "currency_format",
+            "date_format",
+            "legendOrientation",
+            "linear_color_scheme",
+            "number_format",
+            "show_legend",
+        }
+    ),
+    "filters": frozenset({"adhoc_filters", "extra_filters", "filters"}),
+    "time": frozenset(
+        {
+            "granularity_sqla",
+            "since",
+            "time_grain_sqla",
+            "time_range",
+            "until",
+        }
+    ),
+}
+_FORM_DATA_UPDATE_PRESERVE_KEYS = frozenset().union(
+    *FORM_DATA_UPDATE_PRESERVE_KEYS.values()
+)
+
+
+def _merge_preserved_adhoc_filters(
+    existing_form_data: Mapping[str, Any],
+    new_form_data: Mapping[str, Any],
+    *,
+    drop_existing_temporal: bool,
+) -> list[Any] | None:
+    """Merge omitted structured filters while removing stale time bindings."""
+    previous = existing_form_data.get("adhoc_filters")
+    generated = new_form_data.get("adhoc_filters")
+    if not isinstance(previous, list):
+        return list(generated) if isinstance(generated, list) else None
+
+    previous_binding = 
existing_form_data.get(MCP_DASHBOARD_TIME_FILTER_SUBJECT)
+    new_binding = new_form_data.get(MCP_DASHBOARD_TIME_FILTER_SUBJECT)
+    merged: list[Any] = []
+    for filter_ in previous:
+        is_temporal = (
+            isinstance(filter_, dict)
+            and filter_.get("operator") == FilterOperator.TEMPORAL_RANGE.value
+        )
+        stale_generated_binding = (
+            is_temporal
+            and previous_binding
+            and previous_binding != new_binding
+            and filter_.get("subject") == previous_binding
+            and filter_.get("comparator") == NO_TIME_RANGE
+        )
+        if (drop_existing_temporal and is_temporal) or stale_generated_binding:
+            continue
+        merged.append(filter_)
+
+    for filter_ in generated if isinstance(generated, list) else []:
+        if isinstance(filter_, dict):
+            same_filter = any(
+                isinstance(previous_filter, dict)
+                and previous_filter.get("clause") == filter_.get("clause")
+                and previous_filter.get("expressionType")
+                == filter_.get("expressionType")
+                and previous_filter.get("subject") == filter_.get("subject")
+                and previous_filter.get("operator") == filter_.get("operator")
+                for previous_filter in merged
+            )
+            if same_filter:
+                continue
+        elif filter_ in merged:
+            continue
+        merged.append(filter_)
+    return merged
+
+
+def preserve_previous_adhoc_filters(
+    new_form_data: Dict[str, Any], previous_form_data: Mapping[str, Any]
+) -> None:
+    """Compatibility entry point backed by the shared filter merge."""
+    filters = _merge_preserved_adhoc_filters(
+        previous_form_data,
+        new_form_data,
+        drop_existing_temporal=False,
+    )
+    if filters is not None:
+        new_form_data["adhoc_filters"] = filters
+
+
+def _merge_allowlisted_form_data(
+    existing_form_data: Mapping[str, Any],
+    new_form_data: Mapping[str, Any],
+) -> Dict[str, Any]:
+    """Start from mapped target state and add only registry-approved 
omissions."""
+    merged = dict(new_form_data)
+    for key in _FORM_DATA_UPDATE_PRESERVE_KEYS:
+        if key not in merged and key in existing_form_data:
+            merged[key] = existing_form_data[key]
+    return merged
+
+
+def merge_form_data_for_update(  # noqa: C901
+    existing_form_data: Dict[str, Any],
+    new_form_data: Dict[str, Any],
+    config: Any,
+    *,
+    dataset_rebind: bool = False,
+) -> Dict[str, Any]:
+    """Merge mapped updates without leaking query roles across visualizations.
+
+    Same-viz updates retain native controls outside the simplified MCP schema 
by
+    starting from saved form data. Cross-viz updates remain bounded by the
+    shared preservation registry. Explicit clears are applied last.
+    """
+    if dataset_rebind:
+        existing_form_data = scrub_dataset_bound_form_data(existing_form_data)
+
+    same_viz = existing_form_data.get("viz_type") == 
new_form_data.get("viz_type")
+    explicit_control_clears = (
+        _apply_modeled_update_semantics(existing_form_data, new_form_data, 
config)

Review Comment:
   Addressed: `GaugeChartConfig` is included in modeled update controls, so 
omitted presentation fields preserve saved values instead of mapper defaults 
replacing them. `test_partial_gauge_update_preserves_omitted_controls` covers 
`show_progress=False` and `split_number=5`.



##########
superset/mcp_service/chart/chart_utils.py:
##########
@@ -1249,6 +1336,659 @@ def map_gauge_config(config: GaugeChartConfig) -> 
Dict[str, Any]:
     return form_data
 
 
+def map_sunburst_config(config: SunburstChartConfig) -> Dict[str, Any]:
+    """Map typed Sunburst config to the ECharts ``sunburst_v2`` form_data.
+
+    The frontend control panel stores hierarchy levels under ``columns`` and
+    metrics under singular ``metric`` / ``secondary_metric`` keys.  Its
+    buildQuery adds primary-metric descending ordering when ``sort_by_metric``
+    is enabled; server-side query builders mirror that transform separately.
+    """
+    form_data: Dict[str, Any] = {
+        "viz_type": "sunburst_v2",
+        "columns": [dimension.name for dimension in config.hierarchy],
+        "metric": create_metric_object(config.metric),
+        "sort_by_metric": config.sort_by_metric,
+        "row_limit": config.row_limit,
+        "show_labels": config.show_labels,
+        "show_labels_threshold": config.show_labels_threshold,
+        "show_total": config.show_total,
+        "show_null_values": config.show_null_values,
+        "label_type": config.label_type,
+        "number_format": config.number_format,
+        "date_format": config.date_format,
+    }
+    if config.secondary_metric is not None:
+        form_data["secondary_metric"] = 
create_metric_object(config.secondary_metric)
+    if config.color_scheme is not None:
+        form_data["color_scheme"] = config.color_scheme
+    if config.linear_color_scheme is not None:
+        form_data["linear_color_scheme"] = config.linear_color_scheme
+    if config.time_range is not None:
+        form_data["time_range"] = config.time_range
+    if config.temporal_column is not None:
+        form_data["granularity_sqla"] = config.temporal_column
+    if config.time_grain is not None:
+        form_data["time_grain_sqla"] = config.time_grain
+
+    _copy_sunburst_native_envelope(form_data, config)
+
+    add_currency_format(form_data, config.currency_format)
+    _add_adhoc_filters(form_data, config.filters)
+    return form_data
+
+
+# Sunburst fields with explicit omission/clear semantics. Mapper defaults must
+# not overwrite same-viz state when the typed field was omitted, while explicit
+# clears must also beat the shared preservation registry on cross-viz updates.
+# Required query roles (hierarchy and metric) are deliberately absent: a full
+# replacement always updates them.
+_SUNBURST_UPDATE_FIELD_KEYS: dict[str, str] = {
+    "time_range": "time_range",
+    "time_grain": "time_grain_sqla",
+    "temporal_column": "granularity_sqla",
+    "sort_by_metric": "sort_by_metric",
+    "row_limit": "row_limit",
+    "color_scheme": "color_scheme",
+    "linear_color_scheme": "linear_color_scheme",
+    "show_labels": "show_labels",
+    "show_labels_threshold": "show_labels_threshold",
+    "show_total": "show_total",
+    "show_null_values": "show_null_values",
+    "label_type": "label_type",
+    "number_format": "number_format",
+    "date_format": "date_format",
+    "currency_format": "currency_format",
+    "extra_form_data": "extra_form_data",
+    "url_params": "url_params",
+    "standardized_form_data": "standardizedFormData",
+}
+
+
+# Presentation controls emitted sparsely by chart mappers need three-way update
+# semantics: omitted preserves saved native state, an explicit value replaces
+# it, and explicit ``None``/``False`` clears a truthy saved value when the 
mapper
+# has no canonical false/null representation.  Query roles are intentionally
+# absent: a replacement config always owns those through the plugin contract.
+# Paths below also cover nested axis/legend models so an omitted nested 
property
+# is not mistaken for an explicit clear of the whole control.
+_MODELED_UPDATE_CONTROL_PATHS: dict[str, dict[str, tuple[tuple[str, ...], 
...]]] = {
+    "PieChartConfig": {
+        "color_scheme": (("color_scheme",),),
+        "show_labels": (("show_labels",),),
+        "show_legend": (("show_legend",),),
+        "legendOrientation": (("legend_orientation",),),
+        "label_type": (("label_type",),),
+        "number_format": (("number_format",),),
+        "date_format": (("date_format",),),
+        "sort_by_metric": (("sort_by_metric",),),
+        "row_limit": (("row_limit",),),
+        "donut": (("donut",),),
+        "show_total": (("show_total",),),
+        "labels_outside": (("labels_outside",),),
+        "outerRadius": (("outer_radius",),),
+        "innerRadius": (("inner_radius",),),
+        "currency_format": (("currency_format",),),
+    },
+    "TableChartConfig": {
+        "row_limit": (("row_limit",),),
+        "color_scheme": (("color_scheme",),),
+        "column_config": (("column_config",),),
+    },
+    "XYChartConfig": {
+        "row_limit": (("row_limit",),),
+        "series_limit": (("series_limit",),),
+        "stack": (("stacked",),),
+        "orientation": (("orientation",),),
+        "x_axis_title": (("x_axis", "title"),),
+        "x_axis_format": (("x_axis", "format"),),
+        "y_axis_title": (("y_axis", "title"),),
+        "y_axis_format": (("y_axis", "format"),),
+        "y_axis_scale": (("y_axis", "scale"),),
+        "show_legend": (("legend", "show"),),
+        "legendOrientation": (("legend", "position"), ("legend_orientation",)),
+        "x_axis_time_format": (("x_axis_time_format",),),
+        "show_value": (("show_value",),),
+        "currency_format": (("currency_format",),),
+        "color_scheme": (("color_scheme",),),
+    },
+    "HistogramChartConfig": {
+        "bins": (("bins",),),
+        "normalize": (("normalize",),),
+        "cumulative": (("cumulative",),),
+        "row_limit": (("row_limit",),),
+    },
+    "BoxPlotChartConfig": {
+        "whiskerOptions": (
+            ("whisker_type",),
+            ("percentile_low",),
+            ("percentile_high",),
+        ),
+        "row_limit": (("row_limit",),),
+        "number_format": (("number_format",),),
+        "date_format": (("date_format",),),
+    },
+    "WaterfallChartConfig": {
+        "show_total": (("show_total",),),
+        "show_legend": (("show_legend",),),
+        "increase_label": (("increase_label",),),
+        "decrease_label": (("decrease_label",),),
+        "total_label": (("total_label",),),
+        "x_axis_time_format": (("x_axis_time_format",),),
+        "y_axis_format": (("y_axis_format",),),
+        "currency_format": (("currency_format",),),
+        "row_limit": (("row_limit",),),
+    },
+    "BigNumberChartConfig": {
+        "subheader": (("subheader",),),
+        "y_axis_format": (("y_axis_format",),),
+        "time_format": (("time_format",),),
+        "currency_format": (("currency_format",),),
+        "color_scheme": (("color_scheme",),),
+        "start_y_axis_at_zero": (("start_y_axis_at_zero",),),
+        "compare_lag": (("compare_lag",),),
+        "aggregation": (("aggregation",),),
+    },
+    "HandlebarsChartConfig": {
+        "row_limit": (("row_limit",),),
+        "order_desc": (("order_desc",),),
+        "styleTemplate": (("style_template",),),
+    },
+    "PivotTableChartConfig": {
+        "aggregateFunction": (("aggregate_function",),),
+        "rowTotals": (("show_row_totals",),),
+        "colTotals": (("show_column_totals",),),
+        "transposePivot": (("transpose",),),
+        "combineMetric": (("combine_metric",),),
+        "valueFormat": (("value_format",),),
+        "date_format": (("date_format",),),
+        "currency_format": (("currency_format",),),
+        "row_limit": (("row_limit",),),
+    },
+    "InteractivePivotChartConfig": {
+        "order_desc": (("sort_descending",),),
+        "row_limit": (("row_limit",),),
+        "rowGroupCounts": (("show_row_group_counts",),),
+        "rowTotals": (("show_row_totals",),),
+        "colTotals": (("show_column_totals",),),
+        "colSubTotals": (("show_column_subtotals",),),
+        "valueFormat": (("value_format",),),
+        "date_format": (("date_format",),),
+        "currency_format": (("currency_format",),),
+        "colOrder": (("column_sort",),),
+        "allow_render_html": (("allow_render_html",),),
+        "expand_pivot_groups": (("expand_pivot_groups",),),
+        "time_compare": (("comparison_period",),),
+        "comparison_type": (("comparison_type",),),
+    },
+    "MixedTimeseriesChartConfig": {
+        "seriesType": (("primary_kind",),),
+        "area": (("primary_kind",),),
+        "seriesTypeB": (("secondary_kind",),),
+        "areaB": (("secondary_kind",),),
+        "show_legend": (("show_legend",),),
+        "legendOrientation": (("legend_orientation",),),
+        "show_value": (("show_value",),),
+        "color_scheme": (("color_scheme",),),
+        "currency_format": (("currency_format",),),
+        "currency_format_secondary": (("currency_format_secondary",),),
+        "xAxisTitle": (("x_axis", "title"),),
+        "x_axis_time_format": (("x_axis", "format"),),
+        "yAxisTitle": (("y_axis", "title"),),
+        "y_axis_format": (("y_axis", "format"),),
+        "logAxis": (("y_axis", "scale"),),
+        "yAxisTitleSecondary": (("y_axis_secondary", "title"),),
+        "y_axis_format_secondary": (("y_axis_secondary", "format"),),
+        "logAxisSecondary": (("y_axis_secondary", "scale"),),
+        "row_limit": (("row_limit",),),
+    },
+}
+
+
+def _model_path_was_set(config: Any, path: tuple[str, ...]) -> bool:
+    """Return whether every component of a Pydantic model path was supplied."""
+    current = config
+    for field_name in path:
+        if field_name not in getattr(current, "model_fields_set", set()):
+            return False
+        current = getattr(current, field_name, None)
+        if current is None:
+            # An explicit null parent clears all of its mapped descendants.
+            return True
+    return True
+
+
+def _apply_modeled_update_semantics(
+    existing_form_data: Mapping[str, Any],
+    new_form_data: Dict[str, Any],
+    config: Any,
+) -> set[str]:
+    """Preserve truly omitted modeled controls and return explicit clears."""
+    explicit_clears: set[str] = set()
+    controls = _MODELED_UPDATE_CONTROL_PATHS.get(type(config).__name__, {})
+    for form_key, paths in controls.items():
+        if any(_model_path_was_set(config, path) for path in paths):
+            if form_key not in new_form_data:
+                explicit_clears.add(form_key)
+            continue
+        if form_key in existing_form_data:
+            new_form_data[form_key] = existing_form_data[form_key]
+        else:
+            new_form_data.pop(form_key, None)
+    return explicit_clears
+
+
+_TEMPORAL_FORM_DATA_KEYS = frozenset(
+    {
+        "granularity",
+        "granularity_sqla",
+        "since",
+        "time_grain",
+        "time_grain_sqla",
+        "time_range",
+        "until",
+    }
+)
+
+
+def _is_temporal_filter(filter_: Any) -> bool:
+    """Return whether a native, adhoc, or legacy filter carries a time 
range."""
+    return isinstance(filter_, dict) and (
+        filter_.get("operator") == FilterOperator.TEMPORAL_RANGE.value
+        or filter_.get("op") == FilterOperator.TEMPORAL_RANGE.value
+        or filter_.get("col") in {"__time_col", "__time_grain", "__time_range"}
+    )
+
+
+def _without_temporal_filters(value: Any) -> Any:
+    """Copy a filter list without temporal predicates, preserving other 
shapes."""
+    if not isinstance(value, list):
+        return value
+    return [filter_ for filter_ in value if not _is_temporal_filter(filter_)]
+
+
+def _scrub_temporal_form_data(form_data: Mapping[str, Any]) -> Dict[str, Any]:
+    """Remove every source capable of reconstructing explicitly cleared time 
state."""
+    scrubbed = dict(form_data)
+    for key in _TEMPORAL_FORM_DATA_KEYS:
+        scrubbed.pop(key, None)
+    scrubbed.pop(MCP_DASHBOARD_TIME_FILTER_SUBJECT, None)
+
+    for key in ("adhoc_filters", "extra_filters", "filters"):
+        if key in scrubbed:
+            scrubbed[key] = _without_temporal_filters(scrubbed[key])
+
+    extra_form_data = scrubbed.get("extra_form_data")
+    if isinstance(extra_form_data, dict):
+        cleaned_extra = dict(extra_form_data)
+        for key in _TEMPORAL_FORM_DATA_KEYS:
+            cleaned_extra.pop(key, None)
+        for key in ("adhoc_filters", "extra_filters", "filters"):
+            if key in cleaned_extra:
+                cleaned_extra[key] = 
_without_temporal_filters(cleaned_extra[key])
+        scrubbed["extra_form_data"] = cleaned_extra
+    elif extra_form_data is None:
+        scrubbed.pop("extra_form_data", None)
+    return scrubbed
+
+
+# One bounded registry owns state that may survive a form-data replacement.
+# Query roles and plugin-specific controls are deliberately absent. This keeps
+# cross-viz transitions preview/save-safe without chart-by-chart allowlists 
that
+# can drift as new plugins are registered.
+FORM_DATA_UPDATE_PRESERVE_KEYS: dict[str, frozenset[str]] = {
+    "envelope": frozenset(
+        {
+            "dashboardId",
+            "dashboards",
+            "datasource",
+            "extra_form_data",
+            "slice_id",
+            "slice_name",
+            "standardizedFormData",
+            "url_params",
+        }
+    ),
+    "presentation": frozenset(
+        {
+            "color_scheme",
+            "currency_format",
+            "date_format",
+            "legendOrientation",
+            "linear_color_scheme",
+            "number_format",
+            "show_legend",
+        }
+    ),
+    "filters": frozenset({"adhoc_filters", "extra_filters", "filters"}),
+    "time": frozenset(
+        {
+            "granularity_sqla",
+            "since",
+            "time_grain_sqla",
+            "time_range",
+            "until",
+        }
+    ),
+}
+_FORM_DATA_UPDATE_PRESERVE_KEYS = frozenset().union(
+    *FORM_DATA_UPDATE_PRESERVE_KEYS.values()
+)
+
+
+def _merge_preserved_adhoc_filters(
+    existing_form_data: Mapping[str, Any],
+    new_form_data: Mapping[str, Any],
+    *,
+    drop_existing_temporal: bool,
+) -> list[Any] | None:
+    """Merge omitted structured filters while removing stale time bindings."""
+    previous = existing_form_data.get("adhoc_filters")
+    generated = new_form_data.get("adhoc_filters")
+    if not isinstance(previous, list):
+        return list(generated) if isinstance(generated, list) else None
+
+    previous_binding = 
existing_form_data.get(MCP_DASHBOARD_TIME_FILTER_SUBJECT)
+    new_binding = new_form_data.get(MCP_DASHBOARD_TIME_FILTER_SUBJECT)
+    merged: list[Any] = []
+    for filter_ in previous:
+        is_temporal = (
+            isinstance(filter_, dict)
+            and filter_.get("operator") == FilterOperator.TEMPORAL_RANGE.value
+        )
+        stale_generated_binding = (
+            is_temporal
+            and previous_binding
+            and previous_binding != new_binding
+            and filter_.get("subject") == previous_binding
+            and filter_.get("comparator") == NO_TIME_RANGE
+        )
+        if (drop_existing_temporal and is_temporal) or stale_generated_binding:
+            continue
+        merged.append(filter_)
+
+    for filter_ in generated if isinstance(generated, list) else []:
+        if isinstance(filter_, dict):
+            same_filter = any(
+                isinstance(previous_filter, dict)
+                and previous_filter.get("clause") == filter_.get("clause")
+                and previous_filter.get("expressionType")
+                == filter_.get("expressionType")
+                and previous_filter.get("subject") == filter_.get("subject")
+                and previous_filter.get("operator") == filter_.get("operator")
+                for previous_filter in merged
+            )
+            if same_filter:
+                continue
+        elif filter_ in merged:
+            continue
+        merged.append(filter_)
+    return merged
+
+
+def preserve_previous_adhoc_filters(
+    new_form_data: Dict[str, Any], previous_form_data: Mapping[str, Any]
+) -> None:
+    """Compatibility entry point backed by the shared filter merge."""
+    filters = _merge_preserved_adhoc_filters(
+        previous_form_data,
+        new_form_data,
+        drop_existing_temporal=False,
+    )
+    if filters is not None:
+        new_form_data["adhoc_filters"] = filters
+
+
+def _merge_allowlisted_form_data(
+    existing_form_data: Mapping[str, Any],
+    new_form_data: Mapping[str, Any],
+) -> Dict[str, Any]:
+    """Start from mapped target state and add only registry-approved 
omissions."""
+    merged = dict(new_form_data)
+    for key in _FORM_DATA_UPDATE_PRESERVE_KEYS:
+        if key not in merged and key in existing_form_data:
+            merged[key] = existing_form_data[key]
+    return merged
+
+
+def merge_form_data_for_update(  # noqa: C901
+    existing_form_data: Dict[str, Any],
+    new_form_data: Dict[str, Any],
+    config: Any,
+    *,
+    dataset_rebind: bool = False,
+) -> Dict[str, Any]:
+    """Merge mapped updates without leaking query roles across visualizations.
+
+    Same-viz updates retain native controls outside the simplified MCP schema 
by
+    starting from saved form data. Cross-viz updates remain bounded by the
+    shared preservation registry. Explicit clears are applied last.
+    """
+    if dataset_rebind:
+        existing_form_data = scrub_dataset_bound_form_data(existing_form_data)
+
+    same_viz = existing_form_data.get("viz_type") == 
new_form_data.get("viz_type")
+    explicit_control_clears = (
+        _apply_modeled_update_semantics(existing_form_data, new_form_data, 
config)
+        if same_viz
+        else set()
+    )
+    if same_viz:
+        from superset.mcp_service.chart.registry import (
+            query_role_keys_for_viz_type,
+        )
+
+        # Strip every target-owned query role first, then overlay the mapper's
+        # complete replacement. This removes mutually exclusive aliases (for
+        # example Pie ``metrics`` vs ``metric`` and raw vs aggregate table
+        # roles) without dropping unmodeled native presentation controls.
+        query_role_keys = query_role_keys_for_viz_type(
+            str(new_form_data.get("viz_type"))
+        )
+        merged = {
+            key: value
+            for key, value in existing_form_data.items()
+            if key not in query_role_keys
+        }
+        merged.update(new_form_data)
+        if new_form_data.get("viz_type") == "mixed_timeseries" and not 
dataset_rebind:
+            from superset.common.form_data_query_context import (
+                MIXED_TIMESERIES_SECONDARY_QUERY_KEYS,
+            )
+
+            # Query B inherits unsuffixed controls only when the suffixed key 
is
+            # absent. Preserve explicit native clears for controls the typed
+            # mapper did not replace, so []/None never turns into accidental
+            # inheritance from query A. Valid comparison state is also 
retained;
+            # malformed/stale dataset roles remain fail-closed and are dropped.
+            for key in MIXED_TIMESERIES_SECONDARY_QUERY_KEYS:
+                if key in new_form_data or key not in existing_form_data:
+                    continue
+                value = existing_form_data[key]
+                is_explicit_clear = value is None or value in ([], {}, "")
+                is_valid_comparison = key == "comparison_type_b" and value in {
+                    "values",
+                    "difference",
+                    "percentage",
+                    "ratio",
+                }
+                is_valid_list_state = key in {
+                    "adhoc_filters_b",
+                    "annotation_layers_b",
+                    "time_compare_b",
+                } and isinstance(value, list)
+                if is_explicit_clear or is_valid_comparison or 
is_valid_list_state:
+                    merged[key] = value
+    else:
+        merged = _merge_allowlisted_form_data(existing_form_data, 
new_form_data)
+
+    for key in explicit_control_clears:
+        merged.pop(key, None)
+
+    fields_set: set[str] = getattr(config, "model_fields_set", set())
+    if getattr(config, "filters", None) == []:
+        merged.pop("adhoc_filters", None)
+    elif getattr(config, "filters", None) is None:
+        filters = _merge_preserved_adhoc_filters(
+            existing_form_data,
+            new_form_data,
+            drop_existing_temporal=bool(
+                {"temporal_column", "time_grain", "time_range"} & fields_set
+            ),
+        )
+        if filters is not None:
+            merged["adhoc_filters"] = filters
+
+    if not isinstance(config, SunburstChartConfig):
+        return merged
+
+    temporal_fields = {"time_grain", "temporal_column"}
+    for field_name, form_key in _SUNBURST_UPDATE_FIELD_KEYS.items():
+        if field_name in temporal_fields:
+            continue
+        if field_name not in fields_set:
+            if same_viz:
+                if form_key in existing_form_data:
+                    merged[form_key] = existing_form_data[form_key]
+                else:
+                    merged.pop(form_key, None)
+            continue
+
+        value = getattr(config, field_name)
+        if value is None:
+            merged.pop(form_key, None)
+            if field_name == "time_range":
+                # The generic query-context mapper reconstructs time_range from
+                # these legacy keys. A clear must remove all three sources.
+                merged.pop("since", None)
+                merged.pop("until", None)
+
+    # A null temporal control is an atomic clear. Apply it after every merge so
+    # cached/native aliases and extra-form-data overrides cannot recreate time
+    # state in QueryContextFactory.
+    explicit_temporal_clear = any(
+        field_name in fields_set and getattr(config, field_name) is None
+        for field_name in ("temporal_column", "time_grain", "time_range")
+    )
+    if explicit_temporal_clear:
+        merged = _scrub_temporal_form_data(merged)

Review Comment:
   Addressed: after an atomic temporal clear, an explicitly supplied non-null 
`time_range` is reapplied. `test_explicit_range_survives_clearing_saved_grain` 
covers setting `Last month` while clearing the saved grain.



##########
superset/mcp_service/chart/chart_helpers.py:
##########
@@ -446,12 +564,200 @@ def _resolve_deck_gl_metrics(
         if value:
             metrics.append(value)
     elif isinstance(prf, str) and _is_metric_ref(prf):
-        # Legacy deck_scatter: point_radius_fixed as a bare non-numeric metric 
key
-        logger.debug("Legacy point_radius_fixed string metric encountered: 
%s", prf)
         metrics.append(prf)
     return metrics
 
 
+def _deck_query_adapter(  # noqa: C901
+    form_data: dict[str, Any], query: dict[str, Any], viz_type: str
+) -> dict[str, Any]:
+    """Apply the native frontend builder for a single Deck.gl layer."""
+    base_columns = list(query.get("columns") or [])
+    base_metrics = list(query.get("metrics") or [])
+    filters = list(query.get("filters") or [])
+    tooltips = _deck_tooltip_columns(form_data.get("tooltip_contents"))
+
+    def add_null(column: str, *, value: Any = ...) -> None:
+        clause: dict[str, Any] = {"col": column, "op": "IS NOT NULL"}
+        if value is not ...:
+            clause["val"] = value
+        filters.append(clause)
+
+    if viz_type == "deck_geojson":
+        geometry = form_data.get("geojson")
+        if not isinstance(geometry, str) or not geometry:
+            raise ValueError("GeoJSON column is required for GeoJSON charts")
+        columns = _add_deck_columns(base_columns, [geometry] if geometry else 
[])
+        cross_filter = form_data.get("cross_filter_column")
+        if cross_filter:
+            columns = _add_deck_columns(columns, [cross_filter])
+        columns = _add_deck_columns(columns, tooltips)
+        if form_data.get("filter_nulls", True) and isinstance(geometry, str):
+            add_null(geometry)
+        query.update(
+            columns=columns,
+            metrics=[],
+            groupby=[],
+            filters=filters,
+            is_timeseries=False,
+        )
+        return query
+
+    if viz_type == "deck_polygon":
+        line_column = form_data.get("line_column")
+        if not isinstance(line_column, str) or not line_column:
+            raise ValueError("Polygon column is required for Polygon charts")
+        columns = _add_deck_columns(base_columns, [line_column] if line_column 
else [])
+        cross_filter = form_data.get("cross_filter_column")
+        if cross_filter:
+            columns = _add_deck_columns(columns, [cross_filter])
+        columns = _add_deck_columns(columns, tooltips)
+        metrics: list[Any] = []
+        if metric := form_data.get("metric"):
+            metrics.append(metric)
+        radius = form_data.get("point_radius_fixed")
+        if (
+            isinstance(radius, dict)
+            and radius.get("type") == "metric"
+            and radius.get("value") is not None
+        ):
+            metrics.append(radius["value"])
+        if form_data.get("filter_nulls", True) and isinstance(line_column, 
str):
+            add_null(line_column)
+            if metric:
+                add_null(_deck_metric_label(metric))
+        query.update(
+            columns=columns,
+            metrics=metrics,
+            filters=filters,
+            is_timeseries=False,
+        )
+        return query
+
+    if viz_type == "deck_path":
+        line_column = form_data.get("line_column")
+        if not isinstance(line_column, str) or not line_column:
+            raise ValueError("Line column is required for Path charts")
+        columns = list(base_columns)
+        metrics = list(base_metrics)

Review Comment:
   Addressed: Deck Path filters `base_metrics` through the fixed-vs-metric 
discriminator and bases aggregate mode on the filtered metrics. The regression 
now asserts fixed `size="100"` yields no metrics and retains `path_col` as a 
raw column.



-- 
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: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to