blob: 5b13d6f195d07c6dd1bbc9ce0e02961bc956fa9b [file]
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.
import json
import unittest
import pyarrow as pa
from pypaimon import Schema
from pypaimon.catalog.catalog_exception import (BranchAlreadyExistException,
BranchNotExistException,
TableNotExistException,
TagNotExistException)
from pypaimon.common.identifier import Identifier
from pypaimon.tests.rest.rest_base_test import RESTBaseTest
class RESTCatalogBranchCRUDTest(RESTBaseTest):
"""End-to-end Branch CRUD against the in-process mock REST server.
Mirrors the matrix exercised by Java
``RESTCatalogTest::testBranches`` (paimon-core/.../rest/RESTCatalogTest.java:2138-2191).
"""
def _identifier(self):
# The base test creates ``default.test_reader_iterator`` and commits a
# snapshot in setUp.
return Identifier.from_string("default.test_reader_iterator")
@staticmethod
def _write(table, data):
builder = table.new_batch_write_builder()
writer = builder.new_write()
try:
writer.write_arrow(data)
builder.new_commit().commit(writer.prepare_commit())
finally:
writer.close()
@staticmethod
def _read(table):
builder = table.new_read_builder()
return builder.new_read().to_arrow(builder.new_scan().plan().splits())
def test_create_branch_table_not_exist(self):
with self.assertRaises(TableNotExistException):
self.rest_catalog.create_branch(
Identifier.from_string("default.no_such_table"), "b1")
def test_list_branches_table_not_exist(self):
with self.assertRaises(TableNotExistException):
self.rest_catalog.list_branches(
Identifier.from_string("default.no_such_table"))
def test_create_branch_without_from_tag(self):
identifier = self._identifier()
self.rest_catalog.create_branch(identifier, "b1")
self.assertEqual(self.rest_catalog.list_branches(identifier), ["b1"])
branch = self.rest_catalog.get_table(Identifier(
identifier.get_database_name(), identifier.get_table_name(), branch="b1"))
self.assertEqual(self._read(branch).num_rows, 0)
def test_create_branch_from_missing_tag_raises(self):
with self.assertRaises(TagNotExistException):
self.rest_catalog.create_branch(
self._identifier(), "b1", tag_name="missing")
def test_branch_table_uses_branch_schema_manager(self):
identifier = self._identifier()
self.rest_catalog.create_branch(identifier, "b1")
branch_identifier = Identifier(
identifier.get_database_name(),
identifier.get_table_name(),
branch="b1",
)
table = self.rest_catalog.get_table(branch_identifier)
self.assertEqual(table.current_branch(), "b1")
self.assertEqual(table.schema_manager.branch, "b1")
self.assertEqual(
json.loads(table.catalog_environment.rest_table_response)['name'],
identifier.get_object_name() + '$branch_b1')
def test_write_blob_to_data_evolution_branch(self):
schema = pa.schema([
("id", pa.int64()),
("payload", pa.large_binary()),
])
identifier = Identifier.from_string("default.test_de_blob_branch")
self.rest_catalog.create_table(
identifier,
Schema.from_pyarrow_schema(
schema,
options={
"data-evolution.enabled": "true",
"row-tracking.enabled": "true",
"blob-field": "payload",
},
),
False,
)
main = self.rest_catalog.get_table(identifier)
self._write(main, pa.table({
"id": [1],
"payload": pa.array([b"main"], pa.large_binary()),
}, schema=schema))
self.assertEqual(
self._read(main).to_pydict(),
{"id": [1], "payload": [b"main"]},
)
self.rest_catalog.create_tag(identifier, "base")
self.rest_catalog.create_branch(identifier, "b1", tag_name="base")
branch_identifier = Identifier(
identifier.get_database_name(), identifier.get_table_name(), branch="b1")
branch = self.rest_catalog.get_table(branch_identifier)
self._write(branch, pa.table({
"id": [2],
"payload": pa.array([b"branch"], pa.large_binary()),
}, schema=schema))
self.assertEqual(
self._read(main).to_pydict(),
{"id": [1], "payload": [b"main"]},
)
self.assertEqual(
self._read(branch).to_pydict(),
{"id": [1, 2], "payload": [b"main", b"branch"]},
)
def test_create_branch_duplicate_raises(self):
identifier = self._identifier()
self.rest_catalog.create_branch(identifier, "b1")
with self.assertRaises(BranchAlreadyExistException) as cm:
self.rest_catalog.create_branch(identifier, "b1")
self.assertEqual(cm.exception.branch, "b1")
def test_list_branches_returns_created(self):
identifier = self._identifier()
for name in ("b1", "b2"):
self.rest_catalog.create_branch(identifier, name)
self.assertEqual(
sorted(self.rest_catalog.list_branches(identifier)), ["b1", "b2"])
def test_rename_branch_happy(self):
identifier = self._identifier()
self.rest_catalog.create_branch(identifier, "b1")
self.rest_catalog.rename_branch(identifier, "b1", "b2")
result = self.rest_catalog.list_branches(identifier)
self.assertNotIn("b1", result)
self.assertIn("b2", result)
def test_rename_branch_to_existing_raises(self):
identifier = self._identifier()
self.rest_catalog.create_branch(identifier, "b1")
self.rest_catalog.create_branch(identifier, "b2")
with self.assertRaises(BranchAlreadyExistException) as cm:
self.rest_catalog.rename_branch(identifier, "b1", "b2")
self.assertEqual(cm.exception.branch, "b2")
def test_rename_branch_from_missing_raises(self):
identifier = self._identifier()
with self.assertRaises(BranchNotExistException) as cm:
self.rest_catalog.rename_branch(identifier, "absent", "b2")
self.assertEqual(cm.exception.branch, "absent")
def test_drop_branch_happy(self):
identifier = self._identifier()
self.rest_catalog.create_branch(identifier, "b1")
self.rest_catalog.drop_branch(identifier, "b1")
self.assertNotIn("b1", self.rest_catalog.list_branches(identifier))
def test_drop_branch_missing_raises(self):
identifier = self._identifier()
with self.assertRaises(BranchNotExistException) as cm:
self.rest_catalog.drop_branch(identifier, "absent")
self.assertEqual(cm.exception.branch, "absent")
def test_fast_forward_missing_raises(self):
identifier = self._identifier()
with self.assertRaises(BranchNotExistException) as cm:
self.rest_catalog.fast_forward(identifier, "absent")
self.assertEqual(cm.exception.branch, "absent")
def test_fast_forward_happy(self):
identifier = self._identifier()
self.rest_catalog.create_branch(identifier, "b1")
# Mock fast-forward is a no-op acknowledgement; the call must not raise.
self.rest_catalog.fast_forward(identifier, "b1")
# Note: the previous ``FilesystemCatalogBranchInheritsNotImplementedTest``
# class has been removed because FileSystemCatalog now overrides the
# branch CRUD methods (no longer raises NotImplementedError). The new
# behavior is covered by ``pypaimon/tests/filesystem_catalog_branch_test.py``.
if __name__ == "__main__":
unittest.main()