jerryshao commented on code in PR #8841:
URL: https://github.com/apache/gravitino/pull/8841#discussion_r2446540517
##########
clients/client-python/gravitino/dto/util/dto_converters.py:
##########
@@ -76,3 +94,219 @@ def from_function_args(args: list[FunctionArg]) ->
list[Expression]:
if not args:
return Expression.EMPTY_EXPRESSION
return [DTOConverters.from_function_arg(arg) for arg in args]
+
+ @singledispatchmethod
+ @staticmethod
+ def from_dto(dto) -> object:
+ raise IllegalArgumentException(f"Unsupported DTO type: {type(dto)}")
+
+ @from_dto.register
+ @staticmethod
+ def _(dto: DistributionDTO) -> Distribution:
+ """Converts a DistributionDTO to a Distribution.
+
+ Args:
+ dto (DistributionDTO): The distribution DTO.
+
+ Returns:
+ Distribution: The distribution.
+ """
+ if dto is None or DistributionDTO.NONE.equals(dto):
+ return Distributions.NONE
+
+ return Distributions.of(
+ dto.strategy(),
+ dto.number(),
+ *DTOConverters.from_function_args(dto.args()),
+ )
+
+ @from_dto.register
+ @staticmethod
+ def _(dto: IndexDTO) -> Index:
+ """Converts an IndexDTO to an Index.
+
+ Args:
+ dto (IndexDTO): The Index DTO to be converted.
+
+ Returns:
+ Index: The index.
+ """
+ return Indexes.of(dto.type(), dto.name(), dto.field_names())
+
+ @from_dto.register
+ @staticmethod
+ def _(dto: SortOrderDTO) -> SortOrder:
+ """Converts a SortOrderDTO to a SortOrder.
+
+ Args:
+ dto (SortOrderDTO): The sort order DTO to be converted.
+
+ Returns:
+ SortOrder: The sort order.
+ """
+ return SortOrders.of(
+ DTOConverters.from_function_arg(dto.sort_term()),
+ dto.direction(),
+ dto.null_ordering(),
+ )
+
+ @from_dto.register
+ @staticmethod
+ def _(dto: ColumnDTO) -> Column:
+ """Converts a ColumnDTO to a Column.
+
+ Args:
+ dto (ColumnDTO): The column DTO to be converted.
+
+ Returns:
+ Column: The column.
+ """
+ dto_default_value = dto.default_value()
+ return Column.of(
+ dto.name(),
+ dto.data_type(),
+ dto.comment(),
+ dto.nullable(),
+ dto.auto_increment(),
+ (
+ None
+ if dto_default_value == Column.DEFAULT_VALUE_NOT_SET
+ else DTOConverters.from_function_arg(dto.default_value())
+ ),
+ )
+
+ @from_dto.register
+ @staticmethod
+ def _(dto: Partitioning) -> Transform: # pylint:
disable=too-many-return-statements
+ """Converts a partitioning DTO to a Transform.
+
+ Args:
+ dto (Partitioning): The partitioning DTO to be converted.
+
+ Returns:
+ Transform: The transform.
+ """
+ strategy = dto.strategy()
+ if strategy is Partitioning.Strategy.IDENTITY:
+ return Transforms.identity(dto.field_name())
+ if strategy is Partitioning.Strategy.YEAR:
+ return Transforms.year(dto.field_name())
+ if strategy is Partitioning.Strategy.MONTH:
+ return Transforms.month(dto.field_name())
+ if strategy is Partitioning.Strategy.DAY:
+ return Transforms.day(dto.field_name())
+ if strategy is Partitioning.Strategy.HOUR:
+ return Transforms.hour(dto.field_name())
+ if strategy is Partitioning.Strategy.BUCKET:
+ return Transforms.bucket(dto.num_buckets(), *dto.field_names())
+ if strategy is Partitioning.Strategy.TRUNCATE:
+ return Transforms.truncate(dto.width(), dto.field_name())
+ if strategy is Partitioning.Strategy.LIST:
+ return Transforms.list(
+ field_names=dto.field_names(),
+ assignments=[DTOConverters.from_dto(p) for p in
dto.assignments()],
+ )
+ if strategy is Partitioning.Strategy.RANGE:
+ return Transforms.range(
+ dto.field_name(),
+ [DTOConverters.from_dto(p) for p in dto.assignments()],
+ )
+ if strategy is Partitioning.Strategy.FUNCTION:
+ return Transforms.apply(
+ dto.function_name(),
DTOConverters.from_function_args(dto.args())
+ )
+ raise IllegalArgumentException(f"Unsupported partitioning: {strategy}")
+
+ @from_dto.register
+ @staticmethod
+ def _(dto: TableDTO) -> Table:
+ """Converts a TableDTO to a Table.
+
+ Args:
+ dto (TableDTO): The table DTO to be converted.
+
+ Returns:
+ Table: The table.
+ """
+
+ class TableImpl(Table): # pylint: disable=too-many-instance-attributes
+ """A table implementation."""
+
+ def __init__(
+ self,
+ name: str,
+ columns: list[Column],
+ partitioning: list[Transform],
+ sort_order: list[SortOrder],
+ distribution: Distribution,
+ index: list[Index],
+ comment: Optional[str],
+ properties: dict[str, str],
+ audit_info: Audit,
+ ):
+ self._name = name
+ self._columns = columns
+ self._partitioning = partitioning
+ self._sort_order = sort_order
+ self._distribution = distribution
+ self._index = index
+ self._comment = comment
+ self._properties = properties
+ self._audit_info = audit_info
+
+ def name(self) -> str:
+ return self._name
+
+ def columns(self) -> list[Column]:
+ return self._columns
+
+ def partitioning(self) -> list[Transform]:
+ return self._partitioning
+
+ def sort_order(self) -> list[SortOrder]:
+ return self._sort_order
+
+ def distribution(self) -> Distribution:
+ return self._distribution
+
+ def index(self) -> list[Index]:
+ return self._index
+
+ def comment(self) -> Optional[str]:
+ return self._comment
+
+ def properties(self) -> dict[str, str]:
+ return self._properties
+
+ def audit_info(self) -> Audit:
+ return self._audit_info
+
+ return TableImpl(
+ name=dto.name(),
+ columns=DTOConverters.from_dtos(dto.columns()),
+ partitioning=DTOConverters.from_dtos(dto.partitioning()),
+ sort_order=DTOConverters.from_dtos(dto.sort_order()),
+ distribution=DTOConverters.from_dto(dto.distribution()),
+ index=DTOConverters.from_dtos(dto.index()),
+ comment=dto.comment(),
+ properties=dto.properties(),
+ audit_info=dto.audit_info(),
+ )
+
+ @staticmethod
+ def from_dtos(
Review Comment:
I'm fine with either. From my point, we don't have to be too Java-ish. We'd
follow the Python style / convention, just make sure the semantic is the same,
that should be enough.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]