aminghadersohi commented on code in PR #43771: URL: https://github.com/apache/superset/pull/43771#discussion_r3952052043
########## superset/mcp_service/chart/query_result.py: ########## @@ -0,0 +1,1525 @@ +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. + +"""Canonicalize and validate ``ChartDataCommand`` result envelopes.""" + +import math +import time as system_time +from bisect import bisect_right +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.""" + + 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 + 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 + 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 _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 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 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 = ( Review Comment: Fixed in 9f7a3a7ac6840564732d93b0c845bd9fa88704fa. The timestamp path accepts the existing hook-free pytz named-zone normalization, including static generated zones; the canonical UTC singleton is normalized explicitly. Stored wall/instant offsets and nanoseconds remain intact. Regressions cover UTC, US/Pacific, Etc/GMT-3, and both New York DST-fold occurrences with nanoseconds. Focused suite: 845 passed; staged and branch-wide pre-commit passed. The broader local suite passed 5,040 tests. All 13 required checks passed on this exact head; no check failures or pending checks remain. ########## superset/mcp_service/chart/preview_utils.py: ########## @@ -160,11 +205,17 @@ def _generate_ascii_preview_from_data( content = _generate_safe_ascii_line_chart(data) elif viz_type == "pie": content = _generate_safe_ascii_pie_chart(data) + elif viz_type == "sunburst_v2": Review Comment: Fixed in 9f7a3a7ac6840564732d93b0c845bd9fa88704fa. Sunburst reserves two headers and, when needed, a footer before selecting rows (17 rows plus a notice at default height for larger results). Tiny canvases retain an omission notice. Display-column-aware clipping marks truncated lines, and embedded line breaks cannot consume the footer. Regressions cover 1–25-line canvases, 1–30 rows, CJK/emoji/combining characters, and clipped paths with an intact footer. Focused suite: 845 passed; pre-commit passed. The broader local suite passed 5,040 tests. All 13 required checks passed on this exact head; no check failures or pending checks remain. ########## superset/common/form_data_query_context.py: ########## @@ -288,6 +461,1156 @@ def _pie_contribution_post_processing(metrics: list[Any]) -> list[dict[str, Any] ] +def _as_list(value: Any) -> list[Any]: + """Return the frontend ``ensureIsArray`` representation of a value.""" + if value is None: + return [] + return list(value) if isinstance(value, (list, tuple)) else [value] + + +def _label(value: Any, *, metric: bool = False) -> str: + """Resolve a frontend-compatible query-field label.""" + try: + return get_metric_name(value) if metric else get_column_name(value) + except (AttributeError, KeyError, TypeError, ValueError): + if isinstance(value, Mapping): + return str( + value.get("label") + or value.get("column_name") + or value.get("sqlExpression") + or value + ) + return str(value) + + +def _deduplicate_fields(values: list[Any], *, metric: bool = False) -> list[Any]: + """Deduplicate query fields by their frontend-visible label.""" + result: list[Any] = [] + labels: set[str] = set() + for value in values: + if value is None or value == "": + continue + label = _label(value, metric=metric) + if label in labels: + continue + labels.add(label) + result.append(value) + return result + + +def retain_mixed_timeseries_secondary_form_data( + form_data: Mapping[str, Any], +) -> dict[str, Any]: + """Mirror ``retainFormDataSuffix(formData, '_b')`` exactly. + + Suffixed values are installed first, including falsey values, and shared + unsuffixed controls fill only keys that query B did not explicitly set. + """ + secondary: dict[str, Any] = {} + for key, value in form_data.items(): + if key.endswith("_b"): + secondary[key[:-2]] = value + for key, value in form_data.items(): + if not key.endswith("_b") and key not in secondary: + secondary[key] = value + secondary_filter_keys = { + "adhoc_filters": "adhoc_filters_b", + "extra_filters": "extra_filters_b", + "filters": "filters_b", + "having": "having_b", + "where": "where_b", + } + if any(suffixed in form_data for suffixed in secondary_filter_keys.values()): + # The frontend exposes adhoc_filters_b, while saved/server payloads can + # carry equivalent legacy aliases. Treat the family atomically: an + # explicit clear in any B alias must not be repopulated by query A's + # differently named filter representation. + for primary, suffixed in secondary_filter_keys.items(): + if suffixed not in form_data: + secondary.pop(primary, None) + return secondary + + +def _base_query_object( # noqa: C901 + form_data: dict[str, Any], + *, + row_limit: int | None, + order_desc: bool | None, + filters_prepared: bool, +) -> dict[str, Any]: + """Build the shared frontend-equivalent portion of a QueryObject.""" + columns, metrics, orderby = query_fields_from_form_data(form_data) + query: dict[str, Any] = { + "columns": columns, + "metrics": metrics, + } + if orderby: + query["orderby"] = orderby + + if filters_prepared: + query["filters"] = list(form_data.get("filters") or []) + for clause in ("where", "having"): + if form_data.get(clause): + query[clause] = form_data[clause] + if form_data.get("extras"): + query["extras"] = dict(form_data["extras"]) + else: + filters = adhoc_filters_to_query_filters( + form_data.get("adhoc_filters", []), where_only=True + ) + filters.extend( + filter_ + for filter_ in form_data.get("filters") or [] + if isinstance(filter_, dict) and filter_.get("col") is not None + ) + query["filters"] = filters + if extras := freeform_where_having(form_data): + query["extras"] = extras + + extras = dict(query.get("extras") or {}) + if form_data.get("time_grain_sqla") is not None: + extras["time_grain_sqla"] = form_data["time_grain_sqla"] + if extras: + query["extras"] = extras + + effective_limit = row_limit if row_limit is not None else form_data.get("row_limit") + if effective_limit is not None: + query["row_limit"] = effective_limit + if form_data.get("row_offset") is not None: + query["row_offset"] = form_data["row_offset"] + if order_desc is not None: + query["order_desc"] = order_desc + elif "order_desc" in form_data and form_data["order_desc"] is not None: + query["order_desc"] = form_data["order_desc"] + + time_range = form_data.get("time_range") + if not time_range and (form_data.get("since") or form_data.get("until")): + time_range = f"{form_data.get('since') or ''} : {form_data.get('until') or ''}" + if time_range: + query["time_range"] = time_range + for key in ("since", "until", "annotation_layers", "url_params", "custom_params"): + if form_data.get(key) is not None: + query[key] = form_data[key] + + granularity = form_data.get("granularity") or form_data.get("granularity_sqla") + if granularity: + query["granularity"] = granularity + series_limit = form_data.get("series_limit", form_data.get("limit")) + if series_limit is not None: + query["series_limit"] = series_limit + series_limit_metric = form_data.get("series_limit_metric") + if series_limit_metric is None: + series_limit_metric = form_data.get("timeseries_limit_metric") + if series_limit_metric is not None: + query["series_limit_metric"] = series_limit_metric + if form_data.get("group_others_when_limit_reached") is not None: + query["group_others_when_limit_reached"] = form_data[ + "group_others_when_limit_reached" + ] + return query + + +def _temporalized_columns(form_data: dict[str, Any], columns: list[Any]) -> list[Any]: + """Apply the pivot BASE_AXIS temporal-column contract.""" + time_grain = form_data.get("time_grain_sqla") + temporal_lookup = form_data.get("temporal_columns_lookup") or {} + result: list[Any] = [] + for column in columns: + if ( + isinstance(column, str) + and time_grain + and ( + temporal_lookup.get(column) + or form_data.get("granularity_sqla") == column + ) + ): + result.append( + { + "timeGrain": time_grain, + "columnType": "BASE_AXIS", + "sqlExpression": column, + "label": column, + "expressionType": "SQL", + } + ) + else: + result.append(column) + return result + + +def _box_temporalized_columns( + form_data: dict[str, Any], columns: list[Any] +) -> list[Any]: + """Convert only physical columns confirmed temporal by Box Plot metadata.""" + time_grain = form_data.get("time_grain_sqla") + temporal_lookup = form_data.get("temporal_columns_lookup") + if not time_grain or not isinstance(temporal_lookup, Mapping): + return columns + return [ + { + "timeGrain": time_grain, + "columnType": "BASE_AXIS", + "sqlExpression": column, + "label": column, + "expressionType": "SQL", + } + if isinstance(column, str) and temporal_lookup.get(column) is True + else column + for column in columns + ] + + +def _table_temporalized_columns( + form_data: dict[str, Any], columns: list[Any] +) -> list[Any]: + """Promote the first temporal table group-by to the frontend BASE_AXIS. + + Table's builder treats only physical columns named in + ``temporal_columns_lookup`` as temporal and moves the first match to the + front. Later temporal dimensions remain ordinary group-bys. + """ + time_grain = form_data.get("time_grain_sqla") + temporal_lookup = form_data.get("temporal_columns_lookup") or {} + if not time_grain or not isinstance(temporal_lookup, Mapping): + return columns + + temporal_column: dict[str, Any] | None = None + remaining: list[Any] = [] + for column in columns: + if ( + temporal_column is None + and isinstance(column, str) + and temporal_lookup.get(column) + ): + temporal_column = { + "timeGrain": time_grain, + "columnType": "BASE_AXIS", + "sqlExpression": column, + "label": column, + "expressionType": "SQL", + } + else: + remaining.append(column) + return [temporal_column, *remaining] if temporal_column else columns + + +def _histogram_query(form_data: dict[str, Any], query: dict[str, Any]) -> None: + groupby = _as_list(form_data.get("groupby")) + column = form_data.get("column") + query["columns"] = [*groupby, *([column] if column is not None else [])] + query["post_processing"] = [ + { + "operation": "histogram", + "options": { + "column": _label(column), + "groupby": [_label(value) for value in groupby], + "bins": int(form_data.get("bins", 5)), + "cumulative": form_data.get("cumulative", False), + "normalize": form_data.get("normalize", False), + }, + } + ] + if any( + isinstance(filter_, dict) and filter_.get("clause") == "HAVING" + for filter_ in form_data.get("adhoc_filters") or [] + ): + query["metrics"] = [ + { + "expressionType": "SQL", + "sqlExpression": "COUNT(*)", + "label": "COUNT(*)", + } + ] + else: + query["metrics"] = [] + + +def _box_plot_query(form_data: dict[str, Any], query: dict[str, Any]) -> None: + distributed = _as_list(form_data.get("columns")) + if not distributed and form_data.get("granularity_sqla"): + distributed = [form_data["granularity_sqla"]] + groupby = _as_list(form_data.get("groupby")) + query["columns"] = [*_box_temporalized_columns(form_data, distributed), *groupby] + query["series_columns"] = groupby + whisker = form_data.get("whiskerOptions") + if not whisker: + query["post_processing"] = [] + return + whisker_type = "tukey" + percentiles: list[int] | None = None + if whisker == "Min/max (no outliers)": + whisker_type = "min/max" + elif isinstance(whisker, str) and whisker.endswith(" percentiles"): + low, high = whisker.removesuffix(" percentiles").split("/", 1) + whisker_type = "percentile" + percentiles = [int(low), int(high)] + query["post_processing"] = [ + { + "operation": "boxplot", + "options": { + "whisker_type": whisker_type, + "percentiles": percentiles, + "groupby": [_label(value) for value in groupby], + "metrics": [_label(value, metric=True) for value in query["metrics"]], + }, + } + ] + + +_PIVOT_ADDITIVE_AGGREGATES = frozenset({"SUM", "COUNT", "MIN", "MAX"}) + + +def _all_metrics_additive(metrics: list[Any]) -> bool: + """Mirror Pivot's conservative additive-metric fast-path.""" + return bool(metrics) and all( + isinstance(metric, Mapping) + and metric.get("expressionType") == "SIMPLE" + and metric.get("aggregate") in _PIVOT_ADDITIVE_AGGREGATES + for metric in metrics + ) + + +def _pivot_grouping_sets( + form_data: dict[str, Any], rows: list[Any], columns: list[Any] +) -> list[list[str]]: + """Enumerate the rollup levels requested by Pivot's frontend builder.""" + row_prefixes = [[], *(rows[: index + 1] for index in range(len(rows)))] + column_prefixes = [ + [], + *(columns[: index + 1] for index in range(len(columns))), + ] + show_values_as = form_data.get("showValuesAs") + needs_rows_collapsed = show_values_as in {"percent_col", "percent_total"} + needs_columns_collapsed = show_values_as in {"percent_row", "percent_total"} + + def row_prefix_needed(prefix: list[Any]) -> bool: + if len(prefix) == len(rows): + return True + if not prefix: + return bool(form_data.get("colTotals")) or needs_rows_collapsed + return bool(form_data.get("rowSubTotals")) + + def column_prefix_needed(prefix: list[Any]) -> bool: + if len(prefix) == len(columns): + return True + if not prefix: + return bool(form_data.get("rowTotals")) or needs_columns_collapsed + return bool(form_data.get("colSubTotals")) + + levels = [ + (row_prefix, column_prefix) + for row_prefix in row_prefixes + if row_prefix_needed(row_prefix) + for column_prefix in column_prefixes + if column_prefix_needed(column_prefix) + ] + if form_data.get("combineMetric"): + metrics_layout = form_data.get("metricsLayout") + + def forced_denominator(level: tuple[list[Any], list[Any]]) -> bool: + row_prefix, column_prefix = level + return (needs_rows_collapsed and not row_prefix) or ( + needs_columns_collapsed and not column_prefix + ) + + if metrics_layout == "ROWS": + levels = [ + level + for level in levels + if len(level[0]) == len(rows) or forced_denominator(level) + ] + else: + levels = [ + level + for level in levels + if len(level[1]) == len(columns) or forced_denominator(level) + ] + + return [ + [_label(value) for value in _deduplicate_fields([*row_prefix, *column_prefix])] + for row_prefix, column_prefix in levels + ] + + +def _pivot_query(form_data: dict[str, Any], query: dict[str, Any]) -> None: + rows = _as_list(form_data.get("groupbyRows")) + columns = _as_list(form_data.get("groupbyColumns")) + if form_data.get("transposePivot"): + rows, columns = columns, rows + query["columns"] = _temporalized_columns( + form_data, _deduplicate_fields([*rows, *columns]) + ) + metric = query.get("series_limit_metric") or next( + iter(query.get("metrics") or []), None + ) + query["orderby"] = ( + [[metric, not bool(query.get("order_desc", True))]] + if metric is not None + else [] + ) + if not _all_metrics_additive(query.get("metrics") or []): + query["grouping_sets"] = _pivot_grouping_sets(form_data, rows, columns) + + +def _waterfall_query(form_data: dict[str, Any], query: dict[str, Any]) -> None: + x_axis = form_data.get("x_axis") or form_data.get("granularity_sqla") + columns = [*_as_list(x_axis), *_as_list(form_data.get("groupby"))] + query["columns"] = _deduplicate_fields(columns) + query["orderby"] = [[column, True] for column in query["columns"]] + + +def _gantt_query(form_data: dict[str, Any], query: dict[str, Any]) -> None: + groupby = _as_list(form_data.get("series")) + orderby = query_fields_from_form_data(form_data)[2] + columns = [ + form_data.get("start_time"), + form_data.get("end_time"), + form_data.get("y_axis"), + *groupby, + *_as_list(form_data.get("tooltip_columns")), + *(entry[0] for entry in orderby if entry), + ] + query["columns"] = _deduplicate_fields(columns) + query["metrics"] = _as_list(form_data.get("tooltip_metrics")) + query["orderby"] = orderby + query["series_columns"] = groupby + + +def _normalize_query_orderby(query: dict[str, Any]) -> None: + """Mirror ``normalizeOrderBy`` while retaining limit-direction controls.""" + orderby = query.get("orderby") + if ( + isinstance(orderby, list) + and orderby + and isinstance(orderby[0], (list, tuple)) + and len(orderby[0]) == 2 + and orderby[0][0] + and isinstance(orderby[0][1], bool) + ): + return + metric = ( + query.get("series_limit_metric") + or query.get("legacy_order_by") + or next(iter(query.get("metrics") or []), None) + ) + if metric is None: + query.pop("orderby", None) + return + query["orderby"] = [[metric, not bool(query.get("order_desc", True))]] + + +_TIME_COMPARISON_TYPES = frozenset({"values", "difference", "percentage", "ratio"}) + + +def _metric_offset_map( + form_data: dict[str, Any], + metric_labels: list[str], + offsets: list[Any] | None = None, +) -> dict[str, str]: + """Return the frontend time-comparison metric label map.""" + if form_data.get("comparison_type") not in _TIME_COMPARISON_TYPES: + return {} + return { + f"{metric}__{offset}": metric + for metric in metric_labels + for offset in ( + offsets if offsets is not None else _as_list(form_data.get("time_compare")) + ) + } + + +def _table_time_offsets(form_data: dict[str, Any]) -> list[Any]: + """Resolve Table custom/inherited shifts like its frontend query adapter.""" + raw_offsets = _as_list(form_data.get("time_compare")) + offsets = [offset for offset in raw_offsets if offset not in {"custom", "inherit"}] + if "custom" in raw_offsets and form_data.get("start_date_offset") is not None: + offsets.append(form_data["start_date_offset"]) + extra_form_data = form_data.get("extra_form_data") + if isinstance(extra_form_data, Mapping) and extra_form_data.get("time_compare"): + inherited = extra_form_data["time_compare"] + if inherited not in offsets: + offsets = [inherited] + return offsets + + +def _x_axis_column(form_data: Mapping[str, Any]) -> Any | None: + """Return a supported x-axis column, excluding legacy granularity. + + ``column_name`` mappings are retained for old server/native payloads. Big + Number uses the stricter frontend predicate below. + """ + x_axis = form_data.get("x_axis") + if isinstance(x_axis, str): + return x_axis if x_axis else None + if isinstance(x_axis, Mapping): + if isinstance(column_name := x_axis.get("column_name"), str) and column_name: + return column_name + # Frontend SQL adhoc columns remain objects in the QueryObject. + return x_axis if x_axis else None + return None + + +def _frontend_x_axis_column(form_data: Mapping[str, Any]) -> Any | None: + """Mirror ``isQueryFormColumn`` for physical and SQL adhoc columns.""" + x_axis = form_data.get("x_axis") + if isinstance(x_axis, str): + return x_axis if x_axis else None + if ( + isinstance(x_axis, Mapping) + and "sqlExpression" in x_axis + and "label" in x_axis + and x_axis.get("expressionType") in {None, "SQL"} + ): + return x_axis + return None + + +def normalize_time_column( + form_data: Mapping[str, Any], query: dict[str, Any] +) -> dict[str, Any]: + """Apply the final shared frontend ``normalizeTimeColumn`` mutator.""" + x_axis = _frontend_x_axis_column(form_data) + columns = query.get("columns") + if x_axis is None or not isinstance(columns, list): + return query + + axis_index: int | None = None + for index, column in enumerate(columns): + if isinstance(x_axis, str) and isinstance(column, str) and column == x_axis: + axis_index = index + break + if ( + isinstance(x_axis, Mapping) + and isinstance(column, Mapping) + and column.get("sqlExpression") == x_axis.get("sqlExpression") + ): + axis_index = index + break + if axis_index is None: + return query + + normalized = dict(query) + normalized_columns = list(columns) + grain = (query.get("extras") or {}).get("time_grain_sqla") + if isinstance(columns[axis_index], Mapping): + normalized_axis = { + "columnType": "BASE_AXIS", + **({"timeGrain": grain} if grain is not None else {}), + **columns[axis_index], + } + else: + normalized_axis = { + "columnType": "BASE_AXIS", + "sqlExpression": x_axis, + "label": x_axis, + "expressionType": "SQL", + "isColumnReference": True, + **({"timeGrain": grain} if grain is not None else {}), + } + normalized_columns[axis_index] = normalized_axis + normalized["columns"] = normalized_columns + normalized.pop("is_timeseries", None) + return normalized + + +def _finalize_query_objects( + form_data: Mapping[str, Any], queries: list[dict[str, Any]] +) -> list[dict[str, Any]]: + """Run shared query-context mutators after every visualization adapter.""" + return [normalize_time_column(form_data, query) for query in queries] + + +def _x_axis_label( + form_data: Mapping[str, Any], *, frontend_strict: bool = False +) -> str | None: + """Mirror getXAxisColumn/getXAxisLabel for explicit and legacy axes.""" + explicit = ( + _frontend_x_axis_column(form_data) + if frontend_strict + else _x_axis_column(form_data) + ) + if explicit: + return _label(explicit) + if form_data.get("granularity_sqla"): + return DTTM_ALIAS + return None + + +def _rename_operator( + form_data: dict[str, Any], + query: dict[str, Any], + *, + x_axis_label: str | None, +) -> dict[str, Any] | None: + """Mirror the ECharts ``renameOperator`` for Timeseries and Mixed charts.""" + metrics = list(query.get("metrics") or []) + metric_labels = [_label(metric, metric=True) for metric in metrics] + series_columns = query.get("series_columns") + columns = _as_list( + series_columns if series_columns is not None else query.get("columns") + ) + time_offsets = _as_list(form_data.get("time_compare")) + offset_map = _metric_offset_map(form_data, metric_labels) + is_time_comparison = bool(offset_map) + truncate_metric = form_data.get("truncate_metric") + + should_rename = ( + bool(metrics) + and bool(x_axis_label) + and ( + is_time_comparison + or ( + (bool(columns) or len(time_offsets) > 1) + and "truncate_metric" in form_data + and bool(truncate_metric) + ) + ) + ) + if not should_rename: + return None + + renamed: dict[str, str | None] = {} + comparison_type = form_data.get("comparison_type") + if is_time_comparison: + for metric_with_offset, metric_only in offset_map.items(): + offset_label = next( + ( + str(offset) + for offset in time_offsets + if metric_with_offset.endswith(f"__{offset}") + ), + None, + ) + source = ( + metric_with_offset + if comparison_type == "values" + else f"{comparison_type}__{metric_only}__{metric_with_offset}" + ) + renamed[source] = ( + f"{metric_only}, {offset_label}" if len(metrics) > 1 else offset_label + ) + + if ( + comparison_type not in {"difference", "percentage", "ratio"} + and len(metrics) == 1 + and not renamed + ): + renamed[metric_labels[0]] = None + if not renamed: + return None + return { + "operation": "rename", + "options": {"columns": renamed, "level": 0, "inplace": True}, + } + + +def _timeseries_post_processing( # noqa: C901 + form_data: dict[str, Any], + query: dict[str, Any], + *, + x_axis_label: str | None, + groupby: list[Any], + mixed: bool, +) -> tuple[list[dict[str, Any]], list[Any]]: + """Build the Timeseries/Mixed operator pipeline in frontend order.""" + metric_labels = [_label(value, metric=True) for value in query.get("metrics") or []] + offset_map = _metric_offset_map(form_data, metric_labels) + time_offsets = _as_list(form_data.get("time_compare")) if offset_map else [] + post_processing: list[dict[str, Any]] = [] + + if x_axis_label and metric_labels: + aggregate_labels = ( + [*offset_map.values(), *offset_map] if offset_map else metric_labels + ) + post_processing.append( + { + "operation": "pivot", + "options": { + "index": [x_axis_label], + "columns": [_label(value) for value in groupby], + "aggregates": { + label: {"operator": "mean"} for label in aggregate_labels + }, + "drop_missing_columns": not form_data.get( + "show_empty_columns", False + ), + }, + } + ) + + if form_data.get("resample_method") and form_data.get("resample_rule"): + zero_fill = form_data["resample_method"] == "zerofill" + post_processing.append( + { + "operation": "resample", + "options": { + "method": "asfreq" if zero_fill else form_data["resample_method"], + "rule": form_data["resample_rule"], + "fill_value": 0 if zero_fill else None, + }, + } + ) + + rolling_labels = ( + [*offset_map.values(), *offset_map] if offset_map else metric_labels + ) + columns_map = {label: label for label in rolling_labels} + rolling_type = form_data.get("rolling_type") + if rolling_type == "cumsum": + post_processing.append( + { + "operation": "cum", + "options": {"operator": "sum", "columns": columns_map}, + } + ) + elif rolling_type in {"sum", "mean", "std"}: + post_processing.append( + { + "operation": "rolling", + "options": { + "rolling_type": rolling_type, + "window": int(form_data.get("rolling_periods") or 1), + "min_periods": int(form_data.get("min_periods") or 0), + "columns": columns_map, + }, + } + ) + + comparison_type = form_data.get("comparison_type") + if offset_map and comparison_type != "values": + post_processing.append( + { + "operation": "compare", + "options": { + "source_columns": list(offset_map.values()), + "compare_columns": list(offset_map), + "compare_type": comparison_type, + "drop_original_columns": True, + }, + } + ) + + if not mixed and form_data.get("contributionMode"): + post_processing.append( + { + "operation": "contribution", + "options": { + "orientation": form_data["contributionMode"], + "time_shifts": time_offsets, + }, + } + ) + + if rename := _rename_operator(form_data, query, x_axis_label=x_axis_label): + post_processing.append(rename) + + if not mixed: + sortable = { + x_axis_label or "", + *metric_labels, + } + if ( + "x_axis_sort" in form_data + and "x_axis_sort_asc" in form_data + and form_data.get("x_axis_sort") in sortable + and not groupby + ): + options: dict[str, Any] = {"ascending": form_data.get("x_axis_sort_asc")} + if form_data.get("x_axis_sort") == x_axis_label: + options["is_sort_index"] = True + else: + options["by"] = form_data.get("x_axis_sort") + post_processing.append({"operation": "sort", "options": options}) + + post_processing.append({"operation": "flatten"}) + if not mixed and form_data.get("forecastEnabled") and x_axis_label: + post_processing.append( + { + "operation": "prophet", + "options": { + "time_grain": form_data.get("time_grain_sqla"), + "periods": int(form_data.get("forecastPeriods") or 0), + "confidence_interval": float( + form_data.get("forecastInterval") or 0 + ), + "yearly_seasonality": form_data.get("forecastSeasonalityYearly"), + "weekly_seasonality": form_data.get("forecastSeasonalityWeekly"), + "daily_seasonality": form_data.get("forecastSeasonalityDaily"), + "index": x_axis_label, + }, + } + ) + return post_processing, time_offsets + + +def _timeseries_query(form_data: dict[str, Any], query: dict[str, Any]) -> None: + groupby = _as_list(form_data.get("groupby")) + x_axis = _x_axis_column(form_data) + x_axis_label = _x_axis_label(form_data) + query["columns"] = _deduplicate_fields([*_as_list(x_axis), *groupby]) + query["series_columns"] = groupby + if not x_axis: + query["is_timeseries"] = True + + # Timeseries includes its sort-only metric in the SELECT when no series is + # present. This lets the post-processing sort operator use a metric not + # otherwise displayed. + sort_metric = form_data.get("timeseries_limit_metric") + if isinstance(sort_metric, list): + sort_metric = next(iter(sort_metric), None) + if ( + not groupby + and sort_metric is not None + and _label(sort_metric, metric=True) == form_data.get("x_axis_sort") + and _label(sort_metric, metric=True) + not in {_label(metric, metric=True) for metric in query.get("metrics") or []} + ): + query.setdefault("metrics", []).append(sort_metric) + _normalize_query_orderby(query) + post_processing, time_offsets = _timeseries_post_processing( + form_data, + query, + x_axis_label=x_axis_label, + groupby=groupby, + mixed=form_data.get("viz_type") == "mixed_timeseries", + ) + query["post_processing"] = post_processing + query["time_offsets"] = time_offsets + if form_data.get("viz_type") != "mixed_timeseries": + query["time_compare_full_range"] = bool( + time_offsets and form_data.get("time_compare_full_range") + ) + + +def _big_number_queries( + form_data: dict[str, Any], query: dict[str, Any] +) -> list[dict[str, Any]]: + """Mirror Big Number with Trendline's one/two-query contract.""" + explicit_x_axis = _frontend_x_axis_column(form_data) + time_column = _as_list(explicit_x_axis) + x_axis_label = _x_axis_label(form_data, frontend_strict=True) + query["columns"] = time_column + if not time_column: + query["is_timeseries"] = True + metric_labels = [_label(value, metric=True) for value in query.get("metrics") or []] + post_processing: list[dict[str, Any]] = [] + if x_axis_label and metric_labels: + post_processing.append( + { + "operation": "pivot", + "options": { + "index": [x_axis_label], + "columns": [], + "aggregates": { + label: {"operator": "mean"} for label in metric_labels + }, + "drop_missing_columns": not form_data.get( + "show_empty_columns", False + ), + }, + } + ) + if form_data.get("resample_method") and form_data.get("resample_rule"): + zero_fill = form_data["resample_method"] == "zerofill" + post_processing.append( + { + "operation": "resample", + "options": { + "method": "asfreq" if zero_fill else form_data["resample_method"], + "rule": form_data["resample_rule"], + "fill_value": 0 if zero_fill else None, + }, + } + ) + rolling_type = form_data.get("rolling_type") + columns_map = {label: label for label in metric_labels} + if rolling_type == "cumsum": + post_processing.append( + {"operation": "cum", "options": {"operator": "sum", "columns": columns_map}} + ) + elif rolling_type in {"sum", "mean", "std"}: + post_processing.append( + { + "operation": "rolling", + "options": { + "rolling_type": rolling_type, + "window": int(form_data.get("rolling_periods") or 1), + "min_periods": int(form_data.get("min_periods") or 0), + "columns": columns_map, + }, + } + ) + post_processing.append({"operation": "flatten"}) + query["post_processing"] = post_processing + queries = [query] + if form_data.get("aggregation") == "raw": + overall = dict(query) + overall.update( + { + "columns": [], + "is_timeseries": False, + "post_processing": [], + } + ) + queries.append(overall) + return queries + + +def _table_queries( # noqa: C901 + form_data: dict[str, Any], query: dict[str, Any] +) -> list[dict[str, Any]]: + if is_raw_query_mode(form_data): + # The extractor already applies the raw-mode contract, including native + # ``order_by_cols`` parsing. Do not synthesize metric ordering. + query["columns"] = list( + form_data.get("all_columns") or form_data.get("columns") or [] + ) + query["metrics"] = [] + if raw_orderby := orderby_from_form_data(form_data, [], "table"): + query["orderby"] = raw_orderby + else: + query.pop("orderby", None) + return [query] + + metrics = list(query.get("metrics") or []) + query["columns"] = _table_temporalized_columns( + form_data, list(query.get("columns") or []) + ) + percent_metrics = _as_list(form_data.get("percent_metrics")) + for metric in percent_metrics: + if _label(metric, metric=True) not in { + _label(existing, metric=True) for existing in metrics + }: + metrics.append(metric) + query["metrics"] = metrics + query["orderby"] = orderby_from_form_data(form_data, metrics, "table") + post_processing: list[dict[str, Any]] = [] + resolved_offsets = _table_time_offsets(form_data) + comparison_enabled = ( + form_data.get("comparison_type") in _TIME_COMPARISON_TYPES + and bool(metrics) + and bool(_as_list(form_data.get("time_compare"))) + ) + contribution: dict[str, Any] | None = None + if percent_metrics: + base_labels = [_label(metric, metric=True) for metric in percent_metrics] + labels = [ + label + for metric_label in base_labels + for label in ( + [ + metric_label, + *(f"{metric_label}__{offset}" for offset in resolved_offsets), + ] + if comparison_enabled + else [metric_label] + ) + ] + labels = list(dict.fromkeys(labels)) + contribution = { + "operation": "contribution", + "options": { + "columns": labels, + "rename_columns": [f"%{label}" for label in labels], + }, + } + post_processing.append(contribution) + + metric_labels = [_label(metric, metric=True) for metric in metrics] + offset_map = _metric_offset_map(form_data, metric_labels, resolved_offsets) + time_offsets = resolved_offsets if offset_map else [] + if offset_map and form_data.get("comparison_type") != "values": + post_processing.append( + { + "operation": "compare", + "options": { + "source_columns": list(offset_map.values()), + "compare_columns": list(offset_map), + "compare_type": form_data.get("comparison_type"), + "drop_original_columns": True, + }, + } + ) + if post_processing: + query["post_processing"] = post_processing + else: + query.pop("post_processing", None) + query["time_offsets"] = time_offsets + + is_download = form_data.get("result_format") in {"csv", "xlsx"} or ( + form_data.get("result_format") == "json" + and form_data.get("result_type") == "results" + ) + if is_download: + if form_data.get("row_limit") is not None: + query["row_limit"] = int(form_data["row_limit"]) + query["row_offset"] = 0 + elif form_data.get("server_pagination"): Review Comment: Fixed in 9f7a3a7ac6840564732d93b0c845bd9fa88704fa at the Excel rebuild caller: a copied form-data payload sets result_format=xlsx before shared query construction. This preserves the configured row limit and excludes interactive page/count queries without changing interactive MCP query semantics or saved chart params. Aggregate/raw Table regressions assert one query, the full 1000-row limit rather than page length 10, no count query, and unchanged saved params. Focused suite: 845 passed; pre-commit passed. The broader local suite passed 5,040 tests. All 13 required checks passed on this exact head; no check failures or pending checks remain. -- 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]
