Skip to content

Commit 9a39125

Browse files
committed
Normalize database decimal values for strict imports
1 parent 8d55573 commit 9a39125

2 files changed

Lines changed: 62 additions & 1 deletion

File tree

‎src/openstatspec/sql/wide.py‎

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
from datetime import UTC, datetime
99
from contextlib import contextmanager
1010
from uuid import uuid4
11+
from decimal import Decimal
1112
from collections.abc import Iterable, Mapping
1213
from typing import Any
1314

@@ -711,6 +712,26 @@ def _bounded_batches(
711712
yield batch
712713

713714

715+
def _canonicalize_database_numeric_rows(
716+
rows: Iterable[Mapping[str, Any]],
717+
variables: Iterable[Mapping[str, Any]],
718+
) -> list[dict[str, Any]]:
719+
"""Normalize finite DB-driver Decimal wrappers to binary64 values."""
720+
numeric_names = {
721+
str(variable["physical_name"])
722+
for variable in variables
723+
if variable.get("storage_kind") == "numeric"
724+
}
725+
normalized = []
726+
for source in rows:
727+
row = dict(source)
728+
for name in numeric_names:
729+
if isinstance(row.get(name), Decimal) and row[name].is_finite():
730+
row[name] = float(row[name])
731+
normalized.append(row)
732+
return normalized
733+
734+
714735
def data_table_name(dataset_id: str) -> str:
715736
stem = _IDENTIFIER.sub("_", dataset_id).strip("_").lower() or "dataset"
716737
return f"data_{stem[:48]}"
@@ -755,7 +776,7 @@ def create_wide_dataset(
755776
operation_id = str(uuid4())
756777
normative_dataset_id = str(uuid4())
757778
fidelity_events = tuple(fidelity_events)
758-
source_rows = list(rows)
779+
source_rows = _canonicalize_database_numeric_rows(rows, variables)
759780
try:
760781
preflight(profile, variables, rows=source_rows)
761782
validate_spss_catalog(

‎tests/test_sql_profiles.py‎

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
from dataclasses import replace
2+
from decimal import Decimal
23
import os
34
from types import SimpleNamespace
45

@@ -666,6 +667,45 @@ def test_mysql_preflight_matches_emitted_text_limit() -> None:
666667
assert error.value.details["maximum"] == 65_535
667668

668669

670+
def test_database_decimal_numeric_wrappers_are_restored_to_binary64() -> None:
671+
variables = [{"physical_name": "score", "storage_kind": "numeric"}]
672+
673+
rows = wide._canonicalize_database_numeric_rows(
674+
[
675+
{"score": Decimal("1.5000000000"), "name": "alpha"},
676+
{"score": None, "name": "missing"},
677+
],
678+
variables,
679+
)
680+
681+
assert rows == [
682+
{"score": 1.5, "name": "alpha"},
683+
{"score": None, "name": "missing"},
684+
]
685+
assert isinstance(rows[0]["score"], float)
686+
687+
688+
@pytest.mark.parametrize(
689+
"value",
690+
[Decimal("NaN"), Decimal("Infinity"), Decimal("-Infinity")],
691+
)
692+
def test_database_decimal_nonfinite_wrappers_remain_rejected(value) -> None:
693+
variables = [{
694+
"ordinal": 1,
695+
"source_name": "score",
696+
"physical_name": "score",
697+
"storage_kind": "numeric",
698+
}]
699+
rows = wide._canonicalize_database_numeric_rows(
700+
[{"score": value}], variables,
701+
)
702+
703+
assert rows[0]["score"] is value
704+
with pytest.raises(UnsupportedOperationError) as error:
705+
preflight(MYSQL, variables, rows=rows)
706+
assert error.value.details["reason"] == "numeric_value_type"
707+
708+
669709
@pytest.mark.parametrize(
670710
("claimed_supported", "driver_eligible"),
671711
[(False, True), (True, False)],

0 commit comments

Comments
 (0)