def get_bq_schema_for_table(self, table_name: str) -> List[bigquery.SchemaField]: """Return a List of SchemaField objects for a given table name If it is an association table for a schema that includes the region code, a region code schema field is appended. """ table = get_table_class_by_name(table_name, self.sorted_tables) columns = [ bigquery.SchemaField(column.name, BQ_TYPES[type(column.type)], 'NULLABLE' if column.nullable else 'REQUIRED') for column in table.columns if column.name in self._get_table_columns_to_export(table) ] if not schema_has_region_code_query_support(self.metadata_base): # We shouldn't add a region code column for this schema return columns column_names = {column.name for column in table.columns} region_code_col = get_region_code_col(self.metadata_base, table) if region_code_col in column_names: # We already have the region column we need in the table return columns columns.append( bigquery.SchemaField(region_code_col, BQ_TYPES[sqlalchemy.String], 'NULLABLE')) return columns
def select_clause(self) -> str: formatted_columns = self.format_columns_for_sql( self.columns_to_include, table_prefix=self.table_name) if schema_has_region_code_query_support(self.metadata_base): region_code_col = self._get_region_code_col() if region_code_col not in self.columns_to_include: region_code_table = self._get_region_code_table() formatted_columns = \ formatted_columns + f',{region_code_table.name}.{region_code_col} AS {region_code_col}' return 'SELECT {columns}'.format(columns=formatted_columns)
def select_clause(self) -> str: formatted_columns = self._formatted_columns_for_select_clause() if schema_has_region_code_query_support(self.metadata_base): region_code_col = self._get_region_code_col() if region_code_col not in self.columns_to_include: region_code_table = self._get_region_code_table() formatted_columns = ( formatted_columns + f",{region_code_table.name}.{region_code_col} AS {region_code_col}" ) return "SELECT {columns}".format(columns=formatted_columns)
def _get_region_code_col(self) -> Optional[str]: if not schema_has_region_code_query_support(self.metadata_base): return None table = self._get_region_code_table() return get_region_code_col(self.metadata_base, table)
def _join_to_get_region_code(self) -> bool: return schema_has_region_code_query_support( self.metadata_base) and is_association_table(self.table_name)