sqlmesh.dbt.column
1from __future__ import annotations 2 3import typing as t 4import logging 5 6from sqlglot import exp, parse_one 7from sqlglot.helper import ensure_list 8 9from sqlmesh.dbt.common import GeneralConfig 10from sqlmesh.utils.conversions import ensure_bool 11from sqlmesh.utils.pydantic import field_validator 12 13logger = logging.getLogger(__name__) 14 15 16def yaml_to_columns( 17 yaml: t.Dict[str, ColumnConfig] | t.List[t.Dict[str, ColumnConfig]], 18) -> t.Dict[str, ColumnConfig]: 19 columns = {} 20 mappings: t.List[t.Dict[str, ColumnConfig]] = ensure_list(yaml) 21 for column in mappings: 22 config = ColumnConfig(**column) 23 columns[config.name] = config 24 25 return columns 26 27 28def column_types_to_sqlmesh( 29 columns: t.Dict[str, ColumnConfig], dialect: t.Optional[str] = None 30) -> t.Dict[str, exp.DataType]: 31 """ 32 Get the sqlmesh column types 33 34 Returns: 35 A dict of column name to exp.DataType 36 """ 37 col_types_to_sqlmesh: t.Dict[str, exp.DataType] = {} 38 for name, column in columns.items(): 39 if column.enabled and column.data_type: 40 column_def = parse_one( 41 f"{name} {column.data_type}", into=exp.ColumnDef, dialect=dialect or "" 42 ) 43 if column_def.args.get("constraints"): 44 logger.warning( 45 f"Ignoring unsupported constraints for column '{name}' with definition '{column.data_type}'. Please refer to github.com/SQLMesh/sqlmesh/issues/4717 for more information." 46 ) 47 kind = column_def.kind 48 if kind: 49 col_types_to_sqlmesh[name] = kind 50 return col_types_to_sqlmesh 51 52 53def column_descriptions_to_sqlmesh(columns: t.Dict[str, ColumnConfig]) -> t.Dict[str, str]: 54 """ 55 Get the sqlmesh column types 56 57 Returns: 58 A dict of column name to description 59 """ 60 return { 61 name: column.description 62 for name, column in columns.items() 63 if column.enabled and column.description 64 } 65 66 67class ColumnConfig(GeneralConfig): 68 """ 69 Column configuration for a DBT project 70 71 Args: 72 name: Name of the column 73 data_type: The column's data type 74 quote: Boolean flag to use quoting for the column name 75 """ 76 77 name: str 78 data_type: t.Optional[str] = None 79 quote: t.Optional[bool] = False 80 81 @field_validator("quote", mode="before") 82 @classmethod 83 def _validate_bool(cls, v: str) -> bool: 84 return ensure_bool(v)
logger =
<Logger sqlmesh.dbt.column (WARNING)>
def
yaml_to_columns( yaml: Union[Dict[str, ColumnConfig], List[Dict[str, ColumnConfig]]]) -> Dict[str, ColumnConfig]:
17def yaml_to_columns( 18 yaml: t.Dict[str, ColumnConfig] | t.List[t.Dict[str, ColumnConfig]], 19) -> t.Dict[str, ColumnConfig]: 20 columns = {} 21 mappings: t.List[t.Dict[str, ColumnConfig]] = ensure_list(yaml) 22 for column in mappings: 23 config = ColumnConfig(**column) 24 columns[config.name] = config 25 26 return columns
def
column_types_to_sqlmesh( columns: Dict[str, ColumnConfig], dialect: Optional[str] = None) -> Dict[str, sqlglot.expressions.datatypes.DataType]:
29def column_types_to_sqlmesh( 30 columns: t.Dict[str, ColumnConfig], dialect: t.Optional[str] = None 31) -> t.Dict[str, exp.DataType]: 32 """ 33 Get the sqlmesh column types 34 35 Returns: 36 A dict of column name to exp.DataType 37 """ 38 col_types_to_sqlmesh: t.Dict[str, exp.DataType] = {} 39 for name, column in columns.items(): 40 if column.enabled and column.data_type: 41 column_def = parse_one( 42 f"{name} {column.data_type}", into=exp.ColumnDef, dialect=dialect or "" 43 ) 44 if column_def.args.get("constraints"): 45 logger.warning( 46 f"Ignoring unsupported constraints for column '{name}' with definition '{column.data_type}'. Please refer to github.com/SQLMesh/sqlmesh/issues/4717 for more information." 47 ) 48 kind = column_def.kind 49 if kind: 50 col_types_to_sqlmesh[name] = kind 51 return col_types_to_sqlmesh
Get the sqlmesh column types
Returns:
A dict of column name to exp.DataType
54def column_descriptions_to_sqlmesh(columns: t.Dict[str, ColumnConfig]) -> t.Dict[str, str]: 55 """ 56 Get the sqlmesh column types 57 58 Returns: 59 A dict of column name to description 60 """ 61 return { 62 name: column.description 63 for name, column in columns.items() 64 if column.enabled and column.description 65 }
Get the sqlmesh column types
Returns:
A dict of column name to description
68class ColumnConfig(GeneralConfig): 69 """ 70 Column configuration for a DBT project 71 72 Args: 73 name: Name of the column 74 data_type: The column's data type 75 quote: Boolean flag to use quoting for the column name 76 """ 77 78 name: str 79 data_type: t.Optional[str] = None 80 quote: t.Optional[bool] = False 81 82 @field_validator("quote", mode="before") 83 @classmethod 84 def _validate_bool(cls, v: str) -> bool: 85 return ensure_bool(v)
Column configuration for a DBT project
Arguments:
- name: Name of the column
- data_type: The column's data type
- quote: Boolean flag to use quoting for the column name
model_config =
{'json_encoders': {<class 'sqlglot.expressions.core.Expr'>: <function _expression_encoder>, <class 'sqlglot.expressions.datatypes.DataType'>: <function _expression_encoder>, <class 'sqlglot.expressions.query.Tuple'>: <function _expression_encoder>, typing.Union[sqlglot.expressions.query.Query, sqlmesh.core.dialect.JinjaQuery]: <function _expression_encoder>, typing.Union[sqlglot.expressions.query.Query, sqlmesh.core.dialect.JinjaQuery, sqlmesh.core.dialect.MacroFunc]: <function _expression_encoder>, <class 'datetime.tzinfo'>: <function PydanticModel.<lambda>>}, 'arbitrary_types_allowed': True, 'extra': 'allow', 'protected_namespaces': (), 'validate_assignment': True, 'frozen': False}
Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].
Inherited Members
- pydantic.main.BaseModel
- BaseModel
- model_fields
- model_computed_fields
- model_extra
- model_fields_set
- model_construct
- model_copy
- model_dump
- model_dump_json
- model_json_schema
- model_parametrized_name
- model_post_init
- model_rebuild
- model_validate
- model_validate_json
- model_validate_strings
- parse_file
- from_orm
- construct
- schema
- schema_json
- validate
- update_forward_refs