In order to further contribute to the community and in addition to Mikhail's answer, I would like to provide you two other options.
First using the Python API for BigQuery. I created the following script:
import argparse
from google.cloud import bigquery
client = bigquery.Client()
dataset_ref = client.dataset("dataset_name", project="project_id")
dataset = client.get_dataset(dataset_ref)
parser = argparse.ArgumentParser()
parser.add_argument('-t', type=int,
help='number of tables that we need to be queried')
parsed = parser.parse_args()
#retrieve the integer you passed through the command line
arg = parsed.t
def check_tables(arg):
#counting the number of tables give the desired wildcard
query = """SELECT COUNT(DISTINCT _TABLE_SUFFIX)
FROM `project.dataset.table_*`
"""
query_job = client.query(query)
result= query_job.result()
for r in result:
count=r[0]
print("There are {} tables".format(count))
if count==int(arg):
return True
else:
return False
if check_tables(arg):
print("In total BigQuery is querying {} tables".format(arg))
#your new SQL query here, which will execute only if there are the amount of tables you specified
#you can output the results to a new table
As shown above, the script used argparse library from Python. Therefore, in order to run the script, you have to use python script_name.py <int64>, in my case I ran python query.py 3. In other words, you have to specify the number of tables you are expecting to query using wildcards. The script checks how many tables there are with the given wildcard. Then, it compares with the number of tables we expect and if the numbers are equal you can execute another query (after the last if). Otherwise, output a message.
The second option is using the BigQuery Console. We will be taking advantage of Scripting. The script will basically check the number of tables within the desired wildcard, evaluate if there are the expected number of tables. Then, if there are, execute the query. The syntax is below:
DECLARE num_tables INT64 DEFAULT 3;
DECLARE count_tables INT64;
SET count_tables = (SELECT COUNT(DISTINCT _TABLE_SUFFIX) FROM `test-proj-261014.sample.abril_airports_*`);
IF(num_tables = count_tables) THEN
SELECT * FROM `project.dataset.table_*`;
END IF;