Skip to content

Commit acf988a

Browse files
committed
feat(native): extract unexpanded direct-logic SQL tests natively behind the preview engine
1 parent 87d1e3a commit acf988a

5 files changed

Lines changed: 158 additions & 2 deletions

File tree

‎src/sqlbuild/compiler/compile/_helpers/attachment/sql_tests.py‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,10 @@
3131
from sqlbuild.compiler.compile._helpers.sql_tests.core import (
3232
complete_omitted_ceremonial_select,
3333
extract_assertion_target_model_names,
34-
extract_sql_test_ctes,
3534
)
3635
from sqlbuild.compiler.compile._helpers.sql_tests.native import (
3736
extract_expanded_sql_tests_cached,
37+
extract_unexpanded_sql_test,
3838
)
3939
from sqlbuild.compiler.compile.exceptions import CompileInputError
4040
from sqlbuild.compiler.compile.models import (
@@ -422,7 +422,7 @@ def _validate_raw_direct_logic_test_ctes(
422422
test_mode: SqlTestMode,
423423
syntax: SqlLexicalSyntax,
424424
) -> CompileSqlTestCtes:
425-
return extract_sql_test_ctes(
425+
return extract_unexpanded_sql_test(
426426
sql=test_block.sql_body,
427427
file_label=str(test_file.relative_path),
428428
syntax=syntax,

‎src/sqlbuild/compiler/compile/_helpers/sql_tests/native.py‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@
2323
)
2424
from sqlbuild.compiler.compile.types import SqlTestMode
2525
from sqlbuild.compiler.fact_cache.classes.fact_cache_store import FactCacheStore
26+
from sqlbuild.compiler.frontier.main.native_stage_enabled import native_stage_enabled
27+
from sqlbuild.compiler.frontier.types import NativeStage
2628
from sqlbuild.compiler.sql_analysis.models import SqlLexicalSyntax
2729

2830

@@ -140,6 +142,19 @@ def extract_expanded_sql_tests(
140142
)
141143

142144

145+
def extract_unexpanded_sql_test(
146+
*, sql: str, file_label: str, syntax: SqlLexicalSyntax, mode: SqlTestMode
147+
) -> CompileSqlTestCtes:
148+
"""Extract a test before expansion, natively in preview; Python decides what native rejects."""
149+
150+
if native_stage_enabled(NativeStage.ATTACHMENTS):
151+
try:
152+
return extract_expanded_sql_tests(tests=((sql, file_label, mode),), syntax=syntax)[0]
153+
except CompileInputError:
154+
pass
155+
return extract_sql_test_ctes(sql=sql, file_label=file_label, syntax=syntax, mode=mode)
156+
157+
143158
def _extract_expanded_sql_tests_natively(
144159
tests: tuple[tuple[str, str, SqlTestMode], ...],
145160
) -> tuple[CompileSqlTestCtes, ...]:

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,3 +67,14 @@ class OmittedSelectParityTestCase:
6767
count: int
6868
syntax: SqlLexicalSyntax
6969
expected_minimum_completed: int
70+
71+
72+
@dataclass(frozen=True)
73+
class RawDirectLogicParityTestCase:
74+
"""Seeded unexpanded direct-logic test bodies extracted natively and by Python."""
75+
76+
description: str
77+
seed: int
78+
count: int
79+
expected_minimum_extracted: int
80+
expected_minimum_python_errors: int

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

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
from sqlbuild.compiler.compile._helpers.render.parameters import expand_test_parameters
3030
from sqlbuild.compiler.compile._helpers.render.sql_vars import expand_authored_sql_result
3131
from sqlbuild.compiler.compile._helpers.sql_tests.core import complete_omitted_ceremonial_select
32+
from sqlbuild.compiler.compile._helpers.sql_tests.native import extract_unexpanded_sql_test
3233
from sqlbuild.compiler.compile.exceptions import CompileInputError
3334
from sqlbuild.compiler.compile.main._build_compile_inputs import build_compile_inputs
3435
from sqlbuild.compiler.compile.models import (
@@ -37,6 +38,7 @@
3738
CompileProjectInputs,
3839
MacroContext,
3940
)
41+
from sqlbuild.compiler.compile.types import SqlTestMode
4042
from sqlbuild.compiler.discovery.main.discover import discover_project_inputs
4143
from sqlbuild.compiler.discovery.models import DiscoveredProjectInputs
4244
from sqlbuild.compiler.frontier.constants import COMPILER_ENGINE_ENV_VAR
@@ -582,3 +584,66 @@ def completed_body(
582584

583585
monkeypatch.setenv(COMPILER_ENGINE_ENV_VAR, engine.value)
584586
return complete_omitted_ceremonial_select(sql=sql, syntax=syntax)
587+
588+
589+
_DIRECT_MODES: tuple[SqlTestMode, ...] = (SqlTestMode.MACRO, SqlTestMode.UDF, SqlTestMode.TABLE_FN)
590+
_DIRECT_CTE_NAMES: dict[SqlTestMode, tuple[str, str]] = {
591+
SqlTestMode.MACRO: ("__macro_actual__", "__macro_expected__"),
592+
SqlTestMode.UDF: ("__udf_actual__", "__udf_expected__"),
593+
SqlTestMode.TABLE_FN: ("__table_fn_actual__", "__table_fn_expected__"),
594+
}
595+
_ACTUAL_BODIES: tuple[str, ...] = (
596+
'SELECT @tidy_label("status") AS status FROM input_values',
597+
'SELECT __udf("order_label")(status) AS label FROM input_values',
598+
'SELECT * FROM __table_fn("order_rows")(@@limit_rows)',
599+
"SELECT @@region AS region, '@@ENV:SQB_X' AS env",
600+
'SELECT @param("region") AS region',
601+
"SELECT 1 /* @tidy_label('x') */",
602+
"SELECT 'it''s' AS quoted, `tick` AS ticked",
603+
)
604+
_EXPECTED_BODIES: tuple[str, ...] = (
605+
"SELECT 'placed' AS status",
606+
"SELECT 1 AS id, 'web' AS label UNION ALL SELECT 2 AS id, 'store' AS label",
607+
"SELECT *",
608+
"SELECT @tidy_label('x') AS status",
609+
'SELECT __udf("order_label")(1) AS label',
610+
"SELECT 1 AS id, 2",
611+
)
612+
_HELPER_CTES: tuple[str, ...] = (
613+
"",
614+
"input_values AS (SELECT ' Placed ' AS status),\n",
615+
"input_values AS (SELECT @@region AS status),\n",
616+
"__ref__orders AS (SELECT 1 AS id),\n",
617+
"input_values AS (SELECT $$ ) $$ AS status),\n",
618+
)
619+
_TAILS: tuple[str, ...] = ("\nSELECT 1\n", "", ";", "\nSELECT 2", " -- end")
620+
621+
622+
def generated_direct_logic_test(*, rng: random.Random) -> tuple[str, SqlTestMode]:
623+
"""Return one unexpanded direct-logic test body and its mode."""
624+
625+
mode: SqlTestMode = rng.choice(_DIRECT_MODES)
626+
actual, expected = _DIRECT_CTE_NAMES[rng.choice((*[mode] * 27, *_DIRECT_MODES))]
627+
ctes: list[str] = [
628+
f"{actual} AS (\n {rng.choice(_ACTUAL_BODIES)}\n)",
629+
f"{expected} AS (\n {rng.choice(_EXPECTED_BODIES)}\n)",
630+
]
631+
rng.shuffle(ctes)
632+
return (
633+
"WITH\n" + rng.choice(_HELPER_CTES) + ",\n".join(ctes) + rng.choice(_TAILS),
634+
mode,
635+
)
636+
637+
638+
def raw_extraction_outcome(
639+
*, sql: str, mode: SqlTestMode, engine: CompilerEngine, monkeypatch: pytest.MonkeyPatch
640+
) -> object:
641+
"""Extract one unexpanded test under `engine`, or return Python's error text."""
642+
643+
monkeypatch.setenv(COMPILER_ENGINE_ENV_VAR, engine.value)
644+
try:
645+
return extract_unexpanded_sql_test(
646+
sql=sql, file_label="tests/unit/test_orders.sql", syntax=SqlLexicalSyntax(), mode=mode
647+
)
648+
except CompileInputError as error:
649+
return str(error)
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
"""Unexpanded direct-logic SQL tests extract identically under the preview engine."""
2+
3+
from __future__ import annotations
4+
5+
import random
6+
7+
import pytest
8+
9+
from sqlbuild.compiler.compile.models import CompileSqlTestCtes
10+
from sqlbuild.compiler.compile.types import SqlTestMode
11+
from sqlbuild.compiler.frontier.types import CompilerEngine
12+
from tests.integration.src.sqlbuild.compiler.attachments._test_types import (
13+
RawDirectLogicParityTestCase,
14+
)
15+
from tests.integration.src.sqlbuild.compiler.attachments.helpers import (
16+
generated_direct_logic_test,
17+
raw_extraction_outcome,
18+
)
19+
from tests.integration.src.sqlbuild.compiler.helpers import mismatches
20+
21+
22+
@pytest.mark.parametrize(
23+
"test_case",
24+
[
25+
RawDirectLogicParityTestCase(
26+
description="macro, UDF and table-function tests before macro and variable expansion",
27+
seed=20261008,
28+
count=3000,
29+
expected_minimum_extracted=600,
30+
expected_minimum_python_errors=600,
31+
),
32+
],
33+
ids=lambda case: case.description,
34+
)
35+
def test_given_unexpanded_direct_logic_tests_when_extracting_natively_then_python_matches(
36+
test_case: RawDirectLogicParityTestCase, monkeypatch: pytest.MonkeyPatch
37+
) -> None:
38+
rng: random.Random = random.Random(test_case.seed)
39+
tests: list[tuple[str, SqlTestMode]] = [
40+
generated_direct_logic_test(rng=rng) for _ in range(test_case.count)
41+
]
42+
43+
python: list[object] = [
44+
raw_extraction_outcome(
45+
sql=sql, mode=mode, engine=CompilerEngine.PYTHON, monkeypatch=monkeypatch
46+
)
47+
for sql, mode in tests
48+
]
49+
native: list[object] = [
50+
raw_extraction_outcome(
51+
sql=sql, mode=mode, engine=CompilerEngine.NATIVE_PREVIEW, monkeypatch=monkeypatch
52+
)
53+
for sql, mode in tests
54+
]
55+
56+
assert (
57+
mismatches(inputs=[*tests], expected=python, actual=native),
58+
sum(isinstance(item, CompileSqlTestCtes) for item in python)
59+
>= test_case.expected_minimum_extracted,
60+
sum(isinstance(item, str) for item in python) >= test_case.expected_minimum_python_errors,
61+
) == ([], True, True), test_case.description
62+
63+
64+
if __name__ == "__main__":
65+
pytest.main([__file__, "-vv"])

0 commit comments

Comments
 (0)