From 5c928088c916b0346422284eb9fcddca59345097 Mon Sep 17 00:00:00 2001 From: Yuya Ebihara Date: Sun, 24 May 2026 10:25:22 +0900 Subject: [PATCH] Make View.sql_for case-insensitive and return None for unknown dialect --- pyiceberg/view/__init__.py | 9 ++++++--- tests/test_view.py | 10 ++++++++-- 2 files changed, 14 insertions(+), 5 deletions(-) diff --git a/pyiceberg/view/__init__.py b/pyiceberg/view/__init__.py index a0bcefa858..7f9343218c 100644 --- a/pyiceberg/view/__init__.py +++ b/pyiceberg/view/__init__.py @@ -80,9 +80,12 @@ def uuid(self) -> UUID: """Return the view's UUID.""" return UUID(self.metadata.view_uuid) - def sql_for(self, dialect: str) -> SQLViewRepresentation: - """Return the view representation for the sql dialect.""" - return next(repr.root for repr in self.current_version().representations if repr.root.dialect == dialect) + def sql_for(self, dialect: str) -> SQLViewRepresentation | None: + """Return the view representation for the sql dialect, or None if no representation could be resolved.""" + return next( + (repr.root for repr in self.current_version().representations if repr.root.dialect.casefold() == dialect.casefold()), + None, + ) def __eq__(self, other: Any) -> bool: """Return the equality of two instances of the View class.""" diff --git a/tests/test_view.py b/tests/test_view.py index 3919e1ab8d..2c4f00192d 100644 --- a/tests/test_view.py +++ b/tests/test_view.py @@ -96,6 +96,13 @@ def test_view_sql_for_dialect(view: View) -> None: assert repr.sql == "SELECT * FROM prod.db.table" +def test_view_sql_for_dialect_ignore_case(view: View) -> None: + repr = view.sql_for("Spark") + assert isinstance(repr, SQLViewRepresentation) + assert repr.dialect == "spark" + assert repr.sql == "SELECT * FROM prod.db.table" + + def test_view_schemas_multiple(example_view_metadata_v1_multiple_versions: dict[str, Any]) -> None: view = View(("default", "test_view"), ViewMetadata.model_validate(example_view_metadata_v1_multiple_versions)) schemas = view.schemas() @@ -117,5 +124,4 @@ def test_view_version_unknown_id(view: View) -> None: def test_view_sql_for_unknown_dialect(view: View) -> None: - with pytest.raises(StopIteration): - view.sql_for("trino") + assert not view.sql_for("trino")