Repository: googleapis/python-spanner-django

Issue:
django: filtering on annotation with Decimal value crashes with "ValueError: Unknown type"
Similar to #168...
```
======================================================================
ERROR: test_filter_decimal_annotation (annotations.tests.NonAggregateAnnotationTestCase)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/home/tim/code/django/tests/annotations/tests.py", line 265, in test_filter_decimal_annotation
    self.assertEqual(qs.get(), (Decimal(31),))
  File "/home/tim/code/django/django/db/models/query.py", line 402, in get
    num = len(clone)
  File "/home/tim/code/django/django/db/models/query.py", line 256, in __len__
    self._fetch_all()
  File "/home/tim/code/django/django/db/models/query.py", line 1242, in _fetch_all
    self._result_cache = list(self._iterable_class(self))
  File "/home/tim/code/django/django/db/models/query.py", line 144, in __iter__
    return compiler.results_iter(tuple_expected=True, chunked_fetch=self.chunked_fetch, chunk_size=self.chunk_size)
  File "/home/tim/code/django/django/db/models/sql/compiler.py", line 1085, in results_iter
    results = self.execute_sql(MULTI, chunked_fetch=chunked_fetch, chunk_size=chunk_size)
  File "/home/tim/code/django/django/db/models/sql/compiler.py", line 1133, in execute_sql
    cursor.execute(sql, params)
  File "/home/tim/code/django/django/db/backends/utils.py", line 67, in execute
    return self._execute_with_wrappers(sql, params, many=False, executor=self._execute)
  File "/home/tim/code/django/django/db/backends/utils.py", line 76, in _execute_with_wrappers
    return executor(sql, params, many, context)
  File "/home/tim/code/django/django/db/backends/utils.py", line 84, in _execute
    return self.cursor.execute(sql, params)
  File "/home/tim/code/spanner-orm/spanner/dbapi/cursor.py", line 96, in execute
    param_types=param_types,
  File "/home/tim/code/spanner-orm/spanner/dbapi/cursor.py", line 178, in __handle_DQL
    res = snapshot.execute_sql(sql, params=params, param_types=param_types)
  File "/home/tim/.virtualenvs/django37/lib/python3.7/site-packages/google/cloud/spanner_v1/snapshot.py", line 204, in execute_sql
    fields={key: _make_value_pb(value) for key, value in params.items()}
  File "/home/tim/.virtualenvs/django37/lib/python3.7/site-packages/google/cloud/spanner_v1/snapshot.py", line 204, in <dictcomp>
    fields={key: _make_value_pb(value) for key, value in params.items()}
  File "/home/tim/.virtualenvs/django37/lib/python3.7/site-packages/google/cloud/spanner_v1/_helpers.py", line 91, in _make_value_pb
    raise ValueError("Unknown type: %s" % (value,))
ValueError: Unknown type: 31
```


Discussion:
The issue here is that a Decimal is being passed directly and converted to a float.

Here is an isolated repro for the problem
```python
SQL: SELECT (annotations_book.price + @a0) AS new_price FROM
          annotations_book WHERE (annotations_book.price + @a1) = @a2
Params: {'a0': 1, 'a1': 1.0, 'a2': Decimal('31')}
ParamTypes: {'a0': code: INT64, 'a1': code: FLOAT64}
``` 

and somehow that value isn't getting invoked by spannder/django/operations.DatabaseOperations.adapt_decimalfield.

I've printed out what gets invoked by adapt_decimalfield and that value of 31 doesn't show up
```shell
adapting decimal value: 30.00
adapting decimal value: 23.09
adapting decimal value: 29.69
adapting decimal value: 29.69
adapting decimal value: 82.80
adapting decimal value: 75.00
```

Resolving patch:
diff --git a/spanner/dbapi/parse_utils.py b/spanner/dbapi/parse_utils.py
--- a/spanner/dbapi/parse_utils.py
+++ b/spanner/dbapi/parse_utils.py
@@ -12,6 +12,7 @@
 # See the License for the specific language governing permissions and
 # limitations under the License.
 
+import decimal
 import re
 from urllib.parse import urlparse
 
@@ -412,11 +413,19 @@ def sql_pyformat_args_to_spanner(sql, params):
             resolved_value = pyfmt % params
             named_args[key] = resolved_value
         else:
-            named_args[key] = params[i]
+            named_args[key] = cast_for_spanner(params[i])
 
     return sql, named_args
 
 
+def cast_for_spanner(param):
+    """Convert param to its Cloud Spanner equivalent type."""
+    if isinstance(param, decimal.Decimal):
+        return float(param)
+    else:
+        return param
+
+
 def infer_param_types(params, param_types):
     # Given that we now format datetime as a Spanner TimeStamp,
     # i.e. in ISO 8601 format, we need to give Cloud Spanner a
diff --git a/spanner/django/features.py b/spanner/django/features.py
--- a/spanner/django/features.py
+++ b/spanner/django/features.py
@@ -78,9 +78,6 @@ class DatabaseFeatures(BaseDatabaseFeatures):
         # https://github.com/orijtech/spanner-orm/issues/170
         'model_fields.test_datetimefield.DateTimeFieldTests.test_lookup_date_with_use_tz',
         'model_fields.test_datetimefield.DateTimeFieldTests.test_lookup_date_without_use_tz',
-        # filtering on annotation with Decimal value crashes:
-        # https://github.com/orijtech/spanner-orm/issues/210
-        'annotations.tests.NonAggregateAnnotationTestCase.test_filter_decimal_annotation',
         # using NULL with + crashes: https://github.com/orijtech/spanner-orm/issues/201
         'annotations.tests.NonAggregateAnnotationTestCase.test_combined_annotation_commutative',
         # Spanner loses DecimalField precision due to conversion to float:


Test patch:
diff --git a/tests/spanner/dbapi/test_parse_utils.py b/tests/spanner/dbapi/test_parse_utils.py
--- a/tests/spanner/dbapi/test_parse_utils.py
+++ b/tests/spanner/dbapi/test_parse_utils.py
@@ -12,6 +12,7 @@
 # See the License for the specific language governing permissions and
 # limitations under the License.
 
+import decimal
 from unittest import TestCase
 
 from google.cloud.spanner_v1 import param_types
@@ -305,6 +306,10 @@ def test_sql_pyformat_args_to_spanner(self):
                 ('SELECT * from t WHERE id=10', {'f1': 'app', 'f2': 'name'}),
                 ('SELECT * from t WHERE id=10', {'f1': 'app', 'f2': 'name'}),
             ),
+            (
+                ('SELECT (an.p + %s) AS np FROM an WHERE (an.p + %s) = %s', (1, 1.0, decimal.Decimal('31'),)),
+                ('SELECT (an.p + @a0) AS np FROM an WHERE (an.p + @a1) = @a2', {'a0': 1, 'a1': 1.0, 'a2': 31.0}),
+            ),
         ]
         for ((sql_in, params), sql_want) in cases:
             with self.subTest(sql=sql_in):