Skip to content

Checks

Module containing data quality check classes.

ColumnTransformationCheck

Bases: DataQualityCheck, ABC

Abstract class for data quality checks performing checks on a specific column of a table.

Parameters:

Name Type Description Default
transformation_name str

The name to refer to this check (in combination with check_column)

required
table str

Name of BQ table (e.g., "project.dataset.table")

required
check_column Optional[str]

Name of column to be checked (e.g., "category")

None
lower_threshold float

Check will fail if check result < lower_threshold

-inf
upper_threshold float

Check will fail if check result > upper_threshold

inf
monitor_only bool

If True, no checks will be performed

False
extra_info Optional[str]

Optional additional text that will be added to the end of the failure message

None
Source code in src/koality/checks.py
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
class ColumnTransformationCheck(DataQualityCheck, abc.ABC):
    """
    Abstract class for data quality checks performing checks on a specific
    column of a table.

    Args:
        transformation_name: The name to refer to this check (in combination with check_column)
        table: Name of BQ table (e.g., "project.dataset.table")
        check_column: Name of column to be checked (e.g., "category")
        lower_threshold: Check will fail if check result < lower_threshold
        upper_threshold: Check will fail if check result > upper_threshold
        monitor_only: If True, no checks will be performed
        extra_info: Optional additional text that will be added to the end of the failure message
    """

    def __init__(
        self,
        database_accessor: str,
        database_provider: DatabaseProvider | None,
        transformation_name: str,
        table: str,
        check_column: Optional[str] = None,
        lower_threshold: float = -math.inf,
        upper_threshold: float = math.inf,
        monitor_only: bool = False,
        extra_info: Optional[str] = None,
        **kwargs,
    ):
        self.transformation_name = transformation_name

        super().__init__(
            database_accessor=database_accessor,
            database_provider=database_provider,
            table=table,
            check_column=check_column,
            lower_threshold=lower_threshold,
            upper_threshold=upper_threshold,
            monitor_only=monitor_only,
            extra_info=extra_info,
            **kwargs,
        )

    def assemble_name(self):
        return f"{self.check_column.split('.')[-1]}" + "_" + f"{self.transformation_name}"

    @abc.abstractmethod
    def transformation_statement(self) -> str:
        pass

    def query_boilerplate(self, metric_statement: str) -> str:
        return f"""
        SELECT
            {metric_statement}
        FROM
            {self.table}
        """

    def assemble_query(self) -> str:
        main_query = self.query_boilerplate(self.transformation_statement())

        if where_statement := self.assemble_where_statement(self.filters):
            return main_query + "\n" + where_statement

        return main_query

    def assemble_data_exists_query(self) -> str:
        data_exists_query = f"""
        SELECT
            IF(COUNT(*) > 0, '', '{self.table}') AS empty_table
        FROM
            {self.table}
        """

        if where_statement := self.assemble_where_statement(self.filters):
            return f"{data_exists_query}\n{where_statement}"

        return data_exists_query

CountCheck

Bases: ColumnTransformationCheck

Checks the number of rows or distinct values of a specific column. It inherits from koality.checks.ColumnTransformationCheck, and thus, we refer to argument descriptions in its super class, except for the distinct argument which is added in this subclass.

Parameters:

Name Type Description Default
distinct bool

Indicates if the count should count all rows or only distinct values of a specific column. Note: distinct=True cannot be used with check_column="*".

False

Example:

CountCheck( database_accessor="my-gcp-project.SHOP01", database_provider=None, table="my-gcp-project.SHOP01.skufeed_latest", check_column="sku_id", distinct=True, shop_id_filter_column="shop_code", # optional shop_id_filter_value="SHOP01", # optional date_filter_column="DATE", # optional date_filter_value="2023-01-01", # optional lower_threshold=10000.0, upper_threshold=99999.0, )

Source code in src/koality/checks.py
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
class CountCheck(ColumnTransformationCheck):
    """
    Checks the number of rows or distinct values of a specific column. It inherits from
    `koality.checks.ColumnTransformationCheck`, and thus, we refer to argument
    descriptions in its super class, except for the `distinct` argument which is added in
    this subclass.

    Args:
        distinct: Indicates if the count should count all rows or only distinct values
                  of a specific column.
                  Note: distinct=True cannot be used with check_column="*".

    Example:

    CountCheck(
        database_accessor="my-gcp-project.SHOP01",
        database_provider=None,
        table="my-gcp-project.SHOP01.skufeed_latest",
        check_column="sku_id",
        distinct=True,
        shop_id_filter_column="shop_code",  # optional
        shop_id_filter_value="SHOP01",  # optional
        date_filter_column="DATE",  # optional
        date_filter_value="2023-01-01",  # optional
        lower_threshold=10000.0,
        upper_threshold=99999.0,
    )
    """

    def __init__(
        self,
        database_accessor: str,
        database_provider: DatabaseProvider | None,
        table: str,
        check_column: str,
        distinct: bool = False,
        lower_threshold: float = -math.inf,
        upper_threshold: float = math.inf,
        monitor_only: bool = False,
        extra_info: Optional[str] = None,
        **kwargs,
    ):
        if check_column == "*" and distinct:
            raise KoalityError("Cannot COUNT(DISTINCT *)! Either set check_column != '*' or distinct = False.")

        self.distinct = distinct

        super().__init__(
            database_accessor=database_accessor,
            database_provider=database_provider,
            transformation_name="distinct_count" if distinct else "count",
            table=table,
            check_column=check_column,
            lower_threshold=lower_threshold,
            upper_threshold=upper_threshold,
            monitor_only=monitor_only,
            extra_info=extra_info,
            **kwargs,
        )

    def transformation_statement(self) -> str:
        if self.distinct:
            return f"COUNT(DISTINCT {self.check_column}) AS {self.name}"

        return f"COUNT({self.check_column}) AS {self.name}"

    def assemble_name(self):
        if self.check_column == "*":
            return f"row_{self.transformation_name}"

        return super().assemble_name()

DataQualityCheck

Bases: ABC

Abstract class for all data quality checks. It provides generic methods relevant to all data quality check classes.

Parameters:

Name Type Description Default
table str

Name of BQ table (e.g., "project.dataset.table")

required
check_column str | None

Name of column to be checked (e.g., "category")

None
lower_threshold float

Check will fail if check result < lower_threshold

-inf
upper_threshold float

Check will fail if check result > upper_threshold

inf
monitor_only bool

If True, no checks will be performed

False
extra_info str | None

Optional additional text that will be added to the end of the failure message

None
Source code in src/koality/checks.py
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
class DataQualityCheck(abc.ABC):
    """
    Abstract class for all data quality checks. It provides generic methods
    relevant to all data quality check classes.

    Args:
        table: Name of BQ table (e.g., "project.dataset.table")
        check_column: Name of column to be checked (e.g., "category")
        lower_threshold: Check will fail if check result < lower_threshold
        upper_threshold: Check will fail if check result > upper_threshold
        monitor_only: If True, no checks will be performed
        extra_info: Optional additional text that will be added to the end of the failure message
    """

    def __init__(
        self,
        database_accessor: str,
        database_provider: DatabaseProvider | None,
        table: str,
        check_column: str | None = None,
        lower_threshold: float = -math.inf,
        upper_threshold: float = math.inf,
        monitor_only: bool = False,
        extra_info: str | None = None,
        **kwargs,
    ):
        self.database_accessor = database_accessor
        self.database_provider = database_provider
        self.table = table
        self.lower_threshold = lower_threshold
        self.upper_threshold = upper_threshold
        self.monitor_only = monitor_only
        self.extra_info_string = f" {extra_info}" if extra_info else ""
        self.date_info_string = f" ({kwargs['date_info']})" if "date_info" in kwargs else ""

        self.status = "NOT_EXECUTED"
        self.message: str | None = None
        self.bytes_billed: int = 0

        # for where filter handling
        self.filters = self.get_filters(kwargs)

        self.shop_id = self.filters.get("shop_id", {}).get("value", "ALL_SHOPS")
        self.date_filter_value = self.filters.get("date", {}).get("value", dt.date.today().isoformat())

        if check_column is None:
            self.check_column = "*"
        else:
            self.check_column = check_column

        self.name = self.assemble_name()
        self.result: Optional[dict[str, Any]] = None

    @property
    def query(self):
        return self.assemble_query()

    @abc.abstractmethod
    def assemble_query(self) -> str:
        pass

    @abc.abstractmethod
    def assemble_data_exists_query(self) -> str:
        pass

    @abc.abstractmethod
    def assemble_name(self) -> str:
        pass

    def __repr__(self) -> str:
        if not hasattr(self, "shop_id"):
            return self.name

        return self.shop_id + "_" + self.name

    def data_check(self, duckdb_client: duckdb.DuckDBPyConnection) -> dict:
        """
        Performs a check if database tables used in the actual check
        contain data.

        Note: The returned result dict and failure message will be later
        aggregated in order to avoid duplicates in the reported failures.

        Args:
            duckdb_client: DuckDB client for interacting with DuckDB

        Returns:
            If there is a table without data, a dict containing information about
            missing data will be returned, otherwise an empty dict indicating that
            data exists.
        """
        is_empty_table = False
        try:
            result = execute_query(
                self.assemble_data_exists_query(),
                duckdb_client,
                self.database_provider,
            ).fetchone()
        except duckdb.Error:
            empty_table = f"Error while executing data check query on {self.table}"
        else:
            empty_table = result[0] if result else self.table
            is_empty_table = bool(empty_table)

        if not is_empty_table:
            return {}

        date = self.date_filter_value if hasattr(self, "date_filter_value") else dt.datetime.today().isoformat()
        self.message = f"No data in {empty_table} on {date} for: {self.shop_id}"
        self.status = "FAIL"
        return {
            "DATE": date,
            "METRIC_NAME": "data_exists",
            "SHOP_ID": self.shop_id,
            "TABLE": empty_table,
        }

    def _check(self, duckdb_client: duckdb.DuckDBPyConnection, query: str) -> tuple[list[dict], str | None]:
        data = []
        error = None
        try:
            result = execute_query(
                query,
                duckdb_client,
                self.database_provider,
            )
        except duckdb.Error as e:
            error = str(e)
        else:
            data = [dict(zip(result.columns, row, strict=False)) for row in result.fetchall()]
        return data, error

    def check(self, duckdb_client: duckdb.DuckDBPyConnection) -> dict:
        """
        Method that is actually performing the check of a data quality check
        object. If the check is set to `monitor_only`, the results of the
        check will be documented without comparison to the lower and
        upper thresholds.

        Args:
            duckdb_client: DuckDB client for interacting with DuckDB

        Returns:
            A dict containing all information and the result of the check
        """

        result, error = self._check(duckdb_client, self.query)

        check_value = result[0][self.name] if result else None
        check_value = float(check_value) if check_value is not None else None
        if error:
            result = "ERROR"
            self.message = f"{self.shop_id}: Metric {self.name} query errored with {error}"
        else:
            if self.monitor_only:
                result = "MONITOR_ONLY"
            else:
                success = check_value is not None and self.lower_threshold <= check_value <= self.upper_threshold
                result = "SUCCESS" if success else "FAIL"

        date = self.date_filter_value
        result_dict = {
            "DATE": date,
            "METRIC_NAME": self.name,
            "SHOP_ID": self.shop_id,
            "TABLE": self.table,
            "COLUMN": self.check_column,
            "VALUE": check_value,
            "LOWER_THRESHOLD": self.lower_threshold,
            "UPPER_THRESHOLD": self.upper_threshold,
            "RESULT": result,
        }

        if result_dict["RESULT"] == "FAIL":
            value_string = f"{result_dict['VALUE']:.{FLOAT_PRECISION}f}" if result_dict["VALUE"] is not None else "NULL"
            self.message = (
                f"{self.shop_id}: Metric {self.name} failed on {self.date_filter_value}{self.date_info_string} "
                f"for {self.table}. Value {value_string} is not between {self.lower_threshold} and "
                f"{self.upper_threshold}.{self.extra_info_string}"
            )
        self.status = result_dict["RESULT"]
        self.result = result_dict

        return result_dict

    def __call__(self, duckdb_client: duckdb.DuckDBPyConnection) -> dict:
        data_check_result = self.data_check(duckdb_client)
        if data_check_result:
            return data_check_result

        return self.check(duckdb_client)

    @staticmethod
    def get_filters(
        kwargs: dict,
        filter_col_suffix: str = r"_filter_column",
        filter_value_suffix: str = "_filter_value",
    ):
        """
        Generates a filter dict from kwargs using a regex pattern.
        Returns a dict of the format
            {"date": {"column": "date", "value": "2020-01-01"}, ...}
        """

        filters = {}

        # first, get all filter cols that are marked with filter_col_suffix,
        # e.g. shop_filter_column
        for key, value in kwargs.items():
            if match := re.fullmatch(r"(\w+)" + filter_col_suffix, key):
                filters[match[1]] = {"column": value}  # match[1] = "(\w+)" from above

        # next, find values for the filters ()
        for filter_key, filter_val in filters.items():
            for key, value in kwargs.items():
                if re.fullmatch(f"{filter_key}{filter_value_suffix}", key):
                    if filter_key == "date":
                        filter_val["value"] = parse_date(value, offset_days=kwargs.get("date_offset", 0))
                    else:
                        filter_val["value"] = value

                    break  # no need to loop any further

            else:  # no break
                raise ValueError(f"{filter_key}_filter_column has no corresponding value!")

        return filters

    @staticmethod
    def assemble_where_statement(filters: dict) -> str:
        """
        Generates the where statement for the check query using the specified
        filters.

        Args:
            filters: A dict containing filter specifications, e.g.,
                `{
                    'shop_id': {
                        'column': 'shop_code',
                        'value': 'SHOP01'
                    },
                    'date': {
                        'column': 'date',
                        'value': '2023-01-01'
                    }
                }`

        Returns:
            A WHERE statement to restrict the data being used for the check, e.g.,
            'WHERE shop_code = "SHOP01" AND date = "2023-01-01"'
        """

        if len(filters) == 0:
            return ""

        filters_statements = [
            4 * " " + f"{filter_dict['column']} = '{filter_dict['value']}'" for _, filter_dict in filters.items()
        ]

        return "WHERE\n" + "\nAND\n".join(filters_statements)

assemble_where_statement(filters) staticmethod

Generates the where statement for the check query using the specified filters.

Parameters:

Name Type Description Default
filters dict

A dict containing filter specifications, e.g., { 'shop_id': { 'column': 'shop_code', 'value': 'SHOP01' }, 'date': { 'column': 'date', 'value': '2023-01-01' } }

required

Returns:

Type Description
str

A WHERE statement to restrict the data being used for the check, e.g.,

str

'WHERE shop_code = "SHOP01" AND date = "2023-01-01"'

Source code in src/koality/checks.py
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
@staticmethod
def assemble_where_statement(filters: dict) -> str:
    """
    Generates the where statement for the check query using the specified
    filters.

    Args:
        filters: A dict containing filter specifications, e.g.,
            `{
                'shop_id': {
                    'column': 'shop_code',
                    'value': 'SHOP01'
                },
                'date': {
                    'column': 'date',
                    'value': '2023-01-01'
                }
            }`

    Returns:
        A WHERE statement to restrict the data being used for the check, e.g.,
        'WHERE shop_code = "SHOP01" AND date = "2023-01-01"'
    """

    if len(filters) == 0:
        return ""

    filters_statements = [
        4 * " " + f"{filter_dict['column']} = '{filter_dict['value']}'" for _, filter_dict in filters.items()
    ]

    return "WHERE\n" + "\nAND\n".join(filters_statements)

check(duckdb_client)

Method that is actually performing the check of a data quality check object. If the check is set to monitor_only, the results of the check will be documented without comparison to the lower and upper thresholds.

Parameters:

Name Type Description Default
duckdb_client DuckDBPyConnection

DuckDB client for interacting with DuckDB

required

Returns:

Type Description
dict

A dict containing all information and the result of the check

Source code in src/koality/checks.py
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
def check(self, duckdb_client: duckdb.DuckDBPyConnection) -> dict:
    """
    Method that is actually performing the check of a data quality check
    object. If the check is set to `monitor_only`, the results of the
    check will be documented without comparison to the lower and
    upper thresholds.

    Args:
        duckdb_client: DuckDB client for interacting with DuckDB

    Returns:
        A dict containing all information and the result of the check
    """

    result, error = self._check(duckdb_client, self.query)

    check_value = result[0][self.name] if result else None
    check_value = float(check_value) if check_value is not None else None
    if error:
        result = "ERROR"
        self.message = f"{self.shop_id}: Metric {self.name} query errored with {error}"
    else:
        if self.monitor_only:
            result = "MONITOR_ONLY"
        else:
            success = check_value is not None and self.lower_threshold <= check_value <= self.upper_threshold
            result = "SUCCESS" if success else "FAIL"

    date = self.date_filter_value
    result_dict = {
        "DATE": date,
        "METRIC_NAME": self.name,
        "SHOP_ID": self.shop_id,
        "TABLE": self.table,
        "COLUMN": self.check_column,
        "VALUE": check_value,
        "LOWER_THRESHOLD": self.lower_threshold,
        "UPPER_THRESHOLD": self.upper_threshold,
        "RESULT": result,
    }

    if result_dict["RESULT"] == "FAIL":
        value_string = f"{result_dict['VALUE']:.{FLOAT_PRECISION}f}" if result_dict["VALUE"] is not None else "NULL"
        self.message = (
            f"{self.shop_id}: Metric {self.name} failed on {self.date_filter_value}{self.date_info_string} "
            f"for {self.table}. Value {value_string} is not between {self.lower_threshold} and "
            f"{self.upper_threshold}.{self.extra_info_string}"
        )
    self.status = result_dict["RESULT"]
    self.result = result_dict

    return result_dict

data_check(duckdb_client)

Performs a check if database tables used in the actual check contain data.

Note: The returned result dict and failure message will be later aggregated in order to avoid duplicates in the reported failures.

Parameters:

Name Type Description Default
duckdb_client DuckDBPyConnection

DuckDB client for interacting with DuckDB

required

Returns:

Type Description
dict

If there is a table without data, a dict containing information about

dict

missing data will be returned, otherwise an empty dict indicating that

dict

data exists.

Source code in src/koality/checks.py
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
def data_check(self, duckdb_client: duckdb.DuckDBPyConnection) -> dict:
    """
    Performs a check if database tables used in the actual check
    contain data.

    Note: The returned result dict and failure message will be later
    aggregated in order to avoid duplicates in the reported failures.

    Args:
        duckdb_client: DuckDB client for interacting with DuckDB

    Returns:
        If there is a table without data, a dict containing information about
        missing data will be returned, otherwise an empty dict indicating that
        data exists.
    """
    is_empty_table = False
    try:
        result = execute_query(
            self.assemble_data_exists_query(),
            duckdb_client,
            self.database_provider,
        ).fetchone()
    except duckdb.Error:
        empty_table = f"Error while executing data check query on {self.table}"
    else:
        empty_table = result[0] if result else self.table
        is_empty_table = bool(empty_table)

    if not is_empty_table:
        return {}

    date = self.date_filter_value if hasattr(self, "date_filter_value") else dt.datetime.today().isoformat()
    self.message = f"No data in {empty_table} on {date} for: {self.shop_id}"
    self.status = "FAIL"
    return {
        "DATE": date,
        "METRIC_NAME": "data_exists",
        "SHOP_ID": self.shop_id,
        "TABLE": empty_table,
    }

get_filters(kwargs, filter_col_suffix='_filter_column', filter_value_suffix='_filter_value') staticmethod

Generates a filter dict from kwargs using a regex pattern. Returns a dict of the format {"date": {"column": "date", "value": "2020-01-01"}, ...}

Source code in src/koality/checks.py
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
@staticmethod
def get_filters(
    kwargs: dict,
    filter_col_suffix: str = r"_filter_column",
    filter_value_suffix: str = "_filter_value",
):
    """
    Generates a filter dict from kwargs using a regex pattern.
    Returns a dict of the format
        {"date": {"column": "date", "value": "2020-01-01"}, ...}
    """

    filters = {}

    # first, get all filter cols that are marked with filter_col_suffix,
    # e.g. shop_filter_column
    for key, value in kwargs.items():
        if match := re.fullmatch(r"(\w+)" + filter_col_suffix, key):
            filters[match[1]] = {"column": value}  # match[1] = "(\w+)" from above

    # next, find values for the filters ()
    for filter_key, filter_val in filters.items():
        for key, value in kwargs.items():
            if re.fullmatch(f"{filter_key}{filter_value_suffix}", key):
                if filter_key == "date":
                    filter_val["value"] = parse_date(value, offset_days=kwargs.get("date_offset", 0))
                else:
                    filter_val["value"] = value

                break  # no need to loop any further

        else:  # no break
            raise ValueError(f"{filter_key}_filter_column has no corresponding value!")

    return filters

DuplicateCheck

Bases: ColumnTransformationCheck

Checks the number of duplicates for a specific column, i.e., all counts - distinct counts. It inherits from koality.checks.ColumnTransformationCheck, and thus, we refer to argument descriptions in its super class.

Example:

DuplicateCheck( database_accessor="my-gcp-project.SHOP01", database_provider=None, table="my-gcp-project.SHOP01.skufeed_latest", check_column="sku_id", shop_id_filter_column="shop_code", # optional shop_id_filter_value="SHOP01", # optional date_filter_column="DATE", # optional date_filter_value="2023-01-01", # optional lower_threshold=0.0, upper_threshold=0.0, )

Source code in src/koality/checks.py
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
class DuplicateCheck(ColumnTransformationCheck):
    """
    Checks the number of duplicates for a specific column, i.e., all counts - distinct
    counts. It inherits from `koality.checks.ColumnTransformationCheck`, and thus, we
    refer to argument descriptions in its super class.

    Example:

    DuplicateCheck(
        database_accessor="my-gcp-project.SHOP01",
        database_provider=None,
        table="my-gcp-project.SHOP01.skufeed_latest",
        check_column="sku_id",
        shop_id_filter_column="shop_code",  # optional
        shop_id_filter_value="SHOP01",  # optional
        date_filter_column="DATE",  # optional
        date_filter_value="2023-01-01",  # optional
        lower_threshold=0.0,
        upper_threshold=0.0,
    )
    """

    def __init__(
        self,
        database_accessor: str,
        database_provider: DatabaseProvider | None,
        table: str,
        check_column: str,
        lower_threshold: float = -math.inf,
        upper_threshold: float = math.inf,
        monitor_only: bool = False,
        extra_info: Optional[str] = None,
        **kwargs,
    ):
        super().__init__(
            database_accessor=database_accessor,
            database_provider=database_provider,
            transformation_name="duplicates",
            table=table,
            check_column=check_column,
            lower_threshold=lower_threshold,
            upper_threshold=upper_threshold,
            monitor_only=monitor_only,
            extra_info=extra_info,
            **kwargs,
        )

    def transformation_statement(self) -> str:
        return f"COUNT(*) - COUNT(DISTINCT {self.check_column}) AS {self.name}"

IqrOutlierCheck

Bases: ColumnTransformationCheck

Checks if the date-specific value of a column is an outlier based on the interquartile range (IQR) method. It inherits from koality.checks.ColumnTransformationCheck, and thus, we refer to argument descriptions in its super class, except for the date and date_filter_column arguments which are added in this sub class.

The IQR method is based on the 25th and 75th percentiles of the data. The thresholds are calculated as follows: - lower_threshold = q25 - iqr_factor * (q75 - q25)

Example:

IqrOutlierCheck( database_accessor="my-gcp-project.SHOP01", database_provider=None, check_column="num_orders", table="my-gcp-project.SHOP01.orders", date_filter_column="DATE", date_filter_value="2023-01-01", interval_days=14, how="both", # check both upper and lower outliers iqr_factor=1.5, shop_id_filter_column="shop_code", # optional shop_id_filter_value="SHOP01", # optional )

Source code in src/koality/checks.py
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
class IqrOutlierCheck(ColumnTransformationCheck):
    """
    Checks if the date-specific value of a column is an outlier based on the
    interquartile range (IQR) method. It inherits from `koality.checks.ColumnTransformationCheck`,
    and thus, we refer to argument descriptions in its super class, except for the
    `date` and `date_filter_column` arguments which are added in this sub class.

    The IQR method is based on the 25th and 75th percentiles of the data. The
    thresholds are calculated as follows:
    - lower_threshold = q25 - iqr_factor * (q75 - q25)


    Example:

    IqrOutlierCheck(
        database_accessor="my-gcp-project.SHOP01",
        database_provider=None,
        check_column="num_orders",
        table="my-gcp-project.SHOP01.orders",
        date_filter_column="DATE",
        date_filter_value="2023-01-01",
        interval_days=14,
        how="both",  # check both upper and lower outliers
        iqr_factor=1.5,
        shop_id_filter_column="shop_code",  # optional
        shop_id_filter_value="SHOP01",  # optional
    )
    """

    def __init__(
        self,
        database_accessor: str,
        database_provider: DatabaseProvider | None,
        check_column: str,
        table: str,
        date_filter_column: str,
        date_filter_value: str,
        interval_days: int,
        how: Literal["both", "upper", "lower"],
        iqr_factor: float,
        monitor_only: bool = False,
        extra_info: Optional[str] = None,
        **kwargs,
    ):
        self.date_filter_column = date_filter_column
        self.date_filter_value = date_filter_value
        if interval_days < 1:
            raise ValueError("interval_days must be at least 1")
        self.interval_days = int(interval_days)
        if how not in ["both", "upper", "lower"]:
            raise ValueError("how must be one of 'both', 'upper', 'lower'")
        self.how = how
        # reasonable lower bound for iqr_factor
        if iqr_factor < 1.5:
            raise ValueError("iqr_factor must be at least 1.5")
        self.iqr_factor = float(iqr_factor)

        super().__init__(
            database_accessor=database_accessor,
            database_provider=database_provider,
            transformation_name=f"outlier_iqr_{self.how}_{str(self.iqr_factor).replace('.', '_')}",
            table=table,
            check_column=check_column,
            lower_threshold=-math.inf,
            upper_threshold=math.inf,
            monitor_only=monitor_only,
            extra_info=extra_info,
            date_filter_column=date_filter_column,
            date_filter_value=date_filter_value,
            **kwargs,
        )

        self.filters = {
            filter_name: filer_dict for filter_name, filer_dict in self.filters.items() if filter_name != "date"
        }

    def transformation_statement(self) -> str:
        # TODO: currently we only raise an error if there is no data for the date
        #       we could also raise an error if there is not enough data for the
        #       IQR calculation
        where_statement = ""
        filter_columns = ""
        if self.filters:
            filter_columns = ",\n".join([v["column"] for k, v in self.filters.items()])
            filter_columns = ",\n" + filter_columns
            where_statement = self.assemble_where_statement(self.filters)
            where_statement = "\nAND\n" + where_statement.removeprefix("WHERE\n")
        return f"""
        WITH
            base AS (
                SELECT
                    DATE({self.date_filter_column}) AS {self.date_filter_column},
                    {self.check_column}
                    {filter_columns}
                FROM
                    {self.table}
                WHERE
                    DATE({self.date_filter_column}) BETWEEN (DATE '{self.date_filter_value}' - INTERVAL {self.interval_days} DAY)
                    AND DATE '{self.date_filter_value}'
                    {where_statement}
            ),
            compare AS (
                SELECT * FROM base WHERE {self.date_filter_column} < '{self.date_filter_value}'
            ),
            slice AS (
                SELECT * FROM base WHERE {self.date_filter_column} = '{self.date_filter_value}'
            ),
            percentiles AS (
                SELECT
                  QUANTILE_CONT(CAST({self.check_column} AS FLOAT), 0.25) AS q25,
                  QUANTILE_CONT(CAST({self.check_column} AS FLOAT), 0.75) AS q75
                FROM
                  compare
            ),
            stats AS (
                SELECT
                  * exclude ({self.check_column}),
                  {self.check_column} AS {self.name},
                  (percentiles.q25 - {self.iqr_factor} * (percentiles.q75 - percentiles.q25)) AS lower_threshold,
                  (percentiles.q75 + {self.iqr_factor} * (percentiles.q75 - percentiles.q25)) AS upper_threshold,
                FROM
                  slice
                LEFT JOIN percentiles
                ON TRUE
            )
        """  # noqa: S608, E501

    def query_boilerplate(self, metric_statement: str) -> str:
        return f"""
            {metric_statement}

            SELECT
                *
            FROM
                stats
        """

    def _check(self, duckdb_client: duckdb.DuckDBPyConnection, query: str) -> tuple[list[dict], str | None]:
        result, error = super()._check(duckdb_client, query)
        # overwrite the lower and upper thresholds as required
        if result:
            if self.how in ["both", "lower"]:
                self.lower_threshold = result[0]["lower_threshold"]
            if self.how in ["both", "upper"]:
                self.upper_threshold = result[0]["upper_threshold"]
        return result, error

    def assemble_data_exists_query(self) -> str:
        data_exists_query = f"""
        SELECT
            IF(COUNTIF({self.check_column} IS NOT NULL) > 0, '', '{self.table}') AS empty_table
        FROM
            {f"{self.database_accessor}." if self.database_accessor else ""}{self.table}
        """
        where_statement = self.assemble_where_statement(self.filters)
        if where_statement:
            where_statement = f"{where_statement} AND {self.date_filter_column} = '{self.date_filter_value}'"
        else:
            where_statement = f"WHERE {self.date_filter_column} = '{self.date_filter_value}'"
        return f"{data_exists_query}\n{where_statement}"

MatchRateCheck

Bases: DataQualityCheck

Checks the match rate between two tables after joining on specific columns.

If left_join_columns (or right_join_columns) is defined, these columns will be used for joining the data. If not, join_columns will be used as fallback.

Parameters:

Name Type Description Default
left_table str

Name of table for left part of join (e.g., "my-gcp-project.SHOP01.identifier_base")

required
right_table str

Name of table for right part of join (e.g., "my-gcp-project.SHOP01.feature_baseline")

required
check_column str

Name of column to be checked (e.g., "product_number")

required
join_columns list[str] | None

List of columns to join data on (e.g., ["PREDICTION_DATE", "product_number"])

None
join_columns_left list[str] | None

List of columns of left table to join data on (e.g., ["BQ_PARTITIONTIME", "productId"])

None
join_columns_right list[str] | None

List of columns of right table to join data on (e.g., ["PREDICTION_DATE", "product_number"])

None
lower_threshold float

Check will fail if check result < lower_threshold

-inf
upper_threshold float

Check will fail if check result > upper_threshold

inf
monitor_only bool

If True, no checks will be performed

False
extra_info str | None

Optional additional text that will be added to the end of the failure message

None

Example:

MatchRateCheck( database_accessor="my-gcp-project.SHOP01", database_provider=None, left_table="my-gcp-project.SHOP01.pdp_views", right_table="my-gcp-project.SHOP01.skufeed_latest", join_columns_left=["DATE", "product_number_v2"], join_columns_right=["DATE", "product_number"], check_column="product_number", shop_id_filter_column="shop_code", # optional shop_id_filter_value="SHOP01", # optional date_filter_column="DATE", # optional date_filter_value="2023-01-01", # optional )

Source code in src/koality/checks.py
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
class MatchRateCheck(DataQualityCheck):
    """
    Checks the match rate between two tables after joining on specific columns.

    If left_join_columns (or right_join_columns) is defined, these columns will be
    used for joining the data. If not, join_columns will be used as fallback.

    Args:
        left_table: Name of table for left part of join
                    (e.g., "my-gcp-project.SHOP01.identifier_base")
        right_table: Name of table for right part of join
                     (e.g., "my-gcp-project.SHOP01.feature_baseline")
        check_column: Name of column to be checked (e.g., "product_number")
        join_columns: List of columns to join data on (e.g., ["PREDICTION_DATE", "product_number"])
        join_columns_left: List of columns of left table to join data on
                           (e.g., ["BQ_PARTITIONTIME", "productId"])
        join_columns_right: List of columns of right table to join data on
                            (e.g., ["PREDICTION_DATE", "product_number"])
        lower_threshold: Check will fail if check result < lower_threshold
        upper_threshold: Check will fail if check result > upper_threshold
        monitor_only: If True, no checks will be performed
        extra_info: Optional additional text that will be added to the end of the failure message

    Example:

    MatchRateCheck(
        database_accessor="my-gcp-project.SHOP01",
        database_provider=None,
        left_table="my-gcp-project.SHOP01.pdp_views",
        right_table="my-gcp-project.SHOP01.skufeed_latest",
        join_columns_left=["DATE", "product_number_v2"],
        join_columns_right=["DATE", "product_number"],
        check_column="product_number",
        shop_id_filter_column="shop_code",  # optional
        shop_id_filter_value="SHOP01",  # optional
        date_filter_column="DATE",  # optional
        date_filter_value="2023-01-01",  # optional
    )
    """

    def __init__(
        self,
        database_accessor: str,
        database_provider: DatabaseProvider | None,
        left_table: str,
        right_table: str,
        check_column: str,
        join_columns: list[str] | None = None,
        join_columns_left: list[str] | None = None,
        join_columns_right: list[str] | None = None,
        lower_threshold: float = -math.inf,
        upper_threshold: float = math.inf,
        monitor_only: bool = False,
        extra_info: str | None = None,
        **kwargs,
    ):
        self.left_table = left_table
        self.right_table = right_table

        if not (join_columns or (join_columns_left and join_columns_right)):
            raise KoalityError(
                "No join_columns was provided. Use either join_columns or join_columns_left and join_columns_right"
            )

        # mypy typing does not understand that None is not possible, thus, we
        # add `or []`
        self.join_columns_left: list[str] = join_columns_left if join_columns_left else join_columns or []
        self.join_columns_right: list[str] = join_columns_right if join_columns_right else join_columns or []

        if not self.join_columns_right or not self.join_columns_left:
            raise KoalityError(
                "No join_columns was provided. Use join_columns, join_columns_left, and/or join_columns_right"
            )

        if len(self.join_columns_left) != len(self.join_columns_right):
            raise KoalityError(
                "join_columns_left and join_columns_right need to have equal length"
                f" ({len(self.join_columns_left)} vs. {len(self.join_columns_right)})."
            )

        super().__init__(
            database_accessor=database_accessor,
            database_provider=database_provider,
            table=f"{self.left_table}_JOIN_{self.right_table}",
            check_column=check_column,
            lower_threshold=lower_threshold,
            upper_threshold=upper_threshold,
            monitor_only=monitor_only,
            extra_info=extra_info,
            **kwargs,
        )

        self.filters_left = self.filters | self.get_filters(kwargs, filter_col_suffix="filter_column_left")
        self.filters_right = self.filters | self.get_filters(kwargs, filter_col_suffix="filter_column_right")

    def assemble_name(self):
        return f"{self.check_column.split('.')[-1]}_matchrate"

    def assemble_query(self) -> str:
        right_column_statement = ",\n    ".join(self.join_columns_right)

        join_on_statement = "\n    AND\n    ".join(
            [
                f"lefty.{left_col} = righty.{right_col.split('.')[-1]}"
                for left_col, right_col in zip(self.join_columns_left, self.join_columns_right, strict=False)
            ]
        )

        return f"""
        WITH
            righty AS (
                SELECT DISTINCT
                    {right_column_statement},
                    TRUE AS in_right_table
                FROM
                    {f"{self.database_accessor}." if self.database_accessor else ""}{self.right_table}
                {self.assemble_where_statement(self.filters_right)}
            ),
            lefty AS (
                SELECT
                    *
                FROM
                    {f"{self.database_accessor}." if self.database_accessor else ""}{self.left_table}
                {self.assemble_where_statement(self.filters_left)}
            )

            SELECT
                CASE
                    WHEN COUNT(*) = 0 THEN 0.0
                    ELSE ROUND(COUNTIF(in_right_table IS TRUE) / COUNT(*), 3)
                END AS {self.name}
            FROM
                lefty
            LEFT JOIN
                righty
            ON
                {join_on_statement}
        """

    def assemble_data_exists_query(self) -> str:
        """
        First checks left, then right table for data.

        Returns:
            Empty table name or empty string
        """

        return f"""
        WITH
        righty AS (
            SELECT
                COUNT(*) AS right_counter,
            FROM
                {f"{self.database_accessor}." if self.database_accessor else ""}{self.right_table}
            {self.assemble_where_statement(self.filters_right)}
        ),

        lefty AS (
            SELECT
                COUNT(*) AS left_counter,
            FROM
                {f"{self.database_accessor}." if self.database_accessor else ""}{self.left_table}
            {self.assemble_where_statement(self.filters_left)}
        )

        SELECT
            IF(
                (SELECT * FROM lefty) > 0,
                IF((SELECT * FROM righty) > 0, '', '{self.right_table}'),
                '{self.left_table}'
            ) AS empty_table
        """  # noqa: S608

assemble_data_exists_query()

First checks left, then right table for data.

Returns:

Type Description
str

Empty table name or empty string

Source code in src/koality/checks.py
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
def assemble_data_exists_query(self) -> str:
    """
    First checks left, then right table for data.

    Returns:
        Empty table name or empty string
    """

    return f"""
    WITH
    righty AS (
        SELECT
            COUNT(*) AS right_counter,
        FROM
            {f"{self.database_accessor}." if self.database_accessor else ""}{self.right_table}
        {self.assemble_where_statement(self.filters_right)}
    ),

    lefty AS (
        SELECT
            COUNT(*) AS left_counter,
        FROM
            {f"{self.database_accessor}." if self.database_accessor else ""}{self.left_table}
        {self.assemble_where_statement(self.filters_left)}
    )

    SELECT
        IF(
            (SELECT * FROM lefty) > 0,
            IF((SELECT * FROM righty) > 0, '', '{self.right_table}'),
            '{self.left_table}'
        ) AS empty_table
    """  # noqa: S608

NullRatioCheck

Bases: ColumnTransformationCheck

Checks the share of NULL values in a specific column of a table. It inherits from koality.checks.ColumnTransformationCheck, and thus, we refer to argument descriptions in its super class.

Example:

NullRatioCheck( database_accessor="project.dataset", database_provider=None, table="project.dataset.table", check_column="orders", shop_id_filter_column="shop_code", # optional shop_id_filter_value="SHOP01", # optional date_filter_column="date", # optional date_filter_value="2023-01-01", # optional lower_threshold=0.9, upper_threshold=1.0, )

Source code in src/koality/checks.py
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
class NullRatioCheck(ColumnTransformationCheck):
    """
    Checks the share of NULL values in a specific column of a table. It inherits from
    `koality.checks.ColumnTransformationCheck`, and thus, we refer to argument
    descriptions in its super class.


    Example:

    NullRatioCheck(
        database_accessor="project.dataset",
        database_provider=None,
        table="project.dataset.table",
        check_column="orders",
        shop_id_filter_column="shop_code",  # optional
        shop_id_filter_value="SHOP01",  # optional
        date_filter_column="date",  # optional
        date_filter_value="2023-01-01",  # optional
        lower_threshold=0.9,
        upper_threshold=1.0,
    )
    """

    def __init__(
        self,
        database_accessor: str,
        database_provider: DatabaseProvider | None,
        table: str,
        check_column: str,
        lower_threshold: float = -math.inf,
        upper_threshold: float = math.inf,
        monitor_only: bool = False,
        extra_info: str | None = None,
        **kwargs,
    ):
        super().__init__(
            database_accessor=database_accessor,
            database_provider=database_provider,
            transformation_name="null_ratio",
            table=table,
            check_column=check_column,
            lower_threshold=lower_threshold,
            upper_threshold=upper_threshold,
            monitor_only=monitor_only,
            extra_info=extra_info,
            **kwargs,
        )

    def transformation_statement(self) -> str:
        return f"""
            CASE
                WHEN COUNT(*) = 0 THEN 0.0
                ELSE ROUND(COUNTIF({self.check_column} IS NULL) / COUNT(*), 3)
            END AS {self.name}
        """

OccurrenceCheck

Bases: ColumnTransformationCheck

Checks how often any value in a column occurs. It inherits fromkoality.checks.ColumnTransformationCheck, and thus, we refer to argument descriptions in its super class. Useful e.g. to check for a single product occurring unusually often (likely an error)

Parameters:

Name Type Description Default
max_or_min Literal['max', 'min']

Check either the maximum or minimum occurrence of any value. If you want to check if any value occurs more than x times, use 'max' and upper_threshold=x If you want to check if any value occurs less than y times, use 'min' and lower_threshold=y

required

Example:

OccurrenceCheck( database_accessor="my-gcp-project.SHOP01", database_provider=None, max_or_min="max", table="my-gcp-project.SHOP01.skufeed_latest", check_column="sku_id", shop_id_filter_column="shop_code", # optional shop_id_filter_value="SHOP01", # optional date_filter_column="DATE", # optional date_filter_value="2023-01-01", # optional lower_threshold=0, upper_threshold=500, )

Source code in src/koality/checks.py
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
class OccurrenceCheck(ColumnTransformationCheck):
    """
    Checks how often *any* value in a column occurs.
    It inherits from`koality.checks.ColumnTransformationCheck`, and thus, we refer to argument
      descriptions in its super class.
    Useful e.g. to check for a single product occurring unusually often (likely an error)

    Args:
        max_or_min: Check either the maximum or minimum occurrence of any value.
                    If you want to check if any value occurs more than x times, use 'max' and upper_threshold=x
                    If you want to check if any value occurs less than y times, use 'min' and lower_threshold=y

    Example:

    OccurrenceCheck(
        database_accessor="my-gcp-project.SHOP01",
        database_provider=None,
        max_or_min="max",
        table="my-gcp-project.SHOP01.skufeed_latest",
        check_column="sku_id",
        shop_id_filter_column="shop_code",  # optional
        shop_id_filter_value="SHOP01",  # optional
        date_filter_column="DATE",  # optional
        date_filter_value="2023-01-01",  # optional
        lower_threshold=0,
        upper_threshold=500,
    )
    """

    def __init__(
        self,
        database_accessor: str,
        database_provider: DatabaseProvider | None,
        max_or_min: Literal["max", "min"],
        **kwargs,
    ):
        if max_or_min not in ("max", "min"):
            raise ValueError("'max_or_min' not one of supported modes 'min' or 'max'")
        self.max_or_min = max_or_min
        super().__init__(
            database_accessor=database_accessor,
            database_provider=database_provider,
            transformation_name=f"occurrence_{max_or_min}",
            **kwargs,
        )

    def transformation_statement(self) -> str:
        return f"{self.check_column}, COUNT(*) AS {self.name}"

    def assemble_query(self) -> str:
        # Since koality checks only the first entry, the table with value + count_occurence is
        # ordered DESC/ASC depending on whether max/min occurence is supposed to be checked.
        order = {"max": "DESC", "min": "ASC"}[self.max_or_min]
        return f"""
            {self.query_boilerplate(self.transformation_statement())}
            {self.assemble_where_statement(self.filters)}
            GROUP BY {self.check_column}
            ORDER BY {self.name} {order}
            LIMIT 1  -- only the first entry is needed
        """

RegexMatchCheck

Bases: ColumnTransformationCheck

Checks the share of values matching a regex in a specific column of a table. It inherits from koality.checks.ColumnTransformationCheck, and thus, we refer to argument descriptions in its super class, except for regex_to_match which is added in this subclass.

Parameters:

Name Type Description Default
regex_to_match str

The regular expression to be checked on check_column (e.g., "SHOP[0-9]{2}-.*" to check for a shop code prefix like "SHOP01-")

required

Example:

RegexMatchCheck( database_accessor="project.dataset", database_provider=None, table="project.dataset.table", check_column="orders", regex_to_match="^SHOP[0-9]{2}-.*", date_filter_column="date", # optional date_filter_value="2023-01-01", # optional lower_threshold=0.9, upper_threshold=1.0, )

Source code in src/koality/checks.py
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
class RegexMatchCheck(ColumnTransformationCheck):
    """
    Checks the share of values matching a regex in a specific column of a table. It
    inherits from `koality.checks.ColumnTransformationCheck`, and thus, we refer to
    argument descriptions in its super class, except for regex_to_match which is
    added in this subclass.

    Args:
        regex_to_match: The regular expression to be checked on check_column (e.g.,
                        "SHOP[0-9]{2}-.*" to check for a shop code prefix like "SHOP01-")

    Example:

    RegexMatchCheck(
        database_accessor="project.dataset",
        database_provider=None,
        table="project.dataset.table",
        check_column="orders",
        regex_to_match="^SHOP[0-9]{2}-.*",
        date_filter_column="date",  # optional
        date_filter_value="2023-01-01",  # optional
        lower_threshold=0.9,
        upper_threshold=1.0,
    )
    """

    def __init__(
        self,
        database_accessor: str,
        database_provider: DatabaseProvider | None,
        table: str,
        check_column: str,
        regex_to_match: str,
        lower_threshold: float = -math.inf,
        upper_threshold: float = math.inf,
        monitor_only: bool = False,
        extra_info: Optional[str] = None,
        **kwargs,
    ):
        self.regex_to_match = regex_to_match

        super().__init__(
            database_accessor=database_accessor,
            database_provider=database_provider,
            transformation_name="regex_match_ratio",
            table=table,
            check_column=check_column,
            lower_threshold=lower_threshold,
            upper_threshold=upper_threshold,
            monitor_only=monitor_only,
            extra_info=extra_info,
            **kwargs,
        )

    def transformation_statement(self) -> str:
        return f"""AVG(IF(REGEXP_MATCHES({self.check_column}, '{self.regex_to_match}'), 1, 0)) AS {self.name}"""

RelCountChangeCheck

Bases: DataQualityCheck

Checks the relative change of a count in comparison to the average counts of a number of historic days before the check date.

Parameters:

Name Type Description Default
table str

Name of table (e.g., "my-gcp-project.SHOP01.feature_category")

required
check_column str

Name of column to be checked (e.g., "category")

required
rolling_days int

The number of historic days to be taken into account for the historic average baseline for the comparison (e.g., 7).

required
date_filter_column str

The name of the date column

required
date_filter_value str

The date where the check should be performed (e.g., "2023-01-01")

required
lower_threshold float

Check will fail if check result < lower_threshold

-inf
upper_threshold float

Check will fail if check result > upper_threshold

inf
monitor_only bool

If True, no checks will be performed

False
extra_info Optional[str]

Optional additional text that will be added to the end of the failure message

None

Example:

RelCountChangeCheck( database_accessor="my-gcp-project.SHOP01", database_provider=None, table="my-gcp-project.SHOP01.skufeed_latest", check_column="sku_id", rolling_days=7, shop_id_filter_column="shop_code", # optional shop_id_filter_value="SHOP01", # optional date_filter_column="DATE", # mandatory date_filter_value="2023-01-01", # mandatory lower_threshold=-0.15, upper_threshold=0.15, )

Source code in src/koality/checks.py
 979
 980
 981
 982
 983
 984
 985
 986
 987
 988
 989
 990
 991
 992
 993
 994
 995
 996
 997
 998
 999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
class RelCountChangeCheck(DataQualityCheck):  # TODO: (non)distinct counts parameter?
    """
    Checks the relative change of a count in comparison to the average counts of a
    number of historic days before the check date.

    Args:
        table: Name of table (e.g., "my-gcp-project.SHOP01.feature_category")
        check_column: Name of column to be checked (e.g., "category")
        rolling_days: The number of historic days to be taken into account for
                      the historic average baseline for the comparison (e.g., 7).
        date_filter_column: The name of the date column
        date_filter_value: The date where the check should be performed (e.g., "2023-01-01")
        lower_threshold: Check will fail if check result < lower_threshold
        upper_threshold: Check will fail if check result > upper_threshold
        monitor_only: If True, no checks will be performed
        extra_info: Optional additional text that will be added to the end of the failure message

    Example:

    RelCountChangeCheck(
        database_accessor="my-gcp-project.SHOP01",
        database_provider=None,
        table="my-gcp-project.SHOP01.skufeed_latest",
        check_column="sku_id",
        rolling_days=7,
        shop_id_filter_column="shop_code",  # optional
        shop_id_filter_value="SHOP01",  # optional
        date_filter_column="DATE",  # mandatory
        date_filter_value="2023-01-01",  # mandatory
        lower_threshold=-0.15,
        upper_threshold=0.15,
    )
    """

    def __init__(
        self,
        database_accessor: str,
        database_provider: DatabaseProvider | None,
        table: str,
        check_column: str,
        rolling_days: int,
        date_filter_column: str,
        date_filter_value: str,
        lower_threshold: float = -math.inf,
        upper_threshold: float = math.inf,
        monitor_only: bool = False,
        extra_info: Optional[str] = None,
        **kwargs,
    ):
        self.rolling_days = rolling_days
        self.date_filter_value = date_filter_value
        self.date_filter_column = date_filter_column

        super().__init__(
            database_accessor=database_accessor,
            database_provider=database_provider,
            table=table,
            check_column=check_column,
            lower_threshold=lower_threshold,
            upper_threshold=upper_threshold,
            monitor_only=monitor_only,
            date_filter_column=date_filter_column,
            date_filter_value=date_filter_value,
            extra_info=extra_info,
            **kwargs,
        )

        self.filters = {
            filter_name: filer_dict for filter_name, filer_dict in self.filters.items() if filter_name != "date"
        }

    def assemble_name(self):
        return f"{self.check_column.split('.')[-1]}" + "_count_change"

    def assemble_query(self) -> str:
        where_statement = self.assemble_where_statement(self.filters).replace("WHERE", "AND")

        return f"""
        WITH
            base AS (
                SELECT
                    {self.date_filter_column},
                    COUNT(DISTINCT {self.check_column}) AS dist_cnt
                FROM
                    {f"{self.database_accessor}." if self.database_accessor else ""}{self.table}
                WHERE
                    {self.date_filter_column} BETWEEN (DATE '{self.date_filter_value}' - INTERVAL {self.rolling_days} DAY)
                    AND '{self.date_filter_value}'
                {where_statement}
                GROUP BY
                    {self.date_filter_column}
            ),
            rolling_avgs AS (
                SELECT
                    AVG(dist_cnt) AS rolling_avg
                FROM
                    base
                WHERE
                    {self.date_filter_column} BETWEEN (DATE '{self.date_filter_value}' - INTERVAL {self.rolling_days} DAY)
                AND
                    (DATE '{self.date_filter_value}' - INTERVAL 1 DAY)
            ),

            -- Helper is needed to cover case where no current data is available
            dist_cnt_helper AS (
                SELECT
                    MAX(dist_cnt) AS dist_cnt
                FROM
                    (
                        SELECT dist_cnt FROM base WHERE {self.date_filter_column} = '{self.date_filter_value}'
                        UNION ALL
                        SELECT 0 AS dist_cnt
                    )
            )

            SELECT
                CASE
                    WHEN rolling_avg = 0 THEN 0.0
                    ELSE ROUND((dist_cnt - rolling_avg) / rolling_avg, 3)
                END AS {self.name}
            FROM
                dist_cnt_helper
            JOIN
                rolling_avgs
            ON TRUE
        """  # noqa: S608, E501

    def assemble_data_exists_query(self) -> str:
        data_exists_query = f"""
        SELECT
            IF(COUNT(*) > 0, '', '{self.table}') AS empty_table
        FROM
            {f"{self.database_accessor}." if self.database_accessor else ""}{self.table}
        """

        where_statement = self.assemble_where_statement(self.filters)
        if where_statement:
            return f"{data_exists_query}\n{where_statement} AND {self.date_filter_column} = '{self.date_filter_value}'"
        return f"{data_exists_query}\nWHERE {self.date_filter_column} = '{self.date_filter_value}'"

RollingValuesInSetCheck

Bases: ValuesInSetCheck

Checks the share of values that match any value of a value set in a specific column of a table similar to ValuesInSetCheck, but the share is computed for a longer time period (currently also including data of the 14 days before the actual check date). It inherits from koality.checks.ValuesInSetCheck, and thus, also from koality.checks.ColumnTransformationCheck, thus, we also refer to argument descriptions in its super class.

Example:

RollingValuesInSetCheck( database_accessor="my-gcp-project.SHOP01", database_provider=None, table="my-gcp-project.SHOP01.orders", check_column="category", value_set='("toys", "shoes")', shop_id_filter_column="shop_code", # optional shop_id_filter_value="SHOP01", # optional date_filter_column="DATE", # mandatory date_filter_value="2023-01-01", # mandatory lower_threshold=0.9, upper_threshold=1.0, )

Source code in src/koality/checks.py
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
class RollingValuesInSetCheck(ValuesInSetCheck):
    """
    Checks the share of values that match any value of a value set in a specific
    column of a table similar to `ValuesInSetCheck`, but the share is computed for
    a longer time period (currently also including data of the 14 days before the
    actual check date). It inherits from `koality.checks.ValuesInSetCheck`,
    and thus, also from `koality.checks.ColumnTransformationCheck`,
    thus, we also refer to argument descriptions in its super class.

    Example:

    RollingValuesInSetCheck(
        database_accessor="my-gcp-project.SHOP01",
        database_provider=None,
        table="my-gcp-project.SHOP01.orders",
        check_column="category",
        value_set='("toys", "shoes")',
        shop_id_filter_column="shop_code",  # optional
        shop_id_filter_value="SHOP01",  # optional
        date_filter_column="DATE",  # mandatory
        date_filter_value="2023-01-01",  # mandatory
        lower_threshold=0.9,
        upper_threshold=1.0,
    )
    """

    def __init__(
        self,
        database_accessor: str,
        database_provider: DatabaseProvider | None,
        table: str,
        check_column: str,
        value_set: str | bytes | Iterable,
        date_filter_column: str,
        date_filter_value: str,
        lower_threshold: float = -math.inf,
        upper_threshold: float = math.inf,
        monitor_only: bool = False,
        extra_info: Optional[str] = None,
        **kwargs,
    ):
        self.date_filter_column = date_filter_column
        self.date_filter_value = date_filter_value

        super().__init__(
            database_accessor=database_accessor,
            database_provider=database_provider,
            transformation_name="rolling_values_in_set_ratio",
            table=table,
            value_set=value_set,
            check_column=check_column,
            lower_threshold=lower_threshold,
            upper_threshold=upper_threshold,
            monitor_only=monitor_only,
            date_filter_value=date_filter_value,
            date_filter_column=date_filter_column,
            extra_info=extra_info,
            **kwargs,
        )

        self.filters = {
            filter_name: filer_dict for filter_name, filer_dict in self.filters.items() if filter_name != "date"
        }

    def assemble_query(self) -> str:
        main_query = self.query_boilerplate(self.transformation_statement())

        main_query += (
            "WHERE\n    "
            + f"{self.date_filter_column} BETWEEN (DATE '{self.date_filter_value}' - INTERVAL 14 DAY) AND '{self.date_filter_value}'"  # noqa: E501
        )  # TODO: maybe parameterize interval days

        if where_statement := self.assemble_where_statement(self.filters):
            return main_query + "\nAND\n" + where_statement.removeprefix("WHERE\n")

        return main_query

ValuesInSetCheck

Bases: ColumnTransformationCheck

Checks the share of values that match any value of a value set in a specific column of a table. It inherits from koality.checks.ColumnTransformationCheck, and thus, we refer to argument descriptions in its super class, except for value set which is added in this subclass.

Parameters:

Name Type Description Default
value_set str | bytes | Iterable

A list of values (or a string representation of such a list) to be checked. Single values are also allowed. Examples for valid inputs: - ["shoes", "clothing"] - "clothing" - '("shoes", "toys")'

required

Example:

ValuesInSetCheck( database_accessor="project.dataset", database_provider=None, table="project.dataset.table", check_column="category", value_set='("toys", "shoes")', shop_id_filter_column="shop_code", # optional shop_id_filter_value="SHOP01", # optional date_filter_column="date", # optional date_filter_value="2023-01-01", # optional lower_threshold=0.9, upper_threshold=1.0, )

Source code in src/koality/checks.py
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
class ValuesInSetCheck(ColumnTransformationCheck):
    """
    Checks the share of values that match any value of a value set in a specific
    column of a table. It inherits from `koality.checks.ColumnTransformationCheck`,
    and thus, we refer to argument descriptions in its super class, except for
    value set which is added in this subclass.

    Args:
        value_set: A list of values (or a string representation of such a list) to be checked.
                   Single values are also allowed. Examples for valid inputs:
                   - ["shoes", "clothing"]
                   - "clothing"
                   - '("shoes", "toys")'

    Example:

    ValuesInSetCheck(
        database_accessor="project.dataset",
        database_provider=None,
        table="project.dataset.table",
        check_column="category",
        value_set='("toys", "shoes")',
        shop_id_filter_column="shop_code",  # optional
        shop_id_filter_value="SHOP01",  # optional
        date_filter_column="date",  # optional
        date_filter_value="2023-01-01",  # optional
        lower_threshold=0.9,
        upper_threshold=1.0,
    )
    """

    def __init__(
        self,
        database_accessor: str,
        database_provider: DatabaseProvider | None,
        table: str,
        check_column: str,
        value_set: str | bytes | Iterable,
        lower_threshold: float = -math.inf,
        upper_threshold: float = math.inf,
        monitor_only: bool = False,
        transformation_name: Optional[str] = None,
        extra_info: Optional[str] = None,
        **kwargs,
    ):
        self.value_set = to_set(value_set)
        if not self.value_set:
            raise ValueError("'value_set' must not be empty")
        self.value_set_string = f"({str(self.value_set)[1:-1]})"

        super().__init__(
            database_accessor=database_accessor,
            database_provider=database_provider,
            transformation_name=transformation_name if transformation_name else "values_in_set_ratio",
            table=table,
            check_column=check_column,
            lower_threshold=lower_threshold,
            upper_threshold=upper_threshold,
            monitor_only=monitor_only,
            extra_info=extra_info,
            **kwargs,
        )

    def transformation_statement(self) -> str:
        return f"""AVG(IF({self.check_column} IN {self.value_set_string}, 1, 0)) AS {self.name}"""