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
37 changes: 35 additions & 2 deletions pyiceberg/catalog/hive.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
LockType,
MetaException,
NoSuchObjectException,
PrincipalType,
SerDeInfo,
StorageDescriptor,
UnlockRequest,
Expand Down Expand Up @@ -113,7 +114,7 @@
UnknownType,
UUIDType,
)
from pyiceberg.utils.properties import property_as_bool, property_as_float
from pyiceberg.utils.properties import get_first_property_value, property_as_bool, property_as_float
from pyiceberg.view import View
from pyiceberg.view.metadata import ViewVersion

Expand All @@ -123,6 +124,9 @@

COMMENT = "comment"
OWNER = "owner"
HIVE_METASTORE_TABLE_OWNER = "hive.metastore.table.owner"
HIVE_METASTORE_DATABASE_OWNER = "hive.metastore.database.owner"
HIVE_METASTORE_DATABASE_OWNER_TYPE = "hive.metastore.database.owner-type"

# If set to true, HiveCatalog will operate in Hive2 compatibility mode
HIVE2_COMPATIBLE = "hive.hive2-compatible"
Expand Down Expand Up @@ -228,19 +232,31 @@ def _construct_parameters(

if metadata_properties:
for key, value in metadata_properties.items():
if key.lower() == HIVE_METASTORE_TABLE_OWNER:
continue
if key not in properties:
properties[key] = str(value)

return properties


def _annotate_namespace(database: HiveDatabase, properties: Properties) -> HiveDatabase:
if HIVE_METASTORE_DATABASE_OWNER_TYPE in properties and HIVE_METASTORE_DATABASE_OWNER not in properties:
raise ValueError(f"{HIVE_METASTORE_DATABASE_OWNER_TYPE} requires {HIVE_METASTORE_DATABASE_OWNER}")

params = {}
for key, value in properties.items():
if key == COMMENT:
database.description = value
elif key == LOCATION:
Comment thread
Anurag-S1ngh marked this conversation as resolved.
Resolved
database.locationUri = value
elif key == HIVE_METASTORE_DATABASE_OWNER:
database.ownerName = value
elif key == HIVE_METASTORE_DATABASE_OWNER_TYPE:
try:
database.ownerType = PrincipalType._NAMES_TO_VALUES[str(value).upper()]
except KeyError as e:
raise ValueError(f"Invalid {HIVE_METASTORE_DATABASE_OWNER_TYPE}: {value}") from e
else:
params[key] = value
database.parameters = params
Expand Down Expand Up @@ -369,7 +385,7 @@ def _convert_iceberg_into_hive(self, table: Table) -> HiveTable:
return HiveTable(
dbName=database_name,
tableName=table_name,
owner=table.properties[OWNER] if table.properties and OWNER in table.properties else getpass.getuser(),
owner=get_first_property_value(table.properties or {}, OWNER, HIVE_METASTORE_TABLE_OWNER) or getpass.getuser(),
Comment thread
Anurag-S1ngh marked this conversation as resolved.
Resolved
createTime=current_time_millis // 1000,
lastAccessTime=current_time_millis // 1000,
sd=_construct_hive_storage_descriptor(
Expand Down Expand Up @@ -851,6 +867,10 @@ def load_namespace_properties(self, namespace: str | Identifier) -> Properties:
properties[LOCATION] = database.locationUri
if comment := database.description:
properties[COMMENT] = comment
if database.ownerName is not None:
properties[HIVE_METASTORE_DATABASE_OWNER] = database.ownerName
if database.ownerType is not None:
properties[HIVE_METASTORE_DATABASE_OWNER_TYPE] = PrincipalType._VALUES_TO_NAMES[database.ownerType]
return properties
except NoSuchObjectException as e:
raise NoSuchNamespaceError(f"Database does not exists: {database_name}") from e
Expand Down Expand Up @@ -883,10 +903,23 @@ def update_namespace_properties(
updated: set[str] = set()

if removals:
owner_keys = {HIVE_METASTORE_DATABASE_OWNER, HIVE_METASTORE_DATABASE_OWNER_TYPE}
if removals & owner_keys and not owner_keys <= removals:
raise ValueError(
f"Removing {HIVE_METASTORE_DATABASE_OWNER} and {HIVE_METASTORE_DATABASE_OWNER_TYPE} "
"has to be performed together or not at all"
)
for key in removals:
if key in parameters:
parameters.pop(key)
removed.add(key)
if HIVE_METASTORE_DATABASE_OWNER in removals:
if database.ownerName is not None:
database.ownerName = None
removed.add(HIVE_METASTORE_DATABASE_OWNER)
if database.ownerType is not None:
database.ownerType = None
removed.add(HIVE_METASTORE_DATABASE_OWNER_TYPE)
if updates:
for key, value in updates.items():
parameters[key] = value
Expand Down
125 changes: 125 additions & 0 deletions tests/catalog/test_hive.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
LockState,
MetaException,
NoSuchObjectException,
PrincipalType,
SerDeInfo,
SkewedInfo,
StorageDescriptor,
Expand Down Expand Up @@ -286,6 +287,48 @@ def test_check_number_of_namespaces(table_schema_simple: Schema) -> None:
catalog.create_table("table", schema=table_schema_simple)


@patch("time.time", MagicMock(return_value=12345))
def test_create_table_uses_java_owner_property(
table_schema_with_all_types: Schema, hive_database: HiveDatabase, hive_table: HiveTable
) -> None:
catalog = HiveCatalog(HIVE_CATALOG_NAME, uri=HIVE_METASTORE_FAKE_URL)
catalog._client = MagicMock()
catalog._client.__enter__().create_table.return_value = None
catalog._client.__enter__().get_table.return_value = hive_table
catalog._client.__enter__().get_table_req.return_value = GetTableResult(table=hive_table)
catalog._client.__enter__().get_database.return_value = hive_database

catalog.create_table(
("default", "table"), schema=table_schema_with_all_types, properties={"hive.metastore.table.owner": "javaberg"}
)

called_hive_table: HiveTable = catalog._client.__enter__().create_table.call_args[0][0]
assert called_hive_table.owner == "javaberg"
assert "hive.metastore.table.owner" not in called_hive_table.parameters


@patch("time.time", MagicMock(return_value=12345))
def test_create_table_prefers_native_owner_property(
table_schema_with_all_types: Schema, hive_database: HiveDatabase, hive_table: HiveTable
) -> None:
catalog = HiveCatalog(HIVE_CATALOG_NAME, uri=HIVE_METASTORE_FAKE_URL)
catalog._client = MagicMock()
catalog._client.__enter__().create_table.return_value = None
catalog._client.__enter__().get_table.return_value = hive_table
catalog._client.__enter__().get_table_req.return_value = GetTableResult(table=hive_table)
catalog._client.__enter__().get_database.return_value = hive_database

catalog.create_table(
("default", "table"),
schema=table_schema_with_all_types,
properties={"owner": "native", "hive.metastore.table.owner": "javaberg"},
)

called_hive_table: HiveTable = catalog._client.__enter__().create_table.call_args[0][0]
assert called_hive_table.owner == "native"
assert "hive.metastore.table.owner" not in called_hive_table.parameters


@pytest.mark.parametrize("hive2_compatible", [True, False])
@patch("time.time", MagicMock(return_value=12345))
def test_create_table(
Expand Down Expand Up @@ -1152,6 +1195,36 @@ def test_create_database() -> None:
)


def test_create_database_with_java_owner() -> None:
catalog = HiveCatalog(HIVE_CATALOG_NAME, uri=HIVE_METASTORE_FAKE_URL)

catalog._client = MagicMock()
catalog._client.__enter__().create_database.return_value = None

catalog.create_namespace(
"default",
{
"hive.metastore.database.owner": "javaberg",
"hive.metastore.database.owner-type": "USER",
"property": "true",
},
)

called_database: HiveDatabase = catalog._client.__enter__().create_database.call_args[0][0]
assert called_database.ownerName == "javaberg"
assert called_database.ownerType == PrincipalType.USER
assert called_database.parameters == {"property": "true"}


def test_create_database_rejects_owner_type_without_owner() -> None:
catalog = HiveCatalog(HIVE_CATALOG_NAME, uri=HIVE_METASTORE_FAKE_URL)

catalog._client = MagicMock()

with pytest.raises(ValueError, match="hive.metastore.database.owner-type requires hive.metastore.database.owner"):
catalog.create_namespace("default", {"hive.metastore.database.owner-type": "USER"})


def test_create_database_already_exists() -> None:
catalog = HiveCatalog(HIVE_CATALOG_NAME, uri=HIVE_METASTORE_FAKE_URL)

Expand All @@ -1175,6 +1248,24 @@ def test_load_namespace_properties(hive_database: HiveDatabase) -> None:
catalog._client.__enter__().get_database.assert_called_with(name="default2")


def test_load_namespace_properties_includes_owner(hive_database: HiveDatabase) -> None:
database = deepcopy(hive_database)
database.ownerName = "javaberg"
database.ownerType = PrincipalType.USER

catalog = HiveCatalog(HIVE_CATALOG_NAME, uri=HIVE_METASTORE_FAKE_URL)

catalog._client = MagicMock()
catalog._client.__enter__().get_database.return_value = database

assert catalog.load_namespace_properties("default2") == {
"location": database.locationUri,
"test": "property",
"hive.metastore.database.owner": "javaberg",
"hive.metastore.database.owner-type": "USER",
}


def test_load_namespace_properties_does_not_exists() -> None:
catalog = HiveCatalog(HIVE_CATALOG_NAME, uri=HIVE_METASTORE_FAKE_URL)

Expand Down Expand Up @@ -1218,6 +1309,40 @@ def test_update_namespace_properties(hive_database: HiveDatabase) -> None:
)


def test_update_namespace_properties_removes_owner(hive_database: HiveDatabase) -> None:
database = deepcopy(hive_database)
database.ownerName = "javaberg"
database.ownerType = PrincipalType.USER

catalog = HiveCatalog(HIVE_CATALOG_NAME, uri=HIVE_METASTORE_FAKE_URL)

catalog._client = MagicMock()
catalog._client.__enter__().get_database.return_value = database
catalog._client.__enter__().alter_database.return_value = None

summary = catalog.update_namespace_properties(
namespace="default",
removals={"hive.metastore.database.owner", "hive.metastore.database.owner-type"},
)

assert set(summary.removed) == {"hive.metastore.database.owner", "hive.metastore.database.owner-type"}
assert summary.missing == []

altered_database: HiveDatabase = catalog._client.__enter__().alter_database.call_args[0][1]
assert altered_database.ownerName is None
assert altered_database.ownerType is None


def test_update_namespace_properties_rejects_removing_owner_alone(hive_database: HiveDatabase) -> None:
catalog = HiveCatalog(HIVE_CATALOG_NAME, uri=HIVE_METASTORE_FAKE_URL)

catalog._client = MagicMock()
catalog._client.__enter__().get_database.return_value = hive_database

with pytest.raises(ValueError, match="has to be performed together or not at all"):
catalog.update_namespace_properties("default", removals={"hive.metastore.database.owner"})


def test_update_namespace_properties_namespace_does_not_exists() -> None:
catalog = HiveCatalog(HIVE_CATALOG_NAME, uri=HIVE_METASTORE_FAKE_URL)

Expand Down
Loading