1515
1616import enum
1717import re
18- from typing import Dict , Iterable , Optional , Tuple
18+ from typing import Callable , Dict , Iterable , Optional , Tuple
1919
20- from google .cloud import bigquery
2120from pyarrow .parquet import ParquetFile
2221
22+ from feast import type_map
2323from feast .data_format import FileFormat , StreamFormat
2424from feast .protos .feast .core .DataSource_pb2 import DataSource as DataSourceProto
25+ from feast .value_type import ValueType
2526
2627
2728class SourceType (enum .Enum ):
@@ -519,8 +520,9 @@ def to_proto(self) -> DataSourceProto:
519520 """
520521 raise NotImplementedError
521522
522- def infer_event_timestamp_column (self , ts_column_type_regex_pattern ):
523+ def _infer_event_timestamp_column (self , ts_column_type_regex_pattern ):
523524 ERROR_MSG_PREFIX = "Unable to infer DataSource event_timestamp_column"
525+ USER_GUIDANCE = "Please specify event_timestamp_column explicitly."
524526
525527 if isinstance (self , FileSource ) or isinstance (self , BigQuerySource ):
526528 event_timestamp_column , matched_flag = None , False
@@ -530,7 +532,7 @@ def infer_event_timestamp_column(self, ts_column_type_regex_pattern):
530532 raise TypeError (
531533 f"""
532534 { ERROR_MSG_PREFIX } due to multiple possible columns satisfying
533- the criteria.
535+ the criteria. { USER_GUIDANCE }
534536 """
535537 )
536538 matched_flag = True
@@ -541,12 +543,14 @@ def infer_event_timestamp_column(self, ts_column_type_regex_pattern):
541543 raise TypeError (
542544 f"""
543545 { ERROR_MSG_PREFIX } due to an absence of columns that satisfy the criteria.
546+ { USER_GUIDANCE }
544547 """
545548 )
546549 else :
547550 raise TypeError (
548551 f"""
549552 { ERROR_MSG_PREFIX } because this DataSource currently does not support this inference.
553+ { USER_GUIDANCE }
550554 """
551555 )
552556
@@ -595,7 +599,7 @@ def __init__(
595599
596600 super ().__init__ (
597601 event_timestamp_column
598- or self .infer_event_timestamp_column (r"timestamp\[\w\w\]" ),
602+ or self ._infer_event_timestamp_column (r"timestamp\[\w\w\]" ),
599603 created_timestamp_column ,
600604 field_mapping ,
601605 date_partition_column ,
@@ -647,6 +651,10 @@ def to_proto(self) -> DataSourceProto:
647651
648652 return data_source_proto
649653
654+ @staticmethod
655+ def source_datatype_to_feast_value_type () -> Callable [[str ], ValueType ]:
656+ return type_map .pa_to_feast_value_type
657+
650658 def get_table_column_names_and_types (self ) -> Iterable [Tuple [str , str ]]:
651659 schema = ParquetFile (self .path ).schema_arrow
652660 return zip (schema .names , map (str , schema .types ))
@@ -666,7 +674,7 @@ def __init__(
666674
667675 super ().__init__ (
668676 event_timestamp_column
669- or self .infer_event_timestamp_column ("TIMESTAMP|DATETIME" ),
677+ or self ._infer_event_timestamp_column ("TIMESTAMP|DATETIME" ),
670678 created_timestamp_column ,
671679 field_mapping ,
672680 date_partition_column ,
@@ -728,21 +736,36 @@ def get_table_query_string(self) -> str:
728736 else :
729737 return f"({ self .query } )"
730738
739+ @staticmethod
740+ def source_datatype_to_feast_value_type () -> Callable [[str ], ValueType ]:
741+ return type_map .bq_to_feast_value_type
742+
731743 def get_table_column_names_and_types (self ) -> Iterable [Tuple [str , str ]]:
732- assert self .table_ref is not None
733- name_type_pairs = []
734- project_id , dataset_id , table_id = self .table_ref .split ("." )
735- bq_columns_query = f"""
736- SELECT COLUMN_NAME, DATA_TYPE FROM { project_id } .{ dataset_id } .INFORMATION_SCHEMA.COLUMNS
737- WHERE TABLE_NAME = '{ table_id } '
738- """
744+ from google .cloud import bigquery
739745
740746 client = bigquery .Client ()
741- table_schema = client .query (bq_columns_query ).result ().to_dataframe_iterable ()
742- for df in table_schema :
743- name_type_pairs .extend (
744- list (zip (df ["COLUMN_NAME" ].to_list (), df ["DATA_TYPE" ].to_list ()))
747+ bq_columns_query = ""
748+ name_type_pairs = []
749+ if self .table_ref is not None :
750+ project_id , dataset_id , table_id = self .table_ref .split ("." )
751+ bq_columns_query = f"""
752+ SELECT COLUMN_NAME, DATA_TYPE FROM { project_id } .{ dataset_id } .INFORMATION_SCHEMA.COLUMNS
753+ WHERE TABLE_NAME = '{ table_id } '
754+ """
755+ table_schema = (
756+ client .query (bq_columns_query ).result ().to_dataframe_iterable ()
745757 )
758+ for df in table_schema :
759+ name_type_pairs .extend (
760+ list (zip (df ["COLUMN_NAME" ].to_list (), df ["DATA_TYPE" ].to_list ()))
761+ )
762+ else :
763+ bq_columns_query = f"SELECT * FROM { self .query } LIMIT 1"
764+ queryRes = client .query (bq_columns_query ).result ()
765+ name_type_pairs = [
766+ (schema_field .name , schema_field .field_type )
767+ for schema_field in queryRes .schema
768+ ]
746769
747770 return name_type_pairs
748771
0 commit comments