-
Notifications
You must be signed in to change notification settings - Fork 1.3k
feat: Implement Databricks Unity Catalog offline store integration #6515
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
falloficaruss
wants to merge
3
commits into
feast-dev:master
Choose a base branch
from
falloficaruss:feat/databricks-uc-provider
base: master
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.
+512
−0
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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
318 changes: 318 additions & 0 deletions
318
sdk/python/feast/infra/offline_stores/contrib/spark_offline_store/databricks_uc.py
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,318 @@ | ||
| import logging | ||
| from datetime import date, datetime | ||
| from typing import Any, Callable, Dict, Iterable, List, Optional, Tuple, Union | ||
|
|
||
| import pandas as pd | ||
| import pyarrow | ||
| import pyspark | ||
| from pydantic import StrictStr | ||
| from pyspark import SparkConf | ||
| from pyspark.sql import SparkSession | ||
|
|
||
| from feast import FeatureView | ||
| from feast.data_source import DataSource | ||
| from feast.infra.offline_stores.contrib.spark_offline_store.spark import ( | ||
| SparkOfflineStore, | ||
| SparkOfflineStoreConfig, | ||
| ) | ||
| from feast.infra.offline_stores.offline_store import RetrievalJob | ||
| from feast.infra.registry.base_registry import BaseRegistry | ||
| from feast.repo_config import RepoConfig | ||
|
|
||
| logger = logging.getLogger(__name__) | ||
|
|
||
|
|
||
| class DatabricksUCOfflineStoreConfig(SparkOfflineStoreConfig): | ||
| type: StrictStr = "databricks_uc" | ||
| """Offline store type selector""" | ||
|
|
||
| workspace_host: Optional[StrictStr] = None | ||
| """Databricks workspace host (e.g. adb-xxxx.azuredatabricks.net)""" | ||
|
|
||
| token: Optional[StrictStr] = None | ||
| """Databricks Personal Access Token (PAT)""" | ||
|
|
||
| cluster_id: Optional[StrictStr] = None | ||
| """Databricks Cluster ID to connect to for Databricks Connect""" | ||
|
|
||
| default_catalog: Optional[StrictStr] = None | ||
| """Default catalog name to use in Unity Catalog""" | ||
|
|
||
| default_schema: Optional[StrictStr] = None | ||
| """Default schema name to use in Unity Catalog""" | ||
|
|
||
|
|
||
| def get_databricks_session( | ||
| store_config: DatabricksUCOfflineStoreConfig, | ||
| ) -> SparkSession: | ||
| # Check if there is already an active session | ||
| spark_session = SparkSession.getActiveSession() | ||
| if not spark_session: | ||
| workspace_host = store_config.workspace_host | ||
| token = store_config.token | ||
| cluster_id = store_config.cluster_id | ||
|
|
||
| # Clean host URL if it starts with https:// | ||
| if workspace_host: | ||
| if workspace_host.startswith("https://"): | ||
| workspace_host = workspace_host[8:] | ||
| elif workspace_host.startswith("http://"): | ||
| workspace_host = workspace_host[7:] | ||
|
|
||
| if workspace_host and cluster_id: | ||
| # Databricks Connect V2 initialization (Spark Connect URI format) | ||
| conn_str = f"sc://{workspace_host}:443/" | ||
| params = [] | ||
| if token: | ||
| params.append(f"token={token}") | ||
| params.append(f"x-databricks-cluster-id={cluster_id}") | ||
| if params: | ||
| conn_str = f"{conn_str};{';'.join(params)}" | ||
|
|
||
| try: | ||
| from databricks.connect import DatabricksSession | ||
|
|
||
| builder = DatabricksSession.builder.remote(conn_str) | ||
| except ImportError: | ||
| # Fallback to standard PySpark remote connect if databricks-connect not installed | ||
| builder = SparkSession.builder.remote(conn_str) | ||
| else: | ||
| try: | ||
| from databricks.connect import DatabricksSession | ||
|
|
||
| builder = DatabricksSession.builder | ||
| except ImportError: | ||
| builder = SparkSession.builder | ||
|
|
||
| spark_conf = store_config.spark_conf | ||
| if spark_conf: | ||
| builder = builder.config( | ||
| conf=SparkConf().setAll([(k, v) for k, v in spark_conf.items()]) | ||
| ) | ||
|
|
||
| spark_session = builder.getOrCreate() | ||
|
|
||
| # Apply configuration defaults | ||
| spark_session.conf.set("spark.sql.parser.quotedRegexColumnNames", "true") | ||
|
|
||
| if store_config.default_catalog: | ||
| spark_session.sql(f"USE CATALOG `{store_config.default_catalog}`") | ||
| if store_config.default_schema: | ||
| spark_session.sql(f"USE SCHEMA `{store_config.default_schema}`") | ||
|
|
||
| return spark_session | ||
|
|
||
|
|
||
| class DatabricksUCOfflineStore(SparkOfflineStore): | ||
| @staticmethod | ||
| def pull_latest_from_table_or_query( | ||
| config: RepoConfig, | ||
| data_source: DataSource, | ||
| join_key_columns: List[str], | ||
| feature_name_columns: List[str], | ||
| timestamp_field: str, | ||
| created_timestamp_column: Optional[str], | ||
| start_date: datetime, | ||
| end_date: datetime, | ||
| ) -> RetrievalJob: | ||
| assert isinstance(config.offline_store, DatabricksUCOfflineStoreConfig) | ||
| # Initialize/Retrieve the Databricks Spark Session so it's registered as active | ||
| get_databricks_session(config.offline_store) | ||
|
|
||
| return SparkOfflineStore.pull_latest_from_table_or_query( | ||
| config=config, | ||
| data_source=data_source, | ||
| join_key_columns=join_key_columns, | ||
| feature_name_columns=feature_name_columns, | ||
| timestamp_field=timestamp_field, | ||
| created_timestamp_column=created_timestamp_column, | ||
| start_date=start_date, | ||
| end_date=end_date, | ||
| ) | ||
|
|
||
| @staticmethod | ||
| def get_historical_features( | ||
| config: RepoConfig, | ||
| feature_views: List[FeatureView], | ||
| feature_refs: List[str], | ||
| entity_df: Optional[Union[pd.DataFrame, str, pyspark.sql.DataFrame]], | ||
| registry: BaseRegistry, | ||
| project: str, | ||
| full_feature_names: bool = False, | ||
| **kwargs, | ||
| ) -> RetrievalJob: | ||
| assert isinstance(config.offline_store, DatabricksUCOfflineStoreConfig) | ||
| get_databricks_session(config.offline_store) | ||
|
|
||
| return SparkOfflineStore.get_historical_features( | ||
| config=config, | ||
| feature_views=feature_views, | ||
| feature_refs=feature_refs, | ||
| entity_df=entity_df, | ||
| registry=registry, | ||
| project=project, | ||
| full_feature_names=full_feature_names, | ||
| **kwargs, | ||
| ) | ||
|
|
||
| @staticmethod | ||
| def pull_all_from_table_or_query( | ||
| config: RepoConfig, | ||
| data_source: DataSource, | ||
| join_key_columns: List[str], | ||
| feature_name_columns: List[str], | ||
| timestamp_field: str, | ||
| created_timestamp_column: Optional[str] = None, | ||
| start_date: Optional[datetime] = None, | ||
| end_date: Optional[datetime] = None, | ||
| ) -> RetrievalJob: | ||
| assert isinstance(config.offline_store, DatabricksUCOfflineStoreConfig) | ||
| get_databricks_session(config.offline_store) | ||
|
|
||
| return SparkOfflineStore.pull_all_from_table_or_query( | ||
| config=config, | ||
| data_source=data_source, | ||
| join_key_columns=join_key_columns, | ||
| feature_name_columns=feature_name_columns, | ||
| timestamp_field=timestamp_field, | ||
| created_timestamp_column=created_timestamp_column, | ||
| start_date=start_date, | ||
| end_date=end_date, | ||
| ) | ||
|
|
||
| @staticmethod | ||
| def offline_write_batch( | ||
| config: RepoConfig, | ||
| feature_view: FeatureView, | ||
| table: pyarrow.Table, | ||
| progress: Optional[Callable[[int], Any]], | ||
| ): | ||
| assert isinstance(config.offline_store, DatabricksUCOfflineStoreConfig) | ||
| get_databricks_session(config.offline_store) | ||
|
|
||
| return SparkOfflineStore.offline_write_batch( | ||
| config=config, | ||
| feature_view=feature_view, | ||
| table=table, | ||
| progress=progress, | ||
| ) | ||
|
|
||
| @staticmethod | ||
| def compute_monitoring_metrics( | ||
| config: RepoConfig, | ||
| data_source: DataSource, | ||
| feature_columns: List[Tuple[str, str]], | ||
| timestamp_field: str, | ||
| start_date: Optional[datetime] = None, | ||
| end_date: Optional[datetime] = None, | ||
| histogram_bins: int = 20, | ||
| top_n: int = 10, | ||
| ) -> List[Dict[str, Any]]: | ||
| assert isinstance(config.offline_store, DatabricksUCOfflineStoreConfig) | ||
| get_databricks_session(config.offline_store) | ||
|
|
||
| return SparkOfflineStore.compute_monitoring_metrics( | ||
| config=config, | ||
| data_source=data_source, | ||
| feature_columns=feature_columns, | ||
| timestamp_field=timestamp_field, | ||
| start_date=start_date, | ||
| end_date=end_date, | ||
| histogram_bins=histogram_bins, | ||
| top_n=top_n, | ||
| ) | ||
|
|
||
| @staticmethod | ||
| def get_monitoring_max_timestamp( | ||
| config: RepoConfig, | ||
| data_source: DataSource, | ||
| timestamp_field: str, | ||
| ) -> Optional[datetime]: | ||
| assert isinstance(config.offline_store, DatabricksUCOfflineStoreConfig) | ||
| get_databricks_session(config.offline_store) | ||
|
|
||
| return SparkOfflineStore.get_monitoring_max_timestamp( | ||
| config=config, | ||
| data_source=data_source, | ||
| timestamp_field=timestamp_field, | ||
| ) | ||
|
|
||
| @staticmethod | ||
| def ensure_monitoring_tables(config: RepoConfig) -> None: | ||
| assert isinstance(config.offline_store, DatabricksUCOfflineStoreConfig) | ||
| get_databricks_session(config.offline_store) | ||
|
|
||
| return SparkOfflineStore.ensure_monitoring_tables(config=config) | ||
|
|
||
| @staticmethod | ||
| def save_monitoring_metrics( | ||
| config: RepoConfig, | ||
| metric_type: str, | ||
| metrics: List[Dict[str, Any]], | ||
| ) -> None: | ||
| assert isinstance(config.offline_store, DatabricksUCOfflineStoreConfig) | ||
| get_databricks_session(config.offline_store) | ||
|
|
||
| return SparkOfflineStore.save_monitoring_metrics( | ||
| config=config, | ||
| metric_type=metric_type, | ||
| metrics=metrics, | ||
| ) | ||
|
|
||
| @staticmethod | ||
| def query_monitoring_metrics( | ||
| config: RepoConfig, | ||
| project: str, | ||
| metric_type: str, | ||
| filters: Optional[Dict[str, Any]] = None, | ||
| start_date: Optional[date] = None, | ||
| end_date: Optional[date] = None, | ||
| ) -> List[Dict[str, Any]]: | ||
| assert isinstance(config.offline_store, DatabricksUCOfflineStoreConfig) | ||
| get_databricks_session(config.offline_store) | ||
|
|
||
| return SparkOfflineStore.query_monitoring_metrics( | ||
| config=config, | ||
| project=project, | ||
| metric_type=metric_type, | ||
| filters=filters, | ||
| start_date=start_date, | ||
| end_date=end_date, | ||
| ) | ||
|
|
||
| @staticmethod | ||
| def clear_monitoring_baseline( | ||
| config: RepoConfig, | ||
| project: str, | ||
| feature_view_name: Optional[str] = None, | ||
| feature_name: Optional[str] = None, | ||
| data_source_type: Optional[str] = None, | ||
| ) -> None: | ||
| assert isinstance(config.offline_store, DatabricksUCOfflineStoreConfig) | ||
| get_databricks_session(config.offline_store) | ||
|
|
||
| return SparkOfflineStore.clear_monitoring_baseline( | ||
| config=config, | ||
| project=project, | ||
| feature_view_name=feature_view_name, | ||
| feature_name=feature_name, | ||
| data_source_type=data_source_type, | ||
| ) | ||
|
|
||
| @staticmethod | ||
| def validate_data_source( | ||
| config: RepoConfig, | ||
| data_source: DataSource, | ||
| ): | ||
| assert isinstance(config.offline_store, DatabricksUCOfflineStoreConfig) | ||
| get_databricks_session(config.offline_store) | ||
| data_source.validate(config=config) | ||
|
|
||
| @staticmethod | ||
| def get_table_column_names_and_types_from_data_source( | ||
| config: RepoConfig, | ||
| data_source: DataSource, | ||
| ) -> Iterable[Tuple[str, str]]: | ||
| assert isinstance(config.offline_store, DatabricksUCOfflineStoreConfig) | ||
| get_databricks_session(config.offline_store) | ||
| return data_source.get_table_column_names_and_types(config=config) | ||
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
Oops, something went wrong.
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.
When users run
feast applyorfeast planwith source validation enabled, this subclass still inheritsOfflineStore.validate_data_sourceandget_table_column_names_and_types_from_data_source, so validation goes throughSparkSource.validate()and starts a plain Spark session viaget_spark_session_or_start_new_with_repoconfiginstead of this Databricks helper. In a local Databricks Connect setup with no active Databricks session, UC tables are validated against a local/non-UC Spark session and fail before any of the wrapped retrieval methods run; override these validation/schema methods to initialize the Databricks session too.Useful? React with 👍 / 👎.