File size: 4,293 Bytes
065fee7 |
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 |
# Copyright 2023 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
#
# 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 pytest
from google.cloud import bigquery
import google.cloud.bigquery.table
pyarrow = pytest.importorskip("pyarrow", minversion="3.0.0")
def test_to_arrow_with_jobs_query_response():
resource = {
"kind": "bigquery#queryResponse",
"schema": {
"fields": [
{"name": "name", "type": "STRING", "mode": "NULLABLE"},
{"name": "number", "type": "INTEGER", "mode": "NULLABLE"},
]
},
"jobReference": {
"projectId": "test-project",
"jobId": "job_ocd3cb-N62QIslU7R5qKKa2_427J",
"location": "US",
},
"totalRows": "9",
"rows": [
{"f": [{"v": "Tiarra"}, {"v": "6"}]},
{"f": [{"v": "Timothy"}, {"v": "325"}]},
{"f": [{"v": "Tina"}, {"v": "26"}]},
{"f": [{"v": "Tierra"}, {"v": "10"}]},
{"f": [{"v": "Tia"}, {"v": "17"}]},
{"f": [{"v": "Tiara"}, {"v": "22"}]},
{"f": [{"v": "Tiana"}, {"v": "6"}]},
{"f": [{"v": "Tiffany"}, {"v": "229"}]},
{"f": [{"v": "Tiffani"}, {"v": "8"}]},
],
"totalBytesProcessed": "154775150",
"jobComplete": True,
"cacheHit": False,
"queryId": "job_ocd3cb-N62QIslU7R5qKKa2_427J",
}
rows = google.cloud.bigquery.table.RowIterator(
client=None,
api_request=None,
path=None,
schema=[
bigquery.SchemaField.from_api_repr(field)
for field in resource["schema"]["fields"]
],
first_page_response=resource,
)
records = rows.to_arrow()
assert records.column_names == ["name", "number"]
assert records["name"].to_pylist() == [
"Tiarra",
"Timothy",
"Tina",
"Tierra",
"Tia",
"Tiara",
"Tiana",
"Tiffany",
"Tiffani",
]
assert records["number"].to_pylist() == [6, 325, 26, 10, 17, 22, 6, 229, 8]
def test_to_arrow_with_jobs_query_response_and_max_results():
resource = {
"kind": "bigquery#queryResponse",
"schema": {
"fields": [
{"name": "name", "type": "STRING", "mode": "NULLABLE"},
{"name": "number", "type": "INTEGER", "mode": "NULLABLE"},
]
},
"jobReference": {
"projectId": "test-project",
"jobId": "job_ocd3cb-N62QIslU7R5qKKa2_427J",
"location": "US",
},
"totalRows": "9",
"rows": [
{"f": [{"v": "Tiarra"}, {"v": "6"}]},
{"f": [{"v": "Timothy"}, {"v": "325"}]},
{"f": [{"v": "Tina"}, {"v": "26"}]},
{"f": [{"v": "Tierra"}, {"v": "10"}]},
{"f": [{"v": "Tia"}, {"v": "17"}]},
{"f": [{"v": "Tiara"}, {"v": "22"}]},
{"f": [{"v": "Tiana"}, {"v": "6"}]},
{"f": [{"v": "Tiffany"}, {"v": "229"}]},
{"f": [{"v": "Tiffani"}, {"v": "8"}]},
],
"totalBytesProcessed": "154775150",
"jobComplete": True,
"cacheHit": False,
"queryId": "job_ocd3cb-N62QIslU7R5qKKa2_427J",
}
rows = google.cloud.bigquery.table.RowIterator(
client=None,
api_request=None,
path=None,
schema=[
bigquery.SchemaField.from_api_repr(field)
for field in resource["schema"]["fields"]
],
first_page_response=resource,
max_results=3,
)
records = rows.to_arrow()
assert records.column_names == ["name", "number"]
assert records["name"].to_pylist() == [
"Tiarra",
"Timothy",
"Tina",
]
assert records["number"].to_pylist() == [6, 325, 26]
|