|
20 | 20 | import importlib |
21 | 21 | import logging |
22 | 22 | import re |
| 23 | +import time |
23 | 24 | import uuid |
24 | 25 | from abc import ABC, abstractmethod |
25 | 26 | from collections.abc import Callable |
|
71 | 72 | from pyiceberg.utils.config import Config, merge_config |
72 | 73 | from pyiceberg.utils.properties import property_as_bool |
73 | 74 | from pyiceberg.view import View |
74 | | -from pyiceberg.view.metadata import ViewVersion |
| 75 | +from pyiceberg.view.metadata import SQLViewRepresentation, ViewVersion |
75 | 76 |
|
76 | 77 | if TYPE_CHECKING: |
77 | 78 | import pyarrow as pa |
@@ -744,6 +745,49 @@ def create_view( |
744 | 745 | ViewAlreadyExistsError: If a view with the name already exists. |
745 | 746 | """ |
746 | 747 |
|
| 748 | + def create_sql_view( |
| 749 | + self, |
| 750 | + identifier: str | Identifier, |
| 751 | + schema: Schema | pa.Schema, |
| 752 | + dialect: str, |
| 753 | + sql: str, |
| 754 | + default_namespace: str | Identifier, |
| 755 | + location: str | None = None, |
| 756 | + properties: Properties = EMPTY_DICT, |
| 757 | + default_catalog: str | None = None, |
| 758 | + ) -> View: |
| 759 | + """Create a view. |
| 760 | +
|
| 761 | + Args: |
| 762 | + identifier (str | Identifier): View identifier. |
| 763 | + schema (Schema): View's schema. |
| 764 | + dialect (str): SQL dialect for the view. |
| 765 | + sql (str): SQL for the view. |
| 766 | + default_namespace (str | Identifier): Default namespace name. |
| 767 | + location (str | None): Location for the view. Optional Argument. |
| 768 | + properties (Properties): View properties that can be a string based dictionary. |
| 769 | + default_catalog (str | None): Default catalog name. Optional Argument. |
| 770 | +
|
| 771 | + Returns: |
| 772 | + View: the created view instance. |
| 773 | +
|
| 774 | + Raises: |
| 775 | + ViewAlreadyExistsError: If a view with the name already exists. |
| 776 | + """ |
| 777 | + iceberg_schema = self._convert_schema_if_needed(schema) |
| 778 | + namespace_tuple = Catalog.identifier_to_tuple(default_namespace) |
| 779 | + |
| 780 | + view_version = ViewVersion( |
| 781 | + version_id=1, |
| 782 | + schema_id=iceberg_schema.schema_id, |
| 783 | + timestamp_ms=int(time.time() * 1000), |
| 784 | + summary={}, # TODO Set summary field like EnvironmentContext of Iceberg Java |
| 785 | + representations=[SQLViewRepresentation(type="sql", dialect=dialect, sql=sql)], |
| 786 | + default_catalog=default_catalog, |
| 787 | + default_namespace=namespace_tuple, |
| 788 | + ) |
| 789 | + return self.create_view(identifier, iceberg_schema, view_version, location, properties) |
| 790 | + |
747 | 791 | @staticmethod |
748 | 792 | def identifier_to_tuple(identifier: str | Identifier) -> Identifier: |
749 | 793 | """Parse an identifier to a tuple. |
|
0 commit comments