-
Notifications
You must be signed in to change notification settings - Fork 258
Add a new script to run BigQuery queries with python client #694
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
aniket486
wants to merge
7
commits into
ClickHouse:main
Choose a base branch
from
aniket486:main
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+152
−72
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
ea02d17
Add a new script to run bigquery queries with python client
c20bb97
Add results after a run on a project with no reservation (ondemand)
fa9d63d
update readme with updated instructions on running benchmark
1a92778
Minor edits to python script and Readme
1ad61f1
Minor edits to python script and Readme
db478ef
Use only client time and update results
bd40be7
Add more instructions for permissions, add clustering and fix some fo…
aniket486 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,38 +1,41 @@ | ||
| As of 2025, Google Bigquery allow publishing benchmark results, which was not the case earlier. | ||
|
|
||
| It's very difficult to find, how to create a database. | ||
| Databases are named "datasets". You need to press on `⋮` near project. | ||
| Download Google Cloud CLI and configure your project settings using the commands below. | ||
| You can skip this step if you are using [Cloud shell](https://docs.cloud.google.com/shell/docs/launching-cloud-shell) which already comes with gcloud preinstalled: | ||
| ``` | ||
| curl https://sdk.cloud.google.com | bash | ||
| exec -l $SHELL | ||
| gcloud init | ||
| ``` | ||
|
|
||
| Create dataset `test`. | ||
| Go to the query editor and paste the contents of `create.sql`. | ||
| It will take two seconds to create a table. | ||
| Enable BigQuery permissions for this project if they haven't enabled already: | ||
| ``` | ||
| # 1. Store the active project ID and authenticated email in variables for convenience | ||
| export PROJECT_ID=$(gcloud config get-value project) | ||
| export USER_EMAIL=$(gcloud config get-value account) | ||
|
|
||
| # 2. Grant the BigQuery User role (Fixes datasets.create and jobs.create) | ||
| gcloud projects add-iam-policy-binding $PROJECT_ID \ | ||
| --member="user:$USER_EMAIL" \ | ||
| --role="roles/bigquery.user" | ||
| ``` | ||
|
|
||
| Download Google Cloud CLI: | ||
| Create the dataset and table in BigQuery: | ||
| ``` | ||
| wget --continue --progress=dot:giga https://dl.google.com/dl/cloudsdk/channels/rapid/downloads/google-cloud-cli-linux-x86_64.tar.gz | ||
| tar -xf google-cloud-cli-linux-x86_64.tar.gz | ||
| ./google-cloud-sdk/install.sh | ||
| source .bashrc | ||
| ./google-cloud-sdk/bin/gcloud init | ||
| bq mk --dataset test | ||
|
|
||
| bq query --use_legacy_sql=false < create.sql | ||
| ``` | ||
|
|
||
| Load the data: | ||
| Load the data in the table: | ||
| ``` | ||
| wget --continue --progress=dot:giga 'https://datasets.clickhouse.com/hits_compatible/hits.csv.gz' | ||
| gzip -d -f hits.csv.gz | ||
|
|
||
| # No need to unzip, BigQuery can load from GZIP compressed CSV file.: | ||
| echo -n "Load time: " | ||
| command time -f '%e' bq load --source_format CSV --allow_quoted_newlines=1 test.hits hits.csv | ||
| command time -f '%e' bq load --source_format CSV --allow_quoted_newlines=1 test.hits hits.csv.gz | ||
| ``` | ||
|
|
||
| Run the benchmark: | ||
|
|
||
| ``` | ||
| ./run.sh 2>&1 | tee log.txt | ||
|
|
||
| cat log.txt | | ||
| grep -P '^real|^Error' | | ||
| sed -r -e 's/^Error.*$/null/; s/^real\s*([0-9.]+)m([0-9.]+)s$/\1 \2/' | | ||
| awk '{ if ($2 != "") { print $1 * 60 + $2 } else { print $1 } }' | | ||
| awk '{ if ($1 == "null") { skip = 1 } else { if (i % 3 == 0) { printf "[" }; printf skip ? "null" : $1; if (i % 3 != 2) { printf "," } else { print "]," }; ++i; skip = 0; } }' | ||
| pip install google-cloud-bigquery | ||
| python3 run_queries.py > results.txt 2> log.txt | ||
| ``` | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| #!/bin/bash | ||
|
|
||
| bq mk --dataset test | ||
|
|
||
| bq query --use_legacy_sql=false < create.sql |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,71 @@ | ||
| from google.cloud import bigquery | ||
| from google.cloud.bigquery.enums import JobCreationMode | ||
|
|
||
| import sys | ||
| from typing import TextIO, Any | ||
| from datetime import datetime | ||
|
|
||
| def log(*objects: Any, sep: str = ' ', end: str = '\n', file: TextIO = sys.stderr, severity: str = 'INFO') -> None: | ||
| """ | ||
| Mimics the built-in print() function signature but prepends a | ||
| timestamp and a configurable severity level to the output. | ||
|
|
||
| Args: | ||
| *objects: The objects to be printed (converted to strings). | ||
| sep (str): Separator inserted between values, default a space. | ||
| end (str): String appended after the last value, default a newline. | ||
| file (TextIO): Object with a write(string) method, default sys.stdout. | ||
| severity (str): The log level (e.g., "INFO", "WARNING", "ERROR"). | ||
| """ | ||
| # 1. Prepare the standard print content | ||
| # Use an f-string to join the objects with the specified separator | ||
| message = sep.join(str(obj) for obj in objects) | ||
|
|
||
| # 2. Prepare the log prefix | ||
| timestamp = datetime.now().strftime("%Y-%m-%d %H:%M:%S") | ||
| prefix = f"[{timestamp}] [{severity.upper()}]: " | ||
|
|
||
| # 3. Combine the prefix and the message | ||
| full_message = prefix + message | ||
|
|
||
| # 4. Use the file.write method to output the content | ||
| # The 'end' argument is handled explicitly here | ||
| file.write(full_message + end) | ||
|
|
||
| # Ensure the buffer is flushed (important for file/stream output) | ||
| if file is not sys.stdout and file is not sys.stderr: | ||
| file.flush() | ||
|
|
||
|
|
||
| job_config = bigquery.QueryJobConfig() | ||
| job_config.use_query_cache = False | ||
| client = bigquery.Client( | ||
| default_job_creation_mode=JobCreationMode.JOB_CREATION_OPTIONAL | ||
| ) | ||
|
|
||
| file = open('queries.sql', 'r') | ||
| TRIES = 3 | ||
| query_num = 0 | ||
| for query in file: | ||
| query = query.strip() | ||
| print("[", end='') | ||
| query_num = query_num + 1 | ||
| for i in range(TRIES): | ||
| log(f"[q{query_num}: {i}]: {query}") | ||
| try: | ||
| client_start_time = datetime.now() | ||
| results = client.query_and_wait(query, job_config=job_config) | ||
| client_end_time = datetime.now() | ||
|
|
||
| client_time = client_end_time - client_start_time | ||
| client_time_secs = client_time.total_seconds() | ||
| endstr = "],\n" if i == 2 else "," | ||
| print(f"{client_time_secs}", end=endstr) | ||
|
|
||
| log(f"Job ID: **{results.job_id}**") | ||
| log(f"Query ID: **{results.query_id}**") | ||
| log(f"Client time: **{client_time}**") | ||
|
|
||
| except Exception as e: | ||
| log(f"Job failed with error: {e}", severity="ERROR") | ||
|
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
L. 6: That should be
source ~/.bashrcplease