|
| 1 | +# Copyright 2025 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 | + |
| 18 | +import bigframes.core.compile.sqlglot.sqlglot_types as sgt |
| 19 | +import bigframes.dtypes as dtypes |
| 20 | + |
| 21 | + |
| 22 | +def test_from_bigframes_simple_dtypes(): |
| 23 | + assert sgt.SQLGlotType.from_bigframes_dtype(dtypes.INT_DTYPE) == "INT64" |
| 24 | + assert sgt.SQLGlotType.from_bigframes_dtype(dtypes.FLOAT_DTYPE) == "FLOAT64" |
| 25 | + assert sgt.SQLGlotType.from_bigframes_dtype(dtypes.STRING_DTYPE) == "STRING" |
| 26 | + assert sgt.SQLGlotType.from_bigframes_dtype(dtypes.BOOL_DTYPE) == "BOOLEAN" |
| 27 | + assert sgt.SQLGlotType.from_bigframes_dtype(dtypes.DATE_DTYPE) == "DATE" |
| 28 | + assert sgt.SQLGlotType.from_bigframes_dtype(dtypes.TIME_DTYPE) == "TIME" |
| 29 | + assert sgt.SQLGlotType.from_bigframes_dtype(dtypes.DATETIME_DTYPE) == "DATETIME" |
| 30 | + assert sgt.SQLGlotType.from_bigframes_dtype(dtypes.TIMESTAMP_DTYPE) == "TIMESTAMP" |
| 31 | + assert sgt.SQLGlotType.from_bigframes_dtype(dtypes.BYTES_DTYPE) == "BYTES" |
| 32 | + assert sgt.SQLGlotType.from_bigframes_dtype(dtypes.NUMERIC_DTYPE) == "NUMERIC" |
| 33 | + assert sgt.SQLGlotType.from_bigframes_dtype(dtypes.BIGNUMERIC_DTYPE) == "BIGNUMERIC" |
| 34 | + assert sgt.SQLGlotType.from_bigframes_dtype(dtypes.JSON_DTYPE) == "JSON" |
| 35 | + assert sgt.SQLGlotType.from_bigframes_dtype(dtypes.GEO_DTYPE) == "GEOGRAPHY" |
| 36 | + |
| 37 | + |
| 38 | +def test_from_bigframes_struct_dtypes(): |
| 39 | + fields = [pa.field("int_col", pa.int64()), pa.field("bool_col", pa.bool_())] |
| 40 | + struct_type = pd.ArrowDtype(pa.struct(fields)) |
| 41 | + expected = "STRUCT<int_col INT64, bool_col BOOLEAN>" |
| 42 | + assert sgt.SQLGlotType.from_bigframes_dtype(struct_type) == expected |
| 43 | + |
| 44 | + |
| 45 | +def test_from_bigframes_array_dtypes(): |
| 46 | + int_array_type = pd.ArrowDtype(pa.list_(pa.int64())) |
| 47 | + assert sgt.SQLGlotType.from_bigframes_dtype(int_array_type) == "ARRAY<INT64>" |
| 48 | + |
| 49 | + string_array_type = pd.ArrowDtype(pa.list_(pa.string())) |
| 50 | + assert sgt.SQLGlotType.from_bigframes_dtype(string_array_type) == "ARRAY<STRING>" |
| 51 | + |
| 52 | + |
| 53 | +def test_from_bigframes_multi_nested_dtypes(): |
| 54 | + fields = [ |
| 55 | + pa.field("string_col", pa.string()), |
| 56 | + pa.field("date_col", pa.date32()), |
| 57 | + pa.field("array_col", pa.list_(pa.timestamp("us"))), |
| 58 | + ] |
| 59 | + array_type = pd.ArrowDtype(pa.list_(pa.struct(fields))) |
| 60 | + |
| 61 | + expected = ( |
| 62 | + "ARRAY<STRUCT<string_col STRING, date_col DATE, array_col ARRAY<DATETIME>>>" |
| 63 | + ) |
| 64 | + assert sgt.SQLGlotType.from_bigframes_dtype(array_type) == expected |
0 commit comments