|
| 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 | +# http://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 | +import os |
| 16 | +import re |
| 17 | + |
| 18 | +from google.cloud import bigquery |
| 19 | +import pandas as pd |
| 20 | +import pytest |
| 21 | + |
| 22 | +TPCH_PATH = "third_party/bigframes_vendored/tpch" |
| 23 | +PROJECT_ID = "bigframes-dev-perf" |
| 24 | +DATASET_ID = "tpch_0001g" |
| 25 | +DATASET = { |
| 26 | + "line_item_ds": f"{PROJECT_ID}.{DATASET_ID}.LINEITEM", |
| 27 | + "region_ds": f"{PROJECT_ID}.{DATASET_ID}.REGION", |
| 28 | + "nation_ds": f"{PROJECT_ID}.{DATASET_ID}.NATION", |
| 29 | + "supplier_ds": f"{PROJECT_ID}.{DATASET_ID}.SUPPLIER", |
| 30 | + "part_ds": f"{PROJECT_ID}.{DATASET_ID}.PART", |
| 31 | + "part_supp_ds": f"{PROJECT_ID}.{DATASET_ID}.PARTSUPP", |
| 32 | + "customer_ds": f"{PROJECT_ID}.{DATASET_ID}.CUSTOMER", |
| 33 | + "orders_ds": f"{PROJECT_ID}.{DATASET_ID}.ORDERS", |
| 34 | +} |
| 35 | + |
| 36 | + |
| 37 | +def _execute_sql_query(bigquery_client, sql_query): |
| 38 | + sql_query = sql_query.format(**DATASET) |
| 39 | + |
| 40 | + job_config = bigquery.QueryJobConfig(use_query_cache=False) |
| 41 | + query_job = bigquery_client.query(sql_query, job_config=job_config) |
| 42 | + query_job.result() |
| 43 | + df = query_job.to_dataframe() |
| 44 | + df.columns = df.columns.str.upper() |
| 45 | + return df |
| 46 | + |
| 47 | + |
| 48 | +def _execute_bigframes_script(session, bigframes_script): |
| 49 | + bigframes_script = re.sub( |
| 50 | + r"next\((\w+)\.to_pandas_batches\((.*?)\)\)", |
| 51 | + r"return \1.to_pandas()", |
| 52 | + bigframes_script, |
| 53 | + ) |
| 54 | + bigframes_script = re.sub(r"_\s*=\s*(\w+)", r"return \1", bigframes_script) |
| 55 | + |
| 56 | + bigframes_script = ( |
| 57 | + bigframes_script |
| 58 | + + f"\nresult = q('{PROJECT_ID}', '{DATASET_ID}', _initialize_session)" |
| 59 | + ) |
| 60 | + exec_globals = {"_initialize_session": session} |
| 61 | + exec(bigframes_script, exec_globals) |
| 62 | + bigframes_result = exec_globals.get("result") |
| 63 | + return bigframes_result |
| 64 | + |
| 65 | + |
| 66 | +def _verify_result(bigframes_result, sql_result): |
| 67 | + if isinstance(bigframes_result, pd.DataFrame): |
| 68 | + pd.testing.assert_frame_equal( |
| 69 | + sql_result.reset_index(drop=True), |
| 70 | + bigframes_result.reset_index(drop=True), |
| 71 | + check_dtype=False, |
| 72 | + ) |
| 73 | + else: |
| 74 | + assert sql_result.shape == (1, 1) |
| 75 | + sql_scalar = sql_result.iloc[0, 0] |
| 76 | + assert sql_scalar == bigframes_result |
| 77 | + |
| 78 | + |
| 79 | +@pytest.mark.parametrize("query_num", range(1, 23)) |
| 80 | +@pytest.mark.parametrize("ordered", [True, False]) |
| 81 | +def test_tpch_correctness(session, unordered_session, query_num, ordered): |
| 82 | + """Runs verification of TPCH benchmark script outputs to ensure correctness.""" |
| 83 | + # Execute SQL: |
| 84 | + sql_file_path = f"{TPCH_PATH}/sql_queries/q{query_num}.sql" |
| 85 | + assert os.path.exists(sql_file_path) |
| 86 | + with open(sql_file_path, "r") as f: |
| 87 | + sql_query = f.read() |
| 88 | + |
| 89 | + sql_result = _execute_sql_query(session.bqclient, sql_query) |
| 90 | + |
| 91 | + # Execute BigFrames: |
| 92 | + file_path = f"{TPCH_PATH}/queries/q{query_num}.py" |
| 93 | + assert os.path.exists(file_path) |
| 94 | + with open(file_path, "r") as file: |
| 95 | + bigframes_script = file.read() |
| 96 | + |
| 97 | + bigframes_result = _execute_bigframes_script( |
| 98 | + session if ordered else unordered_session, bigframes_script |
| 99 | + ) |
| 100 | + |
| 101 | + _verify_result(bigframes_result, sql_result) |
0 commit comments