From 6e357f7ad72c6bae0c6e10218245ce9a330216ff Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Emil=20M=C3=A5rtensson?= Date: Mon, 18 May 2026 15:24:12 +0200 Subject: [PATCH] Collapse bigquery get_tables_by_pattern_sql to one INFORMATION_SCHEMA call When `target.location` is set, use the region-qualified INFORMATION_SCHEMA.TABLES view so `table_schema` can be filtered directly. That replaces the SCHEMATA lookup + per-dataset UNION ALL with a single query. Falls back to the existing path when no location is configured, so behaviour is unchanged for those users. --- macros/sql/get_tables_by_pattern_sql.sql | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/macros/sql/get_tables_by_pattern_sql.sql b/macros/sql/get_tables_by_pattern_sql.sql index 03f96624d..9f6c8cc2f 100644 --- a/macros/sql/get_tables_by_pattern_sql.sql +++ b/macros/sql/get_tables_by_pattern_sql.sql @@ -45,6 +45,25 @@ {% macro bigquery__get_tables_by_pattern_sql(schema_pattern, table_pattern, exclude='', database=target.database) %} + {# When `target.location` is set, the region-qualified INFORMATION_SCHEMA.TABLES + exposes `table_schema` directly. That collapses a SCHEMATA lookup plus a + per-dataset UNION ALL into one query. Falls back to the original + SCHEMATA + iterate path when no location is configured. #} + {% if '%' in schema_pattern and target.location %} + {% set sql %} + select distinct + table_schema, + table_name, + {{ dbt_utils.get_table_types_sql() }} + from {{ adapter.quote(database) }}.`region-{{ target.location | lower }}`.INFORMATION_SCHEMA.TABLES + where lower(table_schema) like lower('{{ schema_pattern }}') + and lower(table_name) like lower('{{ table_pattern }}') + and lower(table_name) not like lower('{{ exclude }}') + {% endset %} + + {{ return(sql) }} + {% endif %} + {% if '%' in schema_pattern %} {% set schemata=dbt_utils._bigquery__get_matching_schemata(schema_pattern, database) %} {% else %}