hughhhh commented on code in PR #43870: URL: https://github.com/apache/superset/pull/43870#discussion_r4319671452
########## superset/connectors/sqla/partition_mapping.py: ########## @@ -0,0 +1,1613 @@ +# 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. +""" +Partition filter mapping. + +Datasets on Hadoop-family engines are commonly partitioned on a *technical* +column -- an epoch integer, a lowercased region key -- that no analyst would +filter on. Unless a query carries a predicate on that column the engine scans +every partition. + +A dataset owner names one partition column ``p``, one business column that +filters are mirrored from, and a value transform ``T`` (a SQL expression +containing a ``:value`` placeholder). Superset then appends an equivalent +predicate on ``p`` to every query, so chart authors change nothing and queries +prune. + +The load-bearing assumption +--------------------------- +Everything here reasons about ``T(col) op T(v)``, but what is emitted is +``p op T(v)`` -- a predicate on a *physically different column*. The step from +one to the other is:: + + p = T(mapped_col) for every row in the table + +Superset cannot verify that; it is a property of whatever ETL populates the +partition column. If that job lags, backfills with different logic, or writes +the partition key in a different timezone, mirrored predicates silently drop +real rows. The mapping is only as trustworthy as the pipeline behind it. + +Operator safety +--------------- +A mirrored predicate ``P2`` may only be ``AND``-ed onto a query when the +original predicate ``P1`` *implies* it: + +=========================================== ============================= +Original Safe when +=========================================== ============================= +``col = v``, ``col IN (...)`` ``T`` is a function *and* the + engine compares ``col`` + byte-exactly +``col >=|>|<|<= v``, ``TEMPORAL_RANGE`` only if ``T`` is monotonic +``col != v``, ``NOT IN``, ``LIKE``, ... never +=========================================== ============================= + +Equality under the engine's own rules +------------------------------------- +"``T`` is a function" gives ``col = v`` implies ``T(col) = T(v)`` for *value* +equality. The engine compares with *SQL* equality, and the two part company +wherever that comparison is not byte-exact: under a case-insensitive collation +a stored ``country`` of ``'us'`` satisfies a filter for ``'US'``, while the +mirror ``region_key = hex('US')`` excludes the row and the chart loses it with +nothing to indicate why. Trailing-space-insensitive ``CHAR`` comparison says +the same thing about padding. + +So equality and ``IN`` are withdrawn for a *string* mapped column unless the +engine spec declares ``binary_string_comparison`` -- `equality_mirrors_safely`. +Numeric and temporal comparison is exact everywhere and is untouched, and range +mirroring is untouched too: it already rests on the owner declaring ``T`` +order-preserving *with respect to the column's own order*, which is an +assertion about the very semantics this is checking for. + +What the gate cannot see is a column declaring its own collation +(``country COLLATE NOCASE``) on an otherwise byte-exact engine. Nothing in +SQLAlchemy's reflection or the engine specs exposes that, so there it remains +the owner's assumption, like ``p = T(mapped_col)`` itself. + +Negations are never safe because ``T`` need not be injective: +``lower(:value)`` with ``country != 'US'`` mirrors to ``region_key != 'us'``, +which wrongly excludes rows whose ``country`` is already lowercase ``'us'`` -- +rows the original filter *keeps*. + +Monotonicity is a property of the transform, not of the column's data type: +``hour(:value)``, ``date_format(:value, 'dd')`` and ``dayofweek(:value)`` are +all reasonable transforms on a ``TIMESTAMP`` column and none of them preserve +ordering. It is therefore declared by the owner, not inferred. + +Time grains +----------- +A grained filter -- drill-to-detail, mostly -- compares the *truncated* column, +``trunc(col) op v``, so the raw bounds it carries do not describe the rows it +keeps. A row in the final partial bucket satisfies ``trunc(col) < until`` while +``col < until`` excludes it. + +Which direction a grain rounds is not knowable from the duration alone, and the +obvious guess is wrong: ``WEEK_ENDING_SATURDAY`` rounds *forward* on Hive and +Presto, and Ocient's grains are ``ROUND``, i.e. to nearest. What every grain +does satisfy is a bound on the displacement:: + + |trunc(ts) - ts| < width(grain) + +so widening *both* bounds by one bucket width is no narrower than the real +predicate whichever way the grain rounds -- and it needs no monotonicity of +``trunc`` itself, which is what rescues Hive's oddly-anchored ``P1W``. Width is +a property of the grain, not of the engine, which is what makes this +maintainable; see `grain_bucket_width`. A grain whose SQL an operator supplied +has no known width, and does not mirror at all. +""" + +from __future__ import annotations + +import hashlib +import logging +import re +from dataclasses import dataclass +from functools import lru_cache +from typing import Any, cast, TYPE_CHECKING + +import numpy as np +import pandas as pd +import sqlalchemy as sa +from dateutil.relativedelta import relativedelta +from flask import current_app as app +from flask_babel import lazy_gettext as _ +from sqlalchemy.engine.interfaces import Dialect +from sqlalchemy.sql.elements import ColumnElement + +from superset.constants import LRU_CACHE_MAX_SIZE, TimeGrain +from superset.exceptions import ( + QueryClauseValidationException, + SupersetParseError, + SupersetSecurityException, +) +from superset.extensions import cache_manager, feature_flag_manager +from superset.sql.parse import SQLScript, SQLStatement +from superset.utils import core as utils, json +from superset.utils.core import FilterOperator + +if TYPE_CHECKING: + from superset.connectors.sqla.models import SqlaTable, TableColumn + from superset.db_engine_specs.base import BaseEngineSpec + from superset.models.core import Database + +logger = logging.getLogger(__name__) + +FEATURE_FLAG = "PARTITION_FILTER_MAPPING" + +#: Longest transform accepted anywhere. A transform is one SQL expression an +#: owner types by hand, so this is generous; the point is that it is bounded. +#: Every entry point enforces it -- the typed column field, the import schema +#: and the preview request -- because `is_transform_active` parses the stored +#: value on each Explore load, and an unbounded string would make that parse +#: the expensive part of rendering a chart. +MAX_TRANSFORM_LENGTH = 1024 + +#: Placeholder the owner writes in the transform, e.g. ``unix_timestamp(:value)``. +#: Matched with word boundaries so ``:values`` is not mistaken for it. +VALUE_PLACEHOLDER_RE = re.compile(r":value\b") + +#: Balanced Jinja blocks. The probe would render these in a different context +#: at a different time from the chart query, so they are rejected at save time. +JINJA_BLOCK_RE = re.compile(r"\{\{.*?\}\}|\{%.*?%\}|\{#.*?#\}", re.DOTALL) + +#: Substituted for ``:value`` before parsing -- sqlglot rejects a bare ``:value`` +#: on most dialects. Mirrors the ``_JINJA_BLOCK_RE`` -> ``NULL`` trick used by +#: ``validate_stored_expression``. +_PARSE_STANDIN = "NULL" + +#: Functions whose value depends on wall-clock time or randomness. The probe +#: runs in a different session at a different moment from the chart query and +#: its result is then cached, so any of these freezes a snapshot of probe time +#: into the emitted predicate. +NON_DETERMINISTIC_FUNCTIONS = { + "CURRENT_DATE", + "CURRENT_TIME", + "CURRENT_TIMESTAMP", + "NOW", + "RAND", + "RANDOM", + "UUID", +} + +#: Functions that mean "now" only in their zero-argument form. On Hive and +#: Impala ``unix_timestamp()`` is the current time while ``unix_timestamp(x)`` +#: -- the canonical transform for this feature -- is pure. +NON_DETERMINISTIC_WHEN_NILADIC = {"UNIX_TIMESTAMP"} + +#: Safe for any function ``T``. +MIRRORABLE_ALWAYS = {FilterOperator.EQUALS, FilterOperator.IN} + +#: Safe only when ``T`` preserves ordering. +MIRRORABLE_IF_MONOTONIC = { + FilterOperator.GREATER_THAN, + FilterOperator.GREATER_THAN_OR_EQUALS, + FilterOperator.LESS_THAN, + FilterOperator.LESS_THAN_OR_EQUALS, + FilterOperator.TEMPORAL_RANGE, +} + + +#: Every operator that can be mirrored under *some* transform. The preview +#: endpoint accepts these; whether a given one actually mirrors still depends on +#: the monotonicity declaration. +MIRRORABLE_OPERATORS = MIRRORABLE_ALWAYS | MIRRORABLE_IF_MONOTONIC + +#: Operators the preview endpoint can construct. `TEMPORAL_RANGE` is mirrored by +#: the query path, but as *two* bounds that `_collect_partition_mirror_range` +#: decomposes a time range into -- the preview request carries sample values, not +#: a since/until pair, so there is no range for it to build. The editor never +#: asks for one either: `previewOperatorFor` sends `>=`, `=` or `IN`. +PREVIEWABLE_OPERATORS = MIRRORABLE_OPERATORS - {FilterOperator.TEMPORAL_RANGE} + + +def mirrorable_operators( + is_monotonic: bool, *, equality_is_safe: bool = True +) -> set[FilterOperator]: + """ + The operators whose predicates may be mirrored onto the partition column. + + :param is_monotonic: whether the owner declared the transform + order-preserving + :param equality_is_safe: whether the engine's ``=`` on the mapped column + compares the way ``T`` was written against -- see + `equality_mirrors_safely` + + Both call sites -- the query path through `PartitionMapping.mirrors` and the + Explore indicator through `partition_filter_mapping_summary` -- come through + here, which is what keeps the glyph from promising pruning the SQL will not + do. + """ + operators = MIRRORABLE_ALWAYS if equality_is_safe else set() + if is_monotonic: + operators = operators | MIRRORABLE_IF_MONOTONIC + return set(operators) + + +def equality_mirrors_safely( + column: TableColumn | None, + db_engine_spec: type[BaseEngineSpec], +) -> bool: + """ + Whether ``col = v`` on this column implies ``T(col) = T(v)`` in the engine. + + The operator matrix calls equality safe "for any function ``T``" because + ``T`` is a function, so ``col = v`` gives ``T(col) = T(v)``. That reasons + about *value* equality while the engine reasons about *SQL* equality, and + the two part company under any comparison that is not byte-exact: with a + case-insensitive collation a stored ``country`` of ``'us'`` satisfies a + filter for ``'US'``, but the mirror ``region_key = hex('US')`` excludes the + row and the chart loses it with no indication why. Trailing-space-insensitive + ``CHAR`` comparison says the same thing about padding. + + Only string columns are at risk; numeric and temporal comparison is exact + everywhere. And only equality: range mirroring already requires the owner + to declare ``T`` order-preserving *with respect to the column's own order*, + which is an assertion about the same comparison semantics this is checking + for -- so the declaration covers it where equality has nothing to cover it. + + What this cannot see is a column that declares its own collation + (``country COLLATE NOCASE``) on an otherwise byte-exact engine. Neither + SQLAlchemy's reflection nor the engine specs expose it, so on such a column + the assumption stays where ``p = T(mapped_col)`` already is: with the owner. + """ + if column is None: + return True + try: + is_string = column.type_generic == utils.GenericDataType.STRING + except Exception: # pylint: disable=broad-except # noqa: BLE001 + # An unresolvable type is treated as a string: the gate exists to stop + # a silent wrong answer, so it fails closed. + return False + if not is_string: + return True + return bool(db_engine_spec.binary_string_comparison) + + +#: How wide one bucket of each built-in time grain is. +#: +#: Keyed on the ISO duration a filter carries in its ``grain``. Written out +#: rather than parsed: four of the week grains are ISO *intervals* with an +#: anchor (``P1W/1970-01-03T00:00:00Z``) that ``isodate.parse_duration`` +#: rejects outright, and ``PT0.5H`` / ``P0.25Y`` are fractional. A literal +#: table is also the thing a reviewer can check a line at a time. +#: +#: ``relativedelta`` rather than ``timedelta`` so the calendar grains stay +#: calendar arithmetic: a month is not 30 days. +GRAIN_BUCKET_WIDTHS: dict[str, relativedelta] = { + TimeGrain.SECOND: relativedelta(seconds=1), + TimeGrain.FIVE_SECONDS: relativedelta(seconds=5), + TimeGrain.THIRTY_SECONDS: relativedelta(seconds=30), + TimeGrain.MINUTE: relativedelta(minutes=1), + TimeGrain.FIVE_MINUTES: relativedelta(minutes=5), + TimeGrain.TEN_MINUTES: relativedelta(minutes=10), + TimeGrain.FIFTEEN_MINUTES: relativedelta(minutes=15), + TimeGrain.THIRTY_MINUTES: relativedelta(minutes=30), + TimeGrain.HALF_HOUR: relativedelta(minutes=30), + TimeGrain.HOUR: relativedelta(hours=1), + TimeGrain.SIX_HOURS: relativedelta(hours=6), + TimeGrain.DAY: relativedelta(days=1), + TimeGrain.WEEK: relativedelta(days=7), + TimeGrain.WEEK_STARTING_SUNDAY: relativedelta(days=7), + TimeGrain.WEEK_STARTING_MONDAY: relativedelta(days=7), + TimeGrain.WEEK_ENDING_SATURDAY: relativedelta(days=7), + TimeGrain.WEEK_ENDING_SUNDAY: relativedelta(days=7), + TimeGrain.MONTH: relativedelta(months=1), + TimeGrain.QUARTER: relativedelta(months=3), + TimeGrain.QUARTER_YEAR: relativedelta(months=3), + TimeGrain.YEAR: relativedelta(years=1), +} + + +def grain_bucket_width(grain: str | None, engine: str) -> relativedelta | None: + """ + How far a grain's truncation can move a timestamp, or ``None`` if unknown. + + A grained filter compares the *truncated* column, so the raw bounds it + carries do not describe the rows it keeps. Widening both bounds by one + bucket recovers a predicate that is no narrower than the real one -- see + `_collect_partition_mirror_range` for the argument. That only works for a + grain whose bucket width Superset knows, which excludes anything an + operator supplied. + + :param grain: the ISO duration from the filter's ``grain`` + :param engine: the engine spec's ``engine``, to check per-engine overrides + """ + if not grain: + return None + + # An operator-declared grain carries whatever duration string they typed, + # and `TIME_GRAIN_ADDON_EXPRESSIONS` can redefine a *built-in* grain's SQL + # per engine -- `P1D` could be anything at all. Neither has a width we can + # claim to know, so both fall back to not mirroring. + if grain in app.config["TIME_GRAIN_ADDONS"]: + return None + if grain in app.config["TIME_GRAIN_ADDON_EXPRESSIONS"].get(engine, {}): + return None + + return GRAIN_BUCKET_WIDTHS.get(grain) + + +@dataclass(frozen=True) +class PartitionMapping: + """A resolved, usable partition filter mapping.""" + + partition_column: str + mapped_column: str + value_transform: str + is_monotonic: bool + #: Whether the engine compares the mapped column the way ``T`` was written + #: against. Resolved once where the column and the engine are both in hand, + #: rather than re-derived at each `mirrors` call. + equality_is_safe: bool = True + + def mirrors(self, operator: FilterOperator) -> bool: + return operator in mirrorable_operators( + self.is_monotonic, equality_is_safe=self.equality_is_safe + ) + + +def contains_value_placeholder(transform: str | None) -> bool: + """Whether the transform contains the ``:value`` placeholder.""" + return bool(transform) and VALUE_PLACEHOLDER_RE.search(transform or "") is not None + + +def contains_jinja(transform: str | None) -> bool: + """Whether the transform contains a balanced Jinja block.""" + return bool(transform) and JINJA_BLOCK_RE.search(transform or "") is not None + + +def parse_skeleton(transform: str) -> str: + """ + The transform with ``:value`` substituted out, ready for a SQL parser. + + ``sanitize_clause`` / sqlglot choke on a bare ``:value`` on most dialects, + so the placeholder is swapped for a benign literal first -- the same trick + ``validate_stored_expression`` uses for Jinja blocks. + """ + return VALUE_PLACEHOLDER_RE.sub(_PARSE_STANDIN, transform) + + +#: Prefix the transform is wrapped in before parsing. Its length is subtracted +#: from any reported column so positions refer to what the owner actually typed. +_SELECT_PREFIX = "SELECT " + + +def _parse_skeleton(transform: str, engine: str) -> SQLStatement | None: + """ + Parse ``SELECT <transform>`` with the placeholder substituted out. + + Returns ``None`` when the transform does not parse. + """ + try: + return SQLStatement(f"{_SELECT_PREFIX}{parse_skeleton(transform)}", engine) + except SupersetParseError: + return None + + +def parse_error_detail(transform: str, engine: str) -> str | None: + """ + Where the parser gave up on the transform. + + Returns ``None`` when it parses, or when the parser offered no position. + Note that sqlglot parses unknown functions happily -- a misspelled function + name is not a parse error, it is an engine error, and surfaces only when the + transform is evaluated. + + The parser's own ``highlight`` is deliberately dropped: it would name the + ``NULL`` we substituted for ``:value``, which is not a token the owner typed. + """ + try: + SQLStatement(f"{_SELECT_PREFIX}{parse_skeleton(transform)}", engine) + except SupersetParseError as ex: + column = (ex.error.extra or {}).get("column") + if not isinstance(column, int): + return None + return str( + _( + "syntax error at position %(position)d.", + position=_position_in_transform(transform, column), + ) + ) + return None + + +def _position_in_transform(transform: str, parsed_column: int) -> int: + """ + Map a column in the parsed skeleton back to the transform as typed. + + Two substitutions stand between them: the ``SELECT`` prefix, and every + ``:value`` that became a shorter ``NULL``. Without unwinding both, a + reported position drifts left by two characters per placeholder ahead of it + -- which is worst exactly where transforms usually break, at the end. + """ + position = max(parsed_column - len(_SELECT_PREFIX), 0) + shift = len(":value") - len(_PARSE_STANDIN) + preceding = sum( + 1 + for index, match in enumerate(VALUE_PLACEHOLDER_RE.finditer(transform)) + if match.start() - index * shift < position + ) + return position + preceding * shift + + +def is_parseable(transform: str | None, engine: str) -> bool: + """ + Whether the transform parses as a single select expression. + + "Single" is the load-bearing word. `SELECT lower(:value), 'x'` parses just + as happily as `SELECT lower(:value)`, but it returns two columns per input + value, and the probe reads one column per input -- so an `IN` filter would + get a predicate built from the wrong halves of the wrong rows. Rejecting the + list here is what lets the probe trust its own column count. + """ + if not transform or not transform.strip(): + return False + statement = _parse_skeleton(transform, engine) + return statement is not None and statement.count_select_expressions() == 1 + + +def is_bare_expression(transform: str | None, engine: str) -> bool: + """ + Whether the transform is a scalar expression and nothing more. + + `is_parseable` is not enough, because it counts only the projection. + ``secret || :value FROM vault`` holds exactly one select expression, so it + parses as "a single expression" -- and `build_probe_sql` then emits + ``SELECT secret || 'us' AS v0``, where its own alias is read as a table + alias on the FROM clause the transform smuggled in. The probe reads a + column the owner was never granted and hands it back through the predicate + the preview panel renders. + + A transform is a scalar function of ``:value`` by definition -- the whole + feature rests on ``partition_col = T(mapped_col)``, which only type-checks + for scalar ``T`` -- so there is no legitimate transform with a clause of + its own, and none with a sub-query either. + """ + if not transform or not transform.strip(): + return False + statement = _parse_skeleton(transform, engine) + return ( + statement is not None + and statement.is_bare_select_expression() + and not statement.has_subquery() + ) + + +def is_unfinished(transform: str | None, engine: str) -> bool: + """ + Whether the transform is not SQL yet, as opposed to the wrong SQL. + + The distinction matters because the two deserve opposite treatment. A + half-typed ``unix_timestamp(:value`` is what a text input produces on the + way to something valid: a PUT stores it and reports the mapping inactive, + so discarding it would mean an export could not round-trip the dataset it + came from. ``unix_timestamp(:value); DROP TABLE t`` is not on the way to + anything, and has to be refused wherever it is offered. + + Both fail `is_parseable`, and both fail to parse as a single *statement* -- + so neither of those tells them apart. Parsing as a *script* does: the + multi-statement form is two valid statements, while unfinished text is no + statement at all. + """ + if not transform or not transform.strip(): + return False + try: + SQLScript(f"{_SELECT_PREFIX}{parse_skeleton(transform)}", engine) + except SupersetParseError: + return True + return False + + +def find_non_deterministic_functions(transform: str, engine: str) -> set[str]: + """ + Names of non-deterministic functions the transform calls. + + ``UNIX_TIMESTAMP`` is only reported in its zero-argument form, which means + "now" on Hive and Impala; the one-argument form is the canonical temporal + transform and stays allowed. + """ + statement = _parse_skeleton(transform, engine) + if statement is None: + return set() + + found = { + name + for name in NON_DETERMINISTIC_FUNCTIONS + if statement.check_functions_present({name}) + } + return found | _find_niladic_calls(statement) + + +def _find_niladic_calls(statement: SQLStatement) -> set[str]: + """ + Names from ``NON_DETERMINISTIC_WHEN_NILADIC`` called with no arguments. + + Note some dialects resolve the zero-argument form themselves -- Hive parses + ``unix_timestamp()`` straight to ``CURRENT_TIMESTAMP`` -- in which case the + name-based check above has already caught it. This is the backstop for the + dialects that do not. + """ + return NON_DETERMINISTIC_WHEN_NILADIC & statement.get_niladic_functions() + + +def resolve_partition_mapping(datasource: SqlaTable) -> PartitionMapping | None: + """ + Resolve the dataset's mapping, or ``None`` when nothing may be mirrored. + + Every bail-out here is defensive as well as functional: save-time validation + rejects most of these, but rows predating the validation can still violate + the invariants, and a column sync can invalidate a mapping that was fine + when it was written. + + The transform gate is `is_transform_active`, the same function the Explore + indicator reads, which is `validate_transform` with the messages discarded. + Anything narrower here would be a second, weaker statement of the same rule: + a transform calling `now()` that reached storage without passing + `UpdateDatasetCommand` -- through import, or through a bundle written by hand + -- would be reported inactive by the editor and still mirrored by this + function, freezing a snapshot of probe time into the predicate with nothing + on screen to say so. + """ + if not feature_flag_manager.is_feature_enabled(FEATURE_FLAG): + return None + + partition_column = getattr(datasource, "partition_column", None) + if not partition_column: + return None + + columns_by_name = {column.column_name: column for column in datasource.columns} + if partition_column not in columns_by_name: + # The partition column was dropped by a column sync or at the source. + return None + + mapped_column_name = ( + getattr(datasource, "partition_mapped_column", None) or datasource.main_dttm_col + ) + if not mapped_column_name or mapped_column_name not in columns_by_name: + return None + + if mapped_column_name == partition_column: + # Self-mapping: the mirrored predicate would duplicate the original. + return None + + mapped_column = columns_by_name[mapped_column_name] + transform = getattr(mapped_column, "partition_value_transform", None) + if not is_transform_active(transform, datasource.database.backend): + return None + + if has_active_advanced_data_type(mapped_column): + # `translate_filter` builds its own predicate shape from *translated* + # values, so the `(operator, value)` pair the operator matrix reasons + # about does not exist and mirroring would apply the wrong values. + return None + + return PartitionMapping( + partition_column=str(partition_column), + mapped_column=str(mapped_column_name), + value_transform=cast(str, transform), + is_monotonic=bool( + getattr(mapped_column, "partition_transform_is_monotonic", False) + ), + equality_is_safe=equality_mirrors_safely( + mapped_column, datasource.database.db_engine_spec + ), + ) + + +def has_active_advanced_data_type(column: TableColumn) -> bool: + """ + Whether the column's advanced data type is configured and switched on. + + Such a column is never mirrored: ``translate_filter`` builds its own + predicate shape from translated values, so the ``(operator, value)`` pair + the operator matrix reasons about does not exist. + """ + advanced_data_type = getattr(column, "advanced_data_type", None) + if not advanced_data_type: + return False + if not feature_flag_manager.is_feature_enabled("ENABLE_ADVANCED_DATA_TYPES"): + return False + return advanced_data_type in app.config.get("ADVANCED_DATA_TYPES", {}) + + +def _denylist_engine_key(database: "Database") -> str: + """ + The name ``DISALLOWED_SQL_*`` is keyed by for this database. + + The engine spec's own ``engine`` first, because that is what every other + denylist gate uses (`_raise_for_disallowed_sql`, `sql_lab`) and what an + operator writing the config reads in the documentation; a spec covering + several SQLAlchemy backends through ``engine_aliases`` reports the one + canonical name under which its entry is written. + + Falling back to the URL's backend, because resolving the spec loads the + SQLAlchemy dialect entrypoint, which imports the driver package. On a + deployment missing an optional driver that raises rather than merely being + absent, asking for the spec here would turn a dataset import into a hard + failure -- and for every engine without aliases the two names are the same + string anyway. + """ + try: + return database.db_engine_spec.engine + except Exception: # pylint: disable=broad-except # noqa: BLE001 + return database.backend + + +def stored_expression_error( + database: "Database", + catalog: str | None, + schema: str | None, + transform: str, +) -> str | None: + """ + Why this transform may not be stored or run, if there is a reason. + + Stricter than the policy a general stored expression goes through, because + a value transform is a narrower thing: `build_probe_sql` binds only + `:value` and splices the rest of the transform in as SQL text, which the + engine then executes, so an ungated transform is arbitrary SQL. A dataset + editor without SQL Lab could store `(SELECT secret FROM protected_table + LIMIT 1) || :value`, or `secret || :value FROM protected_table`, and read + the answer back out of the emitted predicate the preview panel renders. + + So the transform must be a bare scalar expression: no clause of its own and + no sub-query, whatever `ALLOW_ADHOC_SUBQUERY` says. That is not a + restriction the feature pays for -- it rests on + ``partition_col = T(mapped_col)``, which only type-checks for scalar ``T`` + -- and it is what makes the table denylist and the RLS rewrite moot here, + since neither has a table reference left to govern. + + Returns the engine-agnostic reason as a string rather than raising, + because its four callers disagree about what to do with it: the preview + reports it at the field, a PUT and the legacy datasource save refuse the + write, the importer drops the transform and keeps the dataset, and the + probe simply declines to run. Raising would make three of those four + write a `try` around a question. + + Imported inside the function rather than at module scope: + `connectors.sqla.models` imports this module, so the dependency only runs + one way at import time. + """ + from superset.connectors.sqla.models import ( # pylint: disable=import-outside-toplevel,cyclic-import + validate_stored_expression, + ) + + statement = _parse_skeleton(transform, database.backend) + if statement is None: + return str( + _("A partition value transform must parse as a single SQL expression.") + ) + + # The shape gate again, even though `validate_transform` blocks the same + # thing at save time. This is the last door before the transform becomes SQL + # an engine runs, and the only door a row written by an earlier release -- + # or by an importer that ran with the feature flag off -- still passes + # through. + if not statement.is_bare_select_expression(): + return str( + _( + "A partition value transform must be a single SQL expression, " + "with no FROM, WHERE or other clause." + ) + ) + + # Unconditionally, not under `ALLOW_ADHOC_SUBQUERY`. A transform is a scalar + # function of `:value`, so a sub-query in one has no legitimate use, and it + # is a read primitive whose answer the preview panel renders back to its + # caller. Refusing it is also what lets this function skip the table + # denylist and the row-level-security rewrite: with no FROM clause and no + # sub-query there is no table reference for either to govern. + if statement.has_subquery(): + return str(_("A partition value transform cannot contain a sub-query.")) + + denied = app.config["DISALLOWED_SQL_FUNCTIONS"].get( + _denylist_engine_key(database), set() + ) + if denied and (found := statement.get_disallowed_functions(denied)): + return str( + _( + "A partition value transform cannot call %(functions)s.", + functions=", ".join(sorted(found)), + ) + ) + + # Kept as the tail call rather than replaced: this is where `sanitize_clause` + # runs. Its own sub-query branch returns rewritten, RLS-injected SQL that + # this function has no way to propagate -- but that branch is unreachable + # from here now, because a transform containing a sub-query was refused + # above, so discarding the return value is correct rather than a leak. + try: + validate_stored_expression(database, catalog, schema, parse_skeleton(transform)) + except SupersetSecurityException as ex: + return str(ex.error.message) + except QueryClauseValidationException as ex: + return str(ex.message) + return None + + +def build_probe_sql( + transform: str, + values: list[Any], + dialect: Dialect | None = None, + from_suffix: str = "", +) -> str: + """ + Compile a single ``SELECT`` that evaluates the transform at every value. + + Values are attacker-controlled (a Gamma user picks filter values), so they + are bound as parameters and rendered by the dialect's own literal processor + rather than interpolated into the SQL text. + + ``from_suffix`` comes from the engine spec's ``select_without_from_suffix``. + A ``SELECT`` with no ``FROM`` is not universal SQL: Oracle and Db2 need a + one-row table to select from, and without it every probe on those engines + raises -- which `_run_probe` swallows, so the only symptom is a correctly + configured mapping that silently never prunes. + + Note this deliberately does *not* go through ``BaseEngineSpec``'s text + helper, which escapes ``:`` on every engine but Athena and would destroy the + ``:value`` placeholder before it can be bound. + + Callers must have run `stored_expression_error` first. The ``AS v{index}`` + alias and ``from_suffix`` are only safe on a transform with no clause of its + own; on one carrying its own FROM they attach to that instead. + """ + selections = [] + for index, value in enumerate(values): + clause = sa.text(transform).bindparams(sa.bindparam("value", value=value)) Review Comment: Fixed. `probe_sql_is_evaluable` re-reads the finished compiled statement, so the gates and the engine now see the same SQL and a placeholder smuggled inside a quoted literal cannot open a sub-query the validation never saw. ########## superset/daos/dataset.py: ########## @@ -476,10 +546,98 @@ def _validate_column_date_formats( "python_date_format is an invalid date/timestamp format." ) + @staticmethod + def clear_unmapped_partition_transforms(model: SqlaTable) -> None: + """ + Drop the value transform from every column the mapping does not mirror. + + A mapping has exactly one mirrored column, so at most one column may + carry a transform. A transform parked on any other column is invisible + -- no row but the mapped one renders one -- yet it is still stored, and + it goes live the moment the mapped column resolves back to it. Clearing + an override is enough to do that: a null `partition_mapped_column` means + "follow `main_dttm_col`", so dropping a mapping would otherwise activate + whatever the default datetime column happened to be holding, turning a + request to remove a mapping into a request to add a different one. + + Enforced here rather than in the editor alone because the editor is only + one writer: a PUT, an `override_columns=true` metadata sync and an + import all reach the columns directly. The same argument + `clear_dangling_partition_mapping` makes about dangling columns. + + `DatasetDAO.update` calls this twice, before and after it applies the + request, and the reason is here rather than only at the call site: this + reads the mapping as it stands, so a single call can only enforce the + invariant against one of the two resolutions a mapping change has. + + Gated on the feature flag, unlike its sibling, because this discards + stored configuration rather than repairing a broken reference. With the + flag off nothing mirrors, so there is no armed mapping to disarm and + clearing would be pure loss. + """ + if not feature_flag_manager.is_feature_enabled(PARTITION_FILTER_MAPPING_FLAG): + return + + mapped_column = ( + (model.partition_mapped_column or model.main_dttm_col) + if model.partition_column + else None + ) + # `_upsert_columns` and `_override_columns` insert a new column with + # `db.session.add(TableColumn(..., table_id=model.id))` rather than + # appending to this relationship, and `BaseDAO.update` does not flush -- + # so a column created *and* given a transform in the same request was + # invisible here and kept it. That is exactly the stray transform this + # method exists to clear, waiting for the mapping to resolve back to it. + db.session.flush() + db.session.expire(model, ["columns"]) + for column in model.columns: + if column.column_name == mapped_column: + continue + if ( + column.partition_value_transform + or column.partition_transform_is_monotonic + ): + column.partition_value_transform = None + column.partition_transform_is_monotonic = False + + @staticmethod + def clear_dangling_partition_mapping( + model: SqlaTable, surviving_column_names: set[str] + ) -> None: + """ + Drop parts of the partition filter mapping whose columns no longer exist. + + A metadata sync can remove the partition column at the source, which + would otherwise leave the dataset pointing at a column that isn't there. + The query layer bails out defensively on a dangling mapping, so this is + about the dataset's stored state being honest rather than about + correctness of the SQL. + + Called from `update_columns` for both write paths: the editor clears the + mapping client-side too, but `override_columns=true` (an API-driven + metadata sync) bypasses the editor entirely, and the upsert path drops + every column the payload omits, so either one can take the mapped + column out from under the mapping. + """ + if ( Review Comment: Fixed. The cleanup is keyed on the final requested references — `requested.get("partition_column", item.partition_column)` and the same for `partition_mapped_column` and `main_dttm_col` — instead of the stored ones, so a rename in the same PUT no longer strikes the surviving mapping. ########## superset/datasets/api.py: ########## @@ -323,6 +401,8 @@ class DatasetRestApi(SoftDeleteApiMixin, BaseSupersetModelRestApi): ] show_columns = show_select_columns + [ "columns.type_generic", + # Engine-supplied pre-fill for the editor's value transform input. + "partition_value_transform_default", Review Comment: Fixed. `partition_filter_mapping` is in the GET's `show_columns` alongside `partition_column` and `partition_mapped_column`, so the save/reload boundary from Explore keeps the pruning indicators. ########## superset/datasets/schemas.py: ########## @@ -347,6 +373,12 @@ def fix_extra(self, data: dict[str, Any], **kwargs: Any) -> dict[str, Any]: datetime_format = fields.String( allow_none=True, validate=[Length(1, 100), validate_python_date_format] ) + partition_value_transform = fields.String( + allow_none=True, validate=Length(1, MAX_TRANSFORM_LENGTH) + ) + # Bundles predating the field must not claim their transform preserves + # ordering, which would silently enable range mirroring on import. + partition_transform_is_monotonic = fields.Boolean(load_default=False) Review Comment: Fixed. The import field is `fields.Boolean(allow_none=True)`, matching the model and the PUT schema, so an unchanged export round-trips. -- 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]
