sqlmesh.core.model.decorator API documentation

Edit on GitHub

# sqlmesh.core.model.decorator

View Source

     1from __future__ import annotations
     2
     3import typing as t
     4from pathlib import Path
     5
     6from sqlglot import exp
     7
     8from sqlmesh.core import constants as c
     9from sqlmesh.core.model.definition import Model, create_python_model
    10from sqlmesh.utils import registry_decorator
    11from sqlmesh.utils.errors import ConfigError
    12from sqlmesh.utils.metaprogramming import build_env, serialize_env
    13
    14
    15class model(registry_decorator):
    16    """Specifies a function is a python based model."""
    17
    18    registry_name = "python_models"
    19
    20    def __init__(self, name: str, **kwargs: t.Any) -> None:
    21        if not name:
    22            raise ConfigError("Python model must have a name.")
    23        if not "columns" in kwargs:
    24            raise ConfigError("Python model must define column schema.")
    25
    26        self.name = name
    27        self.kwargs = kwargs
    28
    29        # Make sure that argument values are expressions in order to
    30        # pass validation in ModelMeta.
    31        for calls_key in ("pre", "post", "audits"):
    32            calls = self.kwargs.pop(calls_key, [])
    33            self.kwargs[calls_key] = [
    34                (call, {})
    35                if isinstance(call, str)
    36                else (
    37                    call[0],
    38                    {arg_key: exp.convert(arg_value) for arg_key, arg_value in call[1].items()},
    39                )
    40                for call in calls
    41            ]
    42
    43        self.columns = {
    44            column_name: column_type
    45            if isinstance(column_type, exp.DataType)
    46            else exp.DataType.build(str(column_type))
    47            for column_name, column_type in self.kwargs.pop("columns").items()
    48        }
    49
    50    def model(
    51        self,
    52        *,
    53        module_path: Path,
    54        path: Path,
    55        defaults: t.Optional[t.Dict[str, t.Any]] = None,
    56        time_column_format: str = c.DEFAULT_TIME_COLUMN_FORMAT,
    57    ) -> Model:
    58        """Get the model registered by this function."""
    59        env: t.Dict[str, t.Any] = {}
    60        entrypoint = self.func.__name__
    61
    62        build_env(
    63            self.func,
    64            env=env,
    65            name=entrypoint,
    66            path=module_path,
    67        )
    68
    69        return create_python_model(
    70            self.name,
    71            entrypoint,
    72            defaults=defaults,
    73            path=path,
    74            time_column_format=time_column_format,
    75            python_env=serialize_env(env, path=module_path),
    76            columns=self.columns,
    77            **self.kwargs,
    78        )

class model(sqlmesh.utils.registry_decorator): View Source

    16class model(registry_decorator):
    17    """Specifies a function is a python based model."""
    18
    19    registry_name = "python_models"
    20
    21    def __init__(self, name: str, **kwargs: t.Any) -> None:
    22        if not name:
    23            raise ConfigError("Python model must have a name.")
    24        if not "columns" in kwargs:
    25            raise ConfigError("Python model must define column schema.")
    26
    27        self.name = name
    28        self.kwargs = kwargs
    29
    30        # Make sure that argument values are expressions in order to
    31        # pass validation in ModelMeta.
    32        for calls_key in ("pre", "post", "audits"):
    33            calls = self.kwargs.pop(calls_key, [])
    34            self.kwargs[calls_key] = [
    35                (call, {})
    36                if isinstance(call, str)
    37                else (
    38                    call[0],
    39                    {arg_key: exp.convert(arg_value) for arg_key, arg_value in call[1].items()},
    40                )
    41                for call in calls
    42            ]
    43
    44        self.columns = {
    45            column_name: column_type
    46            if isinstance(column_type, exp.DataType)
    47            else exp.DataType.build(str(column_type))
    48            for column_name, column_type in self.kwargs.pop("columns").items()
    49        }
    50
    51    def model(
    52        self,
    53        *,
    54        module_path: Path,
    55        path: Path,
    56        defaults: t.Optional[t.Dict[str, t.Any]] = None,
    57        time_column_format: str = c.DEFAULT_TIME_COLUMN_FORMAT,
    58    ) -> Model:
    59        """Get the model registered by this function."""
    60        env: t.Dict[str, t.Any] = {}
    61        entrypoint = self.func.__name__
    62
    63        build_env(
    64            self.func,
    65            env=env,
    66            name=entrypoint,
    67            path=module_path,
    68        )
    69
    70        return create_python_model(
    71            self.name,
    72            entrypoint,
    73            defaults=defaults,
    74            path=path,
    75            time_column_format=time_column_format,
    76            python_env=serialize_env(env, path=module_path),
    77            columns=self.columns,
    78            **self.kwargs,
    79        )

Specifies a function is a python based model.

model(name: str, **kwargs: Any) View Source

    21    def __init__(self, name: str, **kwargs: t.Any) -> None:
    22        if not name:
    23            raise ConfigError("Python model must have a name.")
    24        if not "columns" in kwargs:
    25            raise ConfigError("Python model must define column schema.")
    26
    27        self.name = name
    28        self.kwargs = kwargs
    29
    30        # Make sure that argument values are expressions in order to
    31        # pass validation in ModelMeta.
    32        for calls_key in ("pre", "post", "audits"):
    33            calls = self.kwargs.pop(calls_key, [])
    34            self.kwargs[calls_key] = [
    35                (call, {})
    36                if isinstance(call, str)
    37                else (
    38                    call[0],
    39                    {arg_key: exp.convert(arg_value) for arg_key, arg_value in call[1].items()},
    40                )
    41                for call in calls
    42            ]
    43
    44        self.columns = {
    45            column_name: column_type
    46            if isinstance(column_type, exp.DataType)
    47            else exp.DataType.build(str(column_type))
    48            for column_name, column_type in self.kwargs.pop("columns").items()
    49        }

def model( self, *, module_path: pathlib.Path, path: pathlib.Path, defaults: Optional[Dict[str, Any]] = None, time_column_format: str = '%Y-%m-%d') -> Annotated[Union[sqlmesh.core.model.definition.SqlModel, sqlmesh.core.model.definition.SeedModel, sqlmesh.core.model.definition.PythonModel], FieldInfo(default=PydanticUndefined, discriminator='source_type', extra={})]: View Source

    51    def model(
    52        self,
    53        *,
    54        module_path: Path,
    55        path: Path,
    56        defaults: t.Optional[t.Dict[str, t.Any]] = None,
    57        time_column_format: str = c.DEFAULT_TIME_COLUMN_FORMAT,
    58    ) -> Model:
    59        """Get the model registered by this function."""
    60        env: t.Dict[str, t.Any] = {}
    61        entrypoint = self.func.__name__
    62
    63        build_env(
    64            self.func,
    65            env=env,
    66            name=entrypoint,
    67            path=module_path,
    68        )
    69
    70        return create_python_model(
    71            self.name,
    72            entrypoint,
    73            defaults=defaults,
    74            path=path,
    75            time_column_format=time_column_format,
    76            python_env=serialize_env(env, path=module_path),
    77            columns=self.columns,
    78            **self.kwargs,
    79        )

Get the model registered by this function.

##### Inherited Members

sqlmesh.utils.registry_decorator
  registry
  get_registry
  set_registry