diff --git a/sdk/python/feast/infra/offline_stores/bigquery.py b/sdk/python/feast/infra/offline_stores/bigquery.py index afe3e6f97cc..ba2917d96e4 100644 --- a/sdk/python/feast/infra/offline_stores/bigquery.py +++ b/sdk/python/feast/infra/offline_stores/bigquery.py @@ -904,9 +904,9 @@ def _bq_ensure_monitoring_tables(config: RepoConfig) -> None: p90 FLOAT64, p95 FLOAT64, p99 FLOAT64, - histogram STRING + histogram STRING, + PRIMARY KEY (project_id, feature_view_name, feature_name, metric_date, granularity, data_source_type) NOT ENFORCED ) -PRIMARY KEY (project_id, feature_view_name, feature_name, metric_date, granularity, data_source_type) NOT ENFORCED """ view_ddl = f""" CREATE TABLE IF NOT EXISTS `{proj}.{ds}.{MON_TABLE_FEATURE_VIEW}` ( @@ -922,9 +922,9 @@ def _bq_ensure_monitoring_tables(config: RepoConfig) -> None: total_features INT64, features_with_nulls INT64, avg_null_rate FLOAT64, - max_null_rate FLOAT64 + max_null_rate FLOAT64, + PRIMARY KEY (project_id, feature_view_name, metric_date, granularity, data_source_type) NOT ENFORCED ) -PRIMARY KEY (project_id, feature_view_name, metric_date, granularity, data_source_type) NOT ENFORCED """ service_ddl = f""" CREATE TABLE IF NOT EXISTS `{proj}.{ds}.{MON_TABLE_FEATURE_SERVICE}` ( @@ -939,9 +939,9 @@ def _bq_ensure_monitoring_tables(config: RepoConfig) -> None: total_feature_views INT64, total_features INT64, avg_null_rate FLOAT64, - max_null_rate FLOAT64 + max_null_rate FLOAT64, + PRIMARY KEY (project_id, feature_service_name, metric_date, granularity, data_source_type) NOT ENFORCED ) -PRIMARY KEY (project_id, feature_service_name, metric_date, granularity, data_source_type) NOT ENFORCED """ job_ddl = f""" CREATE TABLE IF NOT EXISTS `{proj}.{ds}.{MON_TABLE_JOB}` ( @@ -955,9 +955,9 @@ def _bq_ensure_monitoring_tables(config: RepoConfig) -> None: started_at TIMESTAMP, completed_at TIMESTAMP, error_message STRING, - result_summary STRING + result_summary STRING, + PRIMARY KEY (job_id) NOT ENFORCED ) -PRIMARY KEY (job_id) NOT ENFORCED """ for ddl in (feature_ddl, view_ddl, service_ddl, job_ddl): client.query(ddl).result() diff --git a/sdk/python/tests/unit/infra/offline_stores/test_bigquery.py b/sdk/python/tests/unit/infra/offline_stores/test_bigquery.py index 132339e1034..16a48648791 100644 --- a/sdk/python/tests/unit/infra/offline_stores/test_bigquery.py +++ b/sdk/python/tests/unit/infra/offline_stores/test_bigquery.py @@ -1,3 +1,4 @@ +import re from datetime import datetime, timezone from unittest.mock import Mock, patch @@ -9,9 +10,16 @@ BigQueryOfflineStore, BigQueryOfflineStoreConfig, BigQueryRetrievalJob, + _bq_ensure_monitoring_tables, ) from feast.infra.offline_stores.bigquery_source import BigQuerySource from feast.infra.online_stores.sqlite import SqliteOnlineStoreConfig +from feast.monitoring.monitoring_utils import ( + MON_TABLE_FEATURE, + MON_TABLE_FEATURE_SERVICE, + MON_TABLE_FEATURE_VIEW, + MON_TABLE_JOB, +) from feast.repo_config import RepoConfig @@ -372,3 +380,52 @@ def test_offline_write_batch_enables_list_inference(self, mock_get_client): job_config = call_kwargs[1]["job_config"] assert job_config.parquet_options is not None assert job_config.parquet_options.enable_list_inference is True + + +@patch("feast.infra.offline_stores.bigquery._get_bigquery_client") +def test_ensure_monitoring_tables_primary_key_inside_column_list( + mock_get_bigquery_client, +): + """BigQuery only accepts table constraints inside the table element list. + + ``CREATE TABLE t (...) PRIMARY KEY (...) NOT ENFORCED`` is rejected at parse + time, so each PRIMARY KEY clause must come before the closing parenthesis. + """ + mock_client = Mock() + mock_get_bigquery_client.return_value = mock_client + test_repo_config = RepoConfig( + registry="gs://ml-test/repo/registry.db", + project="test", + provider="gcp", + online_store=SqliteOnlineStoreConfig(type="sqlite"), + offline_store=BigQueryOfflineStoreConfig( + type="bigquery", project_id="my-project", dataset="my_dataset" + ), + ) + + _bq_ensure_monitoring_tables(test_repo_config) + + create_ddls = [ + call.args[0] + for call in mock_client.query.call_args_list + if "CREATE TABLE" in call.args[0] + ] + created_tables = { + re.search(r"`my-project\.my_dataset\.(\w+)`", ddl).group(1) + for ddl in create_ddls + } + assert created_tables == { + MON_TABLE_FEATURE, + MON_TABLE_FEATURE_VIEW, + MON_TABLE_FEATURE_SERVICE, + MON_TABLE_JOB, + } + for ddl in create_ddls: + statement = ddl.strip() + assert statement.endswith(")"), ( + f"Nothing may follow the table element list:\n{ddl}" + ) + element_list = statement[statement.index("(") + 1 : -1] + assert re.search(r"PRIMARY KEY \([^)]*\) NOT ENFORCED\s*$", element_list), ( + f"PRIMARY KEY ... NOT ENFORCED must be inside the element list:\n{ddl}" + )