示例#1
0
 def get_partition_by_columns(self, model: HuskyModel):
     return [
         literal_column(
             model.taxon_sql_accessor(self.ctx, model_attribute.taxon))
         for model_attribute in model.attributes_memoized.values()
         if model_attribute.identifier is True
     ]
示例#2
0
    def to_internal(cls, model: FdqModel, virtual_data_source: str,
                    company_id: str) -> HuskyModel:
        """
        Creates HuskyModel from FdqModel

        :return: Correct HuskyModel
        """
        identifiers = set(model.identifiers)

        data = {
            'name':
            prefix_with_virtual_data_source(virtual_data_source,
                                            model.model_name).lower(),
            'data_sources': [virtual_data_source],
            'model_type':
            HuskyModelType.METRIC,
            'fully_qualified_name_parts':
            model.data_source.split('.'),
            'visibility':
            model.visibility,
            'company_id':
            company_id,
            'attributes': {
                attr.taxon: attr
                for api_attr in model.attributes
                for attr in FdqModelAttributeMapper.to_internal(
                    api_attr, virtual_data_source, identifiers)
            },
            'joins': [
                FdqModelJoinMapper.to_internal(join, virtual_data_source)
                for join in model.joins
            ],
        }
        inst = HuskyModel(data)
        return inst
    def _model_add_time_attributes(cls, model: HuskyModel):
        try:
            time_attrs: List[ModelAttribute] = []

            if model.time_granularity is TimeGranularity.day:
                time_attrs = ModelAugments._prepare_derived_time_attributes(
                    TaxonSlugs.DATE, ModelAugments._model_attributes_day)
            elif model.time_granularity is TimeGranularity.hour:
                # Hourly models should always have date_hour attribute. This is correct.
                time_attrs = ModelAugments._prepare_derived_time_attributes(
                    TaxonSlugs.DATE_HOUR,
                    ModelAugments._model_atrributes_hour_from_date_hour_taxon,
                )

            for time_attr in time_attrs:
                model.add_attribute(time_attr)
        except AttributeNotFound:
            pass
 def _model_add_data_source_attributes(cls, model: HuskyModel):
     if (TaxonSlugs.DATA_SOURCE.upper()
             not in model.taxons  # try to find the date_source taxon
             and len(model.data_sources) ==
             1  # make sure that we know the correct data source name
         ):
         # we have one known data source for this model, so let's add it as attribute to the model
         # this way, we can query it
         model.add_attribute(
             ModelAttribute({
                 'column_name':
                 None,
                 'identifier':
                 False,
                 'taxon':
                 TaxonSlugs.DATA_SOURCE,
                 'tel_transformation':
                 f"'{model.data_sources[0]}'",
             }))
    def _model_add_model_info_attributes(cls, model: HuskyModel):
        taxons_map = model.attributes_by_taxon_memoized

        # if the model is company-scoped and no company_id taxon is provided on the model, augment it as a constant
        if TaxonSlugs.COMPANY_ID not in taxons_map:
            model.add_attribute(
                ModelAttribute({
                    'column_name': None,
                    'identifier': False,
                    'taxon': TaxonSlugs.COMPANY_ID,
                    'tel_transformation': f"'{model.company_id}'",
                }))

        # if the model is project-scoped and no project_id taxon is provided on the model, augment it as a constant
        if model.project_id is not None and TaxonSlugs.PROJECT_ID not in taxons_map:
            model.add_attribute(
                ModelAttribute({
                    'column_name': None,
                    'identifier': False,
                    'taxon': TaxonSlugs.PROJECT_ID,
                    'tel_transformation': f"'{model.project_id}'",
                }))
示例#6
0
 def _get_column_accessor_for_taxon_and_model(
         self, model: HuskyModel,
         taxon_slug_expression: TaxonSlugExpression) -> ColumnClause:
     return literal_column(
         model.taxon_sql_accessor(self.ctx, taxon_slug_expression.slug))
from panoramic.cli.husky.core.model.models import HuskyModel
from panoramic.cli.husky.core.sql_alchemy_util import compile_query
from panoramic.cli.husky.core.tel.tel_dialect import ModelTelDialect
from panoramic.cli.husky.service.context import SNOWFLAKE_HUSKY_CONTEXT

_TMP_MODEL = HuskyModel(
    {
        'name': 'test_model',
        'fully_qualified_name_parts': ['database_a', 'company_a', 'table_a'],
        'attributes': {
            'ds_1|ad_id': {'tel_transformation': '"col_ad_id"', 'taxon': 'ds_1|ad_id', 'identifier': True},
            'ds_1|gender': {'tel_transformation': '"col_gender"', 'taxon': 'ds_1|gender', 'identifier': False},
            'ds_1|spend': {'tel_transformation': '"col_spend"', 'taxon': 'ds_1|spend', 'identifier': False},
            'ds_1|start_time': {
                'tel_transformation': '"col_start_time"',
                'taxon': 'ds_1|start_time',
                'identifier': False,
            },
            'ds_1|end_time': {'tel_transformation': '"col_end_time"', 'taxon': 'ds_1|end_time', 'identifier': False},
        },
        'data_sources': ['ds_1'],
        'company_id': 'company-id',
        'visibility': ModelVisibility.available,
    },
    strict=True,
)


# on purpose, some of the inputs do not make sense (e.g. 10 + true)
# this highlights the fact that TEL only validates that it understands the expression
# (there's no guarantee that the final SQL query will run)
示例#8
0
        'visibility':
        api_model.visibility.name,
    }


@pytest.mark.parametrize(
    'husky_model',
    [
        HuskyModel({
            'data_sources': [_VIRTUAL_DATA_SOURCE],
            'fully_qualified_name_parts':
            ['physical', 'db', 'schema', 'table'],
            'model_type':
            'metric',
            'attributes': {},
            'company_id':
            _COMPANY_ID,
            'joins': [],
            'name':
            prefix_with_virtual_data_source(_VIRTUAL_DATA_SOURCE,
                                            'husky_name_slug'),
            'visibility':
            ModelVisibility.hidden,
        }),
        HuskyModel({
            'name':
            prefix_with_virtual_data_source(_VIRTUAL_DATA_SOURCE,
                                            'api-model-slug-2'),
            'data_sources': [_VIRTUAL_DATA_SOURCE],
            'fully_qualified_name_parts': ['physical', 'db', 'table2'],
            'company_id':
            _COMPANY_ID,