# WL#4370: Decimal support for NDB/Python

Affects: Connector/NDB-0.7   —   Status: Complete   —   Priority: Medium

```A sensible encoding/decoding approach needs to be taken for Decimal types in
Python, most likely mimicing the approach taken by MySQLdb if possible.```
```NDB/Python should return Decimal columns as instances of the python
decimal.Decimal class which is part of the standard library.

A Decimal can be created in the following way:

>>> Decimal('3.14')              # string input
Decimal("3.14")
>>> Decimal((0, (3, 1, 4), -2))  # tuple (sign, digit_tuple, exponent)
Decimal("3.14")
>>> Decimal(314)                 # int or long
Decimal("314")
>>> Decimal(Decimal(314))        # another decimal instance
Decimal("314")

The underlying decimal struct is:

typedef struct st_decimal_t {
int intg, frac, len;
my_bool sign;
decimal_digit_t *buf;
} decimal_t;

There are two options:

One, we could take the decimal_t, pull out the constituent parts, mapping the
array of *buf into a tuple and applying the frac portion into this form:

Decimal((0, (3, 1, 4), -2))

OR, we can use the decimal2string() function to create a string which we can
then pass to the Decimal('3.14') form of the constructor. Obviously, the second
is easier, but involves an intermediary conversion from decimal_t struct to char
* to Python String to Python Decimal. The first method will be more involved,
and would skip the creation of the char * - BUT, would still have to create a 2
Python ints, a Python tuple, and a list of python ints to fill the Python
tuple... so making the char * and Python string might not be more expensive.

For now, we'll do Option #2 - at some point in the future, anyone is free to
implement Option #1 and test the cost difference. ```