|
| 1 | +"""Executable twin of docs/known-limitations.md. |
| 2 | +
|
| 3 | +Every DELIBERATE limitation is asserted here: the SQL that hits it and the |
| 4 | +named build-time rejection (or, for contract choices, the chosen behavior). |
| 5 | +If an engine change lifts one of these, the test fails — update the doc in |
| 6 | +the same commit. Section numbers mirror the document. |
| 7 | +""" |
| 8 | + |
| 9 | +from __future__ import annotations |
| 10 | + |
| 11 | +import pytest |
| 12 | +from pydantic import create_model |
| 13 | +from test_duckdb_interpreter import duck_check, static |
| 14 | + |
| 15 | +from sql_transform._interpreter import DuckDBInferFn |
| 16 | + |
| 17 | +T = create_model("T", a=(int, ...), s=(str | None, None)) |
| 18 | + |
| 19 | + |
| 20 | +def build(sql, statics=None): |
| 21 | + return DuckDBInferFn(sql, row_tables={"__THIS__": T}, static_tables=statics or {}) |
| 22 | + |
| 23 | + |
| 24 | +def rejects(sql, needle, statics=None): |
| 25 | + with pytest.raises(ValueError, match=needle): |
| 26 | + build(sql, statics) |
| 27 | + |
| 28 | + |
| 29 | +# ---- 1. The specialization bargain ---------------------------------------- |
| 30 | + |
| 31 | + |
| 32 | +def test_non_constant_regex_pattern_rejects(): |
| 33 | + # Regexes compile ONCE at prepare; per-row compilation is the opposite |
| 34 | + # of specialization. |
| 35 | + rejects("SELECT regexp_matches(s, s) FROM __THIS__", "non-constant regex") |
| 36 | + rejects( |
| 37 | + "SELECT regexp_replace(s, 'a', s) FROM __THIS__", |
| 38 | + "non-constant regexp_replace replacement", |
| 39 | + ) |
| 40 | + |
| 41 | + |
| 42 | +def test_static_tables_are_frozen_unique_key_maps(): |
| 43 | + dup = static({"id": "int", "v": "int"}, [{"id": 1, "v": 1}, {"id": 1, "v": 2}]) |
| 44 | + # Duplicate keys = 1:N multiplicity — designed (stage B), not built. |
| 45 | + rejects( |
| 46 | + "SELECT v FROM __THIS__ JOIN d ON a = d.id", |
| 47 | + "duplicate map key", |
| 48 | + {"d": dup}, |
| 49 | + ) |
| 50 | + withnull = static({"id": "int", "v": "int?"}, [{"id": 1, "v": None}]) |
| 51 | + rejects( |
| 52 | + "SELECT v FROM __THIS__ JOIN d ON a = d.id", |
| 53 | + "NULL in value column", |
| 54 | + {"d": withnull}, |
| 55 | + ) |
| 56 | + |
| 57 | + |
| 58 | +def test_dynamic_self_join_rejects(): |
| 59 | + rejects( |
| 60 | + "SELECT t2.a FROM __THIS__ JOIN __THIS__ t2 ON __THIS__.a = t2.a", |
| 61 | + "dynamic table", |
| 62 | + ) |
| 63 | + |
| 64 | + |
| 65 | +# ---- 2. Out of scope for row-serving -------------------------------------- |
| 66 | + |
| 67 | + |
| 68 | +@pytest.mark.parametrize( |
| 69 | + ("sql", "needle"), |
| 70 | + [ |
| 71 | + ("SELECT sum(a) FROM __THIS__", "no aggregation"), |
| 72 | + ("SELECT a FROM __THIS__ GROUP BY a", "aggregation"), |
| 73 | + ("SELECT a FROM __THIS__ ORDER BY a", "ORDER BY"), |
| 74 | + ("SELECT a FROM __THIS__ LIMIT 5", "LIMIT"), |
| 75 | + ("SELECT DISTINCT a FROM __THIS__", "DISTINCT"), |
| 76 | + ("WITH c AS (SELECT 1) SELECT a FROM __THIS__", "common table"), |
| 77 | + ("SELECT a FROM __THIS__ UNION SELECT a FROM __THIS__", "UNION"), |
| 78 | + ("SELECT rowid FROM __THIS__", "rowid"), |
| 79 | + ( |
| 80 | + "SELECT a FROM __THIS__ FULL OUTER JOIN x ON a = x.a", |
| 81 | + "join type", |
| 82 | + ), |
| 83 | + ], |
| 84 | +) |
| 85 | +def test_whole_relation_constructs_reject(sql, needle): |
| 86 | + rejects(sql, needle) |
| 87 | + |
| 88 | + |
| 89 | +# ---- 3. Type-system boundaries --------------------------------------------- |
| 90 | + |
| 91 | + |
| 92 | +def test_non_scalar_row_columns_reject(): |
| 93 | + L = create_model("L", xs=(list[int], ...)) |
| 94 | + with pytest.raises(ValueError, match="non-scalar"): |
| 95 | + DuckDBInferFn( |
| 96 | + "SELECT xs FROM __THIS__", row_tables={"__THIS__": L}, static_tables={} |
| 97 | + ) |
| 98 | + |
| 99 | + |
| 100 | +def test_list_valued_regexp_forms_reject(): |
| 101 | + # Gated on list types (wave C), not on regex semantics. |
| 102 | + rejects("SELECT regexp_extract_all(s, 'a') FROM __THIS__", "list-valued") |
| 103 | + rejects("SELECT regexp_split_to_array(s, 'a') FROM __THIS__", "regexp_split") |
| 104 | + |
| 105 | + |
| 106 | +def test_ubigint_static_payloads_reject(): |
| 107 | + import pyarrow as pa |
| 108 | + |
| 109 | + big = pa.table({"id": pa.array([2**64 - 1], pa.uint64()), "v": [1]}) |
| 110 | + rejects( |
| 111 | + "SELECT v FROM __THIS__ JOIN d ON a = d.id", |
| 112 | + "outside BIGINT range", |
| 113 | + {"d": big}, |
| 114 | + ) |
| 115 | + |
| 116 | + |
| 117 | +# ---- 4. Semantics descoped after measurement ------------------------------- |
| 118 | + |
| 119 | + |
| 120 | +@pytest.mark.parametrize( |
| 121 | + ("sql", "needle"), |
| 122 | + [ |
| 123 | + # Grapheme-cluster semantics (UAX-29) not modeled. |
| 124 | + ("SELECT reverse(s) FROM __THIS__", "grapheme"), |
| 125 | + # ^ IS pow, but sqlparser's precedence would compute the wrong tree. |
| 126 | + ("SELECT a ^ 2 FROM __THIS__", "precedence"), |
| 127 | + # Regex reject list: measured RE2 <-> rust-regex divergences. |
| 128 | + ("SELECT regexp_matches(s, 'a\\B') FROM __THIS__", "B in a regex"), |
| 129 | + ("SELECT regexp_matches(s, '\\Qab\\E') FROM __THIS__", "literal quoting"), |
| 130 | + ("SELECT regexp_matches(s, 'a*+') FROM __THIS__", "stacked"), |
| 131 | + ( |
| 132 | + "SELECT regexp_matches(s, '(?P<n>a)(?P<n>b)') FROM __THIS__", |
| 133 | + "duplicate regex capture group", |
| 134 | + ), |
| 135 | + ("SELECT regexp_matches(s, 'a{1001}') FROM __THIS__", "repetition bound"), |
| 136 | + # Not implemented in DuckDB itself. |
| 137 | + ("SELECT s SIMILAR TO 'a' ESCAPE 'x' FROM __THIS__", "escape"), |
| 138 | + # Exec-time conversion order (an empty input succeeds in DuckDB). |
| 139 | + ("SELECT a IN ('abc') FROM __THIS__", "non-numeric"), |
| 140 | + # Only bare COLUMNS as a select item is served. |
| 141 | + ("SELECT COLUMNS('a') + 1 FROM __THIS__", "COLUMNS"), |
| 142 | + # SQLNULL has no home type. |
| 143 | + ("SELECT NULL FROM __THIS__", "NULL literal"), |
| 144 | + ], |
| 145 | +) |
| 146 | +def test_measured_descopes_reject(sql, needle): |
| 147 | + rejects(sql, needle) |
| 148 | + |
| 149 | + |
| 150 | +def test_using_unmerge_exclude_rejects(): |
| 151 | + d = static({"a": "int", "v": "int"}, [{"a": 1, "v": 10}]) |
| 152 | + # DuckDB UNMERGES the coalesced column here — measured, not modeled. |
| 153 | + rejects( |
| 154 | + "SELECT * EXCLUDE (d.a) FROM __THIS__ JOIN d USING (a)", |
| 155 | + "USING-merged", |
| 156 | + {"d": d}, |
| 157 | + ) |
| 158 | + |
| 159 | + |
| 160 | +# ---- 5. Deliberate contract choices ---------------------------------------- |
| 161 | + |
| 162 | + |
| 163 | +def test_duplicate_names_use_duckdbs_boundary_rename(): |
| 164 | + # Raw DuckDB keeps top-level duplicates; a typed model cannot. We apply |
| 165 | + # DuckDB's OWN subquery/CTAS/.df() rename — not an invention. |
| 166 | + fn = DuckDBInferFn( |
| 167 | + "SELECT a, a AS a, a AS a_1 FROM __THIS__", |
| 168 | + row_tables={"__THIS__": T}, |
| 169 | + static_tables={}, |
| 170 | + output="dict", |
| 171 | + ) |
| 172 | + got = fn.infer({"__THIS__": [T(a=7)]}) |
| 173 | + assert list(got[0].keys()) == ["a", "a_1", "a_1_1"] |
| 174 | + |
| 175 | + |
| 176 | +def test_null_op_null_serves_with_measured_types(): |
| 177 | + # The limitation is ONLY the context-free bare NULL; typed NULL ops |
| 178 | + # serve with the pinned result types. |
| 179 | + duck_check( |
| 180 | + "SELECT NULL + NULL AS s, NULL / NULL AS d FROM __THIS__", |
| 181 | + {"a": "int", "s": "str?"}, |
| 182 | + [{"a": 1, "s": None}], |
| 183 | + ) |
| 184 | + |
| 185 | + |
| 186 | +def test_rejections_are_build_time_and_named(): |
| 187 | + # The load-bearing property: limits surface at CONSTRUCTION, before any |
| 188 | + # row is ever inferred — never silently at inference time. |
| 189 | + with pytest.raises(ValueError, match="unsupported"): |
| 190 | + DuckDBInferFn( |
| 191 | + "SELECT sum(a) FROM __THIS__", |
| 192 | + row_tables={"__THIS__": T}, |
| 193 | + static_tables={}, |
| 194 | + ) |
0 commit comments