Skip to content
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

[FEAT]: Daft support for Azure storage for Unity Catalog daft.read_deltalake #3025

Merged
merged 4 commits into from
Oct 15, 2024
Merged
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
55 changes: 32 additions & 23 deletions daft/delta_lake/delta_lake_scan.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import logging
import os
from typing import TYPE_CHECKING
from urllib.parse import urlparse

from deltalake.table import DeltaTable

Expand Down Expand Up @@ -40,31 +41,39 @@ def __init__(
#
# See: https://github.com/delta-io/delta-rs/issues/2117
deltalake_sdk_io_config = storage_config.config.io_config
if any([deltalake_sdk_io_config.s3.key_id is None, deltalake_sdk_io_config.s3.region_name is None]):
try:
s3_config_from_env = S3Config.from_env()
# Sometimes S3Config.from_env throws an error, for example on CI machines with weird metadata servers.
except daft.exceptions.DaftCoreException:
pass
else:
if (
deltalake_sdk_io_config.s3.key_id is None
and deltalake_sdk_io_config.s3.access_key is None
and deltalake_sdk_io_config.s3.session_token is None
):
deltalake_sdk_io_config = deltalake_sdk_io_config.replace(
s3=deltalake_sdk_io_config.s3.replace(
key_id=s3_config_from_env.key_id,
access_key=s3_config_from_env.access_key,
session_token=s3_config_from_env.session_token,
scheme = urlparse(table_uri).scheme
if scheme == "s3" or scheme == "s3a":
if any([deltalake_sdk_io_config.s3.key_id is None, deltalake_sdk_io_config.s3.region_name is None]):
try:
s3_config_from_env = S3Config.from_env()
# Sometimes S3Config.from_env throws an error, for example on CI machines with weird metadata servers.
except daft.exceptions.DaftCoreException:
pass
else:
if (
deltalake_sdk_io_config.s3.key_id is None
and deltalake_sdk_io_config.s3.access_key is None
and deltalake_sdk_io_config.s3.session_token is None
):
deltalake_sdk_io_config = deltalake_sdk_io_config.replace(
s3=deltalake_sdk_io_config.s3.replace(
key_id=s3_config_from_env.key_id,
access_key=s3_config_from_env.access_key,
session_token=s3_config_from_env.session_token,
)
)
)
if deltalake_sdk_io_config.s3.region_name is None:
deltalake_sdk_io_config = deltalake_sdk_io_config.replace(
s3=deltalake_sdk_io_config.s3.replace(
region_name=s3_config_from_env.region_name,
if deltalake_sdk_io_config.s3.region_name is None:
deltalake_sdk_io_config = deltalake_sdk_io_config.replace(
s3=deltalake_sdk_io_config.s3.replace(
region_name=s3_config_from_env.region_name,
)
)
)
elif scheme == "gcs" or scheme == "gs":
# TO-DO: Handle any key-value replacements in `io_config` if there are missing elements
pass
elif scheme == "az" or scheme == "abfs" or scheme == "abfss":
# TO-DO: Handle any key-value replacements in `io_config` if there are missing elements
pass

self._table = DeltaTable(
table_uri, storage_options=io_config_to_storage_options(deltalake_sdk_io_config, table_uri)
Expand Down
33 changes: 22 additions & 11 deletions daft/unity_catalog/unity_catalog.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@

import dataclasses
from typing import Callable
from urllib.parse import urlparse

import unitycatalog

from daft.io import IOConfig, S3Config
from daft.io import AzureConfig, IOConfig, S3Config


@dataclasses.dataclass(frozen=True)
Expand Down Expand Up @@ -96,18 +97,28 @@ def load_table(self, table_name: str) -> UnityCatalogTable:

# Grab credentials from Unity catalog and place it into the Table
temp_table_credentials = self._client.temporary_table_credentials.create(operation="READ", table_id=table_id)
aws_temp_credentials = temp_table_credentials.aws_temp_credentials
io_config = (
IOConfig(
s3=S3Config(
key_id=aws_temp_credentials.access_key_id,
access_key=aws_temp_credentials.secret_access_key,
session_token=aws_temp_credentials.session_token,

scheme = urlparse(storage_location).scheme
if scheme == "s3" or scheme == "s3a":
aws_temp_credentials = temp_table_credentials.aws_temp_credentials
io_config = (
IOConfig(
s3=S3Config(
key_id=aws_temp_credentials.access_key_id,
access_key=aws_temp_credentials.secret_access_key,
session_token=aws_temp_credentials.session_token,
)
)
if aws_temp_credentials is not None
else None
)
elif scheme == "gcs" or scheme == "gs":
# TO-DO: gather GCS credential vending assets from Unity and construct 'io_config``
pass
elif scheme == "az" or scheme == "abfs" or scheme == "abfss":
io_config = IOConfig(
azure=AzureConfig(sas_token=temp_table_credentials.azure_user_delegation_sas.get("sas_token"))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great, do we know what version of the unity catalog SDK this requires? I feel like this might have been added in a later version and might need us to regenerate the SDK. Have you tested this yet?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey @jaychia , I think this should be fine for the existing version shipped out . I tested on Daft 0.3.4 and it includes unitycatalog==0.1.1 . On this SDK version, the sas_token is being returned successfully.
I have tested this internally by walking through the function execution flow, using a virtual environment created with Daft 0.3.4 without any other upgrades and it has worked well for me. I could use the vended credentials in subsequent calls by instantiating an object of DeltaLakeScanOperator from it.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Amazing

)
if aws_temp_credentials is not None
else None
)

return UnityCatalogTable(
table_uri=storage_location,
Expand Down
Loading