|
| 1 | +import os |
| 2 | +from datetime import datetime, timedelta, timezone |
| 3 | +from unittest.mock import Mock, patch |
| 4 | + |
| 5 | +import pytest |
| 6 | + |
| 7 | +from feast.infra.offline_stores.bigquery import ( |
| 8 | + BigQueryOfflineStore, |
| 9 | + BigQueryOfflineStoreConfig, |
| 10 | +) |
| 11 | +from feast.infra.offline_stores.bigquery_source import BigQuerySource |
| 12 | +from feast.infra.online_stores.sqlite import SqliteOnlineStoreConfig |
| 13 | +from feast.repo_config import RepoConfig |
| 14 | + |
| 15 | +__doc__ = """ |
| 16 | +Environment variables: |
| 17 | + FEAST_BQ_BENCH_PROJECT: BigQuery project to run dry-run queries in |
| 18 | + FEAST_BQ_BENCH_TABLE: BigQuery table in Feast format: project:dataset.table |
| 19 | + FEAST_BQ_BENCH_TIMESTAMP_FIELD: event timestamp column name used by Feast |
| 20 | + FEAST_BQ_BENCH_PARTITION_COLUMN: partition column to prune (e.g. _PARTITIONDATE) |
| 21 | + FEAST_BQ_BENCH_LOCATION: optional BigQuery location |
| 22 | + FEAST_BQ_BENCH_START: optional ISO datetime (e.g. 2026-01-01T00:00:00+00:00) |
| 23 | + FEAST_BQ_BENCH_END: optional ISO datetime |
| 24 | + FEAST_BQ_BENCH_REQUIRE_REDUCTION: if truthy, requires strict byte reduction |
| 25 | +""" |
| 26 | + |
| 27 | + |
| 28 | +def _required_env(name: str) -> str: |
| 29 | + val = os.environ.get(name) |
| 30 | + if not val: |
| 31 | + pytest.skip(f"Missing env var {name}") |
| 32 | + return val |
| 33 | + |
| 34 | + |
| 35 | +def _optional_iso_datetime(name: str) -> datetime | None: |
| 36 | + val = os.environ.get(name) |
| 37 | + if not val: |
| 38 | + return None |
| 39 | + return datetime.fromisoformat(val.replace("Z", "+00:00")) |
| 40 | + |
| 41 | + |
| 42 | +def _estimate_bytes_processed(project: str, location: str | None, sql: str) -> int: |
| 43 | + try: |
| 44 | + from google.cloud import bigquery |
| 45 | + except Exception as e: |
| 46 | + pytest.skip(str(e)) |
| 47 | + client = bigquery.Client(project=project, location=location) |
| 48 | + job_config = bigquery.QueryJobConfig(dry_run=True, use_query_cache=False) |
| 49 | + job = client.query(sql, job_config=job_config) |
| 50 | + return int(job.total_bytes_processed or 0) |
| 51 | + |
| 52 | + |
| 53 | +@pytest.mark.benchmark(group="bigquery_partition_pruning") |
| 54 | +@patch("feast.infra.offline_stores.bigquery._get_bigquery_client") |
| 55 | +def test_bigquery_partition_pruning_bytes_processed( |
| 56 | + mock_get_bigquery_client, benchmark |
| 57 | +): |
| 58 | + mock_get_bigquery_client.return_value = Mock() |
| 59 | + |
| 60 | + project = _required_env("FEAST_BQ_BENCH_PROJECT") |
| 61 | + table = _required_env("FEAST_BQ_BENCH_TABLE") |
| 62 | + timestamp_field = _required_env("FEAST_BQ_BENCH_TIMESTAMP_FIELD") |
| 63 | + partition_column = _required_env("FEAST_BQ_BENCH_PARTITION_COLUMN") |
| 64 | + location = os.environ.get("FEAST_BQ_BENCH_LOCATION") |
| 65 | + |
| 66 | + end = _optional_iso_datetime("FEAST_BQ_BENCH_END") |
| 67 | + if end is None: |
| 68 | + end = datetime.now(tz=timezone.utc).replace(microsecond=0) |
| 69 | + start = _optional_iso_datetime("FEAST_BQ_BENCH_START") |
| 70 | + if start is None: |
| 71 | + start = end - timedelta(days=7) |
| 72 | + |
| 73 | + repo_config = RepoConfig( |
| 74 | + registry="gs://ml-test/repo/registry.db", |
| 75 | + project="bench", |
| 76 | + provider="gcp", |
| 77 | + online_store=SqliteOnlineStoreConfig(type="sqlite"), |
| 78 | + offline_store=BigQueryOfflineStoreConfig(type="bigquery", dataset="feast"), |
| 79 | + ) |
| 80 | + |
| 81 | + source_without_partition = BigQuerySource( |
| 82 | + table=table, |
| 83 | + timestamp_field=timestamp_field, |
| 84 | + ) |
| 85 | + source_with_partition = BigQuerySource( |
| 86 | + table=table, |
| 87 | + timestamp_field=timestamp_field, |
| 88 | + date_partition_column=partition_column, |
| 89 | + ) |
| 90 | + |
| 91 | + job_without = BigQueryOfflineStore.pull_all_from_table_or_query( |
| 92 | + config=repo_config, |
| 93 | + data_source=source_without_partition, |
| 94 | + join_key_columns=[], |
| 95 | + feature_name_columns=[], |
| 96 | + timestamp_field=timestamp_field, |
| 97 | + start_date=start, |
| 98 | + end_date=end, |
| 99 | + ) |
| 100 | + job_with = BigQueryOfflineStore.pull_all_from_table_or_query( |
| 101 | + config=repo_config, |
| 102 | + data_source=source_with_partition, |
| 103 | + join_key_columns=[], |
| 104 | + feature_name_columns=[], |
| 105 | + timestamp_field=timestamp_field, |
| 106 | + start_date=start, |
| 107 | + end_date=end, |
| 108 | + ) |
| 109 | + |
| 110 | + sql_without = job_without.to_sql() |
| 111 | + sql_with = job_with.to_sql() |
| 112 | + |
| 113 | + def measure(): |
| 114 | + bytes_without = _estimate_bytes_processed(project, location, sql_without) |
| 115 | + bytes_with = _estimate_bytes_processed(project, location, sql_with) |
| 116 | + return bytes_without, bytes_with |
| 117 | + |
| 118 | + bytes_without, bytes_with = benchmark(measure) |
| 119 | + benchmark.extra_info["total_bytes_processed_without_partition_filter"] = ( |
| 120 | + bytes_without |
| 121 | + ) |
| 122 | + benchmark.extra_info["total_bytes_processed_with_partition_filter"] = bytes_with |
| 123 | + if bytes_without > 0: |
| 124 | + benchmark.extra_info["bytes_ratio_with_over_without"] = ( |
| 125 | + bytes_with / bytes_without |
| 126 | + ) |
| 127 | + |
| 128 | + if os.environ.get("FEAST_BQ_BENCH_REQUIRE_REDUCTION", "").lower() in ( |
| 129 | + "1", |
| 130 | + "true", |
| 131 | + "yes", |
| 132 | + ): |
| 133 | + assert bytes_with < bytes_without |
| 134 | + else: |
| 135 | + assert bytes_with <= bytes_without |
0 commit comments