diff --git a/pyiceberg/catalog/dynamodb.py b/pyiceberg/catalog/dynamodb.py index 2157c76e39..fbac5343f7 100644 --- a/pyiceberg/catalog/dynamodb.py +++ b/pyiceberg/catalog/dynamodb.py @@ -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( diff --git a/pyiceberg/catalog/glue.py b/pyiceberg/catalog/glue.py index 9ba8775747..5180253cf4 100644 --- a/pyiceberg/catalog/glue.py +++ b/pyiceberg/catalog/glue.py @@ -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( diff --git a/pyiceberg/catalog/rest/__init__.py b/pyiceberg/catalog/rest/__init__.py index 12c73610bc..7705188a3d 100644 --- a/pyiceberg/catalog/rest/__init__.py +++ b/pyiceberg/catalog/rest/__init__.py @@ -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 diff --git a/pyiceberg/catalog/sql.py b/pyiceberg/catalog/sql.py index d2c34a692e..99f67394ad 100644 --- a/pyiceberg/catalog/sql.py +++ b/pyiceberg/catalog/sql.py @@ -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 @@ -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) @@ -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 diff --git a/pyiceberg/io/fsspec.py b/pyiceberg/io/fsspec.py index 505b5eebbf..a1d10a7166 100644 --- a/pyiceberg/io/fsspec.py +++ b/pyiceberg/io/fsspec.py @@ -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 @@ -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 = {} @@ -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 diff --git a/pyiceberg/io/pyarrow.py b/pyiceberg/io/pyarrow.py index f5ddaee8f4..609b5d1bef 100644 --- a/pyiceberg/io/pyarrow.py +++ b/pyiceberg/io/pyarrow.py @@ -190,7 +190,6 @@ TimeType, UnknownType, UUIDType, - strtobool, ) from pyiceberg.utils.concurrent import ExecutorFactory from pyiceberg.utils.config import Config @@ -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), } @@ -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) @@ -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: @@ -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, } @@ -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) diff --git a/pyiceberg/utils/properties.py b/pyiceberg/utils/properties.py index f6cb94ecb3..df26022ec1 100644 --- a/pyiceberg/utils/properties.py +++ b/pyiceberg/utils/properties.py @@ -15,25 +15,25 @@ # 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: @@ -41,13 +41,13 @@ def property_as_int( 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: @@ -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 @@ -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 diff --git a/tests/io/test_pyarrow.py b/tests/io/test_pyarrow.py index 5c1933fc20..c18ca58288 100644 --- a/tests/io/test_pyarrow.py +++ b/tests/io/test_pyarrow.py @@ -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 diff --git a/tests/utils/test_properties.py b/tests/utils/test_properties.py index b760eaaa4f..d00880716a 100644 --- a/tests/utils/test_properties.py +++ b/tests/utils/test_properties.py @@ -40,11 +40,9 @@ def test_property_as_int_with_invalid_value() -> None: "some_int_prop": "invalid", } - with pytest.raises(ValueError) as exc: + with pytest.raises(ValueError, match="Could not parse table property some_int_prop to an integer: invalid"): property_as_int(properties, "some_int_prop") - assert "Could not parse table property some_int_prop to an integer: invalid" in str(exc.value) - def test_property_as_float() -> None: properties = { @@ -61,11 +59,9 @@ def test_property_as_float_with_invalid_value() -> None: "some_float_prop": "invalid", } - with pytest.raises(ValueError) as exc: + with pytest.raises(ValueError, match="Could not parse table property some_float_prop to a float: invalid"): property_as_float(properties, "some_float_prop") - assert "Could not parse table property some_float_prop to a float: invalid" in str(exc.value) - @pytest.mark.parametrize( ("value", "expected"), @@ -87,14 +83,16 @@ def test_property_as_bool(value: str | bool, expected: bool) -> None: def test_property_as_bool_with_invalid_value() -> None: - properties = { - "some_bool_prop": "invalid", - } + for value in ("invalid", "", "false", "FALSE", "0", "no"): + assert property_as_bool({"some_bool_prop": value}, "some_bool_prop", True) is False + for value in ("true", "TRUE", "True"): + assert property_as_bool({"some_bool_prop": value}, "some_bool_prop", False) is True - with pytest.raises(ValueError) as exc: - property_as_bool(properties, "some_bool_prop", True) - assert "Could not parse table property some_bool_prop to a boolean: invalid" in str(exc.value) +def test_property_as_bool_warns_on_changed_parsing() -> None: + for value in ("1", "yes", "on", "y", "t"): + with pytest.warns(DeprecationWarning, match=f"Property some_bool_prop='{value}' is parsed as False, previously True"): + assert property_as_bool({"some_bool_prop": value}, "some_bool_prop", True) is False def test_get_first_property_value() -> None: @@ -107,22 +105,25 @@ def test_get_first_property_value() -> None: assert get_first_property_value(properties, "missing", "prop_1") == "value_1" -def test_property_as_int_preserves_zero_and_treats_empty_as_unset() -> None: +def test_property_as_int_preserves_zero_and_treats_only_none_as_unset() -> None: for value in (0, "0"): assert property_as_int({"retries": value}, "retries", default=4) == 0 - for unset in (None, ""): - assert property_as_int({"retries": unset}, "retries", default=4) == 4 + assert property_as_int({"retries": None}, "retries", default=4) == 4 + for invalid in ("", False, 1.5): + with pytest.raises(ValueError, match="Could not parse table property retries to an integer"): + property_as_int({"retries": invalid}, "retries", default=4) -def test_property_as_float_preserves_zero_and_treats_empty_as_unset() -> None: +def test_property_as_float_preserves_zero_and_treats_only_none_as_unset() -> None: for value in (0.0, 0, "0.0"): assert property_as_float({"ratio": value}, "ratio", default=1.5) == 0.0 - for unset in (None, ""): - assert property_as_float({"ratio": unset}, "ratio", default=1.5) == 1.5 + assert property_as_float({"ratio": None}, "ratio", default=1.5) == 1.5 + for invalid in ("", False): + with pytest.raises(ValueError, match="Could not parse table property ratio to a float"): + property_as_float({"ratio": invalid}, "ratio", default=1.5) -def test_get_first_property_value_preserves_falsy_non_empty_values() -> None: - for value in (0, 0.0, False): +def test_get_first_property_value_only_skips_none() -> None: + for value in (0, 0.0, False, ""): assert get_first_property_value({"primary": value, "fallback": "fallback"}, "primary", "fallback") is value - for unset in (None, ""): - assert get_first_property_value({"primary": unset, "fallback": "fallback"}, "primary", "fallback") == "fallback" + assert get_first_property_value({"primary": None, "fallback": "fallback"}, "primary", "fallback") == "fallback"