Skip to content

Commit 561da0c

Browse files
committed
adding test
1 parent da1daf7 commit 561da0c

File tree

2 files changed

+49
-9
lines changed

2 files changed

+49
-9
lines changed

pyiceberg/view/metadata.py

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

19-
from typing import Dict, List, Literal, Optional
19+
from typing import Dict, List, Literal, Optional, Union
2020

2121
from pydantic import Field, field_validator
2222

2323
from pyiceberg.schema import Schema
24-
from pyiceberg.typedef import IcebergBaseModel, Properties
24+
from pyiceberg.typedef import IcebergBaseModel, Identifier, Properties
2525
from pyiceberg.types import transform_dict_value_to_str
2626

2727

@@ -36,10 +36,10 @@ class SQLViewRepresentation(IcebergBaseModel):
3636
"""The dialect of the SQL, e.g. `spark`, `trino`, `presto`."""
3737

3838

39-
class ViewRepresentation(BaseModel):
39+
class ViewRepresentation(IcebergBaseModel):
4040
__root__: SQLViewRepresentation
41-
42-
41+
42+
4343
class ViewVersion(IcebergBaseModel):
4444
"""A version of the view definition."""
4545

@@ -51,11 +51,11 @@ class ViewVersion(IcebergBaseModel):
5151
"""Timestamp when the version was created (ms from epoch)"""
5252
summary: Dict[str, str] = Field()
5353
"""A string to string map of summary metadata about the version"""
54-
representations: List[SQLViewRepresentation] = Field()
54+
representations: List[ViewRepresentation] = Field()
5555
"""A list of representations for the view definition"""
5656
default_catalog: Optional[str] = Field(alias="default-catalog", default=None)
5757
"""Catalog name to use when a reference in the SELECT does not contain a catalog"""
58-
default_namespace: Namespace = Field(alias="default-namespace")
58+
default_namespace: Union[str, Identifier] = Field(alias="default-namespace")
5959
"""Namespace to use when a reference in the SELECT is a single identifier"""
6060

6161

@@ -73,7 +73,7 @@ class ViewMetadata(IcebergBaseModel):
7373

7474
view_uuid: str = Field(alias="view-uuid")
7575
"""A UUID that identifies the view, generated when the view is created."""
76-
format_version: int = Field(alias='format-version', ge=1, le=1)
76+
format_version: int = Field(alias="format-version", ge=1, le=1)
7777
"""An integer version number for the view format; must be 1"""
7878
location: str = Field()
7979
"""The view's base location; used to create metadata file locations"""
@@ -83,7 +83,7 @@ class ViewMetadata(IcebergBaseModel):
8383
"""ID of the current version of the view (version-id)"""
8484
versions: List[ViewVersion] = Field()
8585
"""A list of known versions of the view"""
86-
version_log: List[ViewVersionLogEntry] = Field(alias="version-log")
86+
version_log: List[ViewHistoryEntry] = Field(alias="version-log")
8787
"""A list of version log entries"""
8888
properties: Properties = Field(default_factory=dict)
8989
"""A string to string map of view properties"""

tests/integration/test_writes/test_writes.py

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@
5959
NestedField,
6060
StringType,
6161
)
62+
from pyiceberg.view.metadata import SQLViewRepresentation, ViewVersion
6263
from utils import _create_table
6364

6465

@@ -1519,6 +1520,45 @@ def test_table_v1_with_null_nested_namespace(session_catalog: Catalog, arrow_tab
15191520
session_catalog.drop_table(identifier)
15201521

15211522

1523+
@pytest.mark.integration
1524+
def test_create_view(
1525+
spark: SparkSession,
1526+
session_catalog: Catalog,
1527+
) -> None:
1528+
# Create a view using the REST Catalog.
1529+
identifier = "default.some_view"
1530+
schema = pa.schema([pa.field("some_col", pa.int32())])
1531+
view_version = ViewVersion(
1532+
version_id=1,
1533+
schema_id=1,
1534+
timestamp_ms=int(time.time() * 1000),
1535+
summary={},
1536+
representations=[
1537+
SQLViewRepresentation(
1538+
type="sql",
1539+
sql="SELECT 1 as some_col",
1540+
dialect="spark",
1541+
)
1542+
],
1543+
default_namespace="default",
1544+
)
1545+
session_catalog.create_view(
1546+
identifier=identifier,
1547+
schema=schema,
1548+
view_version=view_version,
1549+
)
1550+
1551+
# Ensure the view exists.
1552+
assert session_catalog.view_exists(identifier)
1553+
1554+
# Query the view in spark to ensure it was properly created.
1555+
df = spark.table(identifier)
1556+
assert df.count() == 1
1557+
assert df.collect()[0].some_col == 1
1558+
1559+
session_catalog.drop_view(identifier) # clean up
1560+
1561+
15221562
@pytest.mark.integration
15231563
def test_view_exists(
15241564
spark: SparkSession,

0 commit comments

Comments
 (0)