-
Notifications
You must be signed in to change notification settings - Fork 2.3k
Fixes 21482: only compare a column against '' when the type can hold it #31130
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
TeddyCr
wants to merge
4
commits into
open-metadata:main
Choose a base branch
from
TeddyCr:ISSUE-21482
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
7693112
Fixes 21482: only compare against '' for types that can hold it
TeddyCr 2cb79e7
Merge branch 'main' into ISSUE-21482
TeddyCr d179777
Merge remote-tracking branch 'upstream/main' into ISSUE-21482
TeddyCr 3d6335b
Merge remote-tracking branch 'origin/ISSUE-21482' into ISSUE-21482
TeddyCr File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
129 changes: 129 additions & 0 deletions
129
ingestion/tests/integration/profiler/test_null_missing_count_metric.py
This file contains hidden or 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,129 @@ | ||
| # Copyright 2025 Collate | ||
| # Licensed under the Collate Community License, Version 1.0 (the "License"); | ||
| # you may not use this file except in compliance with the License. | ||
| # You may obtain a copy of the License at | ||
| # https://github.com/open-metadata/OpenMetadata/blob/main/ingestion/LICENSE | ||
| # 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. | ||
|
|
||
| """ | ||
| Integration tests for the NullMissingCount metric against real engines. | ||
|
|
||
| The metric backs the `columnValuesMissingCount` data quality test. Comparing a | ||
| non-string column against '' fails on PostgreSQL and returns the wrong count on | ||
| MySQL, so both engines are exercised here. | ||
| """ | ||
|
|
||
| import datetime | ||
|
|
||
| import pytest | ||
| from sqlalchemy import Boolean, Column, Date, Integer, String, create_engine, select | ||
| from sqlalchemy.orm import DeclarativeBase | ||
| from testcontainers.mysql import MySqlContainer | ||
| from testcontainers.postgres import PostgresContainer | ||
|
|
||
| from metadata.ingestion.connections.session import create_and_bind_session | ||
| from metadata.profiler.metrics.registry import Metrics | ||
|
|
||
|
|
||
| class Base(DeclarativeBase): | ||
| pass | ||
|
|
||
|
|
||
| class MissingCountTestTable(Base): | ||
| __tablename__ = "missing_count_test" | ||
| row_id = Column(Integer, primary_key=True) | ||
| int_col = Column(Integer) | ||
| date_col = Column(Date) | ||
| bool_col = Column(Boolean) | ||
| str_col = Column(String(64)) | ||
|
|
||
|
|
||
| # 3 NULLs per column, plus the values MySQL would coerce '' into: 2 zeros in | ||
| # int_col and 2 false in bool_col. Only the NULLs are missing for those columns; | ||
| # str_col also counts its 2 empty strings. | ||
| ROWS = [ | ||
| (1, 0, datetime.date(2024, 1, 1), False, ""), | ||
| (2, 0, datetime.date(2024, 1, 2), False, ""), | ||
| (3, 7, datetime.date(2024, 1, 3), True, "a value"), | ||
| (4, None, None, None, None), | ||
| (5, None, None, None, None), | ||
| (6, None, None, None, None), | ||
| ] | ||
|
|
||
| EXPECTED_NULL_COUNT = 3 | ||
| EXPECTED_NULL_AND_EMPTY_COUNT = 5 | ||
|
|
||
|
|
||
| def _seed(engine): | ||
| Base.metadata.create_all(bind=engine) | ||
| session = create_and_bind_session(engine) | ||
| session.add_all( | ||
| [ | ||
| MissingCountTestTable( | ||
| row_id=row_id, | ||
| int_col=int_col, | ||
| date_col=date_col, | ||
| bool_col=bool_col, | ||
| str_col=str_col, | ||
| ) | ||
| for row_id, int_col, date_col, bool_col, str_col in ROWS | ||
| ] | ||
| ) | ||
| session.commit() | ||
| return session | ||
|
|
||
|
|
||
| @pytest.fixture(scope="module") | ||
| def postgres_session(): | ||
| with PostgresContainer("postgres:15") as container: | ||
| engine = create_engine(container.get_connection_url()) | ||
| session = _seed(engine) | ||
| yield session | ||
| session.close() | ||
| engine.dispose() | ||
|
|
||
|
|
||
| @pytest.fixture(scope="module") | ||
| def mysql_session(): | ||
| with MySqlContainer(image="mysql:8.4.5", dbname="test_missing_count") as container: | ||
| engine = create_engine(container.get_connection_url()) | ||
| session = _seed(engine) | ||
| yield session | ||
| session.close() | ||
| engine.dispose() | ||
|
|
||
|
|
||
| @pytest.fixture(params=["postgres", "mysql"]) | ||
| def session(request): | ||
| session = request.getfixturevalue(f"{request.param}_session") | ||
| yield session | ||
| # Postgres refuses every statement after a failed one until the transaction is | ||
| # rolled back, so one real failure would cascade into the other tests. | ||
| session.rollback() | ||
|
|
||
|
|
||
| def run_null_missing_count(session, column) -> int: | ||
| metric_fn = Metrics.nullMissingCount(column).fn() | ||
| return int(session.execute(select(metric_fn).select_from(MissingCountTestTable.__table__)).scalar()) | ||
|
|
||
|
|
||
| def test_integer_column_counts_nulls_only(session): | ||
| """Zeros are not missing values: on MySQL '' would coerce to 0 and count them""" | ||
| assert run_null_missing_count(session, MissingCountTestTable.int_col) == EXPECTED_NULL_COUNT | ||
|
|
||
|
|
||
| def test_date_column_counts_nulls_only(session): | ||
| assert run_null_missing_count(session, MissingCountTestTable.date_col) == EXPECTED_NULL_COUNT | ||
|
|
||
|
|
||
| def test_boolean_column_counts_nulls_only(session): | ||
| """False is not a missing value: on MySQL '' would coerce to 0 and count them""" | ||
| assert run_null_missing_count(session, MissingCountTestTable.bool_col) == EXPECTED_NULL_COUNT | ||
|
|
||
|
|
||
| def test_string_column_counts_nulls_and_empty_strings(session): | ||
| assert run_null_missing_count(session, MissingCountTestTable.str_col) == EXPECTED_NULL_AND_EMPTY_COUNT |
158 changes: 158 additions & 0 deletions
158
ingestion/tests/unit/observability/profiler/sqlalchemy/test_null_missing_count_metric.py
This file contains hidden or 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,158 @@ | ||
| # Copyright 2025 Collate | ||
| # Licensed under the Collate Community License, Version 1.0 (the "License"); | ||
| # you may not use this file except in compliance with the License. | ||
| # You may obtain a copy of the License at | ||
| # https://github.com/open-metadata/OpenMetadata/blob/main/ingestion/LICENSE | ||
| # 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. | ||
|
|
||
| """ | ||
| Test that the NullMissingCount metric only compares a column against the empty | ||
| string when the column can actually hold one. | ||
|
|
||
| Comparing a numeric/temporal/boolean/uuid/enum/interval/ip column with '' is a | ||
| runtime error on strict engines (Postgres, ClickHouse) and silently coerces on | ||
| MySQL and MariaDB, counting real values as missing. | ||
| """ | ||
|
|
||
| import pytest | ||
| from sqlalchemy import ( | ||
| BIGINT, | ||
| CHAR, | ||
| DECIMAL, | ||
| NVARCHAR, | ||
| TEXT, | ||
| VARCHAR, | ||
| Boolean, | ||
| Column, | ||
| Date, | ||
| DateTime, | ||
| Enum, | ||
| Float, | ||
| Integer, | ||
| Interval, | ||
| MetaData, | ||
| Numeric, | ||
| SmallInteger, | ||
| String, | ||
| Table, | ||
| Time, | ||
| select, | ||
| ) | ||
| from sqlalchemy.dialects import mysql, postgresql | ||
|
|
||
| from metadata.generated.schema.entity.data.table import DataType | ||
| from metadata.profiler.metrics.registry import Metrics | ||
| from metadata.profiler.orm.converter.common import CommonMapTypes | ||
| from metadata.profiler.orm.types.custom_ip import CustomIP | ||
| from metadata.profiler.orm.types.custom_time import CustomTime | ||
| from metadata.profiler.orm.types.custom_timestamp import CustomTimestamp | ||
| from metadata.profiler.orm.types.undetermined_type import UndeterminedType | ||
| from metadata.profiler.orm.types.uuid import UUIDString | ||
|
|
||
| EMPTY_STRING_COMPARISON = "= ''" | ||
|
TeddyCr marked this conversation as resolved.
|
||
|
|
||
| DIALECTS = [postgresql.dialect(), mysql.dialect()] | ||
|
|
||
| # OM types listed as supported by columnValuesMissingCount that no engine can | ||
| # store an empty string in. Kept as DataType so the mapping in CommonMapTypes is | ||
| # exercised too: a converter change that reintroduces the bug fails here. | ||
| NON_EMPTY_STRING_DATA_TYPES = [ | ||
| DataType.NUMBER, | ||
| DataType.TINYINT, | ||
| DataType.SMALLINT, | ||
| DataType.INT, | ||
| DataType.BIGINT, | ||
| DataType.BYTEINT, | ||
| DataType.FLOAT, | ||
| DataType.DOUBLE, | ||
| DataType.DECIMAL, | ||
| DataType.NUMERIC, | ||
| DataType.TIMESTAMP, | ||
| DataType.TIME, | ||
| DataType.DATE, | ||
| DataType.DATETIME, | ||
| DataType.INTERVAL, | ||
| DataType.BOOLEAN, | ||
| DataType.ENUM, | ||
| DataType.UUID, | ||
| ] | ||
|
|
||
|
|
||
| def compile_null_missing_count(sqa_type, dialect) -> str: | ||
| """Compile the nullMissingCount expression for a column of the given type""" | ||
| table = Table("a_table", MetaData(), Column("a_column", sqa_type)) | ||
| metric_fn = Metrics.nullMissingCount(table.c.a_column).fn() | ||
| return str( | ||
| select(metric_fn).compile( | ||
| dialect=dialect, | ||
| compile_kwargs={"literal_binds": True}, | ||
| ) | ||
| ) | ||
|
|
||
|
|
||
| @pytest.mark.parametrize("dialect", DIALECTS, ids=lambda d: d.name) | ||
| @pytest.mark.parametrize( | ||
| "sqa_type", | ||
| [ | ||
| Integer(), | ||
| SmallInteger(), | ||
| BIGINT(), | ||
| Numeric(), | ||
| DECIMAL(), | ||
| Float(), | ||
| Date(), | ||
| DateTime(), | ||
| Time(), | ||
| CustomTimestamp(), | ||
| CustomTime(), | ||
| Boolean(), | ||
| UUIDString(), | ||
| Interval(), | ||
| Enum("a", "b", name="an_enum"), | ||
| CustomIP(), | ||
| ], | ||
| ids=lambda t: type(t).__name__, | ||
| ) | ||
| def test_no_empty_string_comparison_for_non_string_types(sqa_type, dialect): | ||
| """A column that cannot hold '' is only checked for NULL""" | ||
| query = compile_null_missing_count(sqa_type, dialect) | ||
|
|
||
| assert "IS NULL" in query | ||
| assert EMPTY_STRING_COMPARISON not in query | ||
|
TeddyCr marked this conversation as resolved.
|
||
|
|
||
|
|
||
| @pytest.mark.parametrize("dialect", DIALECTS, ids=lambda d: d.name) | ||
| @pytest.mark.parametrize( | ||
| "sqa_type", | ||
| [ | ||
| String(), | ||
| VARCHAR(256), | ||
| TEXT(), | ||
| CHAR(8), | ||
| NVARCHAR(256), | ||
| # Fail-open contract: a type we don't recognize keeps the comparison | ||
| UndeterminedType(), | ||
| ], | ||
| ids=lambda t: type(t).__name__, | ||
| ) | ||
| def test_empty_string_comparison_for_string_types(sqa_type, dialect): | ||
| """String-like and unrecognized types keep counting '' as missing""" | ||
| query = compile_null_missing_count(sqa_type, dialect) | ||
|
|
||
| assert "IS NULL" in query | ||
| assert EMPTY_STRING_COMPARISON in query | ||
|
TeddyCr marked this conversation as resolved.
|
||
|
|
||
|
|
||
| @pytest.mark.parametrize("dialect", DIALECTS, ids=lambda d: d.name) | ||
| @pytest.mark.parametrize("data_type", NON_EMPTY_STRING_DATA_TYPES, ids=lambda dt: dt.value) | ||
| def test_supported_om_types_that_cannot_hold_empty_string(data_type, dialect): | ||
| """Guard the OM DataType -> ORM type mapping, not just the ORM types""" | ||
| # Reaching into _TYPE_MAP on purpose: the mapping itself is what we're pinning | ||
| sqa_type = CommonMapTypes._TYPE_MAP[data_type] | ||
| query = compile_null_missing_count(sqa_type(), dialect) | ||
|
|
||
| assert EMPTY_STRING_COMPARISON not in query | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
JSON and array columns can hit this too —
col = ''errors on both Postgres and MySQL, sosqlalchemy.JSON,sqlalchemy.ARRAYandCustomArraycould go in here as well.Binary types are fine as-is though — an empty blob really does match
'', so excluding those would drop real counts.