2022-02-18 07:48:38 +01:00
|
|
|
# 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.
|
|
|
|
|
|
|
|
|
|
"""
|
|
|
|
|
Population Standard deviation Metric definition
|
|
|
|
|
"""
|
2022-03-16 06:05:59 +01:00
|
|
|
from sqlalchemy import column
|
2022-02-18 07:48:38 +01:00
|
|
|
from sqlalchemy.ext.compiler import compiles
|
|
|
|
|
from sqlalchemy.sql.functions import FunctionElement
|
|
|
|
|
|
|
|
|
|
from metadata.orm_profiler.metrics.core import CACHE, StaticMetric, _label
|
2022-03-22 15:55:44 +01:00
|
|
|
from metadata.orm_profiler.orm.registry import Dialects, is_quantifiable
|
2022-02-18 07:48:38 +01:00
|
|
|
from metadata.orm_profiler.utils import logger
|
|
|
|
|
|
|
|
|
|
logger = logger()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class StdDevFn(FunctionElement):
|
|
|
|
|
name = __qualname__
|
|
|
|
|
inherit_cache = CACHE
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@compiles(StdDevFn)
|
|
|
|
|
def _(element, compiler, **kw):
|
|
|
|
|
return "STDDEV_POP(%s)" % compiler.process(element.clauses, **kw)
|
|
|
|
|
|
|
|
|
|
|
2022-03-22 15:55:44 +01:00
|
|
|
@compiles(StdDevFn, Dialects.MSSQL)
|
2022-02-18 07:48:38 +01:00
|
|
|
def _(element, compiler, **kw):
|
|
|
|
|
return "STDEVP(%s)" % compiler.process(element.clauses, **kw)
|
|
|
|
|
|
|
|
|
|
|
2022-03-22 15:55:44 +01:00
|
|
|
@compiles(StdDevFn, Dialects.SQLite) # Needed for unit tests
|
2022-02-18 07:48:38 +01:00
|
|
|
def _(element, compiler, **kw):
|
|
|
|
|
"""
|
|
|
|
|
This actually returns the squared STD, but as
|
|
|
|
|
it is only required for tests we can live with it.
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
proc = compiler.process(element.clauses, **kw)
|
|
|
|
|
return "AVG(%s * %s) - AVG(%s) * AVG(%s)" % ((proc,) * 4)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class StdDev(StaticMetric):
|
|
|
|
|
"""
|
|
|
|
|
STD Metric
|
|
|
|
|
|
|
|
|
|
Given a column, return the Standard Deviation value.
|
|
|
|
|
"""
|
|
|
|
|
|
2022-02-25 18:26:30 +01:00
|
|
|
@classmethod
|
|
|
|
|
def name(cls):
|
|
|
|
|
return "stddev"
|
|
|
|
|
|
2022-03-08 11:44:39 +01:00
|
|
|
@property
|
2022-02-18 07:48:38 +01:00
|
|
|
def metric_type(self):
|
|
|
|
|
return float
|
|
|
|
|
|
|
|
|
|
@_label
|
|
|
|
|
def fn(self):
|
2022-02-22 08:09:02 +01:00
|
|
|
if is_quantifiable(self.col.type):
|
2022-03-16 06:05:59 +01:00
|
|
|
return StdDevFn(column(self.col.name))
|
2022-02-22 08:09:02 +01:00
|
|
|
|
2022-03-02 16:46:28 +01:00
|
|
|
logger.debug(
|
2022-02-22 08:09:02 +01:00
|
|
|
f"{self.col} has type {self.col.type}, which is not listed as quantifiable."
|
|
|
|
|
+ " We won't compute STDDEV for it."
|
|
|
|
|
)
|
|
|
|
|
return None
|