2022-11-15 20:31:10 +05:30
|
|
|
# Copyright 2021 Collate
|
|
|
|
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
|
# you may not use this file except in compliance with the License.
|
|
|
|
|
# You may obtain a copy of the License at
|
|
|
|
|
# http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
# Unless required by applicable law or agreed to in writing, software
|
|
|
|
|
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
|
# See the License for the specific language governing permissions and
|
|
|
|
|
# limitations under the License.
|
|
|
|
|
|
|
|
|
|
"""
|
|
|
|
|
Interfaces with database for all database engine
|
|
|
|
|
supporting sqlalchemy abstraction layer
|
|
|
|
|
"""
|
|
|
|
|
import traceback
|
|
|
|
|
from collections import defaultdict
|
|
|
|
|
from datetime import datetime, timezone
|
2022-12-14 21:14:51 +05:30
|
|
|
from typing import Dict, List
|
2022-11-15 20:31:10 +05:30
|
|
|
|
|
|
|
|
from sqlalchemy import Column
|
|
|
|
|
|
2023-04-11 20:58:31 +05:30
|
|
|
from metadata.generated.schema.entity.data.table import (
|
|
|
|
|
PartitionProfilerConfig,
|
|
|
|
|
TableData,
|
|
|
|
|
)
|
2023-03-01 08:20:38 +01:00
|
|
|
from metadata.generated.schema.entity.services.connections.database.datalakeConnection import (
|
|
|
|
|
DatalakeConnection,
|
|
|
|
|
)
|
2022-11-15 20:31:10 +05:30
|
|
|
from metadata.ingestion.api.processor import ProfilerProcessorStatus
|
2023-01-02 13:52:27 +01:00
|
|
|
from metadata.ingestion.source.connections import get_connection
|
2023-02-22 09:42:34 +01:00
|
|
|
from metadata.ingestion.source.database.datalake.metadata import (
|
|
|
|
|
DATALAKE_DATA_TYPES,
|
2023-04-11 20:58:31 +05:30
|
|
|
DatalakeSource,
|
2023-02-22 09:42:34 +01:00
|
|
|
)
|
2023-04-04 17:16:44 +02:00
|
|
|
from metadata.mixins.pandas.pandas_mixin import PandasInterfaceMixin
|
|
|
|
|
from metadata.profiler.interface.profiler_protocol import ProfilerProtocol
|
2023-03-01 08:20:38 +01:00
|
|
|
from metadata.profiler.metrics.core import MetricTypes
|
|
|
|
|
from metadata.profiler.metrics.registry import Metrics
|
2023-04-04 17:16:44 +02:00
|
|
|
from metadata.profiler.processor.datalake_sampler import DatalakeSampler
|
2022-12-07 14:33:30 +01:00
|
|
|
from metadata.utils.dispatch import valuedispatch
|
2022-11-15 20:31:10 +05:30
|
|
|
from metadata.utils.logger import profiler_interface_registry_logger
|
2023-03-01 08:20:38 +01:00
|
|
|
from metadata.utils.sqa_like_column import SQALikeColumn, Type
|
2022-11-15 20:31:10 +05:30
|
|
|
|
|
|
|
|
logger = profiler_interface_registry_logger()
|
|
|
|
|
|
|
|
|
|
|
2023-03-01 08:20:38 +01:00
|
|
|
class PandasProfilerInterface(ProfilerProtocol, PandasInterfaceMixin):
|
2022-11-15 20:31:10 +05:30
|
|
|
"""
|
|
|
|
|
Interface to interact with registry supporting
|
|
|
|
|
sqlalchemy.
|
|
|
|
|
"""
|
|
|
|
|
|
2023-03-01 08:20:38 +01:00
|
|
|
_profiler_type: str = DatalakeConnection.__name__
|
|
|
|
|
|
|
|
|
|
def __init__(
|
|
|
|
|
self,
|
|
|
|
|
service_connection_config,
|
|
|
|
|
ometa_client,
|
|
|
|
|
thread_count,
|
|
|
|
|
entity,
|
|
|
|
|
profile_sample_config,
|
2023-03-03 18:33:18 +05:30
|
|
|
source_config,
|
2023-03-01 08:20:38 +01:00
|
|
|
sample_query,
|
|
|
|
|
table_partition_config=None,
|
|
|
|
|
**kwargs,
|
|
|
|
|
):
|
2022-11-15 20:31:10 +05:30
|
|
|
"""Instantiate SQA Interface object"""
|
2023-03-01 08:20:38 +01:00
|
|
|
self._thread_count = thread_count
|
|
|
|
|
self.table_entity = entity
|
|
|
|
|
self.ometa_client = ometa_client
|
2023-03-03 18:33:18 +05:30
|
|
|
self.source_config = source_config
|
2023-03-01 08:20:38 +01:00
|
|
|
self.service_connection_config = service_connection_config
|
2023-04-11 20:58:31 +05:30
|
|
|
self.client = self.get_connection_client()
|
2022-11-15 20:31:10 +05:30
|
|
|
self.processor_status = ProfilerProcessorStatus()
|
|
|
|
|
self.processor_status.entity = (
|
|
|
|
|
self.table_entity.fullyQualifiedName.__root__
|
|
|
|
|
if self.table_entity.fullyQualifiedName
|
|
|
|
|
else None
|
2023-03-24 17:59:06 +01:00
|
|
|
)
|
2023-03-01 08:20:38 +01:00
|
|
|
self.profile_sample_config = profile_sample_config
|
|
|
|
|
self.profile_query = sample_query
|
2023-04-11 20:58:31 +05:30
|
|
|
self.table_partition_config: PartitionProfilerConfig = table_partition_config
|
2023-03-01 08:20:38 +01:00
|
|
|
self._table = entity
|
2023-04-11 20:58:31 +05:30
|
|
|
self.dfs = self.return_ometa_dataframes_sampled(
|
|
|
|
|
service_connection_config=self.service_connection_config,
|
2022-12-14 21:14:51 +05:30
|
|
|
client=self.client,
|
|
|
|
|
table=self.table,
|
2023-04-11 20:58:31 +05:30
|
|
|
profile_sample_config=self.profile_sample_config,
|
2022-11-15 20:31:10 +05:30
|
|
|
)
|
2023-03-01 08:20:38 +01:00
|
|
|
if self.dfs and self.table_partition_config:
|
2023-04-11 20:58:31 +05:30
|
|
|
self.dfs = self.get_partitioned_df(self.dfs)
|
2022-11-15 20:31:10 +05:30
|
|
|
|
2022-12-07 14:33:30 +01:00
|
|
|
@valuedispatch
|
|
|
|
|
def _get_metrics(self, *args, **kwargs):
|
|
|
|
|
"""Generic getter method for metrics. To be used with
|
|
|
|
|
specific dispatch methods
|
|
|
|
|
"""
|
|
|
|
|
logger.warning("Could not get metric. No function registered.")
|
|
|
|
|
|
|
|
|
|
# pylint: disable=unused-argument
|
|
|
|
|
@_get_metrics.register(MetricTypes.Table.value)
|
|
|
|
|
def _(
|
|
|
|
|
self,
|
|
|
|
|
metric_type: str,
|
|
|
|
|
metrics: List[Metrics],
|
|
|
|
|
*args,
|
|
|
|
|
**kwargs,
|
|
|
|
|
):
|
|
|
|
|
"""Given a list of metrics, compute the given results
|
|
|
|
|
and returns the values
|
|
|
|
|
|
|
|
|
|
Args:
|
|
|
|
|
metrics: list of metrics to compute
|
|
|
|
|
Returns:
|
|
|
|
|
dictionnary of results
|
|
|
|
|
"""
|
|
|
|
|
import pandas as pd # pylint: disable=import-outside-toplevel
|
|
|
|
|
|
|
|
|
|
try:
|
2023-04-11 20:58:31 +05:30
|
|
|
row_dict = {}
|
|
|
|
|
df_list = [df.where(pd.notnull(df), None) for df in self.dfs]
|
2022-12-07 14:33:30 +01:00
|
|
|
for metric in metrics:
|
2023-04-11 20:58:31 +05:30
|
|
|
row_dict[metric.name()] = metric().df_fn(df_list)
|
|
|
|
|
return row_dict
|
2022-12-07 14:33:30 +01:00
|
|
|
except Exception as exc:
|
|
|
|
|
logger.debug(traceback.format_exc())
|
|
|
|
|
logger.warning(f"Error trying to compute profile for {exc}")
|
|
|
|
|
raise RuntimeError(exc)
|
|
|
|
|
|
|
|
|
|
# pylint: disable=unused-argument
|
|
|
|
|
@_get_metrics.register(MetricTypes.Static.value)
|
|
|
|
|
def _(
|
|
|
|
|
self,
|
|
|
|
|
metric_type: str,
|
|
|
|
|
metrics: List[Metrics],
|
|
|
|
|
column,
|
|
|
|
|
*args,
|
|
|
|
|
**kwargs,
|
|
|
|
|
):
|
|
|
|
|
"""Given a list of metrics, compute the given results
|
|
|
|
|
and returns the values
|
|
|
|
|
|
|
|
|
|
Args:
|
|
|
|
|
column: the column to compute the metrics against
|
|
|
|
|
metrics: list of metrics to compute
|
|
|
|
|
Returns:
|
|
|
|
|
dictionnary of results
|
|
|
|
|
"""
|
|
|
|
|
import pandas as pd # pylint: disable=import-outside-toplevel
|
|
|
|
|
|
|
|
|
|
try:
|
|
|
|
|
row_dict = {}
|
2023-04-11 20:58:31 +05:30
|
|
|
for metric in metrics:
|
|
|
|
|
metric_resp = metric(column).df_fn(self.dfs)
|
|
|
|
|
row_dict[metric.name()] = (
|
|
|
|
|
None if pd.isnull(metric_resp) else metric_resp
|
|
|
|
|
)
|
2022-12-07 14:33:30 +01:00
|
|
|
return row_dict
|
|
|
|
|
except Exception as exc:
|
|
|
|
|
logger.debug(
|
|
|
|
|
f"{traceback.format_exc()}\nError trying to compute profile for {exc}"
|
|
|
|
|
)
|
|
|
|
|
raise RuntimeError(exc)
|
|
|
|
|
|
|
|
|
|
# pylint: disable=unused-argument
|
|
|
|
|
@_get_metrics.register(MetricTypes.Query.value)
|
|
|
|
|
def _(
|
|
|
|
|
self,
|
|
|
|
|
metric_type: str,
|
|
|
|
|
metrics: Metrics,
|
|
|
|
|
column,
|
|
|
|
|
*args,
|
|
|
|
|
**kwargs,
|
|
|
|
|
):
|
|
|
|
|
"""Given a list of metrics, compute the given results
|
|
|
|
|
and returns the values
|
|
|
|
|
|
|
|
|
|
Args:
|
|
|
|
|
column: the column to compute the metrics against
|
|
|
|
|
metrics: list of metrics to compute
|
|
|
|
|
Returns:
|
|
|
|
|
dictionnary of results
|
|
|
|
|
"""
|
|
|
|
|
col_metric = None
|
2023-04-11 20:58:31 +05:30
|
|
|
col_metric = metrics(column).df_fn(self.dfs)
|
2022-12-07 14:33:30 +01:00
|
|
|
if not col_metric:
|
|
|
|
|
return None
|
|
|
|
|
return {metrics.name(): col_metric}
|
|
|
|
|
|
|
|
|
|
# pylint: disable=unused-argument
|
|
|
|
|
@_get_metrics.register(MetricTypes.Window.value)
|
|
|
|
|
def _(
|
|
|
|
|
self,
|
2023-03-03 21:56:32 +01:00
|
|
|
metric_type: str,
|
|
|
|
|
metrics: Metrics,
|
|
|
|
|
column,
|
2022-12-07 14:33:30 +01:00
|
|
|
*args,
|
|
|
|
|
**kwargs,
|
|
|
|
|
):
|
|
|
|
|
"""
|
|
|
|
|
Given a list of metrics, compute the given results
|
|
|
|
|
and returns the values
|
|
|
|
|
"""
|
2023-03-03 21:56:32 +01:00
|
|
|
try:
|
|
|
|
|
metric_values = {}
|
|
|
|
|
for metric in metrics:
|
|
|
|
|
metric_values[metric.name()] = metric(column).df_fn(self.dfs)
|
|
|
|
|
return metric_values if metric_values else None
|
|
|
|
|
except Exception as exc:
|
|
|
|
|
logger.debug(traceback.format_exc())
|
|
|
|
|
logger.warning(f"Unexpected exception computing metrics: {exc}")
|
|
|
|
|
return None
|
2022-12-07 14:33:30 +01:00
|
|
|
|
|
|
|
|
@_get_metrics.register(MetricTypes.System.value)
|
|
|
|
|
def _(
|
|
|
|
|
self,
|
|
|
|
|
*args,
|
|
|
|
|
**kwargs,
|
|
|
|
|
):
|
|
|
|
|
"""
|
|
|
|
|
Given a list of metrics, compute the given results
|
|
|
|
|
and returns the values
|
|
|
|
|
"""
|
|
|
|
|
return None # to be implemented
|
|
|
|
|
|
2022-11-15 20:31:10 +05:30
|
|
|
def compute_metrics(
|
|
|
|
|
self,
|
2022-12-07 14:33:30 +01:00
|
|
|
metrics,
|
|
|
|
|
metric_type,
|
|
|
|
|
column,
|
|
|
|
|
table,
|
2022-11-15 20:31:10 +05:30
|
|
|
):
|
|
|
|
|
"""Run metrics in processor worker"""
|
|
|
|
|
logger.debug(f"Running profiler for {table}")
|
|
|
|
|
try:
|
2023-04-11 20:58:31 +05:30
|
|
|
row = None
|
|
|
|
|
if self.dfs:
|
|
|
|
|
row = self._get_metrics(
|
|
|
|
|
metric_type.value,
|
|
|
|
|
metrics,
|
|
|
|
|
session=self.client,
|
|
|
|
|
column=column,
|
|
|
|
|
)
|
2022-12-07 14:33:30 +01:00
|
|
|
except Exception as exc:
|
2023-03-24 17:59:06 +01:00
|
|
|
name = f"{column if column is not None else table}"
|
|
|
|
|
error = f"{name} metric_type.value: {exc}"
|
|
|
|
|
logger.error(error)
|
|
|
|
|
self.processor_status.failed_profiler(error, traceback.format_exc())
|
2022-11-15 20:31:10 +05:30
|
|
|
row = None
|
|
|
|
|
if column:
|
|
|
|
|
column = column.name
|
2022-12-07 14:33:30 +01:00
|
|
|
return row, column, metric_type.value
|
2022-11-15 20:31:10 +05:30
|
|
|
|
|
|
|
|
def fetch_sample_data(self, table) -> TableData:
|
|
|
|
|
"""Fetch sample data from database
|
|
|
|
|
|
|
|
|
|
Args:
|
|
|
|
|
table: ORM declarative table
|
|
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
|
TableData: sample table data
|
|
|
|
|
"""
|
|
|
|
|
sampler = DatalakeSampler(
|
|
|
|
|
session=self.client,
|
2023-03-01 08:20:38 +01:00
|
|
|
table=self.dfs,
|
2023-01-16 22:17:46 +05:30
|
|
|
profile_sample_config=self.profile_sample_config,
|
2022-11-15 20:31:10 +05:30
|
|
|
profile_sample_query=self.profile_query,
|
|
|
|
|
)
|
|
|
|
|
return sampler.fetch_dl_sample_data()
|
|
|
|
|
|
|
|
|
|
def get_composed_metrics(
|
|
|
|
|
self, column: Column, metric: Metrics, column_results: Dict
|
|
|
|
|
):
|
|
|
|
|
"""Given a list of metrics, compute the given results
|
|
|
|
|
and returns the values
|
|
|
|
|
|
|
|
|
|
Args:
|
|
|
|
|
column: the column to compute the metrics against
|
2022-11-17 10:11:54 +01:00
|
|
|
metric: list of metrics to compute
|
|
|
|
|
column_results: computed values for the column
|
2022-11-15 20:31:10 +05:30
|
|
|
Returns:
|
|
|
|
|
dictionary of results
|
|
|
|
|
"""
|
|
|
|
|
try:
|
|
|
|
|
return metric(column).fn(column_results)
|
|
|
|
|
except Exception as exc:
|
|
|
|
|
logger.debug(traceback.format_exc())
|
|
|
|
|
logger.warning(f"Unexpected exception computing metrics: {exc}")
|
|
|
|
|
return None
|
|
|
|
|
|
2023-03-03 21:56:32 +01:00
|
|
|
def get_hybrid_metrics(
|
|
|
|
|
self, column: Column, metric: Metrics, column_results: Dict, **kwargs
|
|
|
|
|
):
|
|
|
|
|
"""Given a list of metrics, compute the given results
|
|
|
|
|
and returns the values
|
|
|
|
|
|
|
|
|
|
Args:
|
|
|
|
|
column: the column to compute the metrics against
|
|
|
|
|
metric: list of metrics to compute
|
|
|
|
|
column_results: computed values for the column
|
|
|
|
|
Returns:
|
|
|
|
|
dictionary of results
|
|
|
|
|
"""
|
|
|
|
|
try:
|
|
|
|
|
return metric(column).df_fn(column_results, self.dfs)
|
|
|
|
|
except Exception as exc:
|
|
|
|
|
logger.debug(traceback.format_exc())
|
|
|
|
|
logger.warning(f"Unexpected exception computing metrics: {exc}")
|
|
|
|
|
return None
|
|
|
|
|
|
2022-11-15 20:31:10 +05:30
|
|
|
def get_all_metrics(
|
|
|
|
|
self,
|
|
|
|
|
metric_funcs: list,
|
|
|
|
|
):
|
|
|
|
|
"""get all profiler metrics"""
|
|
|
|
|
|
|
|
|
|
profile_results = {"table": {}, "columns": defaultdict(dict)}
|
|
|
|
|
metric_list = [
|
2022-12-07 14:33:30 +01:00
|
|
|
self.compute_metrics(*metric_func) for metric_func in metric_funcs
|
2022-11-15 20:31:10 +05:30
|
|
|
]
|
|
|
|
|
for metric_result in metric_list:
|
2022-12-07 14:33:30 +01:00
|
|
|
profile, column, metric_type = metric_result
|
2023-04-11 20:58:31 +05:30
|
|
|
if profile:
|
|
|
|
|
if metric_type == MetricTypes.Table.value:
|
|
|
|
|
profile_results["table"].update(profile)
|
|
|
|
|
if metric_type == MetricTypes.System.value:
|
|
|
|
|
profile_results["system"] = profile
|
|
|
|
|
else:
|
|
|
|
|
if profile:
|
|
|
|
|
profile_results["columns"][column].update(
|
|
|
|
|
{
|
|
|
|
|
"name": column,
|
|
|
|
|
"timestamp": datetime.now(tz=timezone.utc).timestamp(),
|
|
|
|
|
**profile,
|
|
|
|
|
}
|
|
|
|
|
)
|
2022-11-15 20:31:10 +05:30
|
|
|
return profile_results
|
|
|
|
|
|
|
|
|
|
@property
|
|
|
|
|
def table(self):
|
|
|
|
|
"""OM Table entity"""
|
|
|
|
|
return self._table
|
|
|
|
|
|
|
|
|
|
def get_columns(self):
|
2023-03-01 08:20:38 +01:00
|
|
|
if self.dfs:
|
|
|
|
|
df = self.dfs[0]
|
2023-02-22 09:42:34 +01:00
|
|
|
return [
|
|
|
|
|
SQALikeColumn(
|
|
|
|
|
column_name,
|
2023-04-11 20:58:31 +05:30
|
|
|
Type(DatalakeSource.fetch_col_types(df, column_name)),
|
2023-02-22 09:42:34 +01:00
|
|
|
)
|
|
|
|
|
for column_name in df.columns
|
|
|
|
|
]
|
|
|
|
|
return []
|
2022-11-15 20:31:10 +05:30
|
|
|
|
2023-04-11 20:58:31 +05:30
|
|
|
def get_connection_client(self):
|
|
|
|
|
return get_connection(self.service_connection_config).client
|
|
|
|
|
|
2022-11-15 20:31:10 +05:30
|
|
|
def close(self):
|
2023-03-01 08:20:38 +01:00
|
|
|
"""Nothing to close with pandas"""
|
2022-11-15 20:31:10 +05:30
|
|
|
pass
|