You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@iceberg.apache.org by fo...@apache.org on 2023/02/16 08:05:11 UTC

[iceberg] branch master updated: Python: Add String to Boolean literal conversion (#6851)

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 7d08f4f6fa Python: Add String to Boolean literal conversion (#6851)
7d08f4f6fa is described below

commit 7d08f4f6fa2748ad37a8c3f8046f2ad6a1913558
Author: Pritam <28...@users.noreply.github.com>
AuthorDate: Thu Feb 16 08:05:04 2023 +0000

    Python: Add String to Boolean literal conversion (#6851)
    
    * Convert string to boolean if the binding variable is Boolean
    
    * Update python/pyiceberg/expressions/literals.py
    
    Co-authored-by: Fokko Driesprong <fo...@apache.org>
    
    * addressed review comments
    
    * corrected lint failure
    
    * corrected test failure
    
    ---------
    
    Co-authored-by: pritampan <pr...@pinterest.com>
    Co-authored-by: Fokko Driesprong <fo...@apache.org>
---
 python/pyiceberg/expressions/literals.py  |  8 ++++++++
 python/tests/expressions/test_literals.py | 16 +++++++++++++++-
 2 files changed, 23 insertions(+), 1 deletion(-)

diff --git a/python/pyiceberg/expressions/literals.py b/python/pyiceberg/expressions/literals.py
index fcfaaf7fe9..3e3233af0d 100644
--- a/python/pyiceberg/expressions/literals.py
+++ b/python/pyiceberg/expressions/literals.py
@@ -562,6 +562,14 @@ class StringLiteral(Literal[str]):
                 f"Could not convert {self.value} into a {type_var}, scales differ {type_var.scale} <> {abs(dec.as_tuple().exponent)}"
             )
 
+    @to.register(BooleanType)
+    def _(self, type_var: BooleanType) -> Literal[bool]:
+        value_upper = self.value.upper()
+        if value_upper in ["TRUE", "FALSE"]:
+            return BooleanLiteral(value_upper == "TRUE")
+        else:
+            raise ValueError(f"Could not convert {self.value} into a {type_var}")
+
     def __repr__(self) -> str:
         return f"literal({repr(self.value)})"
 
diff --git a/python/tests/expressions/test_literals.py b/python/tests/expressions/test_literals.py
index 7373ff040b..16aee4dbc3 100644
--- a/python/tests/expressions/test_literals.py
+++ b/python/tests/expressions/test_literals.py
@@ -384,6 +384,20 @@ def test_string_to_decimal_literal() -> None:
     assert Decimal("34.560").as_tuple() == decimal_lit.value.as_tuple()  # type: ignore
 
 
+def test_string_to_boolean_literal() -> None:
+    assert literal(True) == literal("true").to(BooleanType())
+    assert literal(True) == literal("True").to(BooleanType())
+    assert literal(False) == literal("false").to(BooleanType())
+    assert literal(False) == literal("False").to(BooleanType())
+
+
+def test_invalid_string_to_boolean_literal() -> None:
+    invalid_boolean_str = literal("unknown")
+    with pytest.raises(ValueError) as e:
+        _ = invalid_boolean_str.to(BooleanType())
+    assert "Could not convert unknown into a boolean" in str(e.value)
+
+
 # MISC
 
 
@@ -692,7 +706,7 @@ def test_invalid_decimal_conversions() -> None:
 def test_invalid_string_conversions() -> None:
     assert_invalid_conversions(
         literal("abc"),
-        [BooleanType(), FloatType(), DoubleType(), FixedType(1), BinaryType()],
+        [FloatType(), DoubleType(), FixedType(1), BinaryType()],
     )