-
Notifications
You must be signed in to change notification settings - Fork 511
Add create_sql_view method to Catalog #3415
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -20,6 +20,7 @@ | |||||
| import importlib | ||||||
| import logging | ||||||
| import re | ||||||
| import time | ||||||
| import uuid | ||||||
| from abc import ABC, abstractmethod | ||||||
| from collections.abc import Callable | ||||||
|
|
@@ -71,7 +72,7 @@ | |||||
| from pyiceberg.utils.config import Config, merge_config | ||||||
| from pyiceberg.utils.properties import property_as_bool | ||||||
| from pyiceberg.view import View | ||||||
| from pyiceberg.view.metadata import ViewVersion | ||||||
| from pyiceberg.view.metadata import SQLViewRepresentation, ViewVersion | ||||||
|
|
||||||
| if TYPE_CHECKING: | ||||||
| import pyarrow as pa | ||||||
|
|
@@ -744,6 +745,49 @@ def create_view( | |||||
| ViewAlreadyExistsError: If a view with the name already exists. | ||||||
| """ | ||||||
|
|
||||||
| def create_sql_view( | ||||||
| self, | ||||||
| identifier: str | Identifier, | ||||||
| schema: Schema | pa.Schema, | ||||||
| dialect: str, | ||||||
| sql: str, | ||||||
| default_namespace: str | Identifier, | ||||||
| location: str | None = None, | ||||||
| properties: Properties = EMPTY_DICT, | ||||||
| default_catalog: str | None = None, | ||||||
| ) -> View: | ||||||
| """Create a view. | ||||||
|
|
||||||
| Args: | ||||||
| identifier (str | Identifier): View identifier. | ||||||
| schema (Schema): View's schema. | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
| dialect (str): SQL dialect for the view. | ||||||
| sql (str): SQL for the view. | ||||||
| default_namespace (str | Identifier): Default namespace name. | ||||||
| location (str | None): Location for the view. Optional Argument. | ||||||
| properties (Properties): View properties that can be a string based dictionary. | ||||||
| default_catalog (str | None): Default catalog name. Optional Argument. | ||||||
|
|
||||||
| Returns: | ||||||
| View: the created view instance. | ||||||
|
|
||||||
| Raises: | ||||||
| ViewAlreadyExistsError: If a view with the name already exists. | ||||||
| """ | ||||||
| iceberg_schema = self._convert_schema_if_needed(schema) | ||||||
| namespace_tuple = Catalog.identifier_to_tuple(default_namespace) | ||||||
|
|
||||||
| view_version = ViewVersion( | ||||||
| version_id=1, | ||||||
| schema_id=iceberg_schema.schema_id, | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. is this schema_id discarded later in the self.create_view call later?
|
||||||
| timestamp_ms=int(time.time() * 1000), | ||||||
| summary={}, # TODO Set summary field like EnvironmentContext of Iceberg Java | ||||||
| representations=[SQLViewRepresentation(type="sql", dialect=dialect, sql=sql)], | ||||||
| default_catalog=default_catalog, | ||||||
| default_namespace=namespace_tuple, | ||||||
| ) | ||||||
| return self.create_view(identifier, iceberg_schema, view_version, location, properties) | ||||||
|
|
||||||
| @staticmethod | ||||||
| def identifier_to_tuple(identifier: str | Identifier) -> Identifier: | ||||||
| """Parse an identifier to a tuple. | ||||||
|
|
||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -653,6 +653,23 @@ def test_rest_create_view( | |
| assert rest_catalog.load_view(identifier).schema() == view.schema() | ||
|
|
||
|
|
||
| @pytest.mark.integration | ||
| def test_rest_create_sql_view( | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The |
||
| rest_catalog: RestCatalog, example_view_metadata_v1: dict[str, Any], database_name: str, view_name: str | ||
| ) -> None: | ||
| identifier = (database_name, view_name) | ||
|
|
||
| rest_catalog.create_namespace_if_not_exists(database_name) | ||
| view = View(identifier, ViewMetadata.model_validate(example_view_metadata_v1)) | ||
|
|
||
| assert not rest_catalog.view_exists(identifier) | ||
|
|
||
| rest_catalog.create_sql_view(identifier, view.schema(), "spark", "SELECT * FROM prod.db.table", "default") | ||
|
|
||
| assert rest_catalog.view_exists(identifier) | ||
| assert rest_catalog.load_view(identifier).schema() == view.schema() | ||
|
|
||
|
|
||
| @pytest.mark.integration | ||
| def test_rest_drop_view( | ||
| rest_catalog: RestCatalog, example_view_metadata_v1: dict[str, Any], database_name: str, view_name: str | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should this description be different from create_view?