sadpandajoe commented on code in PR #43527: URL: https://github.com/apache/superset/pull/43527#discussion_r3867629899
########## superset/common/tabular_query.py: ########## @@ -0,0 +1,392 @@ +# 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. + +"""Name-based tabular querying for any Explorable datasource. + +Shared by the REST query endpoint and the MCP query tools so the resolve → +validate → build → execute sequence exists once. The REST endpoint owns this +contract; other surfaces adapt to it. + +Type dispatch is deliberately absent: ``Explorable.get_query_result`` already +routes datasets to SQL execution and semantic views to the semantic-layer +mapper, so callers pass the datasource type as data and never branch on it. +""" + +from __future__ import annotations + +import difflib +from collections.abc import Sequence +from dataclasses import dataclass, field +from typing import Any, TYPE_CHECKING + +from superset.charts.data.form_data import set_query_context_form_data +from superset.common.chart_data import ChartDataResultFormat +from superset.daos.datasource import DatasourceDAO +from superset.superset_typing import Column, Metric +from superset.utils.core import DatasourceType, FilterOperator + +if TYPE_CHECKING: + from superset.explorables.base import Explorable + + +# (column name, descending) — mirrors SemanticQuery's OrderTuple, which pairs a +# metric/dimension with an OrderDirection. +OrderSpec = tuple[str, bool] + + +class TabularQueryValidationError(ValueError): + """Raised when a request cannot be satisfied by the target datasource.""" + + +@dataclass +class ResolvedExplorable: + """A datasource resolved and authorized, with its queryable name sets.""" + + explorable: Explorable + display_name: str + time_column: str | None + valid_dimensions: set[str] + valid_metrics: set[str] + dttm_columns: set[str] = field(default_factory=set) + warnings: list[str] = field(default_factory=list) + + def resolve_grain_column( + self, time_column: str | None, dimensions: Sequence[Column] | None + ) -> str | None: + """Pick the column a requested time grain should bucket. + + Precedence: an explicit ``time_column``, else a temporal name already + listed in ``dimensions`` (the natural way to ask for buckets), else the + column a ``time_range`` resolved to. + """ + if time_column: + return time_column + for dimension in dimensions or []: + if isinstance(dimension, str) and dimension in self.dttm_columns: + return dimension + return self.time_column + + +def validate_names( + requested: Sequence[str], + valid: set[str], + kind: str, + *, + empty_hint: str | None = None, + list_valid_on_miss: bool = False, + full_list_hint: str = "call get_dataset_info for the full list", Review Comment: A REST request with an unknown metric on a datasource with more than ten metrics returns this MCP-only `get_dataset_info` instruction, which HTTP clients cannot call. Could the REST path point callers at the new datasource metadata endpoint instead? -- 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]
