You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@iceberg.apache.org by GitBox <gi...@apache.org> on 2022/11/28 19:16:58 UTC

[GitHub] [iceberg] Fokko commented on a diff in pull request #6259: Python: Add boolean expression parser

Fokko commented on code in PR #6259:
URL: https://github.com/apache/iceberg/pull/6259#discussion_r1033946989


##########
python/pyiceberg/expressions/literals.py:
##########
@@ -86,6 +86,8 @@ def __hash__(self) -> int:
         return hash(self.value)
 
     def __eq__(self, other: Any) -> bool:
+        if not isinstance(other, Literal):

Review Comment:
   If we get something else, we could wrap it in a literal and compare it. This would allow `literal(1) == 1`.



##########
python/pyiceberg/expressions/literals.py:
##########
@@ -401,6 +403,22 @@ def _(self, type_var: DecimalType) -> Literal[Decimal]:
             return self
         raise ValueError(f"Could not convert {self.value} into a {type_var}")
 
+    @to.register(IntegerType)
+    def _(self, _: IntegerType) -> Literal[int]:
+        return LongLiteral(int(self.value.to_integral_value()))
+
+    @to.register(LongType)
+    def _(self, _: LongType) -> Literal[int]:
+        return LongLiteral(int(self.value.to_integral_value()))

Review Comment:
   We want to check if they are below or above max.



##########
python/pyiceberg/expressions/literals.py:
##########
@@ -401,6 +403,22 @@ def _(self, type_var: DecimalType) -> Literal[Decimal]:
             return self
         raise ValueError(f"Could not convert {self.value} into a {type_var}")
 
+    @to.register(IntegerType)
+    def _(self, _: IntegerType) -> Literal[int]:
+        return LongLiteral(int(self.value.to_integral_value()))

Review Comment:
   We want to check if they are below or above max.



##########
python/pyiceberg/expressions/parser.py:
##########
@@ -0,0 +1,237 @@
+#  Licensed to the Apache Software Foundation (ASF) under one
+#  or more contributor license agreements.  See the NOTICE file
+#  distributed with this work for additional information
+#  regarding copyright ownership.  The ASF licenses this file
+#  to you under the Apache License, Version 2.0 (the
+#  "License"); you may not use this file except in compliance
+#  with the License.  You may obtain a copy of the License at
+#
+#    http://www.apache.org/licenses/LICENSE-2.0
+#
+#  Unless required by applicable law or agreed to in writing,
+#  software distributed under the License is distributed on an
+#  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+#  KIND, either express or implied.  See the License for the
+#  specific language governing permissions and limitations
+#  under the License.
+from decimal import Decimal
+
+from pyparsing import (
+    CaselessKeyword,
+    Group,
+    ParserElement,
+    ParseResults,
+    Suppress,
+    Word,
+    alphanums,
+    alphas,
+    delimited_list,
+    infix_notation,
+    one_of,
+    opAssoc,
+    sgl_quoted_string,
+)
+from pyparsing.common import pyparsing_common as common
+
+from pyiceberg.expressions import (
+    AlwaysFalse,
+    AlwaysTrue,
+    And,
+    BooleanExpression,
+    EqualTo,
+    GreaterThan,
+    GreaterThanOrEqual,
+    In,
+    IsNaN,
+    IsNull,
+    LessThan,
+    LessThanOrEqual,
+    Not,
+    NotEqualTo,
+    NotIn,
+    NotNaN,
+    NotNull,
+    Or,
+    Reference,
+)
+from pyiceberg.expressions.literals import (
+    DecimalLiteral,
+    Literal,
+    LongLiteral,
+    StringLiteral,
+)
+from pyiceberg.typedef import L
+
+ParserElement.enablePackrat()
+
+AND = CaselessKeyword("and")
+OR = CaselessKeyword("or")
+NOT = CaselessKeyword("not")
+IS = CaselessKeyword("is")
+IN = CaselessKeyword("in")
+NULL = CaselessKeyword("null")
+NAN = CaselessKeyword("nan")
+
+identifier = Word(alphas, alphanums + "_$").set_results_name("identifier")

Review Comment:
   ```suggestion
   identifier = Word(alphas, f"{alphanums}_$").set_results_name("identifier")
   ```



##########
python/pyiceberg/expressions/parser.py:
##########
@@ -0,0 +1,237 @@
+#  Licensed to the Apache Software Foundation (ASF) under one
+#  or more contributor license agreements.  See the NOTICE file
+#  distributed with this work for additional information
+#  regarding copyright ownership.  The ASF licenses this file
+#  to you under the Apache License, Version 2.0 (the
+#  "License"); you may not use this file except in compliance
+#  with the License.  You may obtain a copy of the License at
+#
+#    http://www.apache.org/licenses/LICENSE-2.0
+#
+#  Unless required by applicable law or agreed to in writing,
+#  software distributed under the License is distributed on an
+#  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+#  KIND, either express or implied.  See the License for the
+#  specific language governing permissions and limitations
+#  under the License.
+from decimal import Decimal
+
+from pyparsing import (
+    CaselessKeyword,
+    Group,
+    ParserElement,
+    ParseResults,
+    Suppress,
+    Word,
+    alphanums,
+    alphas,
+    delimited_list,
+    infix_notation,
+    one_of,
+    opAssoc,
+    sgl_quoted_string,
+)
+from pyparsing.common import pyparsing_common as common
+
+from pyiceberg.expressions import (
+    AlwaysFalse,
+    AlwaysTrue,
+    And,
+    BooleanExpression,
+    EqualTo,
+    GreaterThan,
+    GreaterThanOrEqual,
+    In,
+    IsNaN,
+    IsNull,
+    LessThan,
+    LessThanOrEqual,
+    Not,
+    NotEqualTo,
+    NotIn,
+    NotNaN,
+    NotNull,
+    Or,
+    Reference,
+)
+from pyiceberg.expressions.literals import (
+    DecimalLiteral,
+    Literal,
+    LongLiteral,
+    StringLiteral,
+)
+from pyiceberg.typedef import L
+
+ParserElement.enablePackrat()
+
+AND = CaselessKeyword("and")
+OR = CaselessKeyword("or")
+NOT = CaselessKeyword("not")
+IS = CaselessKeyword("is")
+IN = CaselessKeyword("in")
+NULL = CaselessKeyword("null")
+NAN = CaselessKeyword("nan")
+
+identifier = Word(alphas, alphanums + "_$").set_results_name("identifier")
+column = delimited_list(identifier, delim=".", combine=True).set_results_name("column")
+
+
+@column.set_parse_action
+def _(result: ParseResults):

Review Comment:
   ```suggestion
   def _(result: ParseResults) -> Reference[Any]:
   ```



##########
python/pyiceberg/expressions/parser.py:
##########
@@ -0,0 +1,237 @@
+#  Licensed to the Apache Software Foundation (ASF) under one
+#  or more contributor license agreements.  See the NOTICE file
+#  distributed with this work for additional information
+#  regarding copyright ownership.  The ASF licenses this file
+#  to you under the Apache License, Version 2.0 (the
+#  "License"); you may not use this file except in compliance
+#  with the License.  You may obtain a copy of the License at
+#
+#    http://www.apache.org/licenses/LICENSE-2.0
+#
+#  Unless required by applicable law or agreed to in writing,
+#  software distributed under the License is distributed on an
+#  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+#  KIND, either express or implied.  See the License for the
+#  specific language governing permissions and limitations
+#  under the License.
+from decimal import Decimal
+
+from pyparsing import (
+    CaselessKeyword,
+    Group,
+    ParserElement,
+    ParseResults,
+    Suppress,
+    Word,
+    alphanums,
+    alphas,
+    delimited_list,
+    infix_notation,
+    one_of,
+    opAssoc,
+    sgl_quoted_string,
+)
+from pyparsing.common import pyparsing_common as common
+
+from pyiceberg.expressions import (
+    AlwaysFalse,
+    AlwaysTrue,
+    And,
+    BooleanExpression,
+    EqualTo,
+    GreaterThan,
+    GreaterThanOrEqual,
+    In,
+    IsNaN,
+    IsNull,
+    LessThan,
+    LessThanOrEqual,
+    Not,
+    NotEqualTo,
+    NotIn,
+    NotNaN,
+    NotNull,
+    Or,
+    Reference,
+)
+from pyiceberg.expressions.literals import (
+    DecimalLiteral,
+    Literal,
+    LongLiteral,
+    StringLiteral,
+)
+from pyiceberg.typedef import L
+
+ParserElement.enablePackrat()
+
+AND = CaselessKeyword("and")
+OR = CaselessKeyword("or")
+NOT = CaselessKeyword("not")
+IS = CaselessKeyword("is")
+IN = CaselessKeyword("in")
+NULL = CaselessKeyword("null")
+NAN = CaselessKeyword("nan")
+
+identifier = Word(alphas, alphanums + "_$").set_results_name("identifier")
+column = delimited_list(identifier, delim=".", combine=True).set_results_name("column")
+
+
+@column.set_parse_action
+def _(result: ParseResults):
+    return Reference(result.column[0])
+
+
+boolean = one_of(["true", "false"], caseless=True).set_results_name("boolean")
+string = sgl_quoted_string.set_results_name("raw_quoted_string")
+decimal = common.real().set_results_name("decimal")
+integer = common.signed_integer().set_results_name("integer")
+literal = Group(string | decimal | integer).set_results_name("literal")
+literal_set = Group(delimited_list(string) | delimited_list(decimal) | delimited_list(integer)).set_results_name("literal_set")
+
+
+@boolean.set_parse_action
+def _(result: ParseResults) -> BooleanExpression:
+    if "true" == result.boolean.lower():
+        return AlwaysTrue()
+    else:
+        return AlwaysFalse()
+
+
+@string.set_parse_action
+def _(result: ParseResults) -> Literal[str]:
+    return StringLiteral(result.raw_quoted_string[1:-1].replace("''", "'"))
+
+
+@decimal.set_parse_action
+def _(result: ParseResults) -> Literal[Decimal]:
+    return DecimalLiteral(Decimal(result.decimal))
+
+
+@integer.set_parse_action
+def _(result: ParseResults) -> Literal[int]:
+    return LongLiteral(int(result.integer))
+
+
+@literal.set_parse_action
+def _(result: ParseResults) -> Literal[L]:
+    return result[0][0]
+
+
+@literal_set.set_parse_action
+def _(result: ParseResults) -> Literal[L]:
+    return result[0]
+
+
+comparison_op = one_of(["<", "<=", ">", ">=", "=", "==", "!=", "<>"], caseless=True).set_results_name("op")
+left_ref = column + comparison_op + literal
+right_ref = literal + comparison_op + column
+comparison = left_ref | right_ref
+
+
+@left_ref.set_parse_action
+def _(result: ParseResults) -> BooleanExpression:
+    if result.op == "<":
+        return LessThan(result.column, result.literal)
+    elif result.op == "<=":
+        return LessThanOrEqual(result.column, result.literal)
+    elif result.op == ">":
+        return GreaterThan(result.column, result.literal)
+    elif result.op == ">=":
+        return GreaterThanOrEqual(result.column, result.literal)
+    if result.op in ("=", "=="):

Review Comment:
   ```suggestion
       elif result.op in ("=", "=="):
   ```



##########
python/pyiceberg/expressions/parser.py:
##########
@@ -0,0 +1,237 @@
+#  Licensed to the Apache Software Foundation (ASF) under one
+#  or more contributor license agreements.  See the NOTICE file
+#  distributed with this work for additional information
+#  regarding copyright ownership.  The ASF licenses this file
+#  to you under the Apache License, Version 2.0 (the
+#  "License"); you may not use this file except in compliance
+#  with the License.  You may obtain a copy of the License at
+#
+#    http://www.apache.org/licenses/LICENSE-2.0
+#
+#  Unless required by applicable law or agreed to in writing,
+#  software distributed under the License is distributed on an
+#  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+#  KIND, either express or implied.  See the License for the
+#  specific language governing permissions and limitations
+#  under the License.
+from decimal import Decimal
+
+from pyparsing import (
+    CaselessKeyword,
+    Group,
+    ParserElement,
+    ParseResults,
+    Suppress,
+    Word,
+    alphanums,
+    alphas,
+    delimited_list,
+    infix_notation,
+    one_of,
+    opAssoc,
+    sgl_quoted_string,
+)
+from pyparsing.common import pyparsing_common as common
+
+from pyiceberg.expressions import (
+    AlwaysFalse,
+    AlwaysTrue,
+    And,
+    BooleanExpression,
+    EqualTo,
+    GreaterThan,
+    GreaterThanOrEqual,
+    In,
+    IsNaN,
+    IsNull,
+    LessThan,
+    LessThanOrEqual,
+    Not,
+    NotEqualTo,
+    NotIn,
+    NotNaN,
+    NotNull,
+    Or,
+    Reference,
+)
+from pyiceberg.expressions.literals import (
+    DecimalLiteral,
+    Literal,
+    LongLiteral,
+    StringLiteral,
+)
+from pyiceberg.typedef import L
+
+ParserElement.enablePackrat()
+
+AND = CaselessKeyword("and")
+OR = CaselessKeyword("or")
+NOT = CaselessKeyword("not")
+IS = CaselessKeyword("is")
+IN = CaselessKeyword("in")
+NULL = CaselessKeyword("null")
+NAN = CaselessKeyword("nan")
+
+identifier = Word(alphas, alphanums + "_$").set_results_name("identifier")
+column = delimited_list(identifier, delim=".", combine=True).set_results_name("column")
+
+
+@column.set_parse_action
+def _(result: ParseResults):
+    return Reference(result.column[0])
+
+
+boolean = one_of(["true", "false"], caseless=True).set_results_name("boolean")
+string = sgl_quoted_string.set_results_name("raw_quoted_string")
+decimal = common.real().set_results_name("decimal")
+integer = common.signed_integer().set_results_name("integer")
+literal = Group(string | decimal | integer).set_results_name("literal")
+literal_set = Group(delimited_list(string) | delimited_list(decimal) | delimited_list(integer)).set_results_name("literal_set")
+
+
+@boolean.set_parse_action
+def _(result: ParseResults) -> BooleanExpression:
+    if "true" == result.boolean.lower():

Review Comment:
   We could do this in a single line if we're feeling verbose today:
   ```suggestion
       return AlwaysTrue() if "true" == result.boolean.lower() else AlwaysFalse()
   ```



##########
python/pyiceberg/expressions/parser.py:
##########
@@ -0,0 +1,237 @@
+#  Licensed to the Apache Software Foundation (ASF) under one
+#  or more contributor license agreements.  See the NOTICE file
+#  distributed with this work for additional information
+#  regarding copyright ownership.  The ASF licenses this file
+#  to you under the Apache License, Version 2.0 (the
+#  "License"); you may not use this file except in compliance
+#  with the License.  You may obtain a copy of the License at
+#
+#    http://www.apache.org/licenses/LICENSE-2.0
+#
+#  Unless required by applicable law or agreed to in writing,
+#  software distributed under the License is distributed on an
+#  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+#  KIND, either express or implied.  See the License for the
+#  specific language governing permissions and limitations
+#  under the License.
+from decimal import Decimal
+
+from pyparsing import (
+    CaselessKeyword,
+    Group,
+    ParserElement,
+    ParseResults,
+    Suppress,
+    Word,
+    alphanums,
+    alphas,
+    delimited_list,
+    infix_notation,
+    one_of,
+    opAssoc,
+    sgl_quoted_string,
+)
+from pyparsing.common import pyparsing_common as common
+
+from pyiceberg.expressions import (
+    AlwaysFalse,
+    AlwaysTrue,
+    And,
+    BooleanExpression,
+    EqualTo,
+    GreaterThan,
+    GreaterThanOrEqual,
+    In,
+    IsNaN,
+    IsNull,
+    LessThan,
+    LessThanOrEqual,
+    Not,
+    NotEqualTo,
+    NotIn,
+    NotNaN,
+    NotNull,
+    Or,
+    Reference,
+)
+from pyiceberg.expressions.literals import (
+    DecimalLiteral,
+    Literal,
+    LongLiteral,
+    StringLiteral,
+)
+from pyiceberg.typedef import L
+
+ParserElement.enablePackrat()
+
+AND = CaselessKeyword("and")
+OR = CaselessKeyword("or")
+NOT = CaselessKeyword("not")
+IS = CaselessKeyword("is")
+IN = CaselessKeyword("in")
+NULL = CaselessKeyword("null")
+NAN = CaselessKeyword("nan")
+
+identifier = Word(alphas, alphanums + "_$").set_results_name("identifier")
+column = delimited_list(identifier, delim=".", combine=True).set_results_name("column")
+
+
+@column.set_parse_action
+def _(result: ParseResults):
+    return Reference(result.column[0])
+
+
+boolean = one_of(["true", "false"], caseless=True).set_results_name("boolean")
+string = sgl_quoted_string.set_results_name("raw_quoted_string")
+decimal = common.real().set_results_name("decimal")
+integer = common.signed_integer().set_results_name("integer")
+literal = Group(string | decimal | integer).set_results_name("literal")
+literal_set = Group(delimited_list(string) | delimited_list(decimal) | delimited_list(integer)).set_results_name("literal_set")
+
+
+@boolean.set_parse_action
+def _(result: ParseResults) -> BooleanExpression:
+    if "true" == result.boolean.lower():
+        return AlwaysTrue()
+    else:
+        return AlwaysFalse()
+
+
+@string.set_parse_action
+def _(result: ParseResults) -> Literal[str]:
+    return StringLiteral(result.raw_quoted_string[1:-1].replace("''", "'"))
+
+
+@decimal.set_parse_action
+def _(result: ParseResults) -> Literal[Decimal]:
+    return DecimalLiteral(Decimal(result.decimal))
+
+
+@integer.set_parse_action
+def _(result: ParseResults) -> Literal[int]:
+    return LongLiteral(int(result.integer))
+
+
+@literal.set_parse_action
+def _(result: ParseResults) -> Literal[L]:
+    return result[0][0]
+
+
+@literal_set.set_parse_action
+def _(result: ParseResults) -> Literal[L]:
+    return result[0]
+
+
+comparison_op = one_of(["<", "<=", ">", ">=", "=", "==", "!=", "<>"], caseless=True).set_results_name("op")
+left_ref = column + comparison_op + literal
+right_ref = literal + comparison_op + column
+comparison = left_ref | right_ref
+
+
+@left_ref.set_parse_action
+def _(result: ParseResults) -> BooleanExpression:
+    if result.op == "<":
+        return LessThan(result.column, result.literal)
+    elif result.op == "<=":
+        return LessThanOrEqual(result.column, result.literal)
+    elif result.op == ">":
+        return GreaterThan(result.column, result.literal)
+    elif result.op == ">=":
+        return GreaterThanOrEqual(result.column, result.literal)
+    if result.op in ("=", "=="):
+        return EqualTo(result.column, result.literal)
+    if result.op in ("!=", "<>"):

Review Comment:
   ```suggestion
       elif result.op in ("!=", "<>"):
   ```



##########
python/pyiceberg/expressions/parser.py:
##########
@@ -0,0 +1,237 @@
+#  Licensed to the Apache Software Foundation (ASF) under one
+#  or more contributor license agreements.  See the NOTICE file
+#  distributed with this work for additional information
+#  regarding copyright ownership.  The ASF licenses this file
+#  to you under the Apache License, Version 2.0 (the
+#  "License"); you may not use this file except in compliance
+#  with the License.  You may obtain a copy of the License at
+#
+#    http://www.apache.org/licenses/LICENSE-2.0
+#
+#  Unless required by applicable law or agreed to in writing,
+#  software distributed under the License is distributed on an
+#  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+#  KIND, either express or implied.  See the License for the
+#  specific language governing permissions and limitations
+#  under the License.
+from decimal import Decimal
+
+from pyparsing import (
+    CaselessKeyword,
+    Group,
+    ParserElement,
+    ParseResults,
+    Suppress,
+    Word,
+    alphanums,
+    alphas,
+    delimited_list,
+    infix_notation,
+    one_of,
+    opAssoc,
+    sgl_quoted_string,
+)
+from pyparsing.common import pyparsing_common as common
+
+from pyiceberg.expressions import (
+    AlwaysFalse,
+    AlwaysTrue,
+    And,
+    BooleanExpression,
+    EqualTo,
+    GreaterThan,
+    GreaterThanOrEqual,
+    In,
+    IsNaN,
+    IsNull,
+    LessThan,
+    LessThanOrEqual,
+    Not,
+    NotEqualTo,
+    NotIn,
+    NotNaN,
+    NotNull,
+    Or,
+    Reference,
+)
+from pyiceberg.expressions.literals import (
+    DecimalLiteral,
+    Literal,
+    LongLiteral,
+    StringLiteral,
+)
+from pyiceberg.typedef import L
+
+ParserElement.enablePackrat()
+
+AND = CaselessKeyword("and")
+OR = CaselessKeyword("or")
+NOT = CaselessKeyword("not")
+IS = CaselessKeyword("is")
+IN = CaselessKeyword("in")
+NULL = CaselessKeyword("null")
+NAN = CaselessKeyword("nan")
+
+identifier = Word(alphas, alphanums + "_$").set_results_name("identifier")
+column = delimited_list(identifier, delim=".", combine=True).set_results_name("column")
+
+
+@column.set_parse_action
+def _(result: ParseResults):
+    return Reference(result.column[0])
+
+
+boolean = one_of(["true", "false"], caseless=True).set_results_name("boolean")
+string = sgl_quoted_string.set_results_name("raw_quoted_string")
+decimal = common.real().set_results_name("decimal")
+integer = common.signed_integer().set_results_name("integer")
+literal = Group(string | decimal | integer).set_results_name("literal")
+literal_set = Group(delimited_list(string) | delimited_list(decimal) | delimited_list(integer)).set_results_name("literal_set")
+
+
+@boolean.set_parse_action
+def _(result: ParseResults) -> BooleanExpression:
+    if "true" == result.boolean.lower():
+        return AlwaysTrue()
+    else:
+        return AlwaysFalse()
+
+
+@string.set_parse_action
+def _(result: ParseResults) -> Literal[str]:
+    return StringLiteral(result.raw_quoted_string[1:-1].replace("''", "'"))
+
+
+@decimal.set_parse_action
+def _(result: ParseResults) -> Literal[Decimal]:
+    return DecimalLiteral(Decimal(result.decimal))
+
+
+@integer.set_parse_action
+def _(result: ParseResults) -> Literal[int]:
+    return LongLiteral(int(result.integer))
+
+
+@literal.set_parse_action
+def _(result: ParseResults) -> Literal[L]:
+    return result[0][0]
+
+
+@literal_set.set_parse_action
+def _(result: ParseResults) -> Literal[L]:
+    return result[0]
+
+
+comparison_op = one_of(["<", "<=", ">", ">=", "=", "==", "!=", "<>"], caseless=True).set_results_name("op")
+left_ref = column + comparison_op + literal
+right_ref = literal + comparison_op + column
+comparison = left_ref | right_ref
+
+
+@left_ref.set_parse_action
+def _(result: ParseResults) -> BooleanExpression:
+    if result.op == "<":
+        return LessThan(result.column, result.literal)
+    elif result.op == "<=":
+        return LessThanOrEqual(result.column, result.literal)
+    elif result.op == ">":
+        return GreaterThan(result.column, result.literal)
+    elif result.op == ">=":
+        return GreaterThanOrEqual(result.column, result.literal)
+    if result.op in ("=", "=="):
+        return EqualTo(result.column, result.literal)
+    if result.op in ("!=", "<>"):
+        return NotEqualTo(result.column, result.literal)
+    raise ValueError(f"Unsupported operation type: {result.op}")
+
+
+@right_ref.set_parse_action
+def _(result: ParseResults) -> BooleanExpression:
+    if result.op == "<":
+        return GreaterThan(result.column, result.literal)
+    elif result.op == "<=":
+        return GreaterThanOrEqual(result.column, result.literal)
+    elif result.op == ">":
+        return LessThan(result.column, result.literal)
+    elif result.op == ">=":
+        return LessThanOrEqual(result.column, result.literal)
+    if result.op in ("=", "=="):
+        return EqualTo(result.column, result.literal)
+    if result.op in ("!=", "<>"):

Review Comment:
   ```suggestion
       elif result.op in ("!=", "<>"):
   ```



##########
python/pyiceberg/expressions/parser.py:
##########
@@ -0,0 +1,237 @@
+#  Licensed to the Apache Software Foundation (ASF) under one
+#  or more contributor license agreements.  See the NOTICE file
+#  distributed with this work for additional information
+#  regarding copyright ownership.  The ASF licenses this file
+#  to you under the Apache License, Version 2.0 (the
+#  "License"); you may not use this file except in compliance
+#  with the License.  You may obtain a copy of the License at
+#
+#    http://www.apache.org/licenses/LICENSE-2.0
+#
+#  Unless required by applicable law or agreed to in writing,
+#  software distributed under the License is distributed on an
+#  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+#  KIND, either express or implied.  See the License for the
+#  specific language governing permissions and limitations
+#  under the License.
+from decimal import Decimal
+
+from pyparsing import (
+    CaselessKeyword,
+    Group,
+    ParserElement,
+    ParseResults,
+    Suppress,
+    Word,
+    alphanums,
+    alphas,
+    delimited_list,
+    infix_notation,
+    one_of,
+    opAssoc,
+    sgl_quoted_string,
+)
+from pyparsing.common import pyparsing_common as common
+
+from pyiceberg.expressions import (
+    AlwaysFalse,
+    AlwaysTrue,
+    And,
+    BooleanExpression,
+    EqualTo,
+    GreaterThan,
+    GreaterThanOrEqual,
+    In,
+    IsNaN,
+    IsNull,
+    LessThan,
+    LessThanOrEqual,
+    Not,
+    NotEqualTo,
+    NotIn,
+    NotNaN,
+    NotNull,
+    Or,
+    Reference,
+)
+from pyiceberg.expressions.literals import (
+    DecimalLiteral,
+    Literal,
+    LongLiteral,
+    StringLiteral,
+)
+from pyiceberg.typedef import L
+
+ParserElement.enablePackrat()
+
+AND = CaselessKeyword("and")
+OR = CaselessKeyword("or")
+NOT = CaselessKeyword("not")
+IS = CaselessKeyword("is")
+IN = CaselessKeyword("in")
+NULL = CaselessKeyword("null")
+NAN = CaselessKeyword("nan")
+
+identifier = Word(alphas, alphanums + "_$").set_results_name("identifier")
+column = delimited_list(identifier, delim=".", combine=True).set_results_name("column")
+
+
+@column.set_parse_action
+def _(result: ParseResults):
+    return Reference(result.column[0])
+
+
+boolean = one_of(["true", "false"], caseless=True).set_results_name("boolean")
+string = sgl_quoted_string.set_results_name("raw_quoted_string")
+decimal = common.real().set_results_name("decimal")
+integer = common.signed_integer().set_results_name("integer")
+literal = Group(string | decimal | integer).set_results_name("literal")
+literal_set = Group(delimited_list(string) | delimited_list(decimal) | delimited_list(integer)).set_results_name("literal_set")
+
+
+@boolean.set_parse_action
+def _(result: ParseResults) -> BooleanExpression:
+    if "true" == result.boolean.lower():
+        return AlwaysTrue()
+    else:
+        return AlwaysFalse()
+
+
+@string.set_parse_action
+def _(result: ParseResults) -> Literal[str]:
+    return StringLiteral(result.raw_quoted_string[1:-1].replace("''", "'"))
+
+
+@decimal.set_parse_action
+def _(result: ParseResults) -> Literal[Decimal]:
+    return DecimalLiteral(Decimal(result.decimal))
+
+
+@integer.set_parse_action
+def _(result: ParseResults) -> Literal[int]:
+    return LongLiteral(int(result.integer))
+
+
+@literal.set_parse_action
+def _(result: ParseResults) -> Literal[L]:
+    return result[0][0]
+
+
+@literal_set.set_parse_action
+def _(result: ParseResults) -> Literal[L]:
+    return result[0]
+
+
+comparison_op = one_of(["<", "<=", ">", ">=", "=", "==", "!=", "<>"], caseless=True).set_results_name("op")
+left_ref = column + comparison_op + literal
+right_ref = literal + comparison_op + column
+comparison = left_ref | right_ref
+
+
+@left_ref.set_parse_action
+def _(result: ParseResults) -> BooleanExpression:
+    if result.op == "<":
+        return LessThan(result.column, result.literal)
+    elif result.op == "<=":
+        return LessThanOrEqual(result.column, result.literal)
+    elif result.op == ">":
+        return GreaterThan(result.column, result.literal)
+    elif result.op == ">=":
+        return GreaterThanOrEqual(result.column, result.literal)
+    if result.op in ("=", "=="):
+        return EqualTo(result.column, result.literal)
+    if result.op in ("!=", "<>"):
+        return NotEqualTo(result.column, result.literal)
+    raise ValueError(f"Unsupported operation type: {result.op}")
+
+
+@right_ref.set_parse_action
+def _(result: ParseResults) -> BooleanExpression:
+    if result.op == "<":
+        return GreaterThan(result.column, result.literal)
+    elif result.op == "<=":
+        return GreaterThanOrEqual(result.column, result.literal)
+    elif result.op == ">":
+        return LessThan(result.column, result.literal)
+    elif result.op == ">=":
+        return LessThanOrEqual(result.column, result.literal)
+    if result.op in ("=", "=="):
+        return EqualTo(result.column, result.literal)
+    if result.op in ("!=", "<>"):
+        return NotEqualTo(result.column, result.literal)
+    raise ValueError(f"Unsupported operation type: {result.op}")
+
+
+is_null = column + IS + NULL
+not_null = column + IS + NOT + NULL
+null_check = is_null | not_null
+
+
+@is_null.set_parse_action
+def _(result: ParseResults) -> BooleanExpression:
+    return IsNull(result.column)
+
+
+@not_null.set_parse_action
+def _(result: ParseResults) -> BooleanExpression:
+    return NotNull(result.column)
+
+
+is_nan = column + IS + NAN
+not_nan = column + IS + NOT + NAN
+nan_check = is_nan | not_nan
+
+
+@is_nan.set_parse_action
+def _(result: ParseResults) -> BooleanExpression:
+    return IsNaN(result.column)
+
+
+@not_nan.set_parse_action
+def _(result: ParseResults) -> BooleanExpression:
+    return NotNaN(result.column)
+
+
+is_in = column + IN + "(" + literal_set + ")"

Review Comment:
   ```suggestion
   is_in = f"{column}{IN}({literal_set})"
   ```



##########
python/pyiceberg/expressions/parser.py:
##########
@@ -0,0 +1,237 @@
+#  Licensed to the Apache Software Foundation (ASF) under one
+#  or more contributor license agreements.  See the NOTICE file
+#  distributed with this work for additional information
+#  regarding copyright ownership.  The ASF licenses this file
+#  to you under the Apache License, Version 2.0 (the
+#  "License"); you may not use this file except in compliance
+#  with the License.  You may obtain a copy of the License at
+#
+#    http://www.apache.org/licenses/LICENSE-2.0
+#
+#  Unless required by applicable law or agreed to in writing,
+#  software distributed under the License is distributed on an
+#  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+#  KIND, either express or implied.  See the License for the
+#  specific language governing permissions and limitations
+#  under the License.
+from decimal import Decimal
+
+from pyparsing import (
+    CaselessKeyword,
+    Group,
+    ParserElement,
+    ParseResults,
+    Suppress,
+    Word,
+    alphanums,
+    alphas,
+    delimited_list,
+    infix_notation,
+    one_of,
+    opAssoc,
+    sgl_quoted_string,
+)
+from pyparsing.common import pyparsing_common as common
+
+from pyiceberg.expressions import (
+    AlwaysFalse,
+    AlwaysTrue,
+    And,
+    BooleanExpression,
+    EqualTo,
+    GreaterThan,
+    GreaterThanOrEqual,
+    In,
+    IsNaN,
+    IsNull,
+    LessThan,
+    LessThanOrEqual,
+    Not,
+    NotEqualTo,
+    NotIn,
+    NotNaN,
+    NotNull,
+    Or,
+    Reference,
+)
+from pyiceberg.expressions.literals import (
+    DecimalLiteral,
+    Literal,
+    LongLiteral,
+    StringLiteral,
+)
+from pyiceberg.typedef import L
+
+ParserElement.enablePackrat()
+
+AND = CaselessKeyword("and")
+OR = CaselessKeyword("or")
+NOT = CaselessKeyword("not")
+IS = CaselessKeyword("is")
+IN = CaselessKeyword("in")
+NULL = CaselessKeyword("null")
+NAN = CaselessKeyword("nan")
+
+identifier = Word(alphas, alphanums + "_$").set_results_name("identifier")
+column = delimited_list(identifier, delim=".", combine=True).set_results_name("column")
+
+
+@column.set_parse_action
+def _(result: ParseResults):
+    return Reference(result.column[0])
+
+
+boolean = one_of(["true", "false"], caseless=True).set_results_name("boolean")
+string = sgl_quoted_string.set_results_name("raw_quoted_string")
+decimal = common.real().set_results_name("decimal")
+integer = common.signed_integer().set_results_name("integer")
+literal = Group(string | decimal | integer).set_results_name("literal")
+literal_set = Group(delimited_list(string) | delimited_list(decimal) | delimited_list(integer)).set_results_name("literal_set")
+
+
+@boolean.set_parse_action
+def _(result: ParseResults) -> BooleanExpression:
+    if "true" == result.boolean.lower():
+        return AlwaysTrue()
+    else:
+        return AlwaysFalse()
+
+
+@string.set_parse_action
+def _(result: ParseResults) -> Literal[str]:
+    return StringLiteral(result.raw_quoted_string[1:-1].replace("''", "'"))
+
+
+@decimal.set_parse_action
+def _(result: ParseResults) -> Literal[Decimal]:
+    return DecimalLiteral(Decimal(result.decimal))
+
+
+@integer.set_parse_action
+def _(result: ParseResults) -> Literal[int]:
+    return LongLiteral(int(result.integer))
+
+
+@literal.set_parse_action
+def _(result: ParseResults) -> Literal[L]:
+    return result[0][0]
+
+
+@literal_set.set_parse_action
+def _(result: ParseResults) -> Literal[L]:
+    return result[0]
+
+
+comparison_op = one_of(["<", "<=", ">", ">=", "=", "==", "!=", "<>"], caseless=True).set_results_name("op")
+left_ref = column + comparison_op + literal
+right_ref = literal + comparison_op + column
+comparison = left_ref | right_ref
+
+
+@left_ref.set_parse_action
+def _(result: ParseResults) -> BooleanExpression:
+    if result.op == "<":
+        return LessThan(result.column, result.literal)
+    elif result.op == "<=":
+        return LessThanOrEqual(result.column, result.literal)
+    elif result.op == ">":
+        return GreaterThan(result.column, result.literal)
+    elif result.op == ">=":
+        return GreaterThanOrEqual(result.column, result.literal)
+    if result.op in ("=", "=="):
+        return EqualTo(result.column, result.literal)
+    if result.op in ("!=", "<>"):
+        return NotEqualTo(result.column, result.literal)
+    raise ValueError(f"Unsupported operation type: {result.op}")
+
+
+@right_ref.set_parse_action
+def _(result: ParseResults) -> BooleanExpression:
+    if result.op == "<":
+        return GreaterThan(result.column, result.literal)
+    elif result.op == "<=":
+        return GreaterThanOrEqual(result.column, result.literal)
+    elif result.op == ">":
+        return LessThan(result.column, result.literal)
+    elif result.op == ">=":
+        return LessThanOrEqual(result.column, result.literal)
+    if result.op in ("=", "=="):
+        return EqualTo(result.column, result.literal)
+    if result.op in ("!=", "<>"):
+        return NotEqualTo(result.column, result.literal)
+    raise ValueError(f"Unsupported operation type: {result.op}")
+
+
+is_null = column + IS + NULL
+not_null = column + IS + NOT + NULL
+null_check = is_null | not_null
+
+
+@is_null.set_parse_action
+def _(result: ParseResults) -> BooleanExpression:
+    return IsNull(result.column)
+
+
+@not_null.set_parse_action
+def _(result: ParseResults) -> BooleanExpression:
+    return NotNull(result.column)
+
+
+is_nan = column + IS + NAN
+not_nan = column + IS + NOT + NAN
+nan_check = is_nan | not_nan
+
+
+@is_nan.set_parse_action
+def _(result: ParseResults) -> BooleanExpression:
+    return IsNaN(result.column)
+
+
+@not_nan.set_parse_action
+def _(result: ParseResults) -> BooleanExpression:
+    return NotNaN(result.column)
+
+
+is_in = column + IN + "(" + literal_set + ")"
+not_in = column + NOT + IN + "(" + literal_set + ")"

Review Comment:
   ```suggestion
   not_in = f"{column}{NOT}{IN}({literal_set})"
   ```



##########
python/pyiceberg/expressions/literals.py:
##########
@@ -401,6 +403,22 @@ def _(self, type_var: DecimalType) -> Literal[Decimal]:
             return self
         raise ValueError(f"Could not convert {self.value} into a {type_var}")
 
+    @to.register(IntegerType)
+    def _(self, _: IntegerType) -> Literal[int]:
+        return LongLiteral(int(self.value.to_integral_value()))
+
+    @to.register(LongType)
+    def _(self, _: LongType) -> Literal[int]:
+        return LongLiteral(int(self.value.to_integral_value()))
+
+    @to.register(FloatType)
+    def _(self, _: FloatType):

Review Comment:
   We want to check if they are below or above max.



##########
python/pyiceberg/expressions/parser.py:
##########
@@ -0,0 +1,237 @@
+#  Licensed to the Apache Software Foundation (ASF) under one
+#  or more contributor license agreements.  See the NOTICE file
+#  distributed with this work for additional information
+#  regarding copyright ownership.  The ASF licenses this file
+#  to you under the Apache License, Version 2.0 (the
+#  "License"); you may not use this file except in compliance
+#  with the License.  You may obtain a copy of the License at
+#
+#    http://www.apache.org/licenses/LICENSE-2.0
+#
+#  Unless required by applicable law or agreed to in writing,
+#  software distributed under the License is distributed on an
+#  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+#  KIND, either express or implied.  See the License for the
+#  specific language governing permissions and limitations
+#  under the License.
+from decimal import Decimal
+
+from pyparsing import (
+    CaselessKeyword,
+    Group,
+    ParserElement,
+    ParseResults,
+    Suppress,
+    Word,
+    alphanums,
+    alphas,
+    delimited_list,
+    infix_notation,
+    one_of,
+    opAssoc,
+    sgl_quoted_string,
+)
+from pyparsing.common import pyparsing_common as common
+
+from pyiceberg.expressions import (
+    AlwaysFalse,
+    AlwaysTrue,
+    And,
+    BooleanExpression,
+    EqualTo,
+    GreaterThan,
+    GreaterThanOrEqual,
+    In,
+    IsNaN,
+    IsNull,
+    LessThan,
+    LessThanOrEqual,
+    Not,
+    NotEqualTo,
+    NotIn,
+    NotNaN,
+    NotNull,
+    Or,
+    Reference,
+)
+from pyiceberg.expressions.literals import (
+    DecimalLiteral,
+    Literal,
+    LongLiteral,
+    StringLiteral,
+)
+from pyiceberg.typedef import L
+
+ParserElement.enablePackrat()
+
+AND = CaselessKeyword("and")
+OR = CaselessKeyword("or")
+NOT = CaselessKeyword("not")
+IS = CaselessKeyword("is")
+IN = CaselessKeyword("in")
+NULL = CaselessKeyword("null")
+NAN = CaselessKeyword("nan")
+
+identifier = Word(alphas, alphanums + "_$").set_results_name("identifier")
+column = delimited_list(identifier, delim=".", combine=True).set_results_name("column")
+
+
+@column.set_parse_action
+def _(result: ParseResults):
+    return Reference(result.column[0])
+
+
+boolean = one_of(["true", "false"], caseless=True).set_results_name("boolean")
+string = sgl_quoted_string.set_results_name("raw_quoted_string")
+decimal = common.real().set_results_name("decimal")
+integer = common.signed_integer().set_results_name("integer")
+literal = Group(string | decimal | integer).set_results_name("literal")
+literal_set = Group(delimited_list(string) | delimited_list(decimal) | delimited_list(integer)).set_results_name("literal_set")
+
+
+@boolean.set_parse_action
+def _(result: ParseResults) -> BooleanExpression:
+    if "true" == result.boolean.lower():
+        return AlwaysTrue()
+    else:
+        return AlwaysFalse()
+
+
+@string.set_parse_action
+def _(result: ParseResults) -> Literal[str]:
+    return StringLiteral(result.raw_quoted_string[1:-1].replace("''", "'"))
+
+
+@decimal.set_parse_action
+def _(result: ParseResults) -> Literal[Decimal]:
+    return DecimalLiteral(Decimal(result.decimal))
+
+
+@integer.set_parse_action
+def _(result: ParseResults) -> Literal[int]:
+    return LongLiteral(int(result.integer))
+
+
+@literal.set_parse_action
+def _(result: ParseResults) -> Literal[L]:
+    return result[0][0]
+
+
+@literal_set.set_parse_action
+def _(result: ParseResults) -> Literal[L]:
+    return result[0]
+
+
+comparison_op = one_of(["<", "<=", ">", ">=", "=", "==", "!=", "<>"], caseless=True).set_results_name("op")
+left_ref = column + comparison_op + literal
+right_ref = literal + comparison_op + column
+comparison = left_ref | right_ref
+
+
+@left_ref.set_parse_action
+def _(result: ParseResults) -> BooleanExpression:
+    if result.op == "<":
+        return LessThan(result.column, result.literal)
+    elif result.op == "<=":
+        return LessThanOrEqual(result.column, result.literal)
+    elif result.op == ">":
+        return GreaterThan(result.column, result.literal)
+    elif result.op == ">=":
+        return GreaterThanOrEqual(result.column, result.literal)
+    if result.op in ("=", "=="):

Review Comment:
   Do we want to move these operators into module-level variables? I also noticed that we have the same groups below.



##########
python/pyiceberg/expressions/parser.py:
##########
@@ -0,0 +1,237 @@
+#  Licensed to the Apache Software Foundation (ASF) under one
+#  or more contributor license agreements.  See the NOTICE file
+#  distributed with this work for additional information
+#  regarding copyright ownership.  The ASF licenses this file
+#  to you under the Apache License, Version 2.0 (the
+#  "License"); you may not use this file except in compliance
+#  with the License.  You may obtain a copy of the License at
+#
+#    http://www.apache.org/licenses/LICENSE-2.0
+#
+#  Unless required by applicable law or agreed to in writing,
+#  software distributed under the License is distributed on an
+#  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+#  KIND, either express or implied.  See the License for the
+#  specific language governing permissions and limitations
+#  under the License.
+from decimal import Decimal
+
+from pyparsing import (
+    CaselessKeyword,
+    Group,
+    ParserElement,
+    ParseResults,
+    Suppress,
+    Word,
+    alphanums,
+    alphas,
+    delimited_list,
+    infix_notation,
+    one_of,
+    opAssoc,
+    sgl_quoted_string,
+)
+from pyparsing.common import pyparsing_common as common
+
+from pyiceberg.expressions import (
+    AlwaysFalse,
+    AlwaysTrue,
+    And,
+    BooleanExpression,
+    EqualTo,
+    GreaterThan,
+    GreaterThanOrEqual,
+    In,
+    IsNaN,
+    IsNull,
+    LessThan,
+    LessThanOrEqual,
+    Not,
+    NotEqualTo,
+    NotIn,
+    NotNaN,
+    NotNull,
+    Or,
+    Reference,
+)
+from pyiceberg.expressions.literals import (
+    DecimalLiteral,
+    Literal,
+    LongLiteral,
+    StringLiteral,
+)
+from pyiceberg.typedef import L
+
+ParserElement.enablePackrat()
+
+AND = CaselessKeyword("and")
+OR = CaselessKeyword("or")
+NOT = CaselessKeyword("not")
+IS = CaselessKeyword("is")
+IN = CaselessKeyword("in")
+NULL = CaselessKeyword("null")
+NAN = CaselessKeyword("nan")
+
+identifier = Word(alphas, alphanums + "_$").set_results_name("identifier")
+column = delimited_list(identifier, delim=".", combine=True).set_results_name("column")
+
+
+@column.set_parse_action
+def _(result: ParseResults):
+    return Reference(result.column[0])
+
+
+boolean = one_of(["true", "false"], caseless=True).set_results_name("boolean")
+string = sgl_quoted_string.set_results_name("raw_quoted_string")
+decimal = common.real().set_results_name("decimal")
+integer = common.signed_integer().set_results_name("integer")
+literal = Group(string | decimal | integer).set_results_name("literal")
+literal_set = Group(delimited_list(string) | delimited_list(decimal) | delimited_list(integer)).set_results_name("literal_set")
+
+
+@boolean.set_parse_action
+def _(result: ParseResults) -> BooleanExpression:
+    if "true" == result.boolean.lower():
+        return AlwaysTrue()
+    else:
+        return AlwaysFalse()
+
+
+@string.set_parse_action
+def _(result: ParseResults) -> Literal[str]:
+    return StringLiteral(result.raw_quoted_string[1:-1].replace("''", "'"))
+
+
+@decimal.set_parse_action
+def _(result: ParseResults) -> Literal[Decimal]:
+    return DecimalLiteral(Decimal(result.decimal))
+
+
+@integer.set_parse_action
+def _(result: ParseResults) -> Literal[int]:
+    return LongLiteral(int(result.integer))
+
+
+@literal.set_parse_action
+def _(result: ParseResults) -> Literal[L]:
+    return result[0][0]
+
+
+@literal_set.set_parse_action
+def _(result: ParseResults) -> Literal[L]:
+    return result[0]
+
+
+comparison_op = one_of(["<", "<=", ">", ">=", "=", "==", "!=", "<>"], caseless=True).set_results_name("op")
+left_ref = column + comparison_op + literal
+right_ref = literal + comparison_op + column
+comparison = left_ref | right_ref
+
+
+@left_ref.set_parse_action
+def _(result: ParseResults) -> BooleanExpression:
+    if result.op == "<":
+        return LessThan(result.column, result.literal)
+    elif result.op == "<=":
+        return LessThanOrEqual(result.column, result.literal)
+    elif result.op == ">":
+        return GreaterThan(result.column, result.literal)
+    elif result.op == ">=":
+        return GreaterThanOrEqual(result.column, result.literal)
+    if result.op in ("=", "=="):
+        return EqualTo(result.column, result.literal)
+    if result.op in ("!=", "<>"):
+        return NotEqualTo(result.column, result.literal)
+    raise ValueError(f"Unsupported operation type: {result.op}")
+
+
+@right_ref.set_parse_action
+def _(result: ParseResults) -> BooleanExpression:
+    if result.op == "<":
+        return GreaterThan(result.column, result.literal)
+    elif result.op == "<=":
+        return GreaterThanOrEqual(result.column, result.literal)
+    elif result.op == ">":
+        return LessThan(result.column, result.literal)
+    elif result.op == ">=":
+        return LessThanOrEqual(result.column, result.literal)
+    if result.op in ("=", "=="):

Review Comment:
   ```suggestion
       elif result.op in ("=", "=="):
   ```



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@iceberg.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@iceberg.apache.org
For additional commands, e-mail: issues-help@iceberg.apache.org