Skip to content

Commit 6a28f13

Browse files
committed
feat(native): compare reference extraction in the compiler differential
The failure corpus gains reference cases: wrong argument counts, an empty argument, an expression name, unclosed calls, and table functions without an argument list, with a single-quoted name or with an empty call argument. A harness test compares a project with table functions and references hidden in comments, strings and dollar quotes under both engines.
1 parent cebae72 commit 6a28f13

3 files changed

Lines changed: 149 additions & 0 deletions

File tree

‎scripts/compiler_differential/_helpers/corpus/failure_cases.py‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@
88
from scripts.compiler_differential._helpers.corpus.discovery_failure_cases import (
99
discovery_failure_cases,
1010
)
11+
from scripts.compiler_differential._helpers.corpus.reference_failure_cases import (
12+
reference_failure_cases,
13+
)
1114
from scripts.compiler_differential._helpers.corpus.render_failure_cases import (
1215
render_failure_cases,
1316
)
@@ -41,6 +44,7 @@ def all_failure_cases() -> tuple[FailureCase, ...]:
4144
*discovery_failure_cases(),
4245
*render_failure_cases(),
4346
*scope_failure_cases(),
47+
*reference_failure_cases(),
4448
)
4549

4650

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
"""Minimal projects whose model SQL holds a malformed reference call."""
2+
3+
from scripts.compiler_differential._helpers.corpus.case_builder import (
4+
failure_case,
5+
mart_body_files,
6+
)
7+
from scripts.compiler_differential.models import FailureCase
8+
9+
_SELECT: str = "SELECT order_id AS customer_id, 1 AS total_amount\n"
10+
11+
12+
def _mart_from(*, name: str, from_clause: str, expected_message: str) -> FailureCase:
13+
return failure_case(
14+
name=name,
15+
expected_code="P001",
16+
expected_message=expected_message,
17+
files=mart_body_files(f"{_SELECT}FROM {from_clause}\n"),
18+
)
19+
20+
21+
def reference_failure_cases() -> tuple[FailureCase, ...]:
22+
"""Return the reference-extraction failure cases in a stable order."""
23+
24+
return (
25+
_mart_from(
26+
name="reference-two-name-arguments",
27+
from_clause='__ref("stg_orders", "orders")',
28+
expected_message="__ref must contain exactly one name argument",
29+
),
30+
_mart_from(
31+
name="reference-dbt-three-arguments",
32+
from_clause='__dbt_ref("shop", "orders", "extra")',
33+
expected_message="__dbt_ref must contain one name argument or package/name arguments",
34+
),
35+
_mart_from(
36+
name="reference-empty-argument",
37+
from_clause='__ref("stg_orders", )',
38+
expected_message="SQL reference contains an empty argument",
39+
),
40+
_mart_from(
41+
name="reference-expression-name",
42+
from_clause="__ref(concat('stg_', 'orders'))",
43+
expected_message="__ref name argument must be a quoted string or identifier",
44+
),
45+
_mart_from(
46+
name="reference-unclosed-call",
47+
from_clause='__ref("stg_orders"',
48+
expected_message="SQL reference contains an unclosed parenthesis",
49+
),
50+
_mart_from(
51+
name="table-function-without-arguments",
52+
from_clause='__table_fn("table_fn__orders_for") /* no call */',
53+
expected_message="__table_fn must be followed by an argument list",
54+
),
55+
_mart_from(
56+
name="table-function-single-quoted-name",
57+
from_clause="__table_fn('table_fn__orders_for')(1)",
58+
expected_message="__table_fn name argument must be double quoted",
59+
),
60+
_mart_from(
61+
name="table-function-empty-call-argument",
62+
from_clause='__table_fn("table_fn__orders_for")(1, , 2)',
63+
expected_message="SQL reference contains an empty argument",
64+
),
65+
_mart_from(
66+
name="table-function-unclosed-call",
67+
from_clause='__table_fn("table_fn__orders_for")(1 -- )',
68+
expected_message="SQL table function call contains an unclosed parenthesis",
69+
),
70+
)
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
"""The harness finds a project with table functions and hidden references identical by engine."""
2+
3+
from __future__ import annotations
4+
5+
from pathlib import Path
6+
7+
import pytest
8+
9+
from scripts.compiler_differential.constants import FAILURE_BASE_FILES
10+
from scripts.compiler_differential.main.differential import run_compiler_differential
11+
from tests.e2e.scripts.compiler_differential._test_types import HarnessRunTestCase
12+
from tests.e2e.scripts.compiler_differential.helpers import harness_arguments
13+
14+
_REFERENCE_FILES: dict[str, str] = {
15+
**FAILURE_BASE_FILES,
16+
"functions/sql/orders_for.sql": (
17+
"FUNCTION (\n"
18+
' description "Orders for one customer above a floor",\n'
19+
" arguments (p_customer INTEGER, p_floor DOUBLE),\n"
20+
" returns table (\n order_id INTEGER,\n amount DOUBLE\n ),\n);\n\n"
21+
'SELECT order_id, amount\nFROM __ref("stg_orders")\n'
22+
"WHERE customer_id = p_customer AND amount >= p_floor\n"
23+
),
24+
"models/marts/customer_totals.sql": (
25+
'MODEL (\n description "Order totals per customer",\n);\n\n'
26+
'-- __ref("hidden_in_comment")\n'
27+
"SELECT o.customer_id, SUM(f.amount) AS total_amount,\n"
28+
' \'__ref("hidden_in_string")\' AS note, $$__table_fn("hidden")(1)$$ AS raw_note\n'
29+
'FROM __ref("stg_orders") o\n'
30+
'CROSS JOIN __table_fn("orders_for")\n (10, /* floor, */ 1.5) f\n'
31+
"GROUP BY o.customer_id\n"
32+
),
33+
}
34+
35+
36+
@pytest.mark.parametrize(
37+
"test_case",
38+
[
39+
HarnessRunTestCase(
40+
description="table_functions_and_hidden_references",
41+
extra_arguments=(),
42+
expected_exit_code=0,
43+
expected_lines=(
44+
"OK project/table_function_orders",
45+
"Compiler differential passed: 1 projects identical (python vs native-preview)",
46+
),
47+
expected_patterns=(),
48+
expected_absent=("DIFF",),
49+
),
50+
],
51+
ids=lambda case: case.description,
52+
)
53+
def test_given_table_function_project_when_comparing_engines_then_harness_passes(
54+
test_case: HarnessRunTestCase, tmp_path: Path, capsys: pytest.CaptureFixture[str]
55+
) -> None:
56+
project_dir: Path = tmp_path / "table_function_orders"
57+
for relative_path, contents in _REFERENCE_FILES.items():
58+
path: Path = project_dir / relative_path
59+
path.parent.mkdir(parents=True, exist_ok=True)
60+
_ = path.write_text(contents, encoding="utf-8", newline="\n")
61+
62+
exit_code: int = run_compiler_differential(
63+
harness_arguments(
64+
work_dir=tmp_path / "work", extra=test_case.extra_arguments, project=project_dir
65+
)
66+
)
67+
68+
output: str = capsys.readouterr().out
69+
assert exit_code == test_case.expected_exit_code, output
70+
assert all(line in output for line in test_case.expected_lines), output
71+
assert not any(text in output for text in test_case.expected_absent), output
72+
73+
74+
if __name__ == "__main__":
75+
pytest.main([__file__, "-vv"])

0 commit comments

Comments
 (0)