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
18 changes: 16 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 @@ -235,12 +239,22 @@ def _construct_parameters(


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:
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 +383,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(),
createTime=current_time_millis // 1000,
lastAccessTime=current_time_millis // 1000,
sd=_construct_hive_storage_descriptor(
Expand Down
71 changes: 71 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,46 @@ 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"


@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"


@pytest.mark.parametrize("hive2_compatible", [True, False])
@patch("time.time", MagicMock(return_value=12345))
def test_create_table(
Expand Down Expand Up @@ -1152,6 +1193,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 Down
Loading