Skip to content

Commit 28ba321

Browse files
VHenrik007vhenrikz
andauthored
Type f not supported bug fix (#81)
* Type `f` not supported bug fix While ImmuDB supports `FLOAT` types in data tables when working with SQL, the Python SDK had missing implementations for this. For more detail see issue issue-76. Signed-off-by: Henrik <[email protected]> * Float type tests A simpler and a more comprehensive test covering the insertion, querying, aggregation and filtering features for floating point numbers using SQL. * Float test with immudb version check Float got introduced in immudb version 1.5.0: https://github.com/codenotary/immudb/blob/v1.5.0/CHANGELOG.md#features Currently tests run on older versions as well, so this version check is added as seen in `test_sql_verify.py` for example, where it was handled simply by returning. --------- Signed-off-by: Henrik <[email protected]> Co-authored-by: Henrik <[email protected]>
1 parent fb76484 commit 28ba321

File tree

2 files changed

+71
-0
lines changed

2 files changed

+71
-0
lines changed

immudb/typeconv.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
from pprint import pformat
33
from datetime import datetime, timezone
44
from immudb.embedded.store import KVMetadata
5+
import decimal
56

67

78
def py_to_sqlvalue(value):
@@ -20,6 +21,8 @@ def py_to_sqlvalue(value):
2021
elif typ is datetime:
2122
sqlValue = schema_pb2.SQLValue(
2223
ts=int(value.timestamp()*1e6))
24+
elif typ in (float, decimal.Decimal):
25+
sqlValue = schema_pb2.SQLValue(f=float(value))
2326
else:
2427
raise TypeError("Type not supported: {}".format(
2528
value.__class__.__name__))
@@ -37,6 +40,8 @@ def sqlvalue_to_py(sqlValue):
3740
return sqlValue.s
3841
elif sqlValue.HasField("ts"):
3942
return datetime.fromtimestamp(sqlValue.ts/1e6, timezone.utc)
43+
elif sqlValue.HasField("f"):
44+
return sqlValue.f
4045
elif sqlValue.HasField("null"):
4146
return None
4247
else:

tests/immu/test_sql_float.py

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
# Copyright 2022 CodeNotary, Inc. All rights reserved.
2+
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
# http://www.apache.org/licenses/LICENSE-2.0
7+
# Unless required by applicable law or agreed to in writing, software
8+
# distributed under the License is distributed on an "AS IS" BASIS,
9+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10+
# See the License for the specific language governing permissions and
11+
# limitations under the License.
12+
13+
from tests.immuTestClient import ImmuTestClient
14+
import sys
15+
16+
17+
class TestSQLFloat:
18+
19+
def test_sql_float_query(self, wrappedClient: ImmuTestClient):
20+
# Testing the FLOAT type inserting and querying
21+
if (not wrappedClient.serverHigherOrEqualsToVersion("1.5.0")):
22+
return
23+
24+
value_to_test = 1.1
25+
26+
tabname = wrappedClient.createTestTable(
27+
"id INTEGER AUTO_INCREMENT", "tester FLOAT", "PRIMARY KEY id"
28+
)
29+
wrappedClient.insertToTable(
30+
tabname, ["tester"], ["@test"], {"test": value_to_test}
31+
)
32+
result = wrappedClient.simpleSelect(
33+
tabname, ["tester"], {"testvalue": value_to_test},
34+
"tester=@testvalue"
35+
)
36+
37+
assert (len(result) > 0)
38+
assert (result[0][0] == value_to_test)
39+
40+
def test_sql_float_aggreg_and_filter(self, wrappedClient: ImmuTestClient):
41+
# Testing the FLOAT type with aggregation and filtering
42+
if (not wrappedClient.serverHigherOrEqualsToVersion("1.5.0")):
43+
return
44+
45+
values_to_test = [1.1, 2.2, 3.3, 4.4]
46+
47+
tabname = wrappedClient.createTestTable(
48+
"id INTEGER AUTO_INCREMENT", "tester FLOAT", "PRIMARY KEY id"
49+
)
50+
51+
for val in values_to_test:
52+
wrappedClient.insertToTable(
53+
tabname, ["tester"], ["@test"], {"test": val}
54+
)
55+
56+
result = wrappedClient.simpleSelect(
57+
tabname,
58+
["AVG(tester)"],
59+
{"min_val": values_to_test[1] + 0.1},
60+
"tester > @min_val"
61+
)
62+
63+
assert (len(result) == 1)
64+
avg_val = result[0][0]
65+
expected_avg = sum(values_to_test[2:]) / len(values_to_test[2:])
66+
assert (abs(avg_val - expected_avg) < sys.float_info.epsilon)

0 commit comments

Comments
 (0)