mistercrunch commented on code in PR #34766: URL: https://github.com/apache/superset/pull/34766#discussion_r2289300853
########## superset/db_engine_specs/bigquery.py: ########## @@ -828,3 +828,97 @@ def parse_error_exception(cls, exception: Exception) -> Exception: # If for some reason we get an exception, for example, no new line # We will return the original exception return exception + + @classmethod + def get_materialized_view_names( + cls, + database: Database, + inspector: Inspector, + schema: str | None, + ) -> set[str]: + """ + Get all materialized views from BigQuery. + + BigQuery materialized views are not returned by the standard + get_view_names() method, so we need to query INFORMATION_SCHEMA directly. + """ + if not schema: + return set() + + # Construct the query to get materialized views from INFORMATION_SCHEMA + if catalog := database.get_default_catalog(): + information_schema = f"`{catalog}.{schema}.INFORMATION_SCHEMA.TABLES`" + else: + information_schema = f"`{schema}.INFORMATION_SCHEMA.TABLES`" + + # Use string formatting for the table name since it's not user input + # The catalog and schema are from trusted sources (database configuration) + query = f""" + SELECT table_name + FROM {information_schema} + WHERE table_type = 'MATERIALIZED VIEW' + """ # noqa: S608 + + materialized_views = set() + try: + with database.get_raw_connection(catalog=catalog, schema=schema) as conn: + cursor = conn.cursor() + cursor.execute(query) + materialized_views = {row[0] for row in cursor.fetchall()} + except Exception: + # If we can't fetch materialized views, return empty set + logger.warning( + "Unable to fetch materialized views for schema %s", + schema, + exc_info=True, + ) + + return materialized_views + + @classmethod + def get_view_names( + cls, + database: Database, + inspector: Inspector, + schema: str | None, + ) -> set[str]: + """ + Get all views from BigQuery, excluding materialized views. + + BigQuery's standard view discovery includes materialized views, + but we want to separate them for proper categorization. + """ + if not schema: + return set() + + # Construct the query to get regular views from INFORMATION_SCHEMA + catalog = database.get_default_catalog() + if catalog: + information_schema = f"`{catalog}.{schema}.INFORMATION_SCHEMA.TABLES`" + else: + information_schema = f"`{schema}.INFORMATION_SCHEMA.TABLES`" + + # Use string formatting for the table name since it's not user input + # The catalog and schema are from trusted sources (database configuration) + query = f""" + SELECT table_name + FROM {information_schema} + WHERE table_type = 'VIEW' + """ # noqa: S608 + + views = set() + try: + with database.get_raw_connection(catalog=catalog, schema=schema) as conn: Review Comment: similar here, would refactor into some sort of `query(str) -> QueryPayload` -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: notifications-unsubscr...@superset.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org --------------------------------------------------------------------- To unsubscribe, e-mail: notifications-unsubscr...@superset.apache.org For additional commands, e-mail: notifications-h...@superset.apache.org