Repository: apache/superset
File: superset/utils/core.py (Python)

Code before:

    :param column: The column name or identifier to check.
    :param datasource: The datasource containing metrics to search within.
    :return: The inferred metric type as a string, or an empty string if the
             column is not a metric or no valid operation is found.
    """

    from superset.connectors.sqla.models import SqlMetric

    metric: SqlMetric = next(
        (metric for metric in datasource.metrics if metric.metric_name == column),
        SqlMetric(metric_name=""),
    )

    if metric.metric_name == "":
        return ""

    expression: str = metric.expression

    match = re.match(
        r"(SUM|AVG|COUNT|COUNT_DISTINCT|MIN|MAX|FIRST|LAST)\((.*)\)", expression
    )

    if match:
        operation = match.group(1)
        return MetricMapType.get(operation, "")

    logger.warning("Unexpected metric expression type: %s", expression)
    return ""


def extract_dataframe_dtypes(
    df: pd.DataFrame,
    datasource: BaseDatasource | Query | None = None,
) -> list[GenericDataType]:
    """Serialize pandas/numpy dtypes to generic types"""

    # omitting string types as those will be the default type
    inferred_type_map: dict[str, GenericDataType] = {
        "floating": GenericDataType.NUMERIC,
        "integer": GenericDataType.NUMERIC,
        "mixed-integer-float": GenericDataType.NUMERIC,
        "decimal": GenericDataType.NUMERIC,
        "boolean": GenericDataType.BOOLEAN,
        "datetime64": GenericDataType.TEMPORAL,
        "datetime": GenericDataType.TEMPORAL,
        "date": GenericDataType.TEMPORAL,
    }

    columns_by_name: dict[str, Any] = {}
    if datasource:

Review comment (suggestion):
```suggestion
        return METRIC_MAP_TYPE.get(operation, "")
```

Code after:

    :param column: The column name or identifier to check.
    :param datasource: The datasource containing metrics to search within.
    :return: The inferred metric type as a string, or an empty string if the
             column is not a metric or no valid operation is found.
    """

    from superset.connectors.sqla.models import SqlMetric

    metric: SqlMetric = next(
        (metric for metric in datasource.metrics if metric.metric_name == column),
        SqlMetric(metric_name=""),
    )

    if metric.metric_name == "":
        return ""

    expression: str = metric.expression

    match = re.match(
        r"(SUM|AVG|COUNT|COUNT_DISTINCT|MIN|MAX|FIRST|LAST)\((.*)\)", expression
    )

    if match:
        operation = match.group(1)
        return METRIC_MAP_TYPE.get(operation, "")

    logger.warning("Unexpected metric expression type: %s", expression)
    return ""


def extract_dataframe_dtypes(
    df: pd.DataFrame,
    datasource: BaseDatasource | Query | None = None,
) -> list[GenericDataType]:
    """Serialize pandas/numpy dtypes to generic types"""

    # omitting string types as those will be the default type
    inferred_type_map: dict[str, GenericDataType] = {
        "floating": GenericDataType.NUMERIC,
        "integer": GenericDataType.NUMERIC,
        "mixed-integer-float": GenericDataType.NUMERIC,
        "decimal": GenericDataType.NUMERIC,
        "boolean": GenericDataType.BOOLEAN,
        "datetime64": GenericDataType.TEMPORAL,
        "datetime": GenericDataType.TEMPORAL,
        "date": GenericDataType.TEMPORAL,
    }

    columns_by_name: dict[str, Any] = {}
    if datasource: