Skip to content

Commit e0572b1

Browse files
authored
[#9548] improvement(python-client): Add TableUpdateRequest and TableUpdatesRequest (#9870)
### What changes were proposed in this pull request? Add `TableUpdateRequest` and `TableUpdatesRequest` ### Why are the changes needed? Fix: #9548 ### Does this PR introduce _any_ user-facing change? no ### How was this patch tested? local unittest
1 parent bbb14ad commit e0572b1

File tree

11 files changed

+1847
-11
lines changed

11 files changed

+1847
-11
lines changed

clients/client-python/gravitino/api/rel/table_change.py

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -35,16 +35,14 @@ class TableChange(ABC):
3535
"""
3636

3737
@staticmethod
38-
def rename(new_name: str) -> "RenameTable":
38+
def rename(new_name: str, new_schema_name: Optional[str] = None) -> "RenameTable":
3939
"""Create a `TableChange` for renaming a table.
4040
4141
Args:
4242
new_name: The new table name.
43-
44-
Returns:
45-
RenameTable: A `TableChange` for the rename.
43+
new_schema_name: The new schema name if cross-schema rename is requested.
4644
"""
47-
return TableChange.RenameTable(new_name)
45+
return TableChange.RenameTable(new_name, new_schema_name)
4846

4947
@staticmethod
5048
def update_comment(new_comment: str) -> "UpdateComment":
@@ -315,6 +313,9 @@ class RenameTable:
315313
"""A `TableChange` to rename a table."""
316314

317315
_new_name: str = field(metadata=config(field_name="new_name"))
316+
_new_schema_name: Optional[str] = field(
317+
default=None, metadata=config(field_name="new_schema_name")
318+
)
318319

319320
def get_new_name(self) -> str:
320321
"""Retrieves the new name for the table.
@@ -324,17 +325,26 @@ def get_new_name(self) -> str:
324325
"""
325326
return self._new_name
326327

328+
def get_new_schema_name(self) -> Optional[str]:
329+
"""Retrieves the new schema name for the table."""
330+
return self._new_schema_name
331+
327332
def __str__(self):
328-
return f"RENAMETABLE {self._new_name}"
333+
if self._new_schema_name is None:
334+
return f"RENAMETABLE {self._new_name}"
335+
return f"RENAMETABLE {self._new_schema_name}.{self._new_name}"
329336

330337
def __eq__(self, value: object) -> bool:
331338
if not isinstance(value, TableChange.RenameTable):
332339
return False
333340
other = cast(TableChange.RenameTable, value)
334-
return self._new_name == other.get_new_name()
341+
return (
342+
self._new_name == other.get_new_name()
343+
and self._new_schema_name == other.get_new_schema_name()
344+
)
335345

336346
def __hash__(self) -> int:
337-
return hash(self._new_name)
347+
return hash((self._new_name, self._new_schema_name))
338348

339349
@final
340350
@dataclass(frozen=True)

clients/client-python/gravitino/dto/rel/indexes/index_dto.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
# specific language governing permissions and limitations
1616
# under the License.
1717

18+
from __future__ import annotations
19+
1820
from functools import reduce
1921
from typing import ClassVar, List, Optional, Dict
2022

clients/client-python/gravitino/dto/rel/json_serdes/__init__.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,13 @@
1414
# KIND, either express or implied. See the License for the
1515
# specific language governing permissions and limitations
1616
# under the License.
17+
18+
from gravitino.dto.rel.json_serdes.column_position_serdes import ColumnPositionSerdes
19+
from gravitino.dto.rel.json_serdes.distribution_serdes import DistributionSerDes
20+
from gravitino.dto.rel.json_serdes.sort_order_serdes import SortOrderSerdes
21+
22+
__all__ = [
23+
"ColumnPositionSerdes",
24+
"DistributionSerDes",
25+
"SortOrderSerdes",
26+
]
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
# Licensed to the Apache Software Foundation (ASF) under one
2+
# or more contributor license agreements. See the NOTICE file
3+
# distributed with this work for additional information
4+
# regarding copyright ownership. The ASF licenses this file
5+
# to you under the Apache License, Version 2.0 (the
6+
# "License"); you may not use this file except in compliance
7+
# with the License. You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing,
12+
# software distributed under the License is distributed on an
13+
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
# KIND, either express or implied. See the License for the
15+
# specific language governing permissions and limitations
16+
# under the License.
17+
18+
from typing import Union
19+
20+
from dataclasses_json.core import Json
21+
22+
from gravitino.api.rel.table_change import After, Default, First, TableChange
23+
from gravitino.api.rel.types.json_serdes.base import JsonSerializable
24+
25+
26+
class ColumnPositionSerdes(JsonSerializable[TableChange.ColumnPosition]):
27+
"""JSON serializer/deserializer for table column positions."""
28+
29+
_POSITION_FIRST = "first"
30+
_POSITION_AFTER = "after"
31+
_POSITION_DEFAULT = "default"
32+
33+
@classmethod
34+
def serialize(
35+
cls,
36+
value: TableChange.ColumnPosition,
37+
) -> Union[str, dict[str, str]]:
38+
if isinstance(value, First):
39+
return cls._POSITION_FIRST
40+
if isinstance(value, After):
41+
return {cls._POSITION_AFTER: value.get_column()}
42+
if isinstance(value, Default):
43+
return cls._POSITION_DEFAULT
44+
45+
raise ValueError(f"Unknown column position: {value}")
46+
47+
@classmethod
48+
def deserialize(cls, data: Json) -> Union[First, After, Default]:
49+
if isinstance(data, str):
50+
data = data.lower()
51+
if data == cls._POSITION_FIRST:
52+
return TableChange.ColumnPosition.first()
53+
if data == cls._POSITION_DEFAULT:
54+
return TableChange.ColumnPosition.default_pos()
55+
56+
if isinstance(data, dict):
57+
after_column = data.get(cls._POSITION_AFTER)
58+
if after_column:
59+
return TableChange.ColumnPosition.after(after_column)
60+
61+
raise ValueError(f"Unknown json column position: {data}")

0 commit comments

Comments
 (0)