Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions src/databricks/labs/dqx/check_funcs.py
Original file line number Diff line number Diff line change
Expand Up @@ -611,8 +611,8 @@ def is_not_greater_than(
@register_rule("row")
def is_in_range(
column: str | Column,
min_limit: int | datetime.date | datetime.datetime | str | Column | None = None,
max_limit: int | datetime.date | datetime.datetime | str | Column | None = None,
min_limit: int | float | datetime.date | datetime.datetime | str | Column | None = None,
max_limit: int | float | datetime.date | datetime.datetime | str | Column | None = None,
) -> Column:
"""Checks whether the values in the input column are in the provided limits (inclusive of both boundaries).

Expand Down Expand Up @@ -649,8 +649,8 @@ def is_in_range(
@register_rule("row")
def is_not_in_range(
column: str | Column,
min_limit: int | datetime.date | datetime.datetime | str | Column | None = None,
max_limit: int | datetime.date | datetime.datetime | str | Column | None = None,
min_limit: int | float | datetime.date | datetime.datetime | str | Column | None = None,
max_limit: int | float | datetime.date | datetime.datetime | str | Column | None = None,
) -> Column:
"""Checks whether the values in the input column are outside the provided limits (inclusive of both boundaries).

Expand Down
54 changes: 34 additions & 20 deletions tests/integration/test_row_checks.py
Original file line number Diff line number Diff line change
Expand Up @@ -804,15 +804,15 @@ def test_col_is_not_greater_than(spark, set_utc_timezone):


def test_col_is_in_range(spark, set_utc_timezone):
schema_num = "a: int, b: date, c: timestamp, d: int, e: int, f: int, g: decimal(10,2), h: map<string, int>"
schema_num = "a: int, b: date, c: timestamp, d: int, e: int, f: int, g: decimal(10,2), h: map<string, int>, i:float"
test_df = spark.createDataFrame(
[
[0, datetime(2024, 12, 1).date(), datetime(2024, 12, 1), -1, 5, 6, Decimal("2.00"), {"val": 0}],
[1, datetime(2025, 1, 1).date(), datetime(2025, 1, 1), 2, 6, 3, Decimal("1.00"), {"val": 1}],
[2, datetime(2025, 2, 1).date(), datetime(2025, 2, 1), 2, 7, 3, Decimal("3.00"), {"val": 2}],
[3, datetime(2025, 3, 1).date(), datetime(2025, 3, 1), 3, 8, 3, Decimal("1.01"), {"val": 3}],
[4, datetime(2025, 4, 1).date(), datetime(2025, 4, 1), 2, 9, 3, Decimal("3.01"), {"val": 4}],
[None, None, None, None, None, None, None, {"val": None}],
[0, datetime(2024, 12, 1).date(), datetime(2024, 12, 1), -1, 5, 6, Decimal("2.00"), {"val": 0}, 0.0],
[1, datetime(2025, 1, 1).date(), datetime(2025, 1, 1), 2, 6, 3, Decimal("1.00"), {"val": 1}, 0.2],
[2, datetime(2025, 2, 1).date(), datetime(2025, 2, 1), 2, 7, 3, Decimal("3.00"), {"val": 2}, 0.4],
[3, datetime(2025, 3, 1).date(), datetime(2025, 3, 1), 3, 8, 3, Decimal("1.01"), {"val": 3}, 0.6],
[4, datetime(2025, 4, 1).date(), datetime(2025, 4, 1), 2, 9, 3, Decimal("3.01"), {"val": 4}, 0.8],
[None, None, None, None, None, None, None, {"val": None}, None],
],
schema_num,
)
Expand All @@ -827,12 +827,13 @@ def test_col_is_in_range(spark, set_utc_timezone):
is_in_range("f", "a", 5),
is_in_range("g", 1, 3),
is_in_range(F.col("h").getItem("val"), 1, 3),
is_in_range("i", 0.1, 0.7),
)

checked_schema = (
"a_not_in_range: string, b_not_in_range: string, c_not_in_range: string, "
"d_not_in_range: string, f_not_in_range: string, g_not_in_range: string, "
"unresolvedextractvalue_h_val_not_in_range: string"
"unresolvedextractvalue_h_val_not_in_range: string, i_not_in_range: string"
)
expected = spark.createDataFrame(
[
Expand All @@ -844,10 +845,11 @@ def test_col_is_in_range(spark, set_utc_timezone):
"Value '6' in Column 'f' not in range: [0, 5]",
None,
"Value '0' in Column 'UnresolvedExtractValue(h, val)' not in range: [1, 3]",
"Value '0.0' in Column 'i' not in range: [0.1, 0.7]",
],
[None, None, None, None, None, None, None],
[None, None, None, None, None, None, None],
[None, None, None, None, None, None, None],
[None, None, None, None, None, None, None, None],
[None, None, None, None, None, None, None, None],
[None, None, None, None, None, None, None, None],
[
"Value '4' in Column 'a' not in range: [1, 3]",
"Value '2025-04-01' in Column 'b' not in range: [2025-01-01, 2025-03-01]",
Expand All @@ -856,8 +858,9 @@ def test_col_is_in_range(spark, set_utc_timezone):
"Value '3' in Column 'f' not in range: [4, 5]",
"Value '3.01' in Column 'g' not in range: [1, 3]",
"Value '4' in Column 'UnresolvedExtractValue(h, val)' not in range: [1, 3]",
"Value '0.8' in Column 'i' not in range: [0.1, 0.7]",
],
[None, None, None, None, None, None, None],
[None, None, None, None, None, None, None, None],
],
checked_schema,
)
Expand All @@ -866,13 +869,21 @@ def test_col_is_in_range(spark, set_utc_timezone):


def test_col_is_not_in_range(spark, set_utc_timezone):
schema_num = "a: int, b: date, c: timestamp, d: timestamp, e: decimal(10,2), f: array<int>"
schema_num = "a: int, b: date, c: timestamp, d: timestamp, e: decimal(10,2), f: array<int>, g: float"
test_df = spark.createDataFrame(
[
[0, datetime(2024, 12, 31).date(), datetime(2025, 1, 4), datetime(2025, 1, 7), Decimal("0.99"), [0, 1]],
[1, datetime(2025, 1, 1).date(), datetime(2025, 1, 3), datetime(2025, 1, 1), Decimal("1.00"), [1, 2]],
[3, datetime(2025, 2, 1).date(), datetime(2025, 2, 1), datetime(2025, 2, 3), Decimal("3.00"), [3, 4]],
[None, None, None, None, None, [None, 1]],
[
0,
datetime(2024, 12, 31).date(),
datetime(2025, 1, 4),
datetime(2025, 1, 7),
Decimal("0.99"),
[0, 1],
0.0,
],
[1, datetime(2025, 1, 1).date(), datetime(2025, 1, 3), datetime(2025, 1, 1), Decimal("1.00"), [1, 2], 0.3],
[3, datetime(2025, 2, 1).date(), datetime(2025, 2, 1), datetime(2025, 2, 3), Decimal("3.00"), [3, 4], 0.6],
[None, None, None, None, None, [None, 1], None],
],
schema_num,
)
Expand All @@ -886,22 +897,24 @@ def test_col_is_not_in_range(spark, set_utc_timezone):
is_not_in_range("d", "c", F.expr("cast(b as timestamp) + INTERVAL 2 DAY")),
is_not_in_range("e", 1, 3),
is_not_in_range(F.try_element_at("f", F.lit(1)), 1, 3),
is_not_in_range("g", 0.2, 0.5),
)

checked_schema = (
"a_in_range: string, b_in_range: string, c_in_range: string, d_in_range: string, e_in_range: string, "
"try_element_at_f_1_in_range: string"
"try_element_at_f_1_in_range: string, g_in_range: string"
)
expected = spark.createDataFrame(
[
[None, None, None, None, None, None],
[None, None, None, None, None, None, None],
[
"Value '1' in Column 'a' in range: [1, 3]",
"Value '2025-01-01' in Column 'b' in range: [2025-01-01, 2025-01-03]",
"Value '2025-01-03 00:00:00' in Column 'c' in range: [2025-01-01 00:00:00, 2025-01-03 00:00:00]",
None,
"Value '1.00' in Column 'e' in range: [1, 3]",
"Value '1' in Column 'try_element_at(f, 1)' in range: [1, 3]",
"Value '0.3' in Column 'g' in range: [0.2, 0.5]",
],
[
"Value '3' in Column 'a' in range: [1, 3]",
Expand All @@ -910,8 +923,9 @@ def test_col_is_not_in_range(spark, set_utc_timezone):
"Value '2025-02-03 00:00:00' in Column 'd' in range: [2025-02-01 00:00:00, 2025-02-03 00:00:00]",
"Value '3.00' in Column 'e' in range: [1, 3]",
"Value '3' in Column 'try_element_at(f, 1)' in range: [1, 3]",
None,
],
[None, None, None, None, None, None],
[None, None, None, None, None, None, None],
],
checked_schema,
)
Expand Down
23 changes: 23 additions & 0 deletions tests/unit/test_checks_validation.py
Original file line number Diff line number Diff line change
Expand Up @@ -433,3 +433,26 @@ def test_argument_type_list_mismatch_args():
"Item 1 in argument 'columns' should be of type 'str | pyspark.sql.column.Column' "
"for function 'is_unique' in the 'arguments' block" in str(status)
)


def test_is_in_range_float_arguments():
checks = [
{
"criticality": "warn",
"check": {"function": "is_in_range", "arguments": {"column": "a", "min_limit": 1.5, "max_limit": 2.5}},
},
{
"criticality": "warn",
"check": {"function": "is_in_range", "arguments": {"column": "b", "min_limit": 0.1, "max_limit": 0.9}},
},
{
"criticality": "warn",
"check": {"function": "is_not_in_range", "arguments": {"column": "c", "min_limit": 1.5, "max_limit": 2.5}},
},
{
"criticality": "warn",
"check": {"function": "is_not_in_range", "arguments": {"column": "c", "min_limit": 0.1, "max_limit": 0.9}},
},
]
status = DQEngine.validate_checks(checks)
assert not status.has_errors