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

[DAE-63] Handling exception when adding duplicate partitions #37

Merged
merged 8 commits into from
Jan 27, 2021
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
3 changes: 2 additions & 1 deletion docs/source/getstarted.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,5 +36,6 @@ Beyond the default methods, this library also implements the methods below in
the [`HiveMetastoreClient`](https://github.com/quintoandar/hive-metastore-client/blob/main/hive_metastore_client/hive_metastore_client.py) class:

- [`add_columns_to_table`](https://hive-metastore-client.readthedocs.io/en/latest/hive_metastore_client.html#hive_metastore_client.hive_metastore_client.HiveMetastoreClient.add_columns_to_table)
- [`add_partitions_to_table`](https://hive-metastore-client.readthedocs.io/en/latest/hive_metastore_client.html#hive_metastore_client.hive_metastore_client.HiveMetastoreClient.add_partitions_to_table)
- [`drop_columns_from_table`](https://hive-metastore-client.readthedocs.io/en/latest/hive_metastore_client.html#hive_metastore_client.hive_metastore_client.HiveMetastoreClient.drop_columns_from_table)
- [`add_partitions_if_not_exists`](https://hive-metastore-client.readthedocs.io/en/latest/hive_metastore_client.html#hive_metastore_client.hive_metastore_client.HiveMetastoreClient.add_partitions_if_not_exists)
- [`create_database_if_not_exists`](https://hive-metastore-client.readthedocs.io/en/latest/hive_metastore_client.html#hive_metastore_client.hive_metastore_client.HiveMetastoreClient.create_database_if_not_exists)
2 changes: 1 addition & 1 deletion examples/add_partitions.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,4 @@

with HiveMetastoreClient(HIVE_HOST, HIVE_PORT) as hive_client:
# Adding two set of partitions to specified table
hive_client.add_partitions_to_table(DATABASE_NAME, TABLE_NAME, partition_list)
hive_client.add_partitions_if_not_exists(DATABASE_NAME, TABLE_NAME, partition_list)
22 changes: 18 additions & 4 deletions hive_metastore_client/hive_metastore_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,16 +113,24 @@ def drop_columns_from_table(
# call alter table to drop columns removed from list of table columns
self.alter_table(dbname=db_name, tbl_name=table_name, new_tbl=table)

def add_partitions_to_table(
def add_partitions_if_not_exists(
self, db_name: str, table_name: str, partition_list: List[Partition]
) -> None:
"""
Add partitions to a table.
Add partitions to a table if it does not exist.

If the user tries to add a partition twice, the method handles the
AlreadyExistsException, not raising an error.

:param db_name: database name where the table is at
:param table_name: table name which the partitions belong to
:param partition_list: list of partitions to be added to the table
"""
if not partition_list:

Choose a reason for hiding this comment

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

❀️

raise ValueError(
"m=add_partitions_if_not_exists, msg=The partition list is empty."
)

table = self.get_table(dbname=db_name, tbl_name=table_name)

partition_list_with_correct_location = self._format_partitions_location(
Expand All @@ -131,7 +139,10 @@ def add_partitions_to_table(
table_partition_keys=table.partitionKeys,
)

self.add_partitions(partition_list_with_correct_location)
try:
self.add_partitions(partition_list_with_correct_location)
ribaldorafael marked this conversation as resolved.
Show resolved Hide resolved
except AlreadyExistsException:
ribaldorafael marked this conversation as resolved.
Show resolved Hide resolved
LucasMMota marked this conversation as resolved.
Show resolved Hide resolved
pass

def create_database_if_not_exists(self, database: Database) -> None:
"""
Expand Down Expand Up @@ -197,4 +208,7 @@ def _validate_lists_length(list_a: List[Any], list_b: List[Any]) -> None:
:param list_b: second list to be compared
"""
if len(list_a) != len(list_b):
raise ValueError("The length of the two provided lists does not match")
raise ValueError(
"m=_validate_lists_length, msg=The length of the two provided "
"lists does not match"
)
68 changes: 63 additions & 5 deletions tests/unit/hive_metastore_client/test_hive_metastore_client.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
from unittest import mock
from unittest.mock import Mock
from unittest.mock import Mock, ANY

import pytest
from pytest import raises

from hive_metastore_client import HiveMetastoreClient
from thrift_files.libraries.thrift_hive_metastore_client.ThriftHiveMetastore import (
Expand Down Expand Up @@ -164,16 +165,53 @@ def test_add_partitions_to_table(
# arrange
db_name = "database_name"
table_name = "table_name"
formatted_partitions_location = ["abc"]

mocked_table = Mock()
mocked_get_table.return_value = mocked_table

mocked_partition_list = [Mock()]
formatted_partitions_location = ["abc"]
mocked__format_partitions.return_value = formatted_partitions_location

# act
hive_metastore_client.add_partitions_if_not_exists(
db_name=db_name, table_name=table_name, partition_list=mocked_partition_list
)

# assert
mocked_get_table.assert_called_once_with(dbname=db_name, tbl_name=table_name)
mocked__format_partitions.assert_called_once_with(
partition_list=mocked_partition_list,
table_storage_descriptor=mocked_table.sd,
table_partition_keys=mocked_table.partitionKeys,
)
mocked_add_partitions.assert_called_once_with(formatted_partitions_location)

@mock.patch.object(HiveMetastoreClient, "get_table")
@mock.patch.object(HiveMetastoreClient, "_format_partitions_location")
@mock.patch.object(HiveMetastoreClient, "add_partitions")
def test_add_partitions_to_table_with_duplicated_partitions(
self,
mocked_add_partitions,
mocked__format_partitions,
mocked_get_table,
hive_metastore_client,
):
# arrange
db_name = "database_name"
table_name = "table_name"

mocked_table = Mock()
mocked_table.sd = ""
mocked_table.partitionKeys = ""
mocked_get_table.return_value = mocked_table

mocked_partition_list = [Mock()]
formatted_partitions_location = ["abc"]
mocked__format_partitions.return_value = formatted_partitions_location

mocked_add_partitions.side_effect = AlreadyExistsException()

# act
hive_metastore_client.add_partitions_to_table(
hive_metastore_client.add_partitions_if_not_exists(
db_name=db_name, table_name=table_name, partition_list=mocked_partition_list
)

Expand All @@ -186,6 +224,26 @@ def test_add_partitions_to_table(
)
mocked_add_partitions.assert_called_once_with(formatted_partitions_location)

@mock.patch.object(HiveMetastoreClient, "get_table")
@mock.patch.object(HiveMetastoreClient, "_format_partitions_location")
@mock.patch.object(HiveMetastoreClient, "add_partitions")
def test_add_partitions_to_table_with_invalid_partitions(
self,
mocked_add_partitions,
mocked__format_partitions,
mocked_get_table,
hive_metastore_client,
):
# assert
with raises(ValueError):
# act
hive_metastore_client.add_partitions_if_not_exists(
db_name=ANY, table_name=ANY, partition_list=[]
)
mocked_get_table.assert_not_called()
mocked__format_partitions.assert_not_called()
mocked_add_partitions.assert_not_called()
Comment on lines +230 to +245

Choose a reason for hiding this comment

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

niceee


@mock.patch.object(HiveMetastoreClient, "_validate_lists_length")
def test_format_partitions_location(
self, mocked_validate_lists_length, hive_metastore_client
Expand Down