Skip to content
Open
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
9 changes: 5 additions & 4 deletions pyiceberg/catalog/dynamodb.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,14 +120,15 @@ def __init__(self, name: str, client: Optional["DynamoDBClient"] = None, **prope
if client is not None:
self.dynamodb = client
else:
region_name = get_first_property_value(properties, DYNAMODB_REGION, AWS_REGION)
region_name = get_first_property_value(properties, DYNAMODB_REGION, AWS_REGION) or None
session = boto3.Session(
profile_name=properties.get(DYNAMODB_PROFILE_NAME),
region_name=region_name,
botocore_session=properties.get(BOTOCORE_SESSION),
aws_access_key_id=get_first_property_value(properties, DYNAMODB_ACCESS_KEY_ID, AWS_ACCESS_KEY_ID),
aws_secret_access_key=get_first_property_value(properties, DYNAMODB_SECRET_ACCESS_KEY, AWS_SECRET_ACCESS_KEY),
aws_session_token=get_first_property_value(properties, DYNAMODB_SESSION_TOKEN, AWS_SESSION_TOKEN),
aws_access_key_id=get_first_property_value(properties, DYNAMODB_ACCESS_KEY_ID, AWS_ACCESS_KEY_ID) or None,
aws_secret_access_key=get_first_property_value(properties, DYNAMODB_SECRET_ACCESS_KEY, AWS_SECRET_ACCESS_KEY)
or None,
aws_session_token=get_first_property_value(properties, DYNAMODB_SESSION_TOKEN, AWS_SESSION_TOKEN) or None,
)
if role_arn := get_first_property_value(properties, DYNAMODB_ROLE_ARN, AWS_ASSUME_ROLE_ARN, AWS_ROLE_ARN):
session = _get_aws_session_with_assumed_role(
Expand Down
10 changes: 5 additions & 5 deletions pyiceberg/catalog/glue.py
Original file line number Diff line number Diff line change
Expand Up @@ -363,14 +363,14 @@ def __init__(self, name: str, client: Optional["GlueClient"] = None, **propertie
else:
retry_mode_prop_value = get_first_property_value(properties, GLUE_RETRY_MODE)

region_name = get_first_property_value(properties, GLUE_REGION, AWS_REGION)
region_name = get_first_property_value(properties, GLUE_REGION, AWS_REGION) or None
session = boto3.Session(
profile_name=get_first_property_value(properties, GLUE_PROFILE_NAME, AWS_PROFILE_NAME),
profile_name=get_first_property_value(properties, GLUE_PROFILE_NAME, AWS_PROFILE_NAME) or None,
region_name=region_name,
botocore_session=properties.get(BOTOCORE_SESSION),
aws_access_key_id=get_first_property_value(properties, GLUE_ACCESS_KEY_ID, AWS_ACCESS_KEY_ID),
aws_secret_access_key=get_first_property_value(properties, GLUE_SECRET_ACCESS_KEY, AWS_SECRET_ACCESS_KEY),
aws_session_token=get_first_property_value(properties, GLUE_SESSION_TOKEN, AWS_SESSION_TOKEN),
aws_access_key_id=get_first_property_value(properties, GLUE_ACCESS_KEY_ID, AWS_ACCESS_KEY_ID) or None,
aws_secret_access_key=get_first_property_value(properties, GLUE_SECRET_ACCESS_KEY, AWS_SECRET_ACCESS_KEY) or None,
aws_session_token=get_first_property_value(properties, GLUE_SESSION_TOKEN, AWS_SESSION_TOKEN) or None,
)
if role_arn := get_first_property_value(properties, GLUE_ROLE_ARN, AWS_ASSUME_ROLE_ARN, AWS_ROLE_ARN):
session = _get_aws_session_with_assumed_role(
Expand Down
10 changes: 5 additions & 5 deletions pyiceberg/catalog/rest/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -1135,12 +1135,12 @@ def __init__(self, **properties: str):
max_retries = property_as_int(self._properties, SIGV4_MAX_RETRIES, SIGV4_MAX_RETRIES_DEFAULT)
super().__init__(max_retries=max_retries)
self._boto_session = boto3.Session(
profile_name=get_first_property_value(self._properties, AWS_PROFILE_NAME),
region_name=get_first_property_value(self._properties, AWS_REGION),
profile_name=get_first_property_value(self._properties, AWS_PROFILE_NAME) or None,
region_name=get_first_property_value(self._properties, AWS_REGION) or None,
botocore_session=self._properties.get(BOTOCORE_SESSION),
aws_access_key_id=get_first_property_value(self._properties, AWS_ACCESS_KEY_ID),
aws_secret_access_key=get_first_property_value(self._properties, AWS_SECRET_ACCESS_KEY),
aws_session_token=get_first_property_value(self._properties, AWS_SESSION_TOKEN),
aws_access_key_id=get_first_property_value(self._properties, AWS_ACCESS_KEY_ID) or None,
aws_secret_access_key=get_first_property_value(self._properties, AWS_SECRET_ACCESS_KEY) or None,
aws_session_token=get_first_property_value(self._properties, AWS_SESSION_TOKEN) or None,
)

def add_headers(self, request: PreparedRequest, **kwargs: Any) -> None: # pylint: disable=W0613
Expand Down
8 changes: 4 additions & 4 deletions pyiceberg/catalog/sql.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@
TableUpdate,
)
from pyiceberg.typedef import EMPTY_DICT, Identifier, Properties
from pyiceberg.types import strtobool
from pyiceberg.utils.properties import property_as_bool
from pyiceberg.view import View
from pyiceberg.view.metadata import ViewVersion

Expand Down Expand Up @@ -137,8 +137,8 @@ def __init__(self, name: str, engine: Engine | None = None, **properties: str):
raise NoSuchPropertyException("SQL connection URI is required")

echo_str = str(self.properties.get("echo", DEFAULT_ECHO_VALUE)).lower()
echo = strtobool(echo_str) if echo_str != "debug" else "debug"
pool_pre_ping = strtobool(self.properties.get("pool_pre_ping", DEFAULT_POOL_PRE_PING_VALUE))
echo = property_as_bool(self.properties, "echo", DEFAULT_ECHO_VALUE == "true") if echo_str != "debug" else "debug"
pool_pre_ping = property_as_bool(self.properties, "pool_pre_ping", DEFAULT_POOL_PRE_PING_VALUE == "true")

self.engine = create_engine(uri_prop, echo=echo, pool_pre_ping=pool_pre_ping)

Expand All @@ -157,7 +157,7 @@ def _init_catalog(self) -> None:
raise ValueError(f"Invalid schema_version property: '{schema_version_prop}'. Must be 'v0' or 'v1'.")

# Determine if catalog tables should be created if absent
init_catalog_tables = strtobool(self.properties.get("init_catalog_tables", DEFAULT_INIT_CATALOG_TABLES))
init_catalog_tables = property_as_bool(self.properties, "init_catalog_tables", DEFAULT_INIT_CATALOG_TABLES == "true")

with Session(self.engine) as session:
# 1. Check if catalog tables exist; create if absent
Expand Down
14 changes: 5 additions & 9 deletions pyiceberg/io/fsspec.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,6 @@
_is_local_path,
)
from pyiceberg.typedef import Properties
from pyiceberg.types import strtobool
from pyiceberg.utils.datetime import millis_to_datetime
from pyiceberg.utils.properties import get_first_property_value, get_header_properties, property_as_bool
from pyiceberg.utils.transient import _TransientStateMixin
Expand Down Expand Up @@ -183,10 +182,10 @@ def _s3(properties: Properties) -> AbstractFileSystem:

client_kwargs = {
"endpoint_url": properties.get(S3_ENDPOINT),
"aws_access_key_id": get_first_property_value(properties, S3_ACCESS_KEY_ID, AWS_ACCESS_KEY_ID),
"aws_secret_access_key": get_first_property_value(properties, S3_SECRET_ACCESS_KEY, AWS_SECRET_ACCESS_KEY),
"aws_session_token": get_first_property_value(properties, S3_SESSION_TOKEN, AWS_SESSION_TOKEN),
"region_name": get_first_property_value(properties, S3_REGION, AWS_REGION),
"aws_access_key_id": get_first_property_value(properties, S3_ACCESS_KEY_ID, AWS_ACCESS_KEY_ID) or None,
"aws_secret_access_key": get_first_property_value(properties, S3_SECRET_ACCESS_KEY, AWS_SECRET_ACCESS_KEY) or None,
"aws_session_token": get_first_property_value(properties, S3_SESSION_TOKEN, AWS_SESSION_TOKEN) or None,
"region_name": get_first_property_value(properties, S3_REGION, AWS_REGION) or None,
}
config_kwargs = {}
s3_additional_kwargs = {}
Expand Down Expand Up @@ -217,10 +216,7 @@ def _s3(properties: Properties) -> AbstractFileSystem:
if property_as_bool(properties, S3_FORCE_VIRTUAL_ADDRESSING, False):
config_kwargs["s3"] = {"addressing_style": "virtual"}

if s3_anonymous := properties.get(S3_ANONYMOUS):
anon = strtobool(s3_anonymous)
else:
anon = False
anon = property_as_bool(properties, S3_ANONYMOUS, False)

if server_side_encryption := properties.get(S3_SERVER_SIDE_ENCRYPTION):
s3_additional_kwargs["ServerSideEncryption"] = server_side_encryption
Expand Down
25 changes: 12 additions & 13 deletions pyiceberg/io/pyarrow.py
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,6 @@
TimeType,
UnknownType,
UUIDType,
strtobool,
)
from pyiceberg.utils.concurrent import ExecutorFactory
from pyiceberg.utils.config import Config
Expand Down Expand Up @@ -495,10 +494,10 @@ def _initialize_oss_fs(self) -> FileSystem:

client_kwargs: dict[str, Any] = {
"endpoint_override": self.properties.get(S3_ENDPOINT),
"access_key": get_first_property_value(self.properties, S3_ACCESS_KEY_ID, AWS_ACCESS_KEY_ID),
"secret_key": get_first_property_value(self.properties, S3_SECRET_ACCESS_KEY, AWS_SECRET_ACCESS_KEY),
"session_token": get_first_property_value(self.properties, S3_SESSION_TOKEN, AWS_SESSION_TOKEN),
"region": get_first_property_value(self.properties, S3_REGION, AWS_REGION),
"access_key": get_first_property_value(self.properties, S3_ACCESS_KEY_ID, AWS_ACCESS_KEY_ID) or None,
"secret_key": get_first_property_value(self.properties, S3_SECRET_ACCESS_KEY, AWS_SECRET_ACCESS_KEY) or None,
"session_token": get_first_property_value(self.properties, S3_SESSION_TOKEN, AWS_SESSION_TOKEN) or None,
"region": get_first_property_value(self.properties, S3_REGION, AWS_REGION) or None,
"force_virtual_addressing": property_as_bool(self.properties, S3_FORCE_VIRTUAL_ADDRESSING, True),
}

Expand All @@ -519,8 +518,8 @@ def _initialize_oss_fs(self) -> FileSystem:
):
client_kwargs["session_name"] = session_name

if s3_anonymous := self.properties.get(S3_ANONYMOUS):
client_kwargs["anonymous"] = strtobool(s3_anonymous)
if self.properties.get(S3_ANONYMOUS) is not None:
client_kwargs["anonymous"] = property_as_bool(self.properties, S3_ANONYMOUS, False)

self._set_tls_ca_file_path(client_kwargs)
self._set_allow_delayed_open(client_kwargs)
Expand All @@ -530,7 +529,7 @@ def _initialize_oss_fs(self) -> FileSystem:
def _initialize_s3_fs(self, netloc: str | None) -> FileSystem:
from pyarrow.fs import S3FileSystem

provided_region = get_first_property_value(self.properties, S3_REGION, AWS_REGION)
provided_region = get_first_property_value(self.properties, S3_REGION, AWS_REGION) or None

# Do this when we don't provide the region at all, or when we explicitly enable it
if provided_region is None or property_as_bool(self.properties, S3_RESOLVE_REGION, False) is True:
Expand All @@ -547,9 +546,9 @@ def _initialize_s3_fs(self, netloc: str | None) -> FileSystem:

client_kwargs: dict[str, Any] = {
"endpoint_override": self.properties.get(S3_ENDPOINT),
"access_key": get_first_property_value(self.properties, S3_ACCESS_KEY_ID, AWS_ACCESS_KEY_ID),
"secret_key": get_first_property_value(self.properties, S3_SECRET_ACCESS_KEY, AWS_SECRET_ACCESS_KEY),
"session_token": get_first_property_value(self.properties, S3_SESSION_TOKEN, AWS_SESSION_TOKEN),
"access_key": get_first_property_value(self.properties, S3_ACCESS_KEY_ID, AWS_ACCESS_KEY_ID) or None,
"secret_key": get_first_property_value(self.properties, S3_SECRET_ACCESS_KEY, AWS_SECRET_ACCESS_KEY) or None,
"session_token": get_first_property_value(self.properties, S3_SESSION_TOKEN, AWS_SESSION_TOKEN) or None,
"region": bucket_region,
}

Expand Down Expand Up @@ -578,8 +577,8 @@ def _initialize_s3_fs(self, netloc: str | None) -> FileSystem:
):
client_kwargs["retry_strategy"] = retry_instance

if s3_anonymous := self.properties.get(S3_ANONYMOUS):
client_kwargs["anonymous"] = strtobool(s3_anonymous)
if self.properties.get(S3_ANONYMOUS) is not None:
client_kwargs["anonymous"] = property_as_bool(self.properties, S3_ANONYMOUS, False)

self._set_tls_ca_file_path(client_kwargs)
self._set_allow_delayed_open(client_kwargs)
Expand Down
34 changes: 22 additions & 12 deletions pyiceberg/utils/properties.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,39 +15,39 @@
# specific language governing permissions and limitations
# under the License.

from collections.abc import Mapping
from typing import (
Any,
)

from pyiceberg.typedef import Properties
from pyiceberg.types import strtobool
from pyiceberg.utils.deprecated import deprecation_message

HEADER_PREFIX = "header."


def property_as_int(
properties: Mapping[str, Any],
properties: Properties,
property_name: str,
default: int | None = None,
) -> int | None:
if (value := properties.get(property_name)) is not None and value != "":
if (value := properties.get(property_name)) is not None:
try:
return int(value)
return int(str(value))
except ValueError as e:
raise ValueError(f"Could not parse table property {property_name} to an integer: {value}") from e
else:
return default


def property_as_float(
properties: Mapping[str, Any],
properties: Properties,
property_name: str,
default: float | None = None,
) -> float | None:
if (value := properties.get(property_name)) is not None and value != "":
if (value := properties.get(property_name)) is not None:
try:
return float(value)
return float(str(value))
except ValueError as e:
raise ValueError(f"Could not parse table property {property_name} to a float: {value}") from e
else:
Expand All @@ -59,11 +59,21 @@ def property_as_bool(
property_name: str,
default: bool,
) -> bool:
if (value := properties.get(property_name)) not in (None, ""):
if (value := properties.get(property_name)) is not None:
# Matches Java's Boolean.parseBoolean: only "true" (case-insensitive) is true
result = str(value).lower() == "true"
try:
return strtobool(str(value))
except ValueError as e:
raise ValueError(f"Could not parse table property {property_name} to a boolean: {value}") from e
legacy_result = strtobool(str(value))
except ValueError:
legacy_result = result
if legacy_result != result:
deprecation_message(
deprecated_in="0.12.0",
removed_in="0.13.0",
help_message=f"Property {property_name}={value!r} is parsed as {result}, previously {legacy_result}. "
"Only 'true' (case-insensitive) is parsed as True",
)
return result
return default


Expand All @@ -72,7 +82,7 @@ def get_first_property_value(
*property_names: str,
) -> Any | None:
for property_name in property_names:
if (property_value := properties.get(property_name)) is not None and property_value != "":
if (property_value := properties.get(property_name)) is not None:
return property_value
return None

Expand Down
24 changes: 24 additions & 0 deletions tests/io/test_pyarrow.py
Original file line number Diff line number Diff line change
Expand Up @@ -573,6 +573,30 @@ def test_pyarrow_unified_session_properties() -> None:
)


def test_pyarrow_empty_s3_credentials_use_default_credential_chain() -> None:
session_properties: Properties = {
"s3.endpoint": "http://localhost:9000",
"s3.access-key-id": "",
"s3.secret-access-key": "",
"s3.session-token": "",
"s3.anonymous": "",
**UNIFIED_AWS_SESSION_PROPERTIES,
}

with patch("pyarrow.fs.S3FileSystem") as mock_s3fs, patch("pyarrow.fs.resolve_s3_region") as mock_s3_region_resolver:
mock_s3_region_resolver.return_value = "client.region"
PyArrowFileIO(properties=session_properties).new_input(location=f"s3://warehouse/{uuid.uuid4()}")

mock_s3fs.assert_called_with(
anonymous=False,
endpoint_override="http://localhost:9000",
access_key=None,
secret_key=None,
region="client.region",
session_token=None,
)


def test_schema_to_pyarrow_schema_include_field_ids(table_schema_nested: Schema) -> None:
actual = schema_to_pyarrow(table_schema_nested)
expected = """foo: large_string
Expand Down
Loading