forked from open-metadata/OpenMetadata
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
open-metadata#13974 handle for hyphen in schema and median function (o…
- Loading branch information
1 parent
ee176b0
commit aaf84bd
Showing
7 changed files
with
149 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
85 changes: 85 additions & 0 deletions
85
ingestion/src/metadata/profiler/interface/sqlalchemy/mariadb/profiler_interface.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
# 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 | ||
""" | ||
|
||
from typing import List | ||
|
||
from sqlalchemy.exc import ProgrammingError | ||
|
||
from metadata.profiler.interface.sqlalchemy.profiler_interface import ( | ||
SQAProfilerInterface, | ||
handle_query_exception, | ||
) | ||
from metadata.profiler.metrics.registry import Metrics | ||
from metadata.profiler.processor.runner import QueryRunner | ||
from metadata.profiler.source.mariadb.metrics.window.first_quartile import ( | ||
MariaDBFirstQuartile, | ||
) | ||
from metadata.profiler.source.mariadb.metrics.window.median import MariaDBMedian | ||
from metadata.profiler.source.mariadb.metrics.window.third_quartile import ( | ||
MariaDBThirdQuartile, | ||
) | ||
from metadata.utils.logger import profiler_interface_registry_logger | ||
|
||
logger = profiler_interface_registry_logger() | ||
|
||
|
||
class MariaDBProfilerInterface(SQAProfilerInterface): | ||
""" | ||
Interface to interact with registry supporting | ||
sqlalchemy. | ||
""" | ||
|
||
def _compute_window_metrics( | ||
self, | ||
metrics: List[Metrics], | ||
runner: QueryRunner, | ||
*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 | ||
""" | ||
session = kwargs.get("session") | ||
column = kwargs.get("column") | ||
|
||
if not metrics: | ||
return None | ||
|
||
try: | ||
# we patch the metrics at runtime to use the MariaDB specific functions | ||
# as we can't compile the query based on the dialect as it return `mysql` | ||
metrics = [MariaDBFirstQuartile, MariaDBMedian, MariaDBThirdQuartile] # type: ignore | ||
row = runner.select_first_from_sample( | ||
*[metric(column).fn() for metric in metrics], | ||
) | ||
if row: | ||
return dict(row) | ||
except ProgrammingError: | ||
logger.info( | ||
f"Skipping window metrics for {runner.table.__tablename__}.{column.name} due to overflow" | ||
) | ||
return None | ||
|
||
except Exception as exc: | ||
msg = f"Error trying to compute profile for {runner.table.__tablename__}.{column.name}: {exc}" | ||
handle_query_exception(msg, exc, session) | ||
return None |
20 changes: 20 additions & 0 deletions
20
ingestion/src/metadata/profiler/source/mariadb/functions/median.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
"""Median function for MariaDB""" | ||
|
||
from sqlalchemy.ext.compiler import compiles | ||
from sqlalchemy.sql.functions import FunctionElement | ||
|
||
from metadata.profiler.metrics.core import CACHE | ||
|
||
|
||
class MariaDBMedianFn(FunctionElement): | ||
inherit_cache = CACHE | ||
|
||
|
||
@compiles(MariaDBMedianFn) | ||
def _(elements, compiler, **kwargs): # pylint: disable=unused-argument | ||
col = compiler.process(elements.clauses.clauses[0]) | ||
percentile = elements.clauses.clauses[2].value | ||
# According to the documentation available at https://mariadb.com/kb/en/median/#description, | ||
# the PERCENTILE_CONT function can be utilized to calculate the median. Therefore, it is | ||
# being used in this context. | ||
return f"PERCENTILE_CONT({percentile:.2f}) WITHIN GROUP (ORDER BY {col}) OVER()" |
10 changes: 10 additions & 0 deletions
10
ingestion/src/metadata/profiler/source/mariadb/metrics/window/first_quartile.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
"""Override first quartile metric definition for MariaDB""" | ||
|
||
from metadata.profiler.metrics.window.first_quartile import FirstQuartile | ||
from metadata.profiler.source.mariadb.functions.median import MariaDBMedianFn | ||
|
||
|
||
class MariaDBFirstQuartile(FirstQuartile): | ||
def _compute_sqa_fn(self, column, table, percentile): | ||
"""Generic method to compute the quartile using sqlalchemy""" | ||
return MariaDBMedianFn(column, table, percentile) |
10 changes: 10 additions & 0 deletions
10
ingestion/src/metadata/profiler/source/mariadb/metrics/window/median.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
"""Override first quartile metric definition for MariaDB""" | ||
|
||
from metadata.profiler.metrics.window.median import Median | ||
from metadata.profiler.source.mariadb.functions.median import MariaDBMedianFn | ||
|
||
|
||
class MariaDBMedian(Median): | ||
def _compute_sqa_fn(self, column, table, percentile): | ||
"""Generic method to compute the quartile using sqlalchemy""" | ||
return MariaDBMedianFn(column, table, percentile) |
10 changes: 10 additions & 0 deletions
10
ingestion/src/metadata/profiler/source/mariadb/metrics/window/third_quartile.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
"""Override first quartile metric definition for MariaDB""" | ||
|
||
from metadata.profiler.metrics.window.third_quartile import ThirdQuartile | ||
from metadata.profiler.source.mariadb.functions.median import MariaDBMedianFn | ||
|
||
|
||
class MariaDBThirdQuartile(ThirdQuartile): | ||
def _compute_sqa_fn(self, column, table, percentile): | ||
"""Generic method to compute the quartile using sqlalchemy""" | ||
return MariaDBMedianFn(column, table, percentile) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters