|
| 1 | +# Copyright 2026 Google LLC |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +import pandas as pd |
| 16 | +import pyarrow as pa |
| 17 | +import pytest |
| 18 | + |
| 19 | +from bigframes import dtypes |
| 20 | +from bigframes.core.logging import data_types |
| 21 | + |
| 22 | +UNKNOWN_TYPE = pd.ArrowDtype(pa.time64("ns")) |
| 23 | + |
| 24 | +PA_STRUCT_TYPE = pa.struct([("city", pa.string()), ("pop", pa.int64())]) |
| 25 | + |
| 26 | +PA_LIST_TYPE = pa.list_(pa.int64()) |
| 27 | + |
| 28 | + |
| 29 | +@pytest.mark.parametrize( |
| 30 | + ("dtype", "expected_mask"), |
| 31 | + [ |
| 32 | + (UNKNOWN_TYPE, 1 << 0), |
| 33 | + (dtypes.INT_DTYPE, 1 << 1), |
| 34 | + (dtypes.FLOAT_DTYPE, 1 << 2), |
| 35 | + (dtypes.BOOL_DTYPE, 1 << 3), |
| 36 | + (dtypes.STRING_DTYPE, 1 << 4), |
| 37 | + (dtypes.BYTES_DTYPE, 1 << 5), |
| 38 | + (dtypes.DATE_DTYPE, 1 << 6), |
| 39 | + (dtypes.TIME_DTYPE, 1 << 7), |
| 40 | + (dtypes.DATETIME_DTYPE, 1 << 8), |
| 41 | + (dtypes.TIMESTAMP_DTYPE, 1 << 9), |
| 42 | + (dtypes.TIMEDELTA_DTYPE, 1 << 10), |
| 43 | + (dtypes.NUMERIC_DTYPE, 1 << 11), |
| 44 | + (dtypes.BIGNUMERIC_DTYPE, 1 << 12), |
| 45 | + (dtypes.GEO_DTYPE, 1 << 13), |
| 46 | + (dtypes.JSON_DTYPE, 1 << 14), |
| 47 | + (pd.ArrowDtype(PA_STRUCT_TYPE), 1 << 15), |
| 48 | + (pd.ArrowDtype(PA_LIST_TYPE), 1 << 16), |
| 49 | + (dtypes.OBJ_REF_DTYPE, (1 << 15) | (1 << 17)), |
| 50 | + ], |
| 51 | +) |
| 52 | +def test_get_dtype_mask(dtype, expected_mask): |
| 53 | + assert data_types._get_dtype_mask(dtype) == expected_mask |
| 54 | + |
| 55 | + |
| 56 | +def test_add_data_type__type_overlap_no_op(): |
| 57 | + curr_type = dtypes.STRING_DTYPE |
| 58 | + existing_types = data_types._get_dtype_mask(curr_type) |
| 59 | + |
| 60 | + assert data_types._add_data_type(existing_types, curr_type) == existing_types |
| 61 | + |
| 62 | + |
| 63 | +def test_add_data_type__new_type_updated(): |
| 64 | + curr_type = dtypes.STRING_DTYPE |
| 65 | + existing_types = data_types._get_dtype_mask(dtypes.INT_DTYPE) |
| 66 | + |
| 67 | + assert data_types._add_data_type( |
| 68 | + existing_types, curr_type |
| 69 | + ) == existing_types | data_types._get_dtype_mask(curr_type) |
0 commit comments