S-C-O-U-T / Pyadomd

A pythonic approach to query SSAS data models.
https://pyadomd.readthedocs.io/en/latest/index.html
Apache License 2.0
25 stars 6 forks source link

Numeric 0's are read-in to Python from SSAS as None/NaN #6

Closed AlexHuang2 closed 3 years ago

AlexHuang2 commented 3 years ago

In _type_code.py, the function _option_type evaluates to None for, say, _option_type(datatype=float, data=0). This is generally incorrect--even though bool(0) evaluates to False for numeric 0s--as 0 is a valid value for float, and numeric fields in general. The same applies to how adomd_type_map is mapping System.Decimal as that is also a numeric field.

My proposed changes in _type_code.py are as follows:

to _option_type:

def _option_type(datatype, data):
    if data:
        return datatype(data)
    # Add in this clause to make sure numeric 0s are not mistakenly mapped
    elif datatype in [float, int] and (data==0):
        return datatype(0)
    else:
        return None

to adomd_type_map:

adomd_type_map:Dict[str, Type_code] = {
    'System.Boolean': Type_code(partial(_option_type, bool), bool.__name__),
    # Modify how decimals/doubles are handled to account for this as well
    'System.Decimal': Type_code(lambda x: Decimal.ToDouble(x) if x else (Decimal.ToDouble(0) if x==0 else None), float.__name__),
    'System.DateTime': Type_code(lambda x: datetime(x.Year, x.Month, x.Day, x.Hour, x.Minute, x.Second) if x else None, datetime.__name__),
    'System.Double': Type_code(partial(_option_type, float), float.__name__),
    'System.Int64': Type_code(partial(_option_type, int), int.__name__),
    'System.UInt64': Type_code(partial(_option_type, int), int.__name__),
    'System.String': Type_code(partial(_option_type, str), str.__name__),
    'System.Object': Type_code(lambda x: x, 'System.Object')
}

but I'm not sure this is the canonical "developer way" of doing it, so I will just leave this here.

S-C-O-U-T commented 3 years ago

Hi,

Thanks for pointing this one out.

I'll look into patching this.

br

S-C-O-U-T commented 3 years ago

A new version of the package is uploaded to PyPI, please update using:

pip install pyadomd --upgrade

Again thx for raising this - closing the issue