|
16 | 16 | # under the License. |
17 | 17 |
|
18 | 18 | import os |
| 19 | +import time |
19 | 20 | import uuid |
20 | 21 | from collections.abc import Generator |
21 | 22 | from pathlib import Path, PosixPath |
22 | 23 | from typing import Any |
23 | 24 |
|
24 | 25 | import pytest |
| 26 | +from pyspark.sql import SparkSession |
25 | 27 | from pytest_lazy_fixtures import lf |
26 | 28 |
|
27 | 29 | from pyiceberg.catalog import Catalog, MetastoreCatalog, load_catalog |
@@ -917,3 +919,57 @@ def test_load_missing_table(test_catalog: Catalog, database_name: str, table_nam |
917 | 919 |
|
918 | 920 | with pytest.raises(NoSuchTableError): |
919 | 921 | test_catalog.load_table(identifier) |
| 922 | + |
| 923 | + |
| 924 | +@pytest.mark.integration |
| 925 | +def test_list_views( |
| 926 | + session_catalog_hive: HiveCatalog, |
| 927 | + spark: SparkSession, |
| 928 | +) -> None: |
| 929 | + """ |
| 930 | + Verify that a view created by Spark through the Iceberg Hive catalog |
| 931 | + can be discovered by PyIceberg HiveCatalog.list_views(). |
| 932 | +
|
| 933 | + The test also verifies that: |
| 934 | + - Iceberg tables are not returned as views. |
| 935 | + - Multiple views are returned. |
| 936 | + - Returned identifiers use the expected (namespace, view_name) format. |
| 937 | + """ |
| 938 | + suffix = int(time.time()) |
| 939 | + catalog_name = "hive" |
| 940 | + namespace = "default" |
| 941 | + table_name = f"table_{suffix}" |
| 942 | + first_view_name = f"first_view_{suffix}" |
| 943 | + second_view_name = f"second_view_{suffix}" |
| 944 | + table_identifier = f"{catalog_name}.{namespace}.{table_name}" |
| 945 | + first_view_identifier = f"{catalog_name}.{namespace}.{first_view_name}" |
| 946 | + second_view_identifier = f"{catalog_name}.{namespace}.{second_view_name}" |
| 947 | + |
| 948 | + spark.sql(f""" |
| 949 | + CREATE TABLE {table_identifier} ( |
| 950 | + id INTEGER, |
| 951 | + name STRING, |
| 952 | + dt DATE |
| 953 | + ) |
| 954 | + USING iceberg |
| 955 | + """) |
| 956 | + |
| 957 | + spark.sql(f""" |
| 958 | + CREATE VIEW {first_view_identifier} AS |
| 959 | + SELECT id, name |
| 960 | + FROM {table_identifier} |
| 961 | + """) |
| 962 | + |
| 963 | + spark.sql(f""" |
| 964 | + CREATE VIEW {second_view_identifier} AS |
| 965 | + SELECT id, name, dt |
| 966 | + FROM {table_identifier} |
| 967 | + """) |
| 968 | + |
| 969 | + views = set(session_catalog_hive.list_views(namespace)) |
| 970 | + |
| 971 | + assert (namespace, first_view_name) in views |
| 972 | + assert (namespace, second_view_name) in views |
| 973 | + |
| 974 | + # A table in the same namespace must not be returned as a view. |
| 975 | + assert (namespace, table_name) not in views |
0 commit comments