sqlmesh.core.model.common API documentation

Edit on GitHub

# sqlmesh.core.model.common

View Source

     1from __future__ import annotations
     2
     3import typing as t
     4
     5from pydantic import validator
     6from sqlglot import exp, maybe_parse
     7from sqlglot.expressions import split_num_words
     8from sqlglot.helper import seq_get
     9
    10from sqlmesh.core.dialect import parse
    11from sqlmesh.utils.errors import ConfigError
    12
    13
    14def parse_model_name(name: str) -> t.Tuple[t.Optional[str], t.Optional[str], str]:
    15    """Convert a model name into table parts.
    16
    17    Args:
    18        name: model name.
    19
    20    Returns:
    21        A tuple consisting of catalog, schema, table name.
    22    """
    23    return split_num_words(name, ".", 3)  # type: ignore
    24
    25
    26def parse_expression(
    27    v: t.Union[t.List[str], t.List[exp.Expression], str, exp.Expression, t.Callable, None],
    28) -> t.List[exp.Expression] | exp.Expression | t.Callable | None:
    29    """Helper method to deserialize SQLGlot expressions in Pydantic Models."""
    30    if v is None:
    31        return None
    32
    33    if callable(v):
    34        return v
    35
    36    if isinstance(v, list):
    37        return [e for e in (maybe_parse(i) for i in v) if e]
    38
    39    if isinstance(v, str):
    40        return seq_get(parse(v), 0)
    41
    42    if not v:
    43        raise ConfigError(f"Could not parse {v}")
    44
    45    return v
    46
    47
    48expression_validator = validator(
    49    "query", "expressions_", pre=True, allow_reuse=True, check_fields=False
    50)(parse_expression)

def parse_model_name(name: str) -> Tuple[Optional[str], Optional[str], str]: View Source

    15def parse_model_name(name: str) -> t.Tuple[t.Optional[str], t.Optional[str], str]:
    16    """Convert a model name into table parts.
    17
    18    Args:
    19        name: model name.
    20
    21    Returns:
    22        A tuple consisting of catalog, schema, table name.
    23    """
    24    return split_num_words(name, ".", 3)  # type: ignore

Convert a model name into table parts.

###### Arguments:

- name: model name.

###### Returns:

> A tuple consisting of catalog, schema, table name.

def parse_expression( v: Union[List[str], List[sqlglot.expressions.Expression], str, sqlglot.expressions.Expression, Callable, NoneType]) -> Union[List[sqlglot.expressions.Expression], sqlglot.expressions.Expression, Callable, NoneType]: View Source

    27def parse_expression(
    28    v: t.Union[t.List[str], t.List[exp.Expression], str, exp.Expression, t.Callable, None],
    29) -> t.List[exp.Expression] | exp.Expression | t.Callable | None:
    30    """Helper method to deserialize SQLGlot expressions in Pydantic Models."""
    31    if v is None:
    32        return None
    33
    34    if callable(v):
    35        return v
    36
    37    if isinstance(v, list):
    38        return [e for e in (maybe_parse(i) for i in v) if e]
    39
    40    if isinstance(v, str):
    41        return seq_get(parse(v), 0)
    42
    43    if not v:
    44        raise ConfigError(f"Could not parse {v}")
    45
    46    return v

Helper method to deserialize SQLGlot expressions in Pydantic Models.

def expression_validator( v: Union[List[str], List[sqlglot.expressions.Expression], str, sqlglot.expressions.Expression, Callable, NoneType]) -> Union[List[sqlglot.expressions.Expression], sqlglot.expressions.Expression, Callable, NoneType]: View Source

    27def parse_expression(
    28    v: t.Union[t.List[str], t.List[exp.Expression], str, exp.Expression, t.Callable, None],
    29) -> t.List[exp.Expression] | exp.Expression | t.Callable | None:
    30    """Helper method to deserialize SQLGlot expressions in Pydantic Models."""
    31    if v is None:
    32        return None
    33
    34    if callable(v):
    35        return v
    36
    37    if isinstance(v, list):
    38        return [e for e in (maybe_parse(i) for i in v) if e]
    39
    40    if isinstance(v, str):
    41        return seq_get(parse(v), 0)
    42
    43    if not v:
    44        raise ConfigError(f"Could not parse {v}")
    45
    46    return v

Helper method to deserialize SQLGlot expressions in Pydantic Models.