1717from dataclasses import dataclass
1818import functools
1919import typing
20- from typing import Dict , List
20+ from typing import Dict , List , Optional , Sequence
2121
2222import google .cloud .bigquery
2323import pyarrow
@@ -44,21 +44,26 @@ def __iter__(self):
4444 def from_bq_table (
4545 cls ,
4646 table : google .cloud .bigquery .Table ,
47- column_type_overrides : typing . Optional [
47+ column_type_overrides : Optional [
4848 typing .Dict [str , bigframes .dtypes .Dtype ]
4949 ] = None ,
50+ columns : Optional [Sequence [str ]] = None ,
5051 ):
52+ if not columns :
53+ fields = table .schema
54+ else :
55+ lookup = {field .name : field for field in table .schema }
56+ fields = [lookup [col ] for col in columns ]
57+
5158 return ArraySchema .from_bq_schema (
52- table . schema , column_type_overrides = column_type_overrides
59+ fields , column_type_overrides = column_type_overrides
5360 )
5461
5562 @classmethod
5663 def from_bq_schema (
5764 cls ,
5865 schema : List [google .cloud .bigquery .SchemaField ],
59- column_type_overrides : typing .Optional [
60- Dict [str , bigframes .dtypes .Dtype ]
61- ] = None ,
66+ column_type_overrides : Optional [Dict [str , bigframes .dtypes .Dtype ]] = None ,
6267 ):
6368 if column_type_overrides is None :
6469 column_type_overrides = {}
@@ -90,14 +95,16 @@ def to_bigquery(
9095 for item in self .items
9196 )
9297
93- def to_pyarrow (self ) -> pyarrow .Schema :
98+ def to_pyarrow (self , use_storage_type : bool = False ) -> pyarrow .Schema :
9499 fields = []
95100 for item in self .items :
96101 pa_type = bigframes .dtypes .bigframes_dtype_to_arrow_dtype (item .dtype )
97102 fields .append (
98103 pyarrow .field (
99104 item .column ,
100- pa_type ,
105+ pa_type .storage_type
106+ if use_storage_type and isinstance (pa_type , pyarrow .ExtensionType )
107+ else pa_type ,
101108 nullable = not pyarrow .types .is_list (pa_type ),
102109 )
103110 )
0 commit comments