Skip to content

Commit 4dbb967

Browse files
committed
View api
1 parent ee7d04b commit 4dbb967

1 file changed

Lines changed: 43 additions & 4 deletions

File tree

pyiceberg/view/__init__.py

Lines changed: 43 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,11 @@
1616
# under the License.
1717
from __future__ import annotations
1818

19-
from typing import (
20-
Any,
21-
)
19+
from typing import Any
2220

21+
from pyiceberg.schema import Schema
2322
from pyiceberg.typedef import Identifier
24-
from pyiceberg.view.metadata import ViewMetadata
23+
from pyiceberg.view.metadata import SQLViewRepresentation, ViewHistoryEntry, ViewMetadata, ViewVersion
2524

2625

2726
class View:
@@ -42,6 +41,46 @@ def name(self) -> Identifier:
4241
"""Return the identifier of this view."""
4342
return self._identifier
4443

44+
def schema(self) -> Schema:
45+
"""Return the schema for this view."""
46+
return next(schema for schema in self.metadata.schemas if schema.schema_id == self.current_version().schema_id)
47+
48+
def schemas(self) -> dict[int, Schema]:
49+
"""Return the schemas for this view."""
50+
return {schema.schema_id: schema for schema in self.metadata.schemas}
51+
52+
def current_version(self) -> ViewVersion:
53+
"""Get the version of this view."""
54+
return next(version for version in self.metadata.versions if version.version_id == self.metadata.current_version_id)
55+
56+
def versions(self) -> list[ViewVersion]:
57+
"""Get the versions of this view."""
58+
return self.metadata.versions
59+
60+
def version(self, version_id: int) -> ViewVersion:
61+
"""Get the version in this view by ID."""
62+
return next(version for version in self.metadata.versions if version.version_id == version_id)
63+
64+
def history(self) -> list[ViewHistoryEntry]:
65+
"""Get the version of this history view."""
66+
return self.metadata.version_log
67+
68+
def properties(self) -> dict[str, str]:
69+
"""Return a map of string properties for this view."""
70+
return self.metadata.properties
71+
72+
def location(self) -> str:
73+
"""Return the view's base location."""
74+
return self.metadata.location
75+
76+
def uuid(self) -> str:
77+
"""Return the view's UUID."""
78+
return self.metadata.view_uuid
79+
80+
def sql_for(self, dialect: str) -> SQLViewRepresentation:
81+
"""Return the view representation for the sql dialect."""
82+
return next(repr.root for repr in self.current_version().representations if repr.root.dialect == dialect)
83+
4584
def __eq__(self, other: Any) -> bool:
4685
"""Return the equality of two instances of the View class."""
4786
return self.name() == other.name() and self.metadata == other.metadata if isinstance(other, View) else False

0 commit comments

Comments
 (0)