|
| 1 | +# Copyright 2025 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 | +import pathlib |
| 15 | + |
| 16 | +import benchmark.utils as utils |
| 17 | + |
| 18 | +import bigframes.session |
| 19 | + |
| 20 | +PAGE_SIZE = utils.READ_GBQ_COLAB_PAGE_SIZE |
| 21 | + |
| 22 | + |
| 23 | +def aggregate_output( |
| 24 | + *, project_id, dataset_id, table_id, session: bigframes.session.Session |
| 25 | +): |
| 26 | + # TODO(tswast): Support alternative query if table_id is a local DataFrame, |
| 27 | + # e.g. "{local_inline}" or "{local_large}" |
| 28 | + df = session._read_gbq_colab( |
| 29 | + f"SELECT * FROM `{project_id}`.{dataset_id}.{table_id}" |
| 30 | + ) |
| 31 | + |
| 32 | + # Simulate getting the first page, since we'll always do that first in the UI. |
| 33 | + df.shape |
| 34 | + next(iter(df.to_pandas_batches(page_size=PAGE_SIZE))) |
| 35 | + |
| 36 | + # To simulate very small rows that can only fit a boolean, |
| 37 | + # some tables don't have an integer column. If an integer column is available, |
| 38 | + # we prefer to group by that to get a more realistic number of groups. |
| 39 | + group_column = "col_int64_1" |
| 40 | + if group_column not in df.columns: |
| 41 | + group_column = "col_bool_0" |
| 42 | + |
| 43 | + # Simulate the user aggregating by a column and visualizing those results |
| 44 | + df_aggregated = ( |
| 45 | + df.assign(rounded=df[group_column].astype("Int64").round(-9)) |
| 46 | + .groupby("rounded") |
| 47 | + .sum() |
| 48 | + ) |
| 49 | + |
| 50 | + df_aggregated.shape |
| 51 | + next(iter(df_aggregated.to_pandas_batches(page_size=PAGE_SIZE))) |
| 52 | + |
| 53 | + |
| 54 | +if __name__ == "__main__": |
| 55 | + ( |
| 56 | + project_id, |
| 57 | + dataset_id, |
| 58 | + table_id, |
| 59 | + session, |
| 60 | + suffix, |
| 61 | + ) = utils.get_configuration(include_table_id=True) |
| 62 | + current_path = pathlib.Path(__file__).absolute() |
| 63 | + |
| 64 | + utils.get_execution_time( |
| 65 | + aggregate_output, |
| 66 | + current_path, |
| 67 | + suffix, |
| 68 | + project_id=project_id, |
| 69 | + dataset_id=dataset_id, |
| 70 | + table_id=table_id, |
| 71 | + session=session, |
| 72 | + ) |
0 commit comments