From 00cc0b36fb971e7adbb07d3a212313987824b54e Mon Sep 17 00:00:00 2001 From: Beto Dealmeida Date: Wed, 18 Nov 2026 17:51:09 -0500 Subject: [PATCH 1/4] Validate sketch family calls and report dialect compatibility --- .../api/graphql/scalars/node.py | 12 +- .../datajunction_server/models/metric.py | 20 +-- .../datajunction_server/sql/decompose.py | 73 ++++++++++- .../build_v3/sketch_family_e2e_test.py | 43 +++++++ .../tests/sql/decompose_test.py | 116 +++++++++++++++++- 5 files changed, 241 insertions(+), 23 deletions(-) diff --git a/datajunction-server/datajunction_server/api/graphql/scalars/node.py b/datajunction-server/datajunction_server/api/graphql/scalars/node.py index e32d7ed4e..e62a546ae 100644 --- a/datajunction-server/datajunction_server/api/graphql/scalars/node.py +++ b/datajunction-server/datajunction_server/api/graphql/scalars/node.py @@ -53,7 +53,8 @@ from datajunction_server.models.node import NodeType as NodeType_ from datajunction_server.models.reaggregate import parse_reaggregate_spec from datajunction_server.naming import SEPARATOR -from datajunction_server.sql.parsing.backends.antlr4 import ast, parse +from datajunction_server.sql.decompose import incompatible_functions +from datajunction_server.sql.parsing.backends.antlr4 import parse NodeType = strawberry.enum(NodeType_) NodeStatus = strawberry.enum(NodeStatus_) @@ -610,13 +611,10 @@ def metric_metadata( incompatible: set[str] = set() if needs_ast: query_ast = parse(root.query) - functions = [func.function() for func in query_ast.find_all(ast.Function)] expression = str(query_ast.select.projection[0]) - incompatible = { - func.__name__.upper() - for func in functions - if Dialect.DRUID not in func.dialects - } + incompatible = set( + incompatible_functions(query_ast, Dialect.DRUID, root.reaggregate), + ) return MetricMetadata( # type: ignore direction=root.metric_metadata.direction if root.metric_metadata else None, unit=root.metric_metadata.unit.value diff --git a/datajunction-server/datajunction_server/models/metric.py b/datajunction-server/datajunction_server/models/metric.py index b717bda0d..49d52cd14 100644 --- a/datajunction-server/datajunction_server/models/metric.py +++ b/datajunction-server/datajunction_server/models/metric.py @@ -16,18 +16,21 @@ from datajunction_server.errors import DJWarning from datajunction_server.models.cube_materialization import MetricComponent from datajunction_server.models.engine import Dialect +from datajunction_server.models.filter_requirements import FilterRequirement from datajunction_server.models.node import ( DimensionAttributeOutput, MetricMetadataOutput, ) from datajunction_server.models.query import ColumnMetadata, V3ColumnMetadata -from datajunction_server.models.filter_requirements import FilterRequirement from datajunction_server.models.reaggregate import ReaggregateSpec from datajunction_server.models.sql import ScanEstimate, TranspiledSQL from datajunction_server.models.unit import unit_to_dict from datajunction_server.naming import SEPARATOR -from datajunction_server.sql.decompose import MetricComponentExtractor -from datajunction_server.sql.parsing.backends.antlr4 import ast, parse +from datajunction_server.sql.decompose import ( + MetricComponentExtractor, + incompatible_functions, +) +from datajunction_server.sql.parsing.backends.antlr4 import parse from datajunction_server.transpilation import transpile_sql from datajunction_server.typing import UTCDatetime @@ -133,12 +136,11 @@ async def parse_node( Parses a node into a metric. """ query_ast = parse(node.current.query) - functions = [func.function() for func in query_ast.find_all(ast.Function)] - incompatible_druid_functions = [ - func.__name__.upper() - for func in functions - if Dialect.DRUID not in func.dialects - ] + incompatible_druid_functions = incompatible_functions( + query_ast, + Dialect.DRUID, + node.current.reaggregate, + ) upstream_node = ( node.current.non_metric_parents[0].name if node.current.non_metric_parents diff --git a/datajunction-server/datajunction_server/sql/decompose.py b/datajunction-server/datajunction_server/sql/decompose.py index 3fc78dfb1..e5e33283d 100644 --- a/datajunction-server/datajunction_server/sql/decompose.py +++ b/datajunction-server/datajunction_server/sql/decompose.py @@ -203,9 +203,16 @@ class AggDecomposition(ABC): 2. Merge: How to combine pre-aggregated components (via `components`) 3. Combiner: How to produce the final metric value from merged components - Subclasses must define `components` and implement `combine()`. + Subclasses must define `components` and implement `combine()`. They may + validate authored calls with `validate()`. Declared families can advertise + output engines through `supported_dialects` for compatibility metadata. """ + # Output engines a declared family can serve through its combiner. Empty + # preserves the authored function's compatibility metadata for existing + # implementations. This does not describe the engine executing serialize. + supported_dialects: tuple[Dialect, ...] = () + @property @abstractmethod def components(self) -> list[ComponentDef]: @@ -220,6 +227,14 @@ def __init__(self, params: dict[str, Any] | None = None): """ self.params = params or {} + def validate(self, func: ast.Function) -> None: + """Validate an authored call before any decomposition shortcuts. + + Raise DJInvalidInputException for unsupported shapes. In particular, + families can reject DISTINCT before its generic raw-column path skips + combine(). Validation must not mutate the authored AST. + """ + @abstractmethod def combine( self, @@ -907,6 +922,46 @@ def get_decomposition( return decomp_class() +def incompatible_functions( + query_ast: ast.Query, + dialect: Dialect, + reaggregate: ReaggregateSpec | dict | None = None, +) -> list[str]: + """Functions incompatible with an engine, accounting for declared families. + + A family can replace only its registered aggregates, after validating the + authored call and parameters. Continue walking nested calls: replacing an + aggregate cannot make an incompatible scalar input expression executable. + """ + spec = parse_reaggregate_spec(reaggregate) + family = ( + FAMILY_DECOMPOSITION_REGISTRY.get(spec.fn) + if spec is not None and spec.fn is not None + else None + ) + incompatible = [] + for func in query_ast.find_all(ast.Function): + function = func.function() + compatible = dialect in function.dialects + if ( + family + and spec is not None + and function in family.aggregate_functions + and family.decomposition.supported_dialects + ): + try: + decomposition = family.decomposition(params=spec.params) + decomposition.validate(func) + compatible = dialect in decomposition.supported_dialects + except DJInvalidInputException: + # Metadata should still describe a persisted invalid metric; + # extraction reports the detailed validation error. + compatible = False + if not compatible: + incompatible.append(function.__name__.upper()) + return incompatible + + def component_params_match( component: MetricComponent, stored_params: dict[str, Any] | None, @@ -1736,6 +1791,16 @@ def _extract_base( "aggregation in this metric", ) + # Validate every selected implementation before either the + # non-decomposable fallback or the DISTINCT shortcut can bypass it. + decompositions = [ + (func, get_decomposition(dj_function, reaggregate)) + for func, dj_function in agg_funcs + ] + for func, decomposition in decompositions: + if decomposition is not None: + decomposition.validate(func) + # If any aggregation is non-decomposable, abort decomposition # entirely — the metric is non-decomposable as a whole. # @@ -1744,9 +1809,7 @@ def _extract_base( # family, and its aggregation function has no entry of its own. Ask # without the spec and every such metric aborts at this gate and # never reaches the family registry at all. - if any( - get_decomposition(dj_fn, reaggregate) is None for _, dj_fn in agg_funcs - ): + if any(decomposition is None for _, decomposition in decompositions): if dimension_reaggregate_rules(reaggregate): self._raise_unsupported_reaggregate_shape( "dimension-specific reaggregation requires a " @@ -1979,6 +2042,8 @@ def _decompose( # still get a sensible None return. return None + decomposition.validate(func) + # Build components from decomposition components = [ self._make_component(func, comp_def, query_ast) diff --git a/datajunction-server/tests/construction/build_v3/sketch_family_e2e_test.py b/datajunction-server/tests/construction/build_v3/sketch_family_e2e_test.py index 00e3ab45a..c517a3802 100644 --- a/datajunction-server/tests/construction/build_v3/sketch_family_e2e_test.py +++ b/datajunction-server/tests/construction/build_v3/sketch_family_e2e_test.py @@ -153,6 +153,8 @@ def digest_family(): aggregate_functions=(ApproxPercentile,), ) class _DigestDecomposition(AggDecomposition): + supported_dialects = (Dialect.SPARK, Dialect.TRINO, Dialect.DRUID) + @property def compression(self) -> int: return int(self.params.get("compression", 100)) @@ -487,3 +489,44 @@ async def test_metrics_layer_recomputes_from_source(percentile_metric): FROM v3_DOT_order_details_metrics """, ) + + +@pytest.mark.asyncio +async def test_family_compatibility_agrees_in_rest_and_graphql(percentile_metric): + client = percentile_metric + plain = "v3.plain_p95_line_total" + response = await client.post( + "/nodes/metric/", + json={ + "name": plain, + "query": "SELECT APPROX_PERCENTILE(line_total, 0.95) FROM v3.order_details", + "mode": "published", + }, + ) + assert response.status_code in (200, 201), response.json() + for name, expected in ((METRIC, []), (plain, ["APPROXPERCENTILE"])): + response = await client.get(f"/metrics/{name}") + assert response.status_code == 200, response.json() + assert response.json()["incompatible_druid_functions"] == expected + response = await client.post( + "/graphql", + json={ + "query": """ + query($names: [String!]!) { + findNodes(names: $names) { + current { metricMetadata { incompatibleDruidFunctions } } + } + } + """, + "variables": {"names": [name]}, + }, + ) + assert response.status_code == 200, response.json() + payload = response.json() + assert not payload.get("errors"), payload + assert ( + payload["data"]["findNodes"][0]["current"]["metricMetadata"][ + "incompatibleDruidFunctions" + ] + == expected + ) diff --git a/datajunction-server/tests/sql/decompose_test.py b/datajunction-server/tests/sql/decompose_test.py index 996fab3c6..1fb688866 100644 --- a/datajunction-server/tests/sql/decompose_test.py +++ b/datajunction-server/tests/sql/decompose_test.py @@ -28,17 +28,18 @@ from datajunction_server.sql import functions as dj_functions from datajunction_server.sql.decompose import ( DECOMPOSITION_REGISTRY, - FAMILY_DECOMPOSITION_REGISTRY, DUPLICATION_INVARIANT_AGGREGATIONS, + FAMILY_DECOMPOSITION_REGISTRY, AggDecomposition, ComponentDef, MetricComponentExtractor, decomposes_family, get_decomposition, - make_func, - parenthesize_if_needed, + incompatible_functions, is_duplication_invariant, is_metric_duplication_sensitive, + make_func, + parenthesize_if_needed, safe_denominator, wrap_divisions_in_nullif, ) @@ -3394,3 +3395,112 @@ def test_parenthesize_if_needed(outer: str, inner: str, expected: str): ) assert str(query.select.projection[0]) == expected + + +@pytest.mark.parametrize("extra", ["", " + MIN_BY(price, quantity)"]) +def test_family_validation_precedes_distinct_and_nondecomposable_fallback( + registered_family, + monkeypatch, + extra, +): + def validate(self, func): + if func.quantifier == ast.SetQuantifier.Distinct: + raise DJInvalidInputException("family does not support DISTINCT") + + monkeypatch.setattr(registered_family, "validate", validate) + with pytest.raises(DJInvalidInputException, match="does not support DISTINCT"): + MetricComponentExtractor(0)._extract_base( + parse(f"SELECT APPROX_PERCENTILE(DISTINCT price, 0.5){extra} FROM t"), + ReaggregateSpec(fn=ReaggregationFunction.TDIGEST), + ) + + +def test_direct_decomposition_validates_before_building_distinct_components( + registered_family, + monkeypatch, +): + def validate(self, func): + raise DJInvalidInputException("invalid family call") + + monkeypatch.setattr(registered_family, "validate", validate) + query = parse("SELECT APPROX_PERCENTILE(DISTINCT price, 0.5) FROM t") + with pytest.raises(DJInvalidInputException, match="invalid family call"): + MetricComponentExtractor(0)._decompose( + query.select.projection[0], + dj_functions.ApproxPercentile, + query, + ReaggregateSpec(fn=ReaggregationFunction.TDIGEST), + ) + + +@pytest.mark.parametrize("declared", [False, True]) +def test_compatibility_uses_valid_opted_in_family( + registered_family, + monkeypatch, + declared, +): + monkeypatch.setattr(registered_family, "supported_dialects", (Dialect.DRUID,)) + query = parse("SELECT APPROX_PERCENTILE(price, 0.5) FROM t") + spec = ReaggregateSpec(fn=ReaggregationFunction.TDIGEST) if declared else None + assert incompatible_functions(query, Dialect.DRUID, spec) == ( + [] if declared else ["APPROXPERCENTILE"] + ) + + +def test_compatibility_keeps_nested_incompatible_functions( + registered_family, + monkeypatch, +): + monkeypatch.setattr(registered_family, "supported_dialects", (Dialect.DRUID,)) + assert incompatible_functions( + parse("SELECT APPROX_PERCENTILE(IF(price > 0, price, 0), 0.5) FROM t"), + Dialect.DRUID, + {"fn": "tdigest"}, + ) == ["IF"] + + +def test_compatibility_requires_valid_family_calls(registered_family, monkeypatch): + def validate(self, func): + raise DJInvalidInputException("invalid family call") + + monkeypatch.setattr(registered_family, "validate", validate) + monkeypatch.setattr(registered_family, "supported_dialects", (Dialect.DRUID,)) + assert incompatible_functions( + parse("SELECT APPROX_PERCENTILE(price, 0.5) FROM t"), + Dialect.DRUID, + {"fn": "tdigest"}, + ) == ["APPROXPERCENTILE"] + + +def test_compatibility_requires_valid_family_params(registered_family, monkeypatch): + def invalid_params(self, params=None): + raise DJInvalidInputException("invalid family params") + + monkeypatch.setattr(registered_family, "__init__", invalid_params) + monkeypatch.setattr(registered_family, "supported_dialects", (Dialect.DRUID,)) + assert incompatible_functions( + parse("SELECT APPROX_PERCENTILE(price, 0.5) FROM t"), + Dialect.DRUID, + {"fn": "tdigest", "params": {"compression": -1}}, + ) == ["APPROXPERCENTILE"] + + +def test_compatibility_without_family_capabilities_is_unchanged(registered_family): + assert incompatible_functions( + parse("SELECT APPROX_PERCENTILE(price, 0.5) FROM t"), + Dialect.DRUID, + {"fn": "tdigest"}, + ) == ["APPROXPERCENTILE"] + + +def test_compatibility_without_registered_family_is_unchanged(monkeypatch): + monkeypatch.delitem( + FAMILY_DECOMPOSITION_REGISTRY, + ReaggregationFunction.TDIGEST, + raising=False, + ) + assert incompatible_functions( + parse("SELECT APPROX_PERCENTILE(price, 0.5) FROM t"), + Dialect.DRUID, + {"fn": "tdigest"}, + ) == ["APPROXPERCENTILE"] From a83b5f5c346b6fa4cd3c072e45d486bb4ec50638 Mon Sep 17 00:00:00 2001 From: Beto Dealmeida Date: Wed, 18 Nov 2026 18:13:06 -0500 Subject: [PATCH 2/4] Handle invalid stored sketch parameters and whole-metric compatibility --- .../datajunction_server/models/metric.py | 17 ++- .../datajunction_server/sql/decompose.py | 135 +++++++++++++----- .../build_v3/sketch_family_e2e_test.py | 61 ++++++++ .../tests/sql/decompose_test.py | 119 +++++++++++++++ 4 files changed, 293 insertions(+), 39 deletions(-) diff --git a/datajunction-server/datajunction_server/models/metric.py b/datajunction-server/datajunction_server/models/metric.py index 49d52cd14..d5bea7d34 100644 --- a/datajunction-server/datajunction_server/models/metric.py +++ b/datajunction-server/datajunction_server/models/metric.py @@ -4,7 +4,7 @@ from __future__ import annotations -from pydantic.main import BaseModel +from pydantic import BaseModel, ValidationError from sqlalchemy import select from sqlalchemy.ext.asyncio import AsyncSession @@ -13,7 +13,7 @@ ) from datajunction_server.database.column import Column from datajunction_server.database.node import Node, NodeRevision -from datajunction_server.errors import DJWarning +from datajunction_server.errors import DJInvalidInputException, DJWarning from datajunction_server.models.cube_materialization import MetricComponent from datajunction_server.models.engine import Dialect from datajunction_server.models.filter_requirements import FilterRequirement @@ -22,7 +22,10 @@ MetricMetadataOutput, ) from datajunction_server.models.query import ColumnMetadata, V3ColumnMetadata -from datajunction_server.models.reaggregate import ReaggregateSpec +from datajunction_server.models.reaggregate import ( + ReaggregateSpec, + parse_reaggregate_spec, +) from datajunction_server.models.sql import ScanEstimate, TranspiledSQL from datajunction_server.models.unit import unit_to_dict from datajunction_server.naming import SEPARATOR @@ -136,6 +139,12 @@ async def parse_node( Parses a node into a metric. """ query_ast = parse(node.current.query) + try: + reaggregate = parse_reaggregate_spec(node.current.reaggregate) + except ValidationError as exc: + raise DJInvalidInputException( + f"Metric `{node.name}` has an invalid stored reaggregate declaration", + ) from exc incompatible_druid_functions = incompatible_functions( query_ast, Dialect.DRUID, @@ -173,7 +182,7 @@ async def parse_node( required_refs, ), ), - reaggregate=node.current.reaggregate, + reaggregate=reaggregate, fixed_grain=node.current.fixed_grain, required_filters=node.current.required_filters, recommended_filters=node.current.recommended_filters, diff --git a/datajunction-server/datajunction_server/sql/decompose.py b/datajunction-server/datajunction_server/sql/decompose.py index e5e33283d..e9121b558 100644 --- a/datajunction-server/datajunction_server/sql/decompose.py +++ b/datajunction-server/datajunction_server/sql/decompose.py @@ -6,6 +6,7 @@ from dataclasses import dataclass from typing import Any, cast +from pydantic import ValidationError from sqlalchemy import select from sqlalchemy.ext.asyncio import AsyncSession from sqlalchemy.orm import aliased @@ -208,9 +209,10 @@ class AggDecomposition(ABC): output engines through `supported_dialects` for compatibility metadata. """ - # Output engines a declared family can serve through its combiner. Empty - # preserves the authored function's compatibility metadata for existing - # implementations. This does not describe the engine executing serialize. + # Output engines a decomposition can serve through its combiner. Nonempty + # declarations are enforced during extraction and used in family metadata. + # Empty preserves existing behavior. This does not describe the engine + # executing serialize. supported_dialects: tuple[Dialect, ...] = () @property @@ -922,6 +924,20 @@ def get_decomposition( return decomp_class() +def _validated_decompositions( + agg_funcs: list[tuple[ast.Function, type]], + reaggregate: ReaggregateSpec | None, +) -> list[tuple[ast.Function, type, AggDecomposition | None]]: + """Resolve and validate every aggregate before any whole-metric fallback.""" + resolved = [] + for func, function in agg_funcs: + decomposition = get_decomposition(function, reaggregate) + if decomposition is not None: + decomposition.validate(func) + resolved.append((func, function, decomposition)) + return resolved + + def incompatible_functions( query_ast: ast.Query, dialect: Dialect, @@ -933,30 +949,49 @@ def incompatible_functions( authored call and parameters. Continue walking nested calls: replacing an aggregate cannot make an incompatible scalar input expression executable. """ - spec = parse_reaggregate_spec(reaggregate) + try: + spec = parse_reaggregate_spec(reaggregate) + except ValidationError: + # A malformed persisted declaration cannot confer family compatibility. + # The metric endpoint/extractor reports the declaration error itself. + spec = None family = ( FAMILY_DECOMPOSITION_REGISTRY.get(spec.fn) if spec is not None and spec.fn is not None else None ) + functions = [(func, func.function()) for func in query_ast.find_all(ast.Function)] + family_calls: dict[int, AggDecomposition] = {} + if family and family.decomposition.supported_dialects and query_ast.select.from_: + try: + resolved = _validated_decompositions( + [ + (func, function) + for func, function in functions + if function.is_aggregation + ], + spec, + ) + # Extraction abandons the entire metric when even one aggregate + # has no decomposition. In that case no family sketch is built. + if all(decomposition is not None for _, _, decomposition in resolved): + family_calls = { + id(func): decomposition + for func, function, decomposition in resolved + if function in family.aggregate_functions + and decomposition is not None + # The generic DISTINCT path preserves raw values and skips + # the family combiner, even for families without validation. + and func.quantifier != ast.SetQuantifier.Distinct + } + except (DJInvalidInputException, ValidationError): + # Preserve native metadata for invalid stored shapes/parameters. + family_calls = {} incompatible = [] - for func in query_ast.find_all(ast.Function): - function = func.function() + for func, function in functions: compatible = dialect in function.dialects - if ( - family - and spec is not None - and function in family.aggregate_functions - and family.decomposition.supported_dialects - ): - try: - decomposition = family.decomposition(params=spec.params) - decomposition.validate(func) - compatible = dialect in decomposition.supported_dialects - except DJInvalidInputException: - # Metadata should still describe a persisted invalid metric; - # extraction reports the detailed validation error. - compatible = False + if decomposition := family_calls.get(id(func)): + compatible = dialect in decomposition.supported_dialects if not compatible: incompatible.append(function.__name__.upper()) return incompatible @@ -977,11 +1012,15 @@ def component_params_match( if component.reaggregation_family is None: return False family = FAMILY_DECOMPOSITION_REGISTRY.get(component.reaggregation_family) - return bool( - family is not None - and (component.params or {}) - == (family.decomposition(params=stored_params).params or {}), - ) + if family is None: + return False + try: + normalized = family.decomposition(params=stored_params).params or {} + except (DJInvalidInputException, ValidationError): + # Older stored sketches may use parameters the deployment no longer + # accepts. They are not candidates for reuse; do not fail the query. + return False + return (component.params or {}) == normalized def merge_inflates(merge: str | None) -> bool: @@ -1793,13 +1832,7 @@ def _extract_base( # Validate every selected implementation before either the # non-decomposable fallback or the DISTINCT shortcut can bypass it. - decompositions = [ - (func, get_decomposition(dj_function, reaggregate)) - for func, dj_function in agg_funcs - ] - for func, decomposition in decompositions: - if decomposition is not None: - decomposition.validate(func) + decompositions = _validated_decompositions(agg_funcs, reaggregate) # If any aggregation is non-decomposable, abort decomposition # entirely — the metric is non-decomposable as a whole. @@ -1809,7 +1842,7 @@ def _extract_base( # family, and its aggregation function has no entry of its own. Ask # without the spec and every such metric aborts at this gate and # never reaches the family registry at all. - if any(decomposition is None for _, decomposition in decompositions): + if any(decomposition is None for _, _, decomposition in decompositions): if dimension_reaggregate_rules(reaggregate): self._raise_unsupported_reaggregate_shape( "dimension-specific reaggregation requires a " @@ -1824,8 +1857,15 @@ def _extract_base( ) return [], query_ast - for func, dj_function in agg_funcs: - result = self._decompose(func, dj_function, query_ast, reaggregate) + for func, dj_function, decomposition in decompositions: + assert decomposition is not None + result = self._decompose_validated( + func, + dj_function, + query_ast, + decomposition, + reaggregate, + ) if result: # pragma: no branch # Apply combiner to AST func.parent.replace(from_=func, to=result.combiner) # type: ignore @@ -2044,6 +2084,31 @@ def _decompose( decomposition.validate(func) + return self._decompose_validated( + func, + dj_function, + query_ast, + decomposition, + reaggregate, + ) + + def _decompose_validated( + self, + func: ast.Function, + dj_function: type, + query_ast: ast.Query, + decomposition: AggDecomposition, + reaggregate: ReaggregateSpec | None = None, + ) -> DecompositionResult: + """Build components and a combiner from an already validated call.""" + if ( + decomposition.supported_dialects + and self._dialect not in decomposition.supported_dialects + ): + raise DJInvalidInputException( + f"{type(decomposition).__name__} does not support dialect `{self._dialect}`", + ) + # Build components from decomposition components = [ self._make_component(func, comp_def, query_ast) diff --git a/datajunction-server/tests/construction/build_v3/sketch_family_e2e_test.py b/datajunction-server/tests/construction/build_v3/sketch_family_e2e_test.py index c517a3802..5093bfc69 100644 --- a/datajunction-server/tests/construction/build_v3/sketch_family_e2e_test.py +++ b/datajunction-server/tests/construction/build_v3/sketch_family_e2e_test.py @@ -530,3 +530,64 @@ async def test_family_compatibility_agrees_in_rest_and_graphql(percentile_metric ] == expected ) + + +@pytest.mark.asyncio +async def test_malformed_stored_declaration_returns_actionable_rest_error_and_native_graphql_metadata( + percentile_metric, + session, +): + from sqlalchemy import select + from datajunction_server.database.node import NodeRevision + + client = percentile_metric + revision = await session.scalar( + select(NodeRevision).where(NodeRevision.name == METRIC), + ) + revision.reaggregate = {"fn": "unknown_family"} + await session.commit() + response = await client.get(f"/metrics/{METRIC}") + assert response.status_code == 422, response.json() + assert "invalid stored reaggregate declaration" in response.text + response = await client.post( + "/graphql", + json={ + "query": """ + query($names: [String!]!) { + findNodes(names: $names) { + current { metricMetadata { incompatibleDruidFunctions } } + } + } + """, + "variables": {"names": [METRIC]}, + }, + ) + assert response.status_code == 200, response.json() + payload = response.json() + assert not payload.get("errors"), payload + assert payload["data"]["findNodes"][0]["current"]["metricMetadata"][ + "incompatibleDruidFunctions" + ] == ["APPROXPERCENTILE"] + + +@pytest.mark.asyncio +async def test_mixed_nondecomposable_metric_does_not_gain_druid_compatibility( + percentile_metric, +): + client = percentile_metric + name = "v3.mixed_percentile_min_by" + response = await client.post( + "/nodes/metric/", + json={ + "name": name, + "query": "SELECT APPROX_PERCENTILE(line_total, 0.5) + MIN_BY(line_total, quantity) FROM v3.order_details", + "mode": "published", + "reaggregate": {"fn": "tdigest"}, + }, + ) + assert response.status_code in (200, 201), response.json() + response = await client.get(f"/metrics/{name}") + assert response.status_code == 200, response.json() + payload = response.json() + assert payload["measures"] == [] + assert "APPROXPERCENTILE" in payload["incompatible_druid_functions"] diff --git a/datajunction-server/tests/sql/decompose_test.py b/datajunction-server/tests/sql/decompose_test.py index 1fb688866..bff6ebef4 100644 --- a/datajunction-server/tests/sql/decompose_test.py +++ b/datajunction-server/tests/sql/decompose_test.py @@ -35,6 +35,7 @@ MetricComponentExtractor, decomposes_family, get_decomposition, + component_params_match, incompatible_functions, is_duplication_invariant, is_metric_duplication_sensitive, @@ -3504,3 +3505,121 @@ def test_compatibility_without_registered_family_is_unchanged(monkeypatch): Dialect.DRUID, {"fn": "tdigest"}, ) == ["APPROXPERCENTILE"] + + +@pytest.mark.parametrize( + "query", + [ + "SELECT APPROX_PERCENTILE(price, 0.5) + MIN_BY(price, quantity) FROM t", + "SELECT MIN_BY(price, quantity) + APPROX_PERCENTILE(price, 0.5) FROM t", + ], +) +def test_compatibility_requires_the_whole_metric_to_decompose( + registered_family, + monkeypatch, + query, +): + monkeypatch.setattr(registered_family, "supported_dialects", (Dialect.DRUID,)) + # Even if the other aggregate can execute natively in Druid, its missing + # decomposition aborts sketch construction for the entire expression. + monkeypatch.setattr(dj_functions.MinBy, "dialects", [Dialect.DRUID]) + spec = ReaggregateSpec(fn=ReaggregationFunction.TDIGEST) + components, _ = MetricComponentExtractor(0)._extract_base(parse(query), spec) + assert components == [] + assert incompatible_functions(parse(query), Dialect.DRUID, spec) == [ + "APPROXPERCENTILE", + ] + + +@pytest.mark.parametrize( + "stored", + [ + {"fn": "unknown"}, + {"fn": "tdigest", "params": []}, + {"fn": "tdigest", "unexpected": True}, + ], +) +def test_malformed_stored_reaggregate_does_not_grant_compatibility( + registered_family, + monkeypatch, + stored, +): + monkeypatch.setattr(registered_family, "supported_dialects", (Dialect.DRUID,)) + assert incompatible_functions( + parse("SELECT APPROX_PERCENTILE(price, 0.5) FROM t"), + Dialect.DRUID, + stored, + ) == ["APPROXPERCENTILE"] + + +def test_distinct_raw_value_path_cannot_advertise_a_family_combiner( + registered_family, + monkeypatch, +): + monkeypatch.setattr(registered_family, "supported_dialects", (Dialect.DRUID,)) + assert incompatible_functions( + parse("SELECT APPROX_PERCENTILE(DISTINCT price, 0.5) FROM t"), + Dialect.DRUID, + {"fn": "tdigest"}, + ) == ["APPROXPERCENTILE"] + + +def test_supported_family_dialects_are_enforced_during_extraction( + registered_family, + monkeypatch, +): + monkeypatch.setattr(registered_family, "supported_dialects", (Dialect.SPARK,)) + with pytest.raises(DJInvalidInputException, match="does not support dialect"): + MetricComponentExtractor(0, dialect=Dialect.DRUID)._extract_base( + parse("SELECT APPROX_PERCENTILE(price, 0.5) FROM t"), + ReaggregateSpec(fn=ReaggregationFunction.TDIGEST), + ) + + +def test_each_aggregate_is_validated_once_during_extraction( + registered_family, + monkeypatch, +): + calls = [] + monkeypatch.setattr( + registered_family, + "validate", + lambda self, func: calls.append(str(func)), + ) + MetricComponentExtractor(0)._extract_base( + parse( + "SELECT APPROX_PERCENTILE(price, 0.5) + APPROX_PERCENTILE(price, 0.95) FROM t", + ), + ReaggregateSpec(fn=ReaggregationFunction.TDIGEST), + ) + assert len(calls) == 2 + + +@pytest.mark.parametrize( + "stored, expected", + [ + (None, True), + ({"compression": 200}, True), + ({"compression": 100}, False), + ({"compression": 500}, False), + ], +) +def test_invalid_stored_component_params_are_not_reused( + registered_family, + monkeypatch, + stored, + expected, +): + original_init = registered_family.__init__ + + def bounded_init(self, params=None): + if (params or {}).get("compression", 200) > 200: + raise DJInvalidInputException("compression exceeds deployment limit") + original_init(self, params) + + monkeypatch.setattr(registered_family, "__init__", bounded_init) + components, _ = MetricComponentExtractor(0)._extract_base( + parse("SELECT APPROX_PERCENTILE(price, 0.5) FROM t"), + ReaggregateSpec(fn=ReaggregationFunction.TDIGEST), + ) + assert component_params_match(components[0], stored) is expected From 069fb51d1b729689a5e4975a88d4c2129b2fabff Mon Sep 17 00:00:00 2001 From: Beto Dealmeida Date: Wed, 18 Nov 2026 18:51:56 -0500 Subject: [PATCH 3/4] Separate metric metadata extraction from engine validation --- .../datajunction_server/construction/build.py | 3 +- .../datajunction_server/internal/sql.py | 1 + .../datajunction_server/sql/decompose.py | 40 +++++++------ .../build_v3/sketch_family_e2e_test.py | 59 ++++++++++++++++++- .../nodes/derive_frozen_measures_test.py | 6 ++ .../tests/sql/decompose_test.py | 38 +++++++++++- 6 files changed, 123 insertions(+), 24 deletions(-) diff --git a/datajunction-server/datajunction_server/construction/build.py b/datajunction-server/datajunction_server/construction/build.py index e79f6a4e6..4f07f9446 100755 --- a/datajunction-server/datajunction_server/construction/build.py +++ b/datajunction-server/datajunction_server/construction/build.py @@ -372,6 +372,7 @@ def build_materialized_cube_node( async def extract_components_and_parent_columns( metric_nodes: list[Node], session: "AsyncSession", + dialect: Dialect = Dialect.SPARK, ) -> tuple[ collections.defaultdict[str, set[str]], dict[str, tuple[list[MetricComponent], ast.Query]], @@ -398,7 +399,7 @@ async def extract_components_and_parent_columns( parent_columns = collections.defaultdict(set) async def extract_components_and_columns(metric_node: Node): - extractor = MetricComponentExtractor(metric_node.current.id) + extractor = MetricComponentExtractor(metric_node.current.id, dialect=dialect) measures, derived_sql = await extractor.extract(session) metric_ast = parse(metric_node.current.query) return measures, derived_sql, list(metric_ast.find_all(ast.Column)) diff --git a/datajunction-server/datajunction_server/internal/sql.py b/datajunction-server/datajunction_server/internal/sql.py index 69743c6c1..2c4b5e768 100644 --- a/datajunction-server/datajunction_server/internal/sql.py +++ b/datajunction-server/datajunction_server/internal/sql.py @@ -643,6 +643,7 @@ async def get_measures_query( parent_columns, metric_components = await extract_components_and_parent_columns( metric_nodes, session, + dialect=build_criteria.dialect, ) column_name_regex = r"([A-Za-z0-9_\.]+)(\[[A-Za-z0-9_]+\])?" diff --git a/datajunction-server/datajunction_server/sql/decompose.py b/datajunction-server/datajunction_server/sql/decompose.py index e9121b558..0e1ff8798 100644 --- a/datajunction-server/datajunction_server/sql/decompose.py +++ b/datajunction-server/datajunction_server/sql/decompose.py @@ -210,9 +210,10 @@ class AggDecomposition(ABC): """ # Output engines a decomposition can serve through its combiner. Nonempty - # declarations are enforced during extraction and used in family metadata. - # Empty preserves existing behavior. This does not describe the engine - # executing serialize. + # declarations are enforced for explicit query targets and used in family + # metadata. Engine-unspecified extraction prefers Spark, then the first + # declared dialect. Empty preserves existing behavior. This does not + # describe the engine executing serialize. supported_dialects: tuple[Dialect, ...] = () @property @@ -1298,7 +1299,7 @@ def validate_fixed_grain_shape(query: str, fixed_grain: list[str]) -> None: MetricExtractionCache = dict[ - tuple[int, Dialect, bool], + tuple[int, Dialect | None, bool], tuple[list[MetricComponent], ast.Query], ] @@ -1315,24 +1316,18 @@ class MetricComponentExtractor: def __init__( self, node_revision_id: int, - dialect: Dialect = Dialect.SPARK, + dialect: Dialect | None = None, ): """ Extract metric components from a specific metric revision. Args: node_revision_id: ID of the metric node revision - dialect: Dialect the combiner will be rendered for. Every dialect - gets a combiner -- whether one exists at all is a property of - the aggregation function, not the engine -- but its shape can - differ: a sketch family whose engines expose different function - shapes needs the target, as Druid fuses a t-digest's merge and - combine where Spark and Trino keep them separate. - - Defaults to Spark, which is right for the callers that render - for display or for frozen measures. Only the build_v3 path, - which knows the cube's actual target engine, passes anything - else. + dialect: Target engine for query planning. Explicit targets must be + supported by the decomposition. Omit for metadata or frozen + measures, which have no execution engine: their representative + combiner uses Spark when supported, otherwise the first declared + dialect. Query builders must pass their actual target dialect. """ self._node_revision_id = node_revision_id self._dialect = dialect @@ -2102,7 +2097,8 @@ def _decompose_validated( ) -> DecompositionResult: """Build components and a combiner from an already validated call.""" if ( - decomposition.supported_dialects + self._dialect is not None + and decomposition.supported_dialects and self._dialect not in decomposition.supported_dialects ): raise DJInvalidInputException( @@ -2146,7 +2142,15 @@ def _decompose_validated( quantifier=ast.SetQuantifier.Distinct, ) else: - combiner_ast = decomposition.combine(components, func, self._dialect) + combiner_dialect = self._dialect + if combiner_dialect is None: + supported = decomposition.supported_dialects + combiner_dialect = ( + supported[0] + if supported and Dialect.SPARK not in supported + else Dialect.SPARK + ) + combiner_ast = decomposition.combine(components, func, combiner_dialect) # Decomposed AVG / variance / stddev / covariance all build # SUM(...) / SUM(count)-style combiners where the denominator # can legitimately be 0. Wrap to produce NULL rather than diff --git a/datajunction-server/tests/construction/build_v3/sketch_family_e2e_test.py b/datajunction-server/tests/construction/build_v3/sketch_family_e2e_test.py index 5093bfc69..37fe0369c 100644 --- a/datajunction-server/tests/construction/build_v3/sketch_family_e2e_test.py +++ b/datajunction-server/tests/construction/build_v3/sketch_family_e2e_test.py @@ -134,7 +134,7 @@ def infer_type( @pytest.fixture -def digest_family(): +def digest_family(request): """ Register the family and its functions, then put the registries back. @@ -153,7 +153,11 @@ def digest_family(): aggregate_functions=(ApproxPercentile,), ) class _DigestDecomposition(AggDecomposition): - supported_dialects = (Dialect.SPARK, Dialect.TRINO, Dialect.DRUID) + supported_dialects = getattr( + request, + "param", + (Dialect.SPARK, Dialect.TRINO, Dialect.DRUID), + ) @property def compression(self) -> int: @@ -180,6 +184,8 @@ def components(self) -> list[ComponentDef]: ] def combine(self, components, func, dialect=Dialect.SPARK): + if self.supported_dialects: + assert dialect in self.supported_dialects column, fraction = components[0].name, func.args[1] if dialect == Dialect.DRUID: return make_func("DIGEST_QUANTILE", column, fraction) @@ -240,6 +246,55 @@ async def _measures(client: AsyncClient, dialect: str = "spark") -> dict: return response.json() +@pytest.mark.asyncio +@pytest.mark.parametrize( + "digest_family", + [(Dialect.TRINO,), (Dialect.DRUID,), (Dialect.TRINO, Dialect.DRUID)], + indirect=True, +) +async def test_non_spark_family_metadata_and_explicit_planning( + percentile_metric, + digest_family, +): + """Metadata needs no Spark support; an explicit Spark target is rejected.""" + client = percentile_metric + response = await client.get(f"/metrics/{METRIC}") + assert response.status_code == 200, response.json() + assert len(response.json()["measures"]) == 1 + response = await client.post( + "/graphql", + json={ + "query": """ + query($names: [String!]!) { + findNodes(names: $names) { + current { + extractedMeasures { + components { name } + derivedExpression + } + } + } + } + """, + "variables": {"names": [METRIC]}, + }, + ) + assert response.status_code == 200, response.json() + payload = response.json() + assert not payload.get("errors"), payload + extracted = payload["data"]["findNodes"][0]["current"]["extractedMeasures"] + assert len(extracted["components"]) == 1 + assert extracted["derivedExpression"] + for dialect in digest_family.supported_dialects: + await _measures(client, dialect=dialect.value) + response = await client.get( + "/sql/measures/v3/", + params={"metrics": [METRIC], "dimensions": DIMENSIONS, "dialect": "spark"}, + ) + assert response.status_code == 422, response.json() + assert "does not support dialect" in response.text + + async def _publish_preagg(client: AsyncClient) -> None: response = await client.post( "/preaggs/plan", diff --git a/datajunction-server/tests/internal/nodes/derive_frozen_measures_test.py b/datajunction-server/tests/internal/nodes/derive_frozen_measures_test.py index 02b6f793e..e23c993c7 100644 --- a/datajunction-server/tests/internal/nodes/derive_frozen_measures_test.py +++ b/datajunction-server/tests/internal/nodes/derive_frozen_measures_test.py @@ -25,6 +25,7 @@ AggregationRule, MetricComponent, ) +from datajunction_server.models.dialect import Dialect from datajunction_server.models.node import NodeStatus from datajunction_server.models.node_type import NodeType from datajunction_server.models.reaggregate import ReaggregationFunction @@ -147,10 +148,12 @@ async def test_base_metric_populates_derived_expression_and_measure( @pytest.mark.asyncio @pytest.mark.parametrize("bulk", [False, True]) +@pytest.mark.parametrize("dialect", [Dialect.SPARK, Dialect.TRINO, Dialect.DRUID]) async def test_legacy_explicit_default_frozen_measure_is_reused( session: AsyncSession, user: User, bulk: bool, + dialect: Dialect, ): """An old explicit default row remains reusable after family normalization.""" @@ -159,6 +162,8 @@ async def test_legacy_explicit_default_frozen_measure_is_reused( aggregate_functions=(dj_functions.ApproxPercentile,), ) class Digest(AggDecomposition): + supported_dialects = (dialect,) + def __init__(self, params=None): super().__init__(params) compression = int(self.params.get("compression", 200)) @@ -175,6 +180,7 @@ def components(self): ] def combine(self, components, func, dialect=None): + assert dialect in self.supported_dialects return make_func("read_sketch", components[0].name) try: diff --git a/datajunction-server/tests/sql/decompose_test.py b/datajunction-server/tests/sql/decompose_test.py index bff6ebef4..ea5c175cc 100644 --- a/datajunction-server/tests/sql/decompose_test.py +++ b/datajunction-server/tests/sql/decompose_test.py @@ -3025,9 +3025,41 @@ def test_combine_receives_the_originating_call(): assert str(seen_func.args[1]) == "0.5" -def test_extractor_defaults_to_spark_dialect(): - """An extractor built without a dialect renders for Spark.""" - assert MetricComponentExtractor(1)._dialect == Dialect.SPARK +def test_extractor_defaults_to_no_execution_dialect(): + """Metadata extraction does not request an execution engine.""" + assert MetricComponentExtractor(1)._dialect is None + + +@pytest.mark.asyncio +@pytest.mark.parametrize("derived", [False, True]) +async def test_metadata_cache_cannot_bypass_explicit_dialect_validation( + session, + create_metric, + dialect_aware_sum, + monkeypatch, + derived, +): + """Unspecified targets propagate to parents and have separate cache entries.""" + monkeypatch.setattr(dialect_aware_sum, "supported_dialects", (Dialect.DRUID,)) + metric = await create_metric("SELECT SUM(price) FROM parent_node") + if derived: + await session.refresh(metric, ["node"]) + metric = await create_metric( + f"SELECT {metric.name} * 2", + parent=metric.node, + ) + cache = {} + components, sql = await MetricComponentExtractor(metric.id).extract( + session, + extraction_cache=cache, + ) + assert len(components) == 1 + assert "druid_combiner" in str(sql) + with pytest.raises(DJInvalidInputException, match="does not support dialect"): + await MetricComponentExtractor(metric.id, dialect=Dialect.SPARK).extract( + session, + extraction_cache=cache, + ) @pytest.mark.asyncio From e859efa190b110075ae7d239285d98c53b9535a7 Mon Sep 17 00:00:00 2001 From: Beto Dealmeida Date: Wed, 18 Nov 2026 22:57:16 -0500 Subject: [PATCH 4/4] Cover missing sketch family and direct decomposition paths --- .../tests/sql/decompose_test.py | 36 +++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/datajunction-server/tests/sql/decompose_test.py b/datajunction-server/tests/sql/decompose_test.py index ea5c175cc..90cfb1ff2 100644 --- a/datajunction-server/tests/sql/decompose_test.py +++ b/datajunction-server/tests/sql/decompose_test.py @@ -3466,6 +3466,27 @@ def validate(self, func): ) +def test_direct_decomposition_builds_family_components_and_combiner(registered_family): + """Direct callers receive the selected family's sketch and finalizer.""" + query = parse("SELECT APPROX_PERCENTILE(price, 0.5) FROM t") + result = MetricComponentExtractor(0)._decompose( + query.select.projection[0], + dj_functions.ApproxPercentile, + query, + ReaggregateSpec( + fn=ReaggregationFunction.TDIGEST, + params={"compression": 100}, + ), + ) + assert result is not None + (component,) = result.components + assert component.expression == "price" + assert component.aggregation == "build_sketch(price, 100)" + assert component.reaggregation_family == ReaggregationFunction.TDIGEST + assert component.params == {"compression": 100} + assert str(result.combiner) == f"read_sketch({component.name})" + + @pytest.mark.parametrize("declared", [False, True]) def test_compatibility_uses_valid_opted_in_family( registered_family, @@ -3655,3 +3676,18 @@ def bounded_init(self, params=None): ReaggregateSpec(fn=ReaggregationFunction.TDIGEST), ) assert component_params_match(components[0], stored) is expected + + +def test_stored_component_params_are_not_normalized_without_registered_family( + registered_family, + monkeypatch, +): + """A removed family cannot establish that different stored params match.""" + (component,), _ = MetricComponentExtractor(0)._extract_base( + parse("SELECT APPROX_PERCENTILE(price, 0.5) FROM t"), + ReaggregateSpec(fn=ReaggregationFunction.TDIGEST), + ) + stored = {"compression": 200} + assert component_params_match(component, stored) is True + monkeypatch.delitem(FAMILY_DECOMPOSITION_REGISTRY, ReaggregationFunction.TDIGEST) + assert component_params_match(component, stored) is False