Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
109 changes: 82 additions & 27 deletions sdk/python/feast/infra/compute_engines/snowflake/snowflake_engine.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import functools
import os
import re
import shutil
import warnings
from datetime import timezone
from typing import Literal, Optional, Sequence, Union
from tempfile import TemporaryDirectory
from typing import Literal, Optional, Sequence, Set, Tuple, Union

import click
import pandas as pd
Expand Down Expand Up @@ -34,6 +36,7 @@
execute_snowflake_statement,
get_snowflake_online_store_path,
package_snowpark_zip,
snowpark_udf_fingerprint,
)
from feast.on_demand_feature_view import OnDemandFeatureView
from feast.protos.feast.types.EntityKey_pb2 import EntityKey as EntityKeyProto
Expand Down Expand Up @@ -124,6 +127,19 @@ def validate_python_udf_runtime_version(cls, v: str) -> str:
return v


_SNOWPARK_DIR = f"{os.path.dirname(feast.__file__)}/infra/utils/snowflake/snowpark"
_UDF_CREATION_SQL = f"{_SNOWPARK_DIR}/snowflake_python_udfs_creation.sql"
_UDF_DELETION_SQL = f"{_SNOWPARK_DIR}/snowflake_python_udfs_deletion.sql"


def _udf_data(python_udf_runtime_version: str) -> Tuple[str, Set[str]]:
with open(_UDF_CREATION_SQL, "r") as file:
sql = file.read()
fingerprint = snowpark_udf_fingerprint(sql, python_udf_runtime_version)
templates = set(re.findall(r"[\w$]*PROJECT_NAME[\w$]*", sql))
return fingerprint, templates


class SnowflakeComputeEngine(ComputeEngine):
def get_historical_features(
self, registry: BaseRegistry, task: HistoricalRetrievalTask
Expand All @@ -145,17 +161,18 @@ def update(
entities_to_keep: Sequence[Entity],
):
stage_path = f'"{self.repo_config.batch_engine.database}"."{self.repo_config.batch_engine.schema_}"."feast_{project}"'
udf_namespace = self._udf_namespace(project)
udf_path = f"{stage_path}/{udf_namespace}"
with GetSnowflakeConnection(self.repo_config.batch_engine) as conn:
# Always (re)deploy the materialization functions, using
# `CREATE OR REPLACE FUNCTION` below, instead of skipping deployment
# when functions already exist. Previously, an early return here
# (triggered by a `SHOW USER FUNCTIONS` check) combined with
# `CREATE FUNCTION IF NOT EXISTS` in the SQL template meant that
# once UDFs were deployed for a project, they were never
# redeployed -- so a config change to `python_udf_runtime_version`
# (e.g. after Snowflake decommissions a runtime) would silently
# keep using the stale, already-deployed runtime version. See
# https://github.com/feast-dev/feast/pull/6608#discussion_r3602461959.
if self._udf_names(udf_namespace) <= self._query_udf_names(
conn, udf_namespace
):
click.echo(
f"Materialization functions for {Style.BRIGHT + Fore.GREEN}{project}{Style.RESET_ALL} already exist."
)
click.echo()
return None

click.echo(
f"Deploying materialization functions for {Style.BRIGHT + Fore.GREEN}{project}{Style.RESET_ALL}"
)
Expand All @@ -164,21 +181,19 @@ def update(
query = f"CREATE STAGE IF NOT EXISTS {stage_path}"
execute_snowflake_statement(conn, query)

copy_path, zip_path = package_snowpark_zip(project)
query = f"PUT file://{zip_path} @{stage_path}"
execute_snowflake_statement(conn, query)

shutil.rmtree(copy_path)
with TemporaryDirectory() as copy_path:
zip_path = package_snowpark_zip(copy_path)
query = f"PUT file://{zip_path} @{udf_path}"
execute_snowflake_statement(conn, query)

# Execute snowflake python udf creation functions
sql_function_file = f"{os.path.dirname(feast.__file__)}/infra/utils/snowflake/snowpark/snowflake_python_udfs_creation.sql"
with open(sql_function_file, "r") as file:
with open(_UDF_CREATION_SQL, "r") as file:
sqlFile = file.read()

sqlCommands = sqlFile.split(";")
for command in sqlCommands:
command = command.replace("STAGE_HOLDER", f"{stage_path}")
command = command.replace("PROJECT_NAME", f"{project}")
command = command.replace("STAGE_HOLDER", f"{udf_path}")
command = command.replace("PROJECT_NAME", f"{udf_namespace}")
query = command.replace(
"RUNTIME_VERSION_HOLDER",
self.repo_config.batch_engine.python_udf_runtime_version,
Expand All @@ -192,24 +207,59 @@ def teardown_infra(
project: str,
fvs: Sequence[Union[BatchFeatureView, StreamFeatureView, FeatureView]],
entities: Sequence[Entity],
):
try:
self._teardown_udfs(project, fvs, entities)
except Exception as e:
warnings.warn(
f"Could not drop the Snowflake materialization UDFs for {project}, "
f"continuing the teardown: {e}",
RuntimeWarning,
)

def _teardown_udfs(
self,
project: str,
fvs: Sequence[Union[BatchFeatureView, StreamFeatureView, FeatureView]],
entities: Sequence[Entity],
):
stage_path = f'"{self.repo_config.batch_engine.database}"."{self.repo_config.batch_engine.schema_}"."feast_{project}"'
udf_namespace = self._udf_namespace(project)
with GetSnowflakeConnection(self.repo_config.batch_engine) as conn:
query = f"DROP STAGE IF EXISTS {stage_path}"
execute_snowflake_statement(conn, query)

# Execute snowflake python udf deletion functions
sql_function_file = f"{os.path.dirname(feast.__file__)}/infra/utils/snowflake/snowpark/snowflake_python_udfs_deletion.sql"
with open(sql_function_file, "r") as file:
with open(_UDF_DELETION_SQL, "r") as file:
sqlFile = file.read()

sqlCommands = sqlFile.split(";")
for command in sqlCommands:
query = command.replace("PROJECT_NAME", f"{project}")
query = command.replace("PROJECT_NAME", f"{udf_namespace}")
Comment thread
daniel-kristjansson marked this conversation as resolved.
Resolved
execute_snowflake_statement(conn, query)

return None

def _udf_namespace(self, project: str) -> str:
return f"{project}_{self._udf_fingerprint}"

def _udf_names(self, udf_namespace: str) -> Set[str]:
return {
name.replace("PROJECT_NAME", udf_namespace).upper()
for name in self._udf_templates
}

def _query_udf_names(self, conn, udf_namespace: str) -> Set[str]:
schema = f'"{self.repo_config.batch_engine.database}"."{self.repo_config.batch_engine.schema_}"'
query = f"SHOW USER FUNCTIONS LIKE 'FEAST_{udf_namespace.upper()}_%' IN SCHEMA {schema}"
cursor = execute_snowflake_statement(conn, query)
name_index = [column.name for column in cursor.description].index("name")
return {row[name_index].upper() for row in cursor.fetchall()}

@functools.lru_cache(maxsize=None)
def _ensure_udfs(self, project: str) -> None:
self.update(project, [], [], [], [])

def __init__(
self,
*,
Expand All @@ -228,6 +278,9 @@ def __init__(
online_store=online_store,
**kwargs,
)
self._udf_fingerprint, self._udf_templates = _udf_data(
repo_config.batch_engine.python_udf_runtime_version
)

def _materialize_one(
self,
Expand Down Expand Up @@ -262,6 +315,7 @@ def _materialize_one(
job_id = f"{feature_view.name}-{start_date}-{end_date}"

try:
self._ensure_udfs(project)
offline_job = self.offline_store.pull_latest_from_table_or_query(
config=self.repo_config,
data_source=feature_view.batch_source, # type: ignore[arg-type]
Expand Down Expand Up @@ -374,6 +428,7 @@ def generate_snowflake_materialization_query(
project: str,
) -> str:
assert feature_view.batch_source is not None
udf_namespace = self._udf_namespace(project)
if feature_view.batch_source.created_timestamp_column:
fv_created_str = f',"{feature_view.batch_source.created_timestamp_column}"'
else:
Expand All @@ -397,7 +452,7 @@ def generate_snowflake_materialization_query(
feature_value_type_name = feature.dtype.to_value_type().name

feature_sql = _convert_value_name_to_snowflake_udf(
feature_value_type_name, project
feature_value_type_name, udf_namespace
)

if feature_value_type_name == "UNIX_TIMESTAMP":
Expand All @@ -414,9 +469,9 @@ def generate_snowflake_materialization_query(
features_str = ",\n".join(feature_sql_list)

if repo_config.online_store.type == "snowflake.online":
serial_func = f"feast_{project}_serialize_entity_keys"
serial_func = f"feast_{udf_namespace}_serialize_entity_keys"
else:
serial_func = f"feast_{project}_entity_key_proto_to_string"
serial_func = f"feast_{udf_namespace}_entity_key_proto_to_string"

fv_to_proto_sql = f"""
SELECT
Expand Down
49 changes: 34 additions & 15 deletions sdk/python/feast/infra/utils/snowflake/snowflake_utils.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import configparser
import hashlib
import os
import random
import shutil
Expand Down Expand Up @@ -140,24 +141,42 @@ def get_snowflake_online_store_path(
return online_path


def package_snowpark_zip(project_name) -> Tuple[str, str]:
path = os.path.dirname(feast.__file__)
copy_path = path + f"/snowflake_feast_{project_name}"
# Feast source files packaged into the Snowpark UDF zip (paths relative to the
# `feast` package). The UDFs import their serialization code from this zip.
SNOWPARK_ZIP_FILES = (
"/infra/utils/snowflake/snowpark/snowflake_udfs.py",
"/infra/key_encoding_utils.py",
"/type_map.py",
"/value_type.py",
"/protos/feast/types/Value_pb2.py",
"/protos/feast/types/EntityKey_pb2.py",
)


if os.path.exists(copy_path):
shutil.rmtree(copy_path)
def snowpark_udf_fingerprint(*extra: str) -> str:
"""Content hash of the Snowpark UDF payload, plus any `extra` inputs.

copy_files = [
"/infra/utils/snowflake/snowpark/snowflake_udfs.py",
"/infra/key_encoding_utils.py",
"/type_map.py",
"/value_type.py",
"/protos/feast/types/Value_pb2.py",
"/protos/feast/types/EntityKey_pb2.py",
]
Hashes the source files in `SNOWPARK_ZIP_FILES` rather than the zip itself:
`shutil.make_archive` embeds file modification times, so identical code
zips to different bytes on every install.
"""
path = os.path.dirname(feast.__file__)
digest = hashlib.sha256()
for feast_file in SNOWPARK_ZIP_FILES:
digest.update(feast_file.encode("utf8") + b"\0")
with open(path + feast_file, "rb") as file:
digest.update(file.read())
digest.update(b"\0")
for value in extra:
digest.update(value.encode("utf8") + b"\0")
return digest.hexdigest()[:8]


def package_snowpark_zip(copy_path: str) -> str:
path = os.path.dirname(feast.__file__)

package_path = copy_path + "/feast"
for feast_file in copy_files:
for feast_file in SNOWPARK_ZIP_FILES:
idx = feast_file.rfind("/")
if idx > -1:
Path(package_path + feast_file[:idx]).mkdir(parents=True, exist_ok=True)
Expand All @@ -167,7 +186,7 @@ def package_snowpark_zip(project_name) -> Tuple[str, str]:

zip_path = shutil.make_archive(package_path, "zip", copy_path)

return copy_path, zip_path
return zip_path


def _run_snowflake_field_mapping(snowflake_job_sql: str, field_mapping: dict) -> str:
Expand Down
Loading