Skip to content
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

Decimal128Field raise decimal.InvalidOperation error occurs under certain circumstances #212

Open
euynus opened this issue Jun 12, 2024 · 1 comment

Comments

@euynus
Copy link

euynus commented Jun 12, 2024

When initializing a model with a Decimal128Field in the clickhouse_orm library, a decimal.InvalidOperation error occurs under certain circumstances.

Reproducible Example:

class TestDecimalModel(ClickHouseModel):
    value = fields.Decimal128Field(scale=30, default=0)

a = TestDecimalModel(value=1)

Expected Behavior:

I expected the model TestDecimalModel to be initialized without any errors.

Environment Details:

Python version: 3.8.16
clickhouse_orm version: 2.1.0
Operating System: macOS 14.5

Additional Context:

The error seems to occur specifically when using a Decimal128Field with certain parameters. This issue might be related to how the to_python method handles the value.

from python docs

Unlike hardware based binary floating point, the decimal module has a user alterable precision (defaulting to 28 places) which can be as large as needed for a given problem:

@euynus
Copy link
Author

euynus commented Jun 12, 2024

Additional Context:

The following code can fix this.
I want to know if this is the best practice for fixing this issue.

class DecimalField(Field):
    '''
    Base class for all decimal fields. Can also be used directly.
    '''

    ...
    def _round(self, value):
        with localcontext() as ctx:
           ctx.prec = self.precision
           return value.quantize(self.exp)
    ...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant