Skip to content

Commit a86b008

Browse files
committed
test(native): gate enrichment deferrals, CTE recovery and standalone pivot proofs
1 parent c2fa7f6 commit a86b008

3 files changed

Lines changed: 164 additions & 11 deletions

File tree

‎tests/integration/src/sqlbuild/compiler/analysis_session/_test_types.py‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ class GeneratedAnalysisParityTestCase:
1616
expected_minimum_expression_shapes: int
1717
expected_minimum_pivot_proofs: int
1818
expected_minimum_proven_pivots: int
19+
expected_maximum_enrichment_deferrals: int
20+
expected_minimum_native_enrichments: int
1921

2022

2123
@dataclass(frozen=True)
@@ -37,3 +39,13 @@ class SessionFailureTestCase:
3739
seed: int
3840
model_count: int
3941
expected_kinds: dict[str, int]
42+
43+
44+
@dataclass(frozen=True)
45+
class StandalonePivotProofTestCase:
46+
"""A pivot model left out of model analysis, whose proof assembly takes natively."""
47+
48+
description: str
49+
analysed_models: frozenset[str]
50+
expected_native_proofs: int
51+
expected_proven_by_model: dict[str, bool | None]

‎tests/integration/src/sqlbuild/compiler/analysis_session/helpers.py‎

Lines changed: 92 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,9 @@
1414
import pytest
1515

1616
import sqlbuild._native as native_module
17+
import sqlbuild.compiler.analysis_session.classes.native_model_analysis as native_model_analysis
1718
import sqlbuild.compiler.compile._helpers.assembly.project as project_assembly
19+
import sqlbuild.compiler.compile._helpers.native_stages.assembly as native_stage_assembly
1820
from sqlbuild.adapter.contract.models import ExpressionInferenceProfile
1921
from sqlbuild.adapters.duckdb.classes.duckdb_adapter import DuckDbAdapter
2022
from sqlbuild.compiler.analysis_session.main._analyze_native_model_sql import (
@@ -35,6 +37,7 @@
3537
from sqlbuild.compiler.compile.models import (
3638
CompactLineageFacts,
3739
CompileAdapterContext,
40+
CompiledLineageColumnFact,
3841
CompileProjectInputs,
3942
DynamicColumnContractProof,
4043
ModelSqlAnalysis,
@@ -64,6 +67,17 @@
6467
description: Customers feed.
6568
expression: >-
6669
(SELECT 10 AS customer_id, 'Ada' AS Customer_Name, 'east' AS region)
70+
- name: raw_payments
71+
description: Typed payments feed.
72+
expression: >-
73+
(SELECT 10 AS customer_id, 'east' AS region, CAST(5 AS DECIMAL(10, 2)) AS amount)
74+
columns:
75+
- name: customer_id
76+
type: INTEGER
77+
- name: region
78+
type: VARCHAR
79+
- name: amount
80+
type: DECIMAL(10, 2)
6781
- name: raw_events
6882
description: Untyped events feed.
6983
columns:
@@ -113,21 +127,38 @@
113127
'SELECT * FROM __source("raw_orders") o\nJOIN __source("raw_customers") c USING (customer_id)',
114128
)
115129
_PIVOT_AGGREGATES: tuple[str, ...] = ("MAX", "MIN", "ANY_VALUE", "SUM")
130+
_TYPED_PIVOT: str = (
131+
_PIVOT_HEADER.format(pivot="status", value="amount", aggregate="MAX")
132+
+ _PIVOT_BODIES[0].format(aggregate="MAX")
133+
+ "\n"
134+
)
135+
_TYPED_PIVOT_PASSTHROUGH: str = (
136+
_PIVOT_HEADER.format(pivot="status", value="amount", aggregate="MAX")
137+
+ 'SELECT * FROM __ref("status_amounts")\n'
138+
)
139+
_CONTRACT_CTE_MODELS: dict[str, str] = {
140+
"models/marts/payments_by_customer.sql": (
141+
'MODEL (\n description "Payments passed through a CTE",\n contract enforced,\n'
142+
" columns (customer_id (type INTEGER), region (type VARCHAR)),\n);\n\n"
143+
'WITH base AS (SELECT customer_id, region FROM __source("raw_payments"))\n'
144+
"SELECT customer_id, region FROM base\n"
145+
),
146+
"models/marts/payment_totals.sql": (
147+
'MODEL (\n description "Payment totals aggregated in a CTE",\n contract enforced,\n'
148+
" columns (total_amount (type DECIMAL(38, 2)), region (type VARCHAR)),\n);\n\n"
149+
"WITH base AS (\n SELECT SUM(amount) AS total_amount, region\n"
150+
' FROM __source("raw_payments")\n GROUP BY region\n)\n'
151+
"SELECT total_amount, region FROM base\n"
152+
),
153+
}
116154

117155

118156
def _pivot_models(rng: random.Random) -> dict[str, str]:
119157
"""A typed pivot every passthrough reads, and seeded pivot shapes proven or refused."""
120158

121159
files: dict[str, str] = {
122-
"models/marts/status_amounts.sql": _PIVOT_HEADER.format(
123-
pivot="status", value="amount", aggregate="MAX"
124-
)
125-
+ _PIVOT_BODIES[0].format(aggregate="MAX")
126-
+ "\n",
127-
"models/marts/status_passthrough.sql": _PIVOT_HEADER.format(
128-
pivot="status", value="amount", aggregate="MAX"
129-
)
130-
+ 'SELECT * FROM __ref("status_amounts")\n',
160+
"models/marts/status_amounts.sql": _TYPED_PIVOT,
161+
"models/marts/status_passthrough.sql": _TYPED_PIVOT_PASSTHROUGH,
131162
}
132163
for index, body in enumerate(rng.sample(_PIVOT_BODIES, k=4)):
133164
aggregate: str = rng.choice(_PIVOT_AGGREGATES)
@@ -256,13 +287,55 @@ def _subquery(rng: random.Random, inputs: list[_Relation]) -> tuple[str, str, tu
256287
)
257288

258289

290+
def pivot_project_files() -> dict[str, str]:
291+
"""A typed pivot with its passthrough and one plain model, for selective assembly."""
292+
293+
return {
294+
"sqlbuild_project.toml": _PROJECT_TOML,
295+
"sources/raw.yml": _SOURCES,
296+
"models/marts/status_amounts.sql": _TYPED_PIVOT,
297+
"models/marts/status_passthrough.sql": _TYPED_PIVOT_PASSTHROUGH,
298+
"models/staging/orders_list.sql": (
299+
'MODEL (description "Order ids");\n\nSELECT order_id FROM __source("raw_orders")\n'
300+
),
301+
}
302+
303+
304+
def native_pivot_proofs(
305+
*, monkeypatch: pytest.MonkeyPatch
306+
) -> list[DynamicColumnContractProof | None]:
307+
"""Record every standalone native pivot proof assembly takes, failing any wheel pivot proof."""
308+
309+
proofs: list[DynamicColumnContractProof | None] = []
310+
native_proof: Callable[..., DynamicColumnContractProof | None] = (
311+
native_stage_assembly.prove_native_dynamic_contract
312+
)
313+
314+
def recorded(**keywords: Any) -> DynamicColumnContractProof | None:
315+
proofs.append(native_proof(**keywords))
316+
return proofs[-1]
317+
318+
python_proof: Callable[..., DynamicColumnContractProof | None] = (
319+
project_assembly.analyze_dynamic_column_contract
320+
)
321+
322+
def wheel_not_expected(**keywords: Any) -> DynamicColumnContractProof | None:
323+
assert not keywords["families"], "the Python wheel proved a pivot native should prove"
324+
return python_proof(**keywords)
325+
326+
monkeypatch.setattr(native_stage_assembly, "prove_native_dynamic_contract", recorded)
327+
monkeypatch.setattr(project_assembly, "analyze_dynamic_column_contract", wheel_not_expected)
328+
return proofs
329+
330+
259331
def generated_analysis_files(*, rng: random.Random, model_count: int) -> dict[str, str]:
260332
"""Sources (typed, inferred and untyped), a pivot and models drawing on every template."""
261333

262334
files: dict[str, str] = {
263335
"sqlbuild_project.toml": _PROJECT_TOML,
264336
"sources/raw.yml": _SOURCES,
265337
**_pivot_models(rng),
338+
**_CONTRACT_CTE_MODELS,
266339
}
267340
relations: list[_Relation] = list(_INITIAL_RELATIONS)
268341
for index in range(model_count):
@@ -302,6 +375,7 @@ class AnalysisParity:
302375
pivot_proofs: int = 0
303376
standalone_proofs: int = 0
304377
proven_pivots: int = 0
378+
native_enrichments: int = 0
305379

306380

307381
def compare_analyses(
@@ -380,7 +454,16 @@ def shapes_by_both(
380454
)
381455
return python
382456

457+
native_lineage_facts: Callable[..., tuple[CompiledLineageColumnFact, ...]] = (
458+
native_model_analysis.lineage_facts
459+
)
460+
461+
def counted_native_enrichment(rows: list[Any]) -> tuple[CompiledLineageColumnFact, ...]:
462+
parity.native_enrichments += 1
463+
return native_lineage_facts(rows)
464+
383465
with monkeypatch.context() as patch:
466+
patch.setattr(native_model_analysis, "lineage_facts", counted_native_enrichment)
384467
patch.setattr(project_assembly, "analyze_model_sql_by_engine", models_by_both)
385468
patch.setattr(project_assembly, "expression_source_shapes_by_engine", shapes_by_both)
386469
patch.setattr(project_assembly, "dynamic_column_contract_by_engine", proofs_by_both)

‎tests/integration/src/sqlbuild/compiler/analysis_session/test_native_model_analysis.py‎

Lines changed: 60 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,20 @@
1414
analyze_native_model_sql,
1515
)
1616
from sqlbuild.compiler.analysis_session.models import NativeModelAnalysisRequest
17-
from sqlbuild.compiler.compile.models import CompileProjectInputs
17+
from sqlbuild.compiler.compile._helpers.assembly.project import assemble_compiled_project
18+
from sqlbuild.compiler.compile.models import (
19+
CompiledProject,
20+
CompileProjectInputs,
21+
DynamicColumnContractProof,
22+
)
23+
from sqlbuild.compiler.frontier.constants import COMPILER_ENGINE_ENV_VAR
24+
from sqlbuild.compiler.frontier.types import CompilerEngine
1825
from sqlbuild.compiler.sql_analysis.constants import ANALYSIS_RECORD_DIR_ENV_VAR
1926
from tests.integration.src.sqlbuild.compiler.analysis_session._test_types import (
2027
AnalysisFallbackTestCase,
2128
GeneratedAnalysisParityTestCase,
2229
SessionFailureTestCase,
30+
StandalonePivotProofTestCase,
2331
)
2432
from tests.integration.src.sqlbuild.compiler.analysis_session.helpers import (
2533
AnalysisParity,
@@ -30,6 +38,8 @@
3038
deferral_kinds,
3139
failing_provide_sessions,
3240
generated_analysis_files,
41+
native_pivot_proofs,
42+
pivot_project_files,
3343
)
3444
from tests.integration.src.sqlbuild.compiler.helpers import mismatches
3545

@@ -44,7 +54,7 @@
4454
"test_case",
4555
[
4656
GeneratedAnalysisParityTestCase(
47-
description="stars, CTEs, set operations, untyped inputs, contracts and pivots",
57+
description="stars, CTEs, set operations, untyped inputs, contracts, contract CTEs, pivots",
4858
seed=20261008,
4959
count=6,
5060
model_count=24,
@@ -53,6 +63,8 @@
5363
expected_minimum_expression_shapes=40,
5464
expected_minimum_pivot_proofs=80,
5565
expected_minimum_proven_pivots=12,
66+
expected_maximum_enrichment_deferrals=120,
67+
expected_minimum_native_enrichments=90,
5668
)
5769
],
5870
ids=lambda case: case.description,
@@ -86,6 +98,11 @@ def test_given_generated_projects_when_analysing_natively_then_matches_python(
8698
assert parity.pivot_proofs >= test_case.expected_minimum_pivot_proofs
8799
assert parity.standalone_proofs >= test_case.expected_minimum_pivot_proofs
88100
assert parity.proven_pivots >= test_case.expected_minimum_proven_pivots
101+
assert (
102+
kinds["analysis_session:input_enrichment"]
103+
<= test_case.expected_maximum_enrichment_deferrals
104+
)
105+
assert parity.native_enrichments >= test_case.expected_minimum_native_enrichments
89106

90107

91108
@pytest.mark.parametrize(
@@ -175,5 +192,46 @@ def test_given_session_failure_after_deferrals_when_analysing_then_records_only_
175192
assert deferral_kinds(record_dir) == Counter(test_case.expected_kinds)
176193

177194

195+
@pytest.mark.parametrize(
196+
"test_case",
197+
[
198+
StandalonePivotProofTestCase(
199+
description="a selection that leaves the pivots out of model analysis",
200+
analysed_models=frozenset({"orders_list"}),
201+
expected_native_proofs=2,
202+
expected_proven_by_model={
203+
"orders_list": None,
204+
"status_amounts": True,
205+
"status_passthrough": True,
206+
},
207+
)
208+
],
209+
ids=lambda case: case.description,
210+
)
211+
def test_given_unanalysed_pivot_model_when_assembling_then_native_proves_it_without_the_wheel(
212+
test_case: StandalonePivotProofTestCase, tmp_path: Path, monkeypatch: pytest.MonkeyPatch
213+
) -> None:
214+
inputs: CompileProjectInputs = compile_inputs(
215+
project_dir=tmp_path / "project", files=pivot_project_files()
216+
)
217+
monkeypatch.setenv(COMPILER_ENGINE_ENV_VAR, CompilerEngine.NATIVE_PREVIEW.value)
218+
proofs: list[DynamicColumnContractProof | None] = native_pivot_proofs(monkeypatch=monkeypatch)
219+
220+
project: CompiledProject = assemble_compiled_project(
221+
inputs=inputs,
222+
inference_profile=ExpressionInferenceProfile(sql_analysis_dialect="duckdb"),
223+
analysis_model_names=test_case.analysed_models,
224+
)
225+
226+
assert (
227+
all(proof is not None for proof in proofs),
228+
len(proofs),
229+
{
230+
model.name: getattr(model.dynamic_column_contract, "output_proven", None)
231+
for model in project.models
232+
},
233+
) == (True, test_case.expected_native_proofs, test_case.expected_proven_by_model)
234+
235+
178236
if __name__ == "__main__":
179237
pytest.main([__file__, "-vv"])

0 commit comments

Comments
 (0)