|
| 1 | +# Licensed to the Apache Software Foundation (ASF) under one |
| 2 | +# or more contributor license agreements. See the NOTICE file |
| 3 | +# distributed with this work for additional information |
| 4 | +# regarding copyright ownership. The ASF licenses this file |
| 5 | +# to you under the Apache License, Version 2.0 (the |
| 6 | +# "License"); you may not use this file except in compliance |
| 7 | +# with the License. You may obtain a copy of the License at |
| 8 | +# |
| 9 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +# |
| 11 | +# Unless required by applicable law or agreed to in writing, |
| 12 | +# software distributed under the License is distributed on an |
| 13 | +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 14 | +# KIND, either express or implied. See the License for the |
| 15 | +# specific language governing permissions and limitations |
| 16 | +# under the License. |
| 17 | + |
| 18 | +from datetime import date, datetime |
| 19 | + |
| 20 | +import pyarrow as pa |
| 21 | +import pytest |
| 22 | +from pyiceberg_core import ArrowArrayTransform |
| 23 | + |
| 24 | + |
| 25 | +def test_identity_transform(): |
| 26 | + arr = pa.array([1, 2]) |
| 27 | + result = ArrowArrayTransform.identity(arr) |
| 28 | + assert result == arr |
| 29 | + |
| 30 | + |
| 31 | +def test_bucket_transform(): |
| 32 | + arr = pa.array([1, 2]) |
| 33 | + result = ArrowArrayTransform.bucket(arr, 10) |
| 34 | + expected = pa.array([6, 2], type=pa.int32()) |
| 35 | + assert result == expected |
| 36 | + |
| 37 | + |
| 38 | +def test_bucket_transform_fails_for_list_type_input(): |
| 39 | + arr = pa.array([[1, 2], [3, 4]]) |
| 40 | + with pytest.raises( |
| 41 | + ValueError, |
| 42 | + match=r"FeatureUnsupported => Unsupported data type for bucket transform", |
| 43 | + ): |
| 44 | + ArrowArrayTransform.bucket(arr, 10) |
| 45 | + |
| 46 | + |
| 47 | +def test_bucket_chunked_array(): |
| 48 | + chunked = pa.chunked_array([pa.array([1, 2]), pa.array([3, 4])]) |
| 49 | + result_chunks = [] |
| 50 | + for arr in chunked.iterchunks(): |
| 51 | + result_chunks.append(ArrowArrayTransform.bucket(arr, 10)) |
| 52 | + |
| 53 | + expected = pa.chunked_array( |
| 54 | + [pa.array([6, 2], type=pa.int32()), pa.array([5, 0], type=pa.int32())] |
| 55 | + ) |
| 56 | + assert pa.chunked_array(result_chunks).equals(expected) |
| 57 | + |
| 58 | + |
| 59 | +def test_year_transform(): |
| 60 | + arr = pa.array([date(1970, 1, 1), date(2000, 1, 1)]) |
| 61 | + result = ArrowArrayTransform.year(arr) |
| 62 | + expected = pa.array([0, 30], type=pa.int32()) |
| 63 | + assert result == expected |
| 64 | + |
| 65 | + |
| 66 | +def test_month_transform(): |
| 67 | + arr = pa.array([date(1970, 1, 1), date(2000, 4, 1)]) |
| 68 | + result = ArrowArrayTransform.month(arr) |
| 69 | + expected = pa.array([0, 30 * 12 + 3], type=pa.int32()) |
| 70 | + assert result == expected |
| 71 | + |
| 72 | + |
| 73 | +def test_day_transform(): |
| 74 | + arr = pa.array([date(1970, 1, 1), date(2000, 4, 1)]) |
| 75 | + result = ArrowArrayTransform.day(arr) |
| 76 | + expected = pa.array([0, 11048], type=pa.int32()) |
| 77 | + assert result == expected |
| 78 | + |
| 79 | + |
| 80 | +def test_hour_transform(): |
| 81 | + arr = pa.array([datetime(1970, 1, 1, 19, 1, 23), datetime(2000, 3, 1, 12, 1, 23)]) |
| 82 | + result = ArrowArrayTransform.hour(arr) |
| 83 | + expected = pa.array([19, 264420], type=pa.int32()) |
| 84 | + assert result == expected |
| 85 | + |
| 86 | + |
| 87 | +def test_truncate_transform(): |
| 88 | + arr = pa.array(["this is a long string", "hi my name is sung"]) |
| 89 | + result = ArrowArrayTransform.truncate(arr, 5) |
| 90 | + expected = pa.array(["this ", "hi my"]) |
| 91 | + assert result == expected |
0 commit comments