Skip to content

Commit

Permalink
fix: casting 'False' to boolean returns True
Browse files Browse the repository at this point in the history
  • Loading branch information
hantmac committed Jun 17, 2024
1 parent fdcf6f6 commit 0cd039a
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 1 deletion.
8 changes: 7 additions & 1 deletion databend_py/datetypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ def type_convert_fn(type_str: str):
elif DOUBLETYPE in type_str.lower():
return float
elif BOOLEANTYPE in type_str.lower():
return bool
return str_to_bool
elif MAPTYPE in type_str.lower():
return ast.literal_eval
elif ARRAYTYPE in type_str.lower():
Expand All @@ -34,6 +34,12 @@ def type_convert_fn(type_str: str):
return str


def str_to_bool(s):
if isinstance(s, str) and s.isdigit():
return bool(int(s))
return bool(s)


if __name__ == '__main__':
d = DatabendDataType()
print(d.type_convert_fn("Uint64")('0'))
6 changes: 6 additions & 0 deletions tests/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,11 @@ def test_rollback(self):
_, data = client.execute("select * from test_rollback")
self.assertEqual(data, [])

def test_cast_bool(self):
client = Client.from_url(self.databend_url)
_, data = client.execute("select 'False'::boolean union select 'True'::boolean")
self.assertEqual(data, [(True,), (False,)])


if __name__ == '__main__':
print("start test......")
Expand All @@ -269,4 +274,5 @@ def test_rollback(self):
dt.test_cookies()
dt.test_null_to_none()
dt.tearDown()
dt.test_cast_bool()
print("end test.....")

0 comments on commit 0cd039a

Please sign in to comment.