File size: 6,980 Bytes
1856027 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 | # Copyright 2021 Google LLC
#
# Licensed 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
#
# https://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.
"""System tests for Arrow connector."""
from typing import Optional
import pyarrow
import pytest
from google.cloud import bigquery
from google.cloud.bigquery import enums
@pytest.mark.parametrize(
("max_results", "scalars_table_name"),
(
(None, "scalars_table"), # Use BQ Storage API.
(10, "scalars_table"), # Use REST API.
(None, "scalars_extreme_table"), # Use BQ Storage API.
(10, "scalars_extreme_table"), # Use REST API.
),
)
def test_list_rows_nullable_scalars_dtypes(
bigquery_client: bigquery.Client,
scalars_table: str,
scalars_extreme_table: str,
max_results: Optional[int],
scalars_table_name: str,
):
table_id = scalars_table
if scalars_table_name == "scalars_extreme_table":
table_id = scalars_extreme_table
# TODO(GH#836): Avoid INTERVAL columns until they are supported by the
# BigQuery Storage API and pyarrow.
schema = [
bigquery.SchemaField("bool_col", enums.SqlTypeNames.BOOLEAN),
bigquery.SchemaField("bignumeric_col", enums.SqlTypeNames.BIGNUMERIC),
bigquery.SchemaField("bytes_col", enums.SqlTypeNames.BYTES),
bigquery.SchemaField("date_col", enums.SqlTypeNames.DATE),
bigquery.SchemaField("datetime_col", enums.SqlTypeNames.DATETIME),
bigquery.SchemaField("float64_col", enums.SqlTypeNames.FLOAT64),
bigquery.SchemaField("geography_col", enums.SqlTypeNames.GEOGRAPHY),
bigquery.SchemaField("int64_col", enums.SqlTypeNames.INT64),
bigquery.SchemaField("numeric_col", enums.SqlTypeNames.NUMERIC),
bigquery.SchemaField("string_col", enums.SqlTypeNames.STRING),
bigquery.SchemaField("time_col", enums.SqlTypeNames.TIME),
bigquery.SchemaField("timestamp_col", enums.SqlTypeNames.TIMESTAMP),
]
arrow_table = bigquery_client.list_rows(
table_id,
max_results=max_results,
selected_fields=schema,
).to_arrow()
schema = arrow_table.schema
bignumeric_type = schema.field("bignumeric_col").type
# 77th digit is partial.
# https://cloud.google.com/bigquery/docs/reference/standard-sql/data-types#decimal_types
assert bignumeric_type.precision in {76, 77}
assert bignumeric_type.scale == 38
bool_type = schema.field("bool_col").type
assert bool_type.equals(pyarrow.bool_())
bytes_type = schema.field("bytes_col").type
assert bytes_type.equals(pyarrow.binary())
date_type = schema.field("date_col").type
assert date_type.equals(pyarrow.date32())
datetime_type = schema.field("datetime_col").type
assert datetime_type.unit == "us"
assert datetime_type.tz is None
float64_type = schema.field("float64_col").type
assert float64_type.equals(pyarrow.float64())
geography_type = schema.field("geography_col").type
assert geography_type.equals(pyarrow.string())
int64_type = schema.field("int64_col").type
assert int64_type.equals(pyarrow.int64())
numeric_type = schema.field("numeric_col").type
assert numeric_type.precision == 38
assert numeric_type.scale == 9
string_type = schema.field("string_col").type
assert string_type.equals(pyarrow.string())
time_type = schema.field("time_col").type
assert time_type.equals(pyarrow.time64("us"))
timestamp_type = schema.field("timestamp_col").type
assert timestamp_type.unit == "us"
assert timestamp_type.tz is not None
@pytest.mark.parametrize("do_insert", [True, False])
def test_arrow_extension_types_same_for_storage_and_REST_APIs_894(
dataset_client, test_table_name, do_insert
):
types = dict(
astring=("STRING", "'x'"),
astring9=("STRING(9)", "'x'"),
abytes=("BYTES", "b'x'"),
abytes9=("BYTES(9)", "b'x'"),
anumeric=("NUMERIC", "42"),
anumeric9=("NUMERIC(9)", "42"),
anumeric92=("NUMERIC(9,2)", "42"),
abignumeric=("BIGNUMERIC", "42e30"),
abignumeric49=("BIGNUMERIC(37)", "42e30"),
abignumeric492=("BIGNUMERIC(37,2)", "42e30"),
abool=("BOOL", "true"),
adate=("DATE", "'2021-09-06'"),
adatetime=("DATETIME", "'2021-09-06T09:57:26'"),
ageography=("GEOGRAPHY", "ST_GEOGFROMTEXT('point(0 0)')"),
# Can't get arrow data for interval :(
# ainterval=('INTERVAL', "make_interval(1, 2, 3, 4, 5, 6)"),
aint64=("INT64", "42"),
afloat64=("FLOAT64", "42.0"),
astruct=("STRUCT<v int64>", "struct(42)"),
atime=("TIME", "'1:2:3'"),
atimestamp=("TIMESTAMP", "'2021-09-06T09:57:26'"),
)
columns = ", ".join(f"{k} {t[0]}" for k, t in types.items())
dataset_client.query(f"create table {test_table_name} ({columns})").result()
if do_insert:
names = list(types)
values = ", ".join(types[name][1] for name in names)
names = ", ".join(names)
dataset_client.query(
f"insert into {test_table_name} ({names}) values ({values})"
).result()
at = dataset_client.query(f"select * from {test_table_name}").result().to_arrow()
storage_api_metadata = {
at.field(i).name: at.field(i).metadata for i in range(at.num_columns)
}
at = (
dataset_client.query(f"select * from {test_table_name}")
.result()
.to_arrow(create_bqstorage_client=False)
)
rest_api_metadata = {
at.field(i).name: at.field(i).metadata for i in range(at.num_columns)
}
assert rest_api_metadata == storage_api_metadata
assert rest_api_metadata["adatetime"] == {
b"ARROW:extension:name": b"google:sqlType:datetime"
}
assert rest_api_metadata["ageography"] == {
b"ARROW:extension:name": b"google:sqlType:geography",
b"ARROW:extension:metadata": b'{"encoding": "WKT"}',
}
def test_list_rows_range_csv(
bigquery_client: bigquery.Client,
scalars_table_csv: str,
):
table_id = scalars_table_csv
schema = [
bigquery.SchemaField(
"range_date", enums.SqlTypeNames.RANGE, range_element_type="DATE"
),
]
arrow_table = bigquery_client.list_rows(
table_id,
selected_fields=schema,
).to_arrow()
schema = arrow_table.schema
expected_type = pyarrow.struct(
[("start", pyarrow.date32()), ("end", pyarrow.date32())]
)
range_type = schema.field("range_date").type
assert range_type == expected_type
|