samredai commented on a change in pull request #4262:
URL: https://github.com/apache/iceberg/pull/4262#discussion_r820015707



##########
File path: python/tests/test_misc_literal_conversions.py
##########
@@ -0,0 +1,308 @@
+# 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.
+
+import sys
+import uuid
+from decimal import Decimal
+
+import pytest
+
+import iceberg.literals
+from iceberg.types import (
+    BinaryType,
+    BooleanType,
+    DateType,
+    DecimalType,
+    DoubleType,
+    FixedType,
+    FloatType,
+    IntegerType,
+    LongType,
+    StringType,
+    TimestampType,
+    TimestamptzType,
+    TimeType,
+    UUIDType,
+)
+
+
+def test_identity_conversions():
+    pairs = [
+        (iceberg.literals.of(True), BooleanType()),
+        (iceberg.literals.of(34), IntegerType()),
+        (iceberg.literals.of(3400000000), LongType()),
+        (iceberg.literals.of(34.11), FloatType()),
+        (iceberg.literals.of(34.11), DoubleType()),
+        (iceberg.literals.of(Decimal(34.55).quantize(Decimal("0.01"))), 
DecimalType(9, 2)),
+        (iceberg.literals.of("2017-08-18"), DateType()),
+        (iceberg.literals.of("14:21:01.919"), TimeType()),
+        (iceberg.literals.of("2017-08-18T14:21:01.919"), TimestampType()),
+        (iceberg.literals.of("abc"), StringType()),
+        (iceberg.literals.of(uuid.uuid4()), UUIDType()),
+    ]
+
+    if sys.version_info[0] >= 3:
+        pairs = pairs + [
+            (iceberg.literals.of(bytes([0x01, 0x02, 0x03])), FixedType(3)),
+            (iceberg.literals.of(bytearray([0x03, 0x04, 0x05, 0x06])), 
BinaryType()),
+        ]
+
+    for pair in pairs:
+        expected = pair[0].to(pair[1])
+        assert expected is expected.to(pair[1])
+
+
+def test_binary_to_fixed():
+    if sys.version_info[0] >= 3:
+        lit = iceberg.literals.of(bytearray([0x00, 0x01, 0x02]))
+        fixed_lit = lit.to(FixedType(3))
+        assert fixed_lit is not None
+        assert lit.value == fixed_lit.value
+        assert lit.to(FixedType(4)) is None
+        assert lit.to(FixedType(2)) is None
+
+
+def test_fixed_to_binary():
+    if sys.version_info[0] >= 3:
+        lit = iceberg.literals.of(bytes([0x00, 0x01, 0x02]))
+        binary_lit = lit.to(BinaryType())
+        assert binary_lit is not None
+        assert lit.value == binary_lit.value
+
+
+def test_invalid_boolean_conversions():
+    assert_invalid_conversions(
+        iceberg.literals.of(True),
+        [
+            IntegerType(),
+            LongType(),
+            FloatType(),
+            DoubleType(),
+            DateType(),
+            TimeType(),
+            TimestampType(),
+            TimestamptzType(),
+            DecimalType(9, 2),
+            StringType(),
+            UUIDType(),
+            FixedType(1),
+            BinaryType(),
+        ],
+    )
+
+
+def test_invalid_integer_conversions():
+    assert_invalid_conversions(
+        iceberg.literals.of(34),
+        [BooleanType(), TimeType(), TimestampType(), TimestamptzType(), 
StringType(), UUIDType(), FixedType(1), BinaryType()],
+    )
+
+
+def test_invalid_long_conversions():
+    assert_invalid_conversions(
+        iceberg.literals.of(34).to(LongType()), [BooleanType(), DateType(), 
StringType(), UUIDType(), FixedType(1), BinaryType()]
+    )
+
+
[email protected](
+    "lit",
+    [
+        iceberg.literals.of(34.11),
+        # double
+        iceberg.literals.of(34.11).to(DoubleType()),
+    ],
+)
[email protected](
+    "test_type",
+    [
+        BooleanType(),
+        IntegerType(),
+        LongType(),
+        DateType(),
+        TimeType(),
+        TimestampType(),
+        TimestamptzType(),
+        StringType(),
+        UUIDType(),
+        FixedType(1),
+        BinaryType(),
+    ],
+)
+def test_invalid_float_conversions(lit, test_type):
+    assert lit.to(test_type) is None
+
+
[email protected]("lit", 
[iceberg.literals.of("2017-08-18").to(DateType())])
[email protected](
+    "test_type",
+    [
+        BooleanType(),
+        IntegerType(),
+        LongType(),
+        FloatType(),
+        DoubleType(),
+        TimeType(),
+        TimestampType(),
+        TimestamptzType(),
+        DecimalType(9, 2),
+        StringType(),
+        UUIDType(),
+        FixedType(1),
+        BinaryType(),
+    ],
+)
+def test_invalid_datetime_conversions(lit, test_type):
+    assert_invalid_conversions(lit, (test_type,))
+
+
+def test_invalid_time_conversions():
+    assert_invalid_conversions(
+        iceberg.literals.of("14:21:01.919").to(TimeType()),
+        [
+            BooleanType(),
+            IntegerType(),
+            LongType(),
+            FloatType(),
+            DoubleType(),
+            DateType(),
+            TimestampType(),
+            TimestamptzType(),
+            DecimalType(9, 2),
+            StringType(),
+            UUIDType(),
+            FixedType(1),
+            BinaryType(),
+        ],
+    )
+
+
+def test_invalid_timestamp_conversions():
+    assert_invalid_conversions(
+        iceberg.literals.of("2017-08-18T14:21:01.919").to(TimestampType()),
+        [
+            BooleanType(),
+            IntegerType(),
+            LongType(),
+            FloatType(),
+            DoubleType(),
+            TimeType(),
+            DecimalType(9, 2),
+            StringType(),
+            UUIDType(),
+            FixedType(1),
+            BinaryType(),
+        ],
+    )
+
+
+def test_invalid_decimal_conversions():
+    assert_invalid_conversions(
+        iceberg.literals.of(Decimal("34.11")),
+        [
+            BooleanType(),
+            IntegerType(),
+            LongType(),
+            FloatType(),
+            DoubleType(),
+            DateType(),
+            TimeType(),
+            TimestampType(),
+            TimestamptzType(),
+            DecimalType(9, 4),
+            StringType(),
+            UUIDType(),
+            FixedType(1),
+            BinaryType(),
+        ],
+    )
+
+
+def test_invalid_string_conversions():
+    assert_invalid_conversions(
+        iceberg.literals.of("abc"),
+        [BooleanType(), IntegerType(), LongType(), FloatType(), DoubleType(), 
FixedType(1), BinaryType()],
+    )
+
+
+def test_invalid_uuid_conversions():
+    assert_invalid_conversions(
+        iceberg.literals.of(uuid.uuid4()),
+        [
+            BooleanType(),
+            IntegerType(),
+            LongType(),
+            FloatType(),
+            DoubleType(),
+            DateType(),
+            TimeType(),
+            TimestampType(),
+            TimestamptzType(),
+            DecimalType(9, 2),
+            StringType(),
+            FixedType(1),
+            BinaryType(),
+        ],
+    )
+
+
+def test_invalid_fixed_conversions():
+    if sys.version_info[0] >= 3:

Review comment:
       Good catch @kbendick, I think there was an agreement early on to support 
python 3.7+ and it's set in the 
[setup.cfg](https://github.com/apache/iceberg/blob/master/python/setup.cfg#L44) 
file. The current state of python packaging feels like it's in flux (maybe it 
always has been?) and it's hard to tell where the right place to set this stuff 
is. I **think** the current standard is to use the pyproject.toml+setup.cfg 
combo...
   
   @dramaticlly for these if statements you can definitely replace them with 
the assumption that python 3 is being used.




-- 
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: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]



---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to