|
| 1 | +# Copyright 2026 Google LLC |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# https://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +# [START bigquery_search_exclude_columns] |
| 16 | +import sys |
| 17 | + |
| 18 | +from google.api_core import exceptions |
| 19 | +from google.cloud import bigquery |
| 20 | + |
| 21 | + |
| 22 | +def search_exclude_columns( |
| 23 | + project_id: str, |
| 24 | +) -> None: |
| 25 | + """Searches a BigQuery table while excluding specific columns using EXCEPT. |
| 26 | +
|
| 27 | + Args: |
| 28 | + project_id: Google Cloud project ID used to bill the query job |
| 29 | + (for example, 'example-project'). |
| 30 | + """ |
| 31 | + client = bigquery.Client(project=project_id) |
| 32 | + # Query the public theLook eCommerce dataset in bigquery-public-data |
| 33 | + # (https://cloud.google.com/bigquery/public-data). |
| 34 | + table_ref = "bigquery-public-data.thelook_ecommerce.users" |
| 35 | + # Exclude street_address, city, state, and country from the searched fields using SELECT AS STRUCT ... EXCEPT. |
| 36 | + search_query = "Nelson Apt" |
| 37 | + job_config = bigquery.QueryJobConfig( |
| 38 | + query_parameters=[ |
| 39 | + bigquery.ScalarQueryParameter("search_query", "STRING", search_query), |
| 40 | + ] |
| 41 | + ) |
| 42 | + sql = f""" |
| 43 | + SELECT |
| 44 | + u.id, |
| 45 | + u.first_name, |
| 46 | + u.last_name, |
| 47 | + u.email |
| 48 | + FROM `{table_ref}` AS u |
| 49 | + WHERE SEARCH( |
| 50 | + (SELECT AS STRUCT u.* EXCEPT (street_address, city, state, country)), |
| 51 | + @search_query |
| 52 | + ) |
| 53 | + ORDER BY u.id ASC |
| 54 | + LIMIT 10; |
| 55 | + """ |
| 56 | + |
| 57 | + try: |
| 58 | + print( |
| 59 | + f"Searching {table_ref} excluding address columns for term: {search_query}" |
| 60 | + ) |
| 61 | + query_job = client.query(sql, job_config=job_config) |
| 62 | + rows = list(query_job.result()) |
| 63 | + except exceptions.NotFound as err: |
| 64 | + print( |
| 65 | + f"error: table '{table_ref}' not found when querying from project " |
| 66 | + f"'{project_id}': {err.message}", |
| 67 | + file=sys.stderr, |
| 68 | + ) |
| 69 | + return |
| 70 | + except exceptions.GoogleAPICallError as err: |
| 71 | + print( |
| 72 | + f"error: failed to search '{table_ref}' with excluded columns in project " |
| 73 | + f"'{project_id}': {err.message}", |
| 74 | + file=sys.stderr, |
| 75 | + ) |
| 76 | + return |
| 77 | + |
| 78 | + print(f"Resource ID: {table_ref}") |
| 79 | + print(f"Job ID: {query_job.job_id}") |
| 80 | + print(f"Matching Rows: {len(rows)}") |
| 81 | + print(f"Search Term: {search_query}") |
| 82 | + |
| 83 | + |
| 84 | +# [END bigquery_search_exclude_columns] |
0 commit comments