|
| 1 | +"""Tests for type hinting SQLMesh models""" |
| 2 | + |
| 3 | +import pytest |
| 4 | + |
| 5 | +from sqlglot import exp, parse_one |
| 6 | + |
| 7 | +from sqlmesh.core.context import Context |
| 8 | +from sqlmesh.lsp.context import LSPContext, ModelTarget |
| 9 | +from sqlmesh.lsp.hints import get_hints, _get_type_hints_for_model_from_query |
| 10 | +from sqlmesh.lsp.uri import URI |
| 11 | + |
| 12 | + |
| 13 | +@pytest.mark.fast |
| 14 | +def test_hints() -> None: |
| 15 | + context = Context(paths=["examples/sushi"]) |
| 16 | + lsp_context = LSPContext(context) |
| 17 | + |
| 18 | + # Find model URIs |
| 19 | + active_customers_path = next( |
| 20 | + path |
| 21 | + for path, info in lsp_context.map.items() |
| 22 | + if isinstance(info, ModelTarget) and "sushi.active_customers" in info.names |
| 23 | + ) |
| 24 | + customer_revenue_lifetime_path = next( |
| 25 | + path |
| 26 | + for path, info in lsp_context.map.items() |
| 27 | + if isinstance(info, ModelTarget) and "sushi.customer_revenue_lifetime" in info.names |
| 28 | + ) |
| 29 | + customer_revenue_by_day_path = next( |
| 30 | + path |
| 31 | + for path, info in lsp_context.map.items() |
| 32 | + if isinstance(info, ModelTarget) and "sushi.customer_revenue_by_day" in info.names |
| 33 | + ) |
| 34 | + |
| 35 | + active_customers_uri = URI.from_path(active_customers_path) |
| 36 | + ac_hints = get_hints(lsp_context, active_customers_uri, start_line=0, end_line=9999) |
| 37 | + assert len(ac_hints) == 2 |
| 38 | + assert ac_hints[0].label == "::INT" |
| 39 | + assert ac_hints[1].label == "::TEXT" |
| 40 | + |
| 41 | + customer_revenue_lifetime_uri = URI.from_path(customer_revenue_lifetime_path) |
| 42 | + crl_hints = get_hints( |
| 43 | + lsp_context=lsp_context, |
| 44 | + document_uri=customer_revenue_lifetime_uri, |
| 45 | + start_line=0, |
| 46 | + end_line=9999, |
| 47 | + ) |
| 48 | + assert len(crl_hints) == 3 |
| 49 | + assert crl_hints[0].label == "::INT" |
| 50 | + assert crl_hints[1].label == "::DOUBLE" |
| 51 | + assert crl_hints[2].label == "::DATE" |
| 52 | + |
| 53 | + customer_revenue_by_day_uri = URI.from_path(customer_revenue_by_day_path) |
| 54 | + crbd_hints = get_hints( |
| 55 | + lsp_context=lsp_context, |
| 56 | + document_uri=customer_revenue_by_day_uri, |
| 57 | + start_line=0, |
| 58 | + end_line=9999, |
| 59 | + ) |
| 60 | + assert len(crbd_hints) == 1 |
| 61 | + assert crbd_hints[0].label == "::INT" |
| 62 | + |
| 63 | + |
| 64 | +@pytest.mark.fast |
| 65 | +def test_union_hints() -> None: |
| 66 | + query_str = """SELECT a FROM table_a UNION SELECT b FROM table_b UNION SELECT c FROM table_c""" |
| 67 | + query = parse_one(query_str, dialect="postgres") |
| 68 | + |
| 69 | + result = _get_type_hints_for_model_from_query( |
| 70 | + query=query, |
| 71 | + dialect="postgres", |
| 72 | + columns_to_types={ |
| 73 | + "a": exp.DataType.build("TEXT"), |
| 74 | + "b": exp.DataType.build("INT"), |
| 75 | + "c": exp.DataType.build("DATE"), |
| 76 | + }, |
| 77 | + start_line=0, |
| 78 | + end_line=1, |
| 79 | + ) |
| 80 | + |
| 81 | + assert len(result) == 3 |
| 82 | + assert result[0].label == "::DATE" |
| 83 | + assert result[1].label == "::TEXT" |
| 84 | + assert result[2].label == "::INT" |
| 85 | + |
| 86 | + |
| 87 | +@pytest.mark.fast |
| 88 | +def test_complex_hints() -> None: |
| 89 | + query = parse_one("SELECT a, b FROM c", dialect="postgres") |
| 90 | + |
| 91 | + result = _get_type_hints_for_model_from_query( |
| 92 | + query=query, |
| 93 | + dialect="postgres", |
| 94 | + columns_to_types={ |
| 95 | + "a": exp.DataType.build("VARCHAR(100)"), |
| 96 | + "b": exp.DataType.build("STRUCT<INT, STRUCT<TEXT, ARRAY<INT>>>"), |
| 97 | + }, |
| 98 | + start_line=0, |
| 99 | + end_line=1, |
| 100 | + ) |
| 101 | + |
| 102 | + assert len(result) == 2 |
| 103 | + assert result[0].label == "::VARCHAR(100)" |
| 104 | + assert result[1].label == "::STRUCT<INT, STRUCT<TEXT, INT[]>>" |
| 105 | + |
| 106 | + |
| 107 | +@pytest.mark.fast |
| 108 | +def test_simple_cast_hints() -> None: |
| 109 | + """Don't add type hints if the expression is already a cast""" |
| 110 | + query = parse_one("SELECT a::INT, CAST(b AS DATE), c FROM d", dialect="postgres") |
| 111 | + |
| 112 | + result = _get_type_hints_for_model_from_query( |
| 113 | + query=query, |
| 114 | + dialect="postgres", |
| 115 | + columns_to_types={ |
| 116 | + "a": exp.DataType.build("INT"), |
| 117 | + "b": exp.DataType.build("DATE"), |
| 118 | + "c": exp.DataType.build("TEXT"), |
| 119 | + }, |
| 120 | + start_line=0, |
| 121 | + end_line=1, |
| 122 | + ) |
| 123 | + |
| 124 | + assert len(result) == 1 |
| 125 | + assert result[0].label == "::TEXT" |
| 126 | + |
| 127 | + |
| 128 | +@pytest.mark.fast |
| 129 | +def test_alias_cast_hints() -> None: |
| 130 | + """Don't add type hints if the expression is already a cast""" |
| 131 | + query = parse_one( |
| 132 | + "SELECT raw_a::INT AS a, CAST(raw_b AS DATE) AS b, c FROM d", dialect="postgres" |
| 133 | + ) |
| 134 | + |
| 135 | + result = _get_type_hints_for_model_from_query( |
| 136 | + query=query, |
| 137 | + dialect="postgres", |
| 138 | + columns_to_types={ |
| 139 | + "a": exp.DataType.build("INT"), |
| 140 | + "b": exp.DataType.build("DATE"), |
| 141 | + "c": exp.DataType.build("TEXT"), |
| 142 | + }, |
| 143 | + start_line=0, |
| 144 | + end_line=1, |
| 145 | + ) |
| 146 | + |
| 147 | + assert len(result) == 1 |
| 148 | + assert result[0].label == "::TEXT" |
| 149 | + |
| 150 | + |
| 151 | +@pytest.mark.fast |
| 152 | +def test_simple_cte_hints() -> None: |
| 153 | + """Don't add type hints if the expression is already a cast""" |
| 154 | + query = parse_one("WITH t AS (SELECT a FROM b) SELECT a AS c FROM t", dialect="postgres") |
| 155 | + |
| 156 | + result = _get_type_hints_for_model_from_query( |
| 157 | + query=query, |
| 158 | + dialect="postgres", |
| 159 | + columns_to_types={ |
| 160 | + "c": exp.DataType.build("INT"), |
| 161 | + }, |
| 162 | + start_line=0, |
| 163 | + end_line=1, |
| 164 | + ) |
| 165 | + |
| 166 | + assert len(result) == 1 |
| 167 | + assert result[0].label == "::INT" |
| 168 | + |
| 169 | + |
| 170 | +@pytest.mark.fast |
| 171 | +def test_cte_with_union_hints() -> None: |
| 172 | + """Don't add type hints if the expression is already a cast""" |
| 173 | + query = parse_one( |
| 174 | + """WITH x AS (SELECT a FROM t), |
| 175 | + y AS (SELECT b FROM t), |
| 176 | + z AS (SELECT c FROM t) |
| 177 | + SELECT a AS d FROM x |
| 178 | + UNION |
| 179 | + SELECT b AS e FROM y |
| 180 | + UNION |
| 181 | + SELECT c AS f FROM z""", |
| 182 | + dialect="postgres", |
| 183 | + ) |
| 184 | + |
| 185 | + result = _get_type_hints_for_model_from_query( |
| 186 | + query=query, |
| 187 | + dialect="postgres", |
| 188 | + columns_to_types={ |
| 189 | + "a": exp.DataType.build("TEXT"), |
| 190 | + "b": exp.DataType.build("DATE"), |
| 191 | + "c": exp.DataType.build("INT"), |
| 192 | + "d": exp.DataType.build("TEXT"), |
| 193 | + "e": exp.DataType.build("DATE"), |
| 194 | + "f": exp.DataType.build("INT"), |
| 195 | + }, |
| 196 | + start_line=0, |
| 197 | + end_line=9999, |
| 198 | + ) |
| 199 | + |
| 200 | + assert len(result) == 3 |
| 201 | + assert result[0].label == "::INT" |
| 202 | + assert result[1].label == "::TEXT" |
| 203 | + assert result[2].label == "::DATE" |
0 commit comments