|
| 1 | +"""Fixture-column inference implementation.""" |
| 2 | + |
| 3 | +from __future__ import annotations |
| 4 | + |
| 5 | +from typing import Any, cast |
| 6 | + |
| 7 | +from sqlbuild.adapter.contract.models import ExpressionInferenceProfile |
| 8 | +from sqlbuild.compiler.compile._helpers.analysis.columns import ( |
| 9 | + analyze_columns_and_lineage_with_polyglot, |
| 10 | +) |
| 11 | +from sqlbuild.compiler.compile.constants import ( |
| 12 | + POLYGLOT_ARRAY_KIND, |
| 13 | + POLYGLOT_FUNCTION_KIND, |
| 14 | + POLYGLOT_LITERAL_KIND, |
| 15 | + POLYGLOT_STRUCTURED_KINDS, |
| 16 | +) |
| 17 | +from sqlbuild.compiler.compile.models import ( |
| 18 | + FixtureColumnInference, |
| 19 | + InferredColumn, |
| 20 | + PolyglotAnalysisResult, |
| 21 | +) |
| 22 | +from sqlbuild.compiler.sql_analysis.constants import POLYGLOT_KIND_NULL |
| 23 | +from sqlbuild.compiler.sql_analysis.main.import_polyglot_sql import import_polyglot_sql |
| 24 | + |
| 25 | + |
| 26 | +def infer_fixture_column_facts( |
| 27 | + *, query_sql: str, inference_profile: ExpressionInferenceProfile |
| 28 | +) -> FixtureColumnInference | None: |
| 29 | + """Infer fixture columns and identify explicitly untyped NULL projections.""" |
| 30 | + |
| 31 | + analysis: PolyglotAnalysisResult = analyze_columns_and_lineage_with_polyglot( |
| 32 | + query_sql=query_sql, |
| 33 | + inference_profile=inference_profile, |
| 34 | + ) |
| 35 | + if not analysis.analysis_succeeded or analysis.has_star or analysis.columns is None: |
| 36 | + return None |
| 37 | + simple_types, null_literal_names, quoted_names = _simple_projection_facts( |
| 38 | + query_sql=query_sql, |
| 39 | + dialect=inference_profile.sql_analysis_dialect, |
| 40 | + ) |
| 41 | + return FixtureColumnInference( |
| 42 | + columns=tuple( |
| 43 | + InferredColumn( |
| 44 | + name=column.name, |
| 45 | + type=column.type or simple_types.get(column.name.casefold()), |
| 46 | + nullability=column.nullability, |
| 47 | + ) |
| 48 | + for column in analysis.columns |
| 49 | + ), |
| 50 | + null_literal_names=null_literal_names, |
| 51 | + quoted_names=quoted_names, |
| 52 | + ) |
| 53 | + |
| 54 | + |
| 55 | +def _simple_projection_facts( |
| 56 | + *, query_sql: str, dialect: str | None |
| 57 | +) -> tuple[dict[str, str], frozenset[str], frozenset[str]]: |
| 58 | + polyglot_sql: Any = import_polyglot_sql() |
| 59 | + try: |
| 60 | + payload: dict[str, Any] = polyglot_sql.parse_one( |
| 61 | + query_sql, dialect=dialect or "generic" |
| 62 | + ).to_dict() |
| 63 | + except polyglot_sql.PolyglotError: |
| 64 | + return {}, frozenset(), frozenset() |
| 65 | + direct_select_payload: object = payload.get("select") |
| 66 | + select_payload: dict[str, Any] | None = _first_select(payload) |
| 67 | + if select_payload is None: |
| 68 | + return {}, frozenset(), frozenset() |
| 69 | + types, null_literal_names, quoted_names = _collect_projection_facts( |
| 70 | + select_payload=select_payload |
| 71 | + ) |
| 72 | + effective_null_literal_names: frozenset[str] = ( |
| 73 | + null_literal_names if isinstance(direct_select_payload, dict) else frozenset() |
| 74 | + ) |
| 75 | + return types, effective_null_literal_names, quoted_names |
| 76 | + |
| 77 | + |
| 78 | +def _collect_projection_facts( |
| 79 | + *, select_payload: dict[str, Any] |
| 80 | +) -> tuple[dict[str, str], frozenset[str], frozenset[str]]: |
| 81 | + types: dict[str, str] = {} |
| 82 | + null_literal_names: set[str] = set() |
| 83 | + quoted_names: set[str] = set() |
| 84 | + for expression in select_payload.get("expressions", []): |
| 85 | + alias_payload: object = expression.get("alias") if isinstance(expression, dict) else None |
| 86 | + if not isinstance(alias_payload, dict): |
| 87 | + continue |
| 88 | + alias: object = alias_payload.get("alias") |
| 89 | + value: object = alias_payload.get("this") |
| 90 | + if not isinstance(alias, dict) or not isinstance(value, dict): |
| 91 | + continue |
| 92 | + name: object = alias.get("name") |
| 93 | + if not isinstance(name, str): |
| 94 | + continue |
| 95 | + if alias.get("quoted") is True: |
| 96 | + quoted_names.add(name.casefold()) |
| 97 | + raw_value_kind: object = next(iter(value), "") |
| 98 | + if not isinstance(raw_value_kind, str): |
| 99 | + continue |
| 100 | + value_payload: object = value.get(raw_value_kind) |
| 101 | + if raw_value_kind == POLYGLOT_KIND_NULL: |
| 102 | + null_literal_names.add(name.casefold()) |
| 103 | + continue |
| 104 | + inferred_type: str | None = _simple_expression_type( |
| 105 | + value_kind=raw_value_kind, |
| 106 | + value_payload=value_payload, |
| 107 | + ) |
| 108 | + if inferred_type is not None: |
| 109 | + types[name.casefold()] = inferred_type |
| 110 | + return types, frozenset(null_literal_names), frozenset(quoted_names) |
| 111 | + |
| 112 | + |
| 113 | +def _simple_expression_type(*, value_kind: str, value_payload: object) -> str | None: |
| 114 | + if value_kind == POLYGLOT_LITERAL_KIND and isinstance(value_payload, dict): |
| 115 | + value_dict: dict[object, object] = cast(dict[object, object], value_payload) |
| 116 | + return { |
| 117 | + "string": "VARCHAR", |
| 118 | + "number": "NUMBER", |
| 119 | + "boolean": "BOOLEAN", |
| 120 | + }.get(str(value_dict.get("literal_type"))) |
| 121 | + if value_kind == POLYGLOT_ARRAY_KIND: |
| 122 | + return "ARRAY" |
| 123 | + if value_kind in POLYGLOT_STRUCTURED_KINDS: |
| 124 | + return "VARIANT" |
| 125 | + if value_kind == POLYGLOT_FUNCTION_KIND and isinstance(value_payload, dict): |
| 126 | + value_dict = cast(dict[object, object], value_payload) |
| 127 | + function_name: str = str(value_dict.get("name") or "").upper() |
| 128 | + return { |
| 129 | + "ARRAY_CONSTRUCT": "ARRAY", |
| 130 | + "OBJECT_CONSTRUCT": "OBJECT", |
| 131 | + "PARSE_JSON": "VARIANT", |
| 132 | + }.get(function_name) |
| 133 | + return None |
| 134 | + |
| 135 | + |
| 136 | +def _first_select(payload: object) -> dict[str, Any] | None: |
| 137 | + if isinstance(payload, dict): |
| 138 | + payload_dict: dict[object, object] = cast(dict[object, object], payload) |
| 139 | + select_payload: object = payload_dict.get("select") |
| 140 | + if isinstance(select_payload, dict): |
| 141 | + return cast(dict[str, Any], select_payload) |
| 142 | + for value in payload_dict.values(): |
| 143 | + found: dict[str, Any] | None = _first_select(value) |
| 144 | + if found is not None: |
| 145 | + return found |
| 146 | + if isinstance(payload, list): |
| 147 | + for value in payload: |
| 148 | + found = _first_select(value) |
| 149 | + if found is not None: |
| 150 | + return found |
| 151 | + return None |
0 commit comments