This is an automated email from the ASF dual-hosted git repository.

fokko pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/iceberg.git


The following commit(s) were added to refs/heads/master by this push:
     new 370a43e26f [Python Legacy] Convert string to boolean if the binding 
variable is Boolean (#6843)
370a43e26f is described below

commit 370a43e26f8602ea4ed82a24295856211a7974f1
Author: Pritam <[email protected]>
AuthorDate: Thu Feb 16 11:01:15 2023 +0000

    [Python Legacy] Convert string to boolean if the binding variable is 
Boolean (#6843)
---
 python_legacy/iceberg/api/expressions/literals.py              |  3 +++
 .../tests/api/expressions/test_string_literal_conversions.py   | 10 +++++++++-
 2 files changed, 12 insertions(+), 1 deletion(-)

diff --git a/python_legacy/iceberg/api/expressions/literals.py 
b/python_legacy/iceberg/api/expressions/literals.py
index 41387ce2bc..483ff8f61e 100644
--- a/python_legacy/iceberg/api/expressions/literals.py
+++ b/python_legacy/iceberg/api/expressions/literals.py
@@ -382,6 +382,7 @@ class StringLiteral(BaseLiteral):
         super(StringLiteral, self).__init__(value, TypeID.STRING)
 
     def to(self, type_var):  # noqa: C901
+        value_upper = self.value.upper()
         import dateutil.parser
         if type_var.type_id == TypeID.DATE:
             return DateLiteral((dateutil.parser.parse(self.value) - 
Literals.EPOCH).days)
@@ -414,6 +415,8 @@ class StringLiteral(BaseLiteral):
                     return DecimalLiteral(Decimal(str(self.value))
                                           .quantize(Decimal("." + "".join(["0" 
for i in range(1, type_var.scale)]) + "1"),
                                                     rounding=ROUND_HALF_UP))
+        elif type_var.type_id == TypeID.BOOLEAN and value_upper in ["TRUE", 
"FALSE"]:
+            return BooleanLiteral(value_upper == "TRUE")
 
     def __eq__(self, other):
         if id(self) == id(other):
diff --git 
a/python_legacy/tests/api/expressions/test_string_literal_conversions.py 
b/python_legacy/tests/api/expressions/test_string_literal_conversions.py
index 57c2140af9..6103b96f2d 100644
--- a/python_legacy/tests/api/expressions/test_string_literal_conversions.py
+++ b/python_legacy/tests/api/expressions/test_string_literal_conversions.py
@@ -22,7 +22,8 @@ import uuid
 import dateutil.parser
 from fastavro.write import LOGICAL_WRITERS as avro_conversion
 from iceberg.api.expressions import Literal
-from iceberg.api.types import (DateType,
+from iceberg.api.types import (BooleanType,
+                               DateType,
                                DecimalType,
                                StringType,
                                TimestampType,
@@ -100,3 +101,10 @@ def test_string_to_decimal_literal():
 
     assert decimal_str.to(DecimalType.of(9, 2)) is None
     assert decimal_str.to(DecimalType.of(9, 4)) is None
+
+
+def test_string_to_boolean_literal():
+    assert Literal.of(True) == Literal.of("true").to(BooleanType.get())
+    assert Literal.of(True) == Literal.of("True").to(BooleanType.get())
+    assert Literal.of(False) == Literal.of("false").to(BooleanType.get())
+    assert Literal.of(False) == Literal.of("False").to(BooleanType.get())

Reply via email to