|
20 | 20 | from dataclasses import dataclass |
21 | 21 | from typing import Dict, Generic, List, Optional |
22 | 22 |
|
| 23 | +from pypaimon.common.identifier import Identifier |
23 | 24 | from pypaimon.common.json_util import T, json_field |
24 | 25 | from pypaimon.common.options import Options |
| 26 | +from pypaimon.schema.data_types import DataField |
25 | 27 | from pypaimon.schema.schema import Schema |
26 | 28 | from pypaimon.snapshot.snapshot_commit import PartitionStatistics |
27 | 29 | from pypaimon.snapshot.table_snapshot import TableSnapshot |
@@ -327,3 +329,227 @@ def __init__(self, snapshot: Optional[TableSnapshot] = None): |
327 | 329 |
|
328 | 330 | def get_snapshot(self) -> Optional[TableSnapshot]: |
329 | 331 | return self.snapshot |
| 332 | + |
| 333 | + |
| 334 | +@dataclass |
| 335 | +class GetFunctionResponse(AuditRESTResponse): |
| 336 | + """Response for getting a function.""" |
| 337 | + FIELD_UUID = "uuid" |
| 338 | + FIELD_NAME = "name" |
| 339 | + FIELD_INPUT_PARAMS = "inputParams" |
| 340 | + FIELD_RETURN_PARAMS = "returnParams" |
| 341 | + FIELD_DETERMINISTIC = "deterministic" |
| 342 | + FIELD_DEFINITIONS = "definitions" |
| 343 | + FIELD_COMMENT = "comment" |
| 344 | + FIELD_OPTIONS = "options" |
| 345 | + |
| 346 | + uuid: Optional[str] = json_field(FIELD_UUID, default=None) |
| 347 | + name: Optional[str] = json_field(FIELD_NAME, default=None) |
| 348 | + input_params: Optional[List[DataField]] = json_field(FIELD_INPUT_PARAMS, default=None) |
| 349 | + return_params: Optional[List[DataField]] = json_field(FIELD_RETURN_PARAMS, default=None) |
| 350 | + deterministic: bool = json_field(FIELD_DETERMINISTIC, default=False) |
| 351 | + definitions: Optional[Dict[str, 'FunctionDefinition']] = json_field(FIELD_DEFINITIONS, default=None) |
| 352 | + comment: Optional[str] = json_field(FIELD_COMMENT, default=None) |
| 353 | + options: Optional[Dict[str, str]] = json_field(FIELD_OPTIONS, default=None) |
| 354 | + |
| 355 | + def __init__( |
| 356 | + self, |
| 357 | + uuid: Optional[str] = None, |
| 358 | + name: Optional[str] = None, |
| 359 | + input_params: Optional[List[DataField]] = None, |
| 360 | + return_params: Optional[List[DataField]] = None, |
| 361 | + deterministic: bool = False, |
| 362 | + definitions: Optional[Dict[str, 'FunctionDefinition']] = None, |
| 363 | + comment: Optional[str] = None, |
| 364 | + options: Optional[Dict[str, str]] = None, |
| 365 | + owner: Optional[str] = None, |
| 366 | + created_at: Optional[int] = None, |
| 367 | + created_by: Optional[str] = None, |
| 368 | + updated_at: Optional[int] = None, |
| 369 | + updated_by: Optional[str] = None, |
| 370 | + ): |
| 371 | + super().__init__(owner, created_at, created_by, updated_at, updated_by) |
| 372 | + self.uuid = uuid |
| 373 | + self.name = name |
| 374 | + self.input_params = input_params |
| 375 | + self.return_params = return_params |
| 376 | + self.deterministic = deterministic |
| 377 | + self.definitions = definitions |
| 378 | + self.comment = comment |
| 379 | + self.options = options |
| 380 | + |
| 381 | + def to_function(self, identifier): |
| 382 | + from pypaimon.function.function import FunctionImpl |
| 383 | + return FunctionImpl( |
| 384 | + identifier=identifier, |
| 385 | + input_params=self.input_params, |
| 386 | + return_params=self.return_params, |
| 387 | + deterministic=self.deterministic, |
| 388 | + definitions=self.definitions or {}, |
| 389 | + comment=self.comment, |
| 390 | + options=self.options or {}, |
| 391 | + ) |
| 392 | + |
| 393 | + @staticmethod |
| 394 | + def _parse_data_fields(raw: Optional[list]) -> Optional[List[DataField]]: |
| 395 | + if raw is None: |
| 396 | + return None |
| 397 | + return [DataField.from_dict(f) if isinstance(f, dict) else f for f in raw] |
| 398 | + |
| 399 | + @staticmethod |
| 400 | + def _parse_definitions(raw) -> Optional[Dict[str, 'FunctionDefinition']]: |
| 401 | + from pypaimon.function.function_definition import FunctionDefinition |
| 402 | + if raw is None: |
| 403 | + return None |
| 404 | + return { |
| 405 | + k: FunctionDefinition.from_dict(v) if isinstance(v, dict) else v |
| 406 | + for k, v in raw.items() |
| 407 | + } |
| 408 | + |
| 409 | + @classmethod |
| 410 | + def from_dict(cls, data: Dict) -> "GetFunctionResponse": |
| 411 | + return cls( |
| 412 | + uuid=data.get("uuid"), |
| 413 | + name=data.get("name"), |
| 414 | + input_params=cls._parse_data_fields(data.get("inputParams")), |
| 415 | + return_params=cls._parse_data_fields(data.get("returnParams")), |
| 416 | + deterministic=data.get("deterministic", False), |
| 417 | + definitions=cls._parse_definitions(data.get("definitions")), |
| 418 | + comment=data.get("comment"), |
| 419 | + options=data.get("options"), |
| 420 | + owner=data.get("owner"), |
| 421 | + created_at=data.get("createdAt"), |
| 422 | + created_by=data.get("createdBy"), |
| 423 | + updated_at=data.get("updatedAt"), |
| 424 | + updated_by=data.get("updatedBy"), |
| 425 | + ) |
| 426 | + |
| 427 | + def to_dict(self) -> Dict: |
| 428 | + result = {} |
| 429 | + if self.uuid is not None: |
| 430 | + result["uuid"] = self.uuid |
| 431 | + result["name"] = self.name |
| 432 | + result["inputParams"] = ( |
| 433 | + [p.to_dict() if hasattr(p, 'to_dict') else p for p in self.input_params] |
| 434 | + if self.input_params is not None else None |
| 435 | + ) |
| 436 | + result["returnParams"] = ( |
| 437 | + [p.to_dict() if hasattr(p, 'to_dict') else p for p in self.return_params] |
| 438 | + if self.return_params is not None else None |
| 439 | + ) |
| 440 | + result["deterministic"] = self.deterministic |
| 441 | + if self.definitions is not None: |
| 442 | + result["definitions"] = { |
| 443 | + k: v.to_dict() if hasattr(v, 'to_dict') else v |
| 444 | + for k, v in self.definitions.items() |
| 445 | + } |
| 446 | + else: |
| 447 | + result["definitions"] = None |
| 448 | + result["comment"] = self.comment |
| 449 | + result["options"] = self.options |
| 450 | + if self.owner is not None: |
| 451 | + result["owner"] = self.owner |
| 452 | + if self.created_at is not None: |
| 453 | + result["createdAt"] = self.created_at |
| 454 | + if self.created_by is not None: |
| 455 | + result["createdBy"] = self.created_by |
| 456 | + if self.updated_at is not None: |
| 457 | + result["updatedAt"] = self.updated_at |
| 458 | + if self.updated_by is not None: |
| 459 | + result["updatedBy"] = self.updated_by |
| 460 | + return result |
| 461 | + |
| 462 | + |
| 463 | +@dataclass |
| 464 | +class ListFunctionsResponse(PagedResponse[str]): |
| 465 | + """Response for listing functions.""" |
| 466 | + FIELD_FUNCTIONS = "functions" |
| 467 | + |
| 468 | + functions: Optional[List[str]] = json_field(FIELD_FUNCTIONS, default=None) |
| 469 | + next_page_token: Optional[str] = json_field( |
| 470 | + PagedResponse.FIELD_NEXT_PAGE_TOKEN, default=None) |
| 471 | + |
| 472 | + def data(self) -> Optional[List[str]]: |
| 473 | + return self.functions |
| 474 | + |
| 475 | + def get_next_page_token(self) -> Optional[str]: |
| 476 | + return self.next_page_token |
| 477 | + |
| 478 | + |
| 479 | +@dataclass |
| 480 | +class ListFunctionDetailsResponse(PagedResponse['GetFunctionResponse']): |
| 481 | + """Response for listing function details.""" |
| 482 | + FIELD_FUNCTION_DETAILS = "functionDetails" |
| 483 | + |
| 484 | + function_details: Optional[List[GetFunctionResponse]] = json_field( |
| 485 | + FIELD_FUNCTION_DETAILS, default=None) |
| 486 | + next_page_token: Optional[str] = json_field( |
| 487 | + PagedResponse.FIELD_NEXT_PAGE_TOKEN, default=None) |
| 488 | + |
| 489 | + def data(self) -> Optional[List[GetFunctionResponse]]: |
| 490 | + return self.function_details |
| 491 | + |
| 492 | + def get_next_page_token(self) -> Optional[str]: |
| 493 | + return self.next_page_token |
| 494 | + |
| 495 | + @classmethod |
| 496 | + def from_dict(cls, data: Dict) -> "ListFunctionDetailsResponse": |
| 497 | + details = data.get("functionDetails") |
| 498 | + if details is not None: |
| 499 | + details = [GetFunctionResponse.from_dict(d) for d in details] |
| 500 | + return cls( |
| 501 | + function_details=details, |
| 502 | + next_page_token=data.get("nextPageToken"), |
| 503 | + ) |
| 504 | + |
| 505 | + def to_dict(self) -> Dict: |
| 506 | + result = {} |
| 507 | + if self.function_details is not None: |
| 508 | + result["functionDetails"] = [d.to_dict() for d in self.function_details] |
| 509 | + else: |
| 510 | + result["functionDetails"] = None |
| 511 | + result["nextPageToken"] = self.next_page_token |
| 512 | + return result |
| 513 | + |
| 514 | + |
| 515 | +@dataclass |
| 516 | +class ListFunctionsGloballyResponse(PagedResponse[Identifier]): |
| 517 | + """Response for listing functions globally across databases.""" |
| 518 | + FIELD_FUNCTIONS = "functions" |
| 519 | + |
| 520 | + functions: Optional[List[Identifier]] = json_field(FIELD_FUNCTIONS, default=None) |
| 521 | + next_page_token: Optional[str] = json_field( |
| 522 | + PagedResponse.FIELD_NEXT_PAGE_TOKEN, default=None) |
| 523 | + |
| 524 | + def data(self) -> Optional[List[Identifier]]: |
| 525 | + return self.functions |
| 526 | + |
| 527 | + def get_next_page_token(self) -> Optional[str]: |
| 528 | + return self.next_page_token |
| 529 | + |
| 530 | + @classmethod |
| 531 | + def from_dict(cls, data: Dict) -> "ListFunctionsGloballyResponse": |
| 532 | + functions = data.get("functions") |
| 533 | + if functions is not None: |
| 534 | + functions = [ |
| 535 | + Identifier.from_string(f) if isinstance(f, str) else |
| 536 | + Identifier.create(f.get("database"), f.get("object")) |
| 537 | + if isinstance(f, dict) else f |
| 538 | + for f in functions |
| 539 | + ] |
| 540 | + return cls( |
| 541 | + functions=functions, |
| 542 | + next_page_token=data.get("nextPageToken"), |
| 543 | + ) |
| 544 | + |
| 545 | + def to_dict(self) -> Dict: |
| 546 | + result = {} |
| 547 | + if self.functions is not None: |
| 548 | + result["functions"] = [ |
| 549 | + {"database": f.get_database_name(), "object": f.get_object_name()} |
| 550 | + for f in self.functions |
| 551 | + ] |
| 552 | + else: |
| 553 | + result["functions"] = None |
| 554 | + result["nextPageToken"] = self.next_page_token |
| 555 | + return result |
0 commit comments