Skip to content
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
9 changes: 8 additions & 1 deletion paimon-python/pypaimon/catalog/catalog_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
from typing import Optional

from pypaimon.api.api_response import PagedList, Partition
from pypaimon.catalog.catalog import Catalog
from pypaimon.common.identifier import Identifier
from pypaimon.table.table import Table

Expand Down Expand Up @@ -49,9 +50,15 @@ def check_not_branch(identifier: Identifier, method: str) -> None:
.format(method, identifier.get_full_name()))


def check_not_system_database(name: str) -> None:
"""Mirrors Java ``CatalogUtils.checkNotSystemDatabase``."""
if name == Catalog.SYSTEM_DATABASE_NAME:
raise ValueError("Can't do operation on system database.")


def check_not_system_table(identifier: Identifier, method: str) -> None:
"""Mirrors Java ``CatalogUtils.checkNotSystemTable``."""
if identifier.is_system_table():
if identifier.get_database_name() == Catalog.SYSTEM_DATABASE_NAME or identifier.is_system_table():
raise ValueError(
"Cannot '{}' for system table '{}', please use data table."
.format(method, identifier.get_full_name()))
Expand Down
4 changes: 3 additions & 1 deletion paimon-python/pypaimon/catalog/filesystem_catalog.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
from pypaimon.catalog.catalog import Catalog
from pypaimon.catalog.catalog_context import CatalogContext
from pypaimon.catalog.catalog_environment import CatalogEnvironment
from pypaimon.catalog.catalog_utils import check_not_branch, check_not_system_table
from pypaimon.catalog.catalog_utils import check_not_branch, check_not_system_database, check_not_system_table
from pypaimon.catalog.catalog_exception import (BranchAlreadyExistException,
BranchNotExistException,
DatabaseAlreadyExistException,
Expand Down Expand Up @@ -87,6 +87,7 @@ def get_database(self, name: str) -> Database:

def create_database(self, name: str, ignore_if_exists: bool,
properties: Optional[Dict[str, str]] = None) -> None:
check_not_system_database(name)
try:
self.get_database(name)
if not ignore_if_exists:
Expand All @@ -98,6 +99,7 @@ def create_database(self, name: str, ignore_if_exists: bool,
self.file_io.mkdirs(path)

def drop_database(self, name: str, ignore_if_not_exists: bool = False, cascade: bool = False) -> None:
check_not_system_database(name)
try:
self.get_database(name)
except DatabaseNotExistException:
Expand Down
4 changes: 4 additions & 0 deletions paimon-python/pypaimon/catalog/jdbc_catalog.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
from pypaimon.catalog.catalog_environment import CatalogEnvironment
from pypaimon.catalog.catalog_utils import (
check_not_branch,
check_not_system_database,
check_not_system_table,
encoded_branch_name
)
Expand Down Expand Up @@ -324,6 +325,7 @@ def get_database(self, name: str) -> Database:

def create_database(self, name: str, ignore_if_exists: bool,
properties: Optional[Dict[str, str]] = None) -> None:
check_not_system_database(name)
if self._database_exists(name):
if not ignore_if_exists:
raise DatabaseAlreadyExistException(name)
Expand All @@ -337,6 +339,7 @@ def create_database(self, name: str, ignore_if_exists: bool,
self._insert_database_properties(name, create_props)

def drop_database(self, name: str, ignore_if_not_exists: bool = False, cascade: bool = False) -> None:
check_not_system_database(name)
if not self._database_exists(name):
if not ignore_if_not_exists:
raise DatabaseNotExistException(name)
Expand All @@ -362,6 +365,7 @@ def drop_database(self, name: str, ignore_if_not_exists: bool = False, cascade:
)

def alter_database(self, name: str, changes: List[PropertyChange]) -> None:
check_not_system_database(name)
self.get_database(name)
set_properties, remove_keys = PropertyChange.get_set_properties_to_remove_keys(changes)
current = self._fetch_database_properties(name)
Expand Down
5 changes: 4 additions & 1 deletion paimon-python/pypaimon/catalog/rest/rest_catalog.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
from pypaimon.catalog.catalog import Catalog
from pypaimon.catalog.catalog_context import CatalogContext
from pypaimon.catalog.catalog_environment import CatalogEnvironment
from pypaimon.catalog.catalog_utils import check_not_branch, check_not_system_table
from pypaimon.catalog.catalog_utils import check_not_branch, check_not_system_database, check_not_system_table
from pypaimon.catalog.catalog_exception import (
ColumnAlreadyExistException, ColumnNotExistException,
TableNotExistException, DatabaseAlreadyExistException,
Expand Down Expand Up @@ -164,6 +164,7 @@ def list_databases_paged(self, max_results: Optional[int] = None, page_token: Op

def create_database(self, name: str, ignore_if_exists: bool,
properties: Optional[Dict[str, str]] = None) -> None:
check_not_system_database(name)
try:
self.rest_api.create_database(name, properties)
except AlreadyExistsException as e:
Expand All @@ -187,6 +188,7 @@ def get_database(self, name: str) -> Database:
raise DatabaseNoPermissionException(name) from e

def drop_database(self, name: str, ignore_if_not_exists: bool = False, cascade: bool = False) -> None:
check_not_system_database(name)
if not cascade:
try:
tables = self.list_tables(name)
Expand All @@ -209,6 +211,7 @@ def drop_database(self, name: str, ignore_if_not_exists: bool = False, cascade:
raise DatabaseNoPermissionException(name) from e

def alter_database(self, name: str, changes: List[PropertyChange]) -> None:
check_not_system_database(name)
try:
set_properties, remove_keys = PropertyChange.get_set_properties_to_remove_keys(changes)
self.rest_api.alter_database(name, list(remove_keys), set_properties)
Expand Down
21 changes: 21 additions & 0 deletions paimon-python/pypaimon/tests/catalog_table_guard_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
import pyarrow as pa

from pypaimon import CatalogFactory, Schema
from pypaimon.catalog.catalog import Catalog
from pypaimon.catalog.rest.property_change import PropertyChange
from pypaimon.common.identifier import Identifier
from pypaimon.schema.data_types import AtomicType
from pypaimon.schema.schema_change import SchemaChange
Expand Down Expand Up @@ -79,6 +81,25 @@ def assert_table_guards(test, catalog, database):
catalog.drop_table(Identifier(database, 'm2', branch='main'), False)
test.assertEqual(catalog.list_tables(database), ['t'])

# As in Java, the sys database and the tables in it cannot be created, dropped or changed.
operations = [
lambda: catalog.create_database('sys', True),
lambda: catalog.drop_database('sys', True),
lambda: catalog.create_table('sys.t', schema, True),
lambda: catalog.drop_table('sys.t', True),
lambda: catalog.rename_table(name, 'sys.t'),
lambda: catalog.alter_table('sys.t', [add_column]),
]
if type(catalog).alter_database is not Catalog.alter_database:
operations.append(lambda: catalog.alter_database('sys', [PropertyChange.set_property('k', 'v')]))
for operation in operations:
with test.assertRaises(ValueError):
operation()
with test.assertRaisesRegex(ValueError, "Can't do operation on system database."):
catalog.create_database('sys', False)
test.assertNotIn('sys', catalog.list_databases())
test.assertEqual(catalog.list_tables(database), ['t'])


class CatalogTableGuardTest(unittest.TestCase):

Expand Down
Loading