|
| 1 | +from jsoniq import RumbleSession |
| 2 | +from unittest import TestCase |
| 3 | +import uuid |
| 4 | + |
| 5 | + |
| 6 | +class TestCatalogsUpdate(TestCase): |
| 7 | + """ |
| 8 | + Default Spark session catalog for Delta + custom Iceberg catalogs. |
| 9 | + - Delta uses spark_catalog. |
| 10 | + - Iceberg uses named catalogs (e.g., iceberg, ice_b, ice_one). |
| 11 | + """ |
| 12 | + @classmethod |
| 13 | + def setUpClass(cls): |
| 14 | + RumbleSession._rumbleSession = None |
| 15 | + RumbleSession._builder = RumbleSession.Builder() |
| 16 | + cls.rumble = ( |
| 17 | + RumbleSession.builder |
| 18 | + .withDelta() |
| 19 | + .withIceberg(["iceberg", "ice_b", "ice_one"]) |
| 20 | + .getOrCreate() |
| 21 | + ) |
| 22 | + |
| 23 | + def _create_insert_count(self, rumble, create_query, insert_query, count_query): |
| 24 | + rumble.jsoniq(create_query).applyPUL() |
| 25 | + rumble.jsoniq(insert_query).applyPUL() |
| 26 | + count_value = rumble.jsoniq(count_query).json() |
| 27 | + self.assertEqual(count_value, (2,)) |
| 28 | + |
| 29 | + def _assert_query_fails(self, rumble, query): |
| 30 | + with self.assertRaises(Exception): |
| 31 | + rumble.jsoniq(query).json() |
| 32 | + |
| 33 | + @staticmethod |
| 34 | + def _cleanup_warehouses(): |
| 35 | + import os |
| 36 | + import shutil |
| 37 | + |
| 38 | + for dirname in ("spark-warehouse", "iceberg-warehouse"): |
| 39 | + path = os.path.join(os.getcwd(), dirname) |
| 40 | + shutil.rmtree(path, ignore_errors=True) |
| 41 | + |
| 42 | + @classmethod |
| 43 | + def tearDownClass(cls): |
| 44 | + try: |
| 45 | + if cls.rumble is not None: |
| 46 | + cls.rumble._sparksession.stop() |
| 47 | + finally: |
| 48 | + RumbleSession._rumbleSession = None |
| 49 | + cls._cleanup_warehouses() |
| 50 | + |
| 51 | + def test_default_catalogs(self): |
| 52 | + """ |
| 53 | + Delta uses spark_catalog and Iceberg uses the named catalog "iceberg". |
| 54 | + Also verifies that cross-catalog reads are rejected. |
| 55 | + """ |
| 56 | + suffix = uuid.uuid4().hex |
| 57 | + delta_table = f"default.delta_default_{suffix}" |
| 58 | + iceberg_table = f"iceberg.default.iceberg_default_{suffix}" |
| 59 | + |
| 60 | + self._create_insert_count( |
| 61 | + self.rumble, |
| 62 | + f'create collection table("{delta_table}") with {{"k": 1}}', |
| 63 | + f'insert {{"k": 2}} last into collection table("{delta_table}")', |
| 64 | + f'count(table("{delta_table}"))' |
| 65 | + ) |
| 66 | + self._create_insert_count( |
| 67 | + self.rumble, |
| 68 | + f'create collection iceberg-table("{iceberg_table}") with {{"k": 1}}', |
| 69 | + f'insert {{"k": 2}} last into collection iceberg-table("{iceberg_table}")', |
| 70 | + f'count(iceberg-table("{iceberg_table}"))' |
| 71 | + ) |
| 72 | + self._assert_query_fails( |
| 73 | + self.rumble, |
| 74 | + f'iceberg-table("ice_b.{iceberg_table.split(".", 1)[1]}")' |
| 75 | + ) |
| 76 | + |
| 77 | + def test_single_custom_catalogs(self): |
| 78 | + """ |
| 79 | + Iceberg on a single custom catalog (ice_one). |
| 80 | + Ensures unqualified access does not resolve to this catalog. |
| 81 | + """ |
| 82 | + suffix = uuid.uuid4().hex |
| 83 | + iceberg_table = f"ice_one.default.ice_single_{suffix}" |
| 84 | + |
| 85 | + self._create_insert_count( |
| 86 | + self.rumble, |
| 87 | + f'create collection iceberg-table("{iceberg_table}") with {{"k": 1}}', |
| 88 | + f'insert {{"k": 2}} last into collection iceberg-table("{iceberg_table}")', |
| 89 | + f'count(iceberg-table("{iceberg_table}"))' |
| 90 | + ) |
| 91 | + self._assert_query_fails( |
| 92 | + self.rumble, |
| 93 | + f'iceberg-table("{iceberg_table.split(".", 1)[1]}")' |
| 94 | + ) |
| 95 | + |
| 96 | + def test_multiple_catalogs(self): |
| 97 | + """ |
| 98 | + Iceberg on multiple catalogs (iceberg + ice_b). |
| 99 | + Verifies isolation by asserting cross-catalog reads fail. |
| 100 | + """ |
| 101 | + suffix = uuid.uuid4().hex |
| 102 | + iceberg_default_table = f"iceberg.default.iceberg_multi_default_{suffix}" |
| 103 | + iceberg_custom_table = f"ice_b.default.iceberg_multi_{suffix}" |
| 104 | + |
| 105 | + self._create_insert_count( |
| 106 | + self.rumble, |
| 107 | + f'create collection iceberg-table("{iceberg_default_table}") with {{"k": 1}}', |
| 108 | + f'insert {{"k": 2}} last into collection iceberg-table("{iceberg_default_table}")', |
| 109 | + f'count(iceberg-table("{iceberg_default_table}"))' |
| 110 | + ) |
| 111 | + self._create_insert_count( |
| 112 | + self.rumble, |
| 113 | + f'create collection iceberg-table("{iceberg_custom_table}") with {{"k": 1}}', |
| 114 | + f'insert {{"k": 2}} last into collection iceberg-table("{iceberg_custom_table}")', |
| 115 | + f'count(iceberg-table("{iceberg_custom_table}"))' |
| 116 | + ) |
| 117 | + self._assert_query_fails( |
| 118 | + self.rumble, |
| 119 | + f'iceberg-table("ice_b.{iceberg_default_table.split(".", 1)[1]}")' |
| 120 | + ) |
| 121 | + self._assert_query_fails( |
| 122 | + self.rumble, |
| 123 | + f'iceberg-table("{iceberg_custom_table.split(".", 1)[1]}")' |
| 124 | + ) |
| 125 | + |
| 126 | + def test_resolution_order(self): |
| 127 | + """ |
| 128 | + Matches Iceberg's catalog/namespace resolution order for spark.table(). |
| 129 | + Ensures unqualified access fails when spark_catalog is not Iceberg. |
| 130 | + """ |
| 131 | + suffix = uuid.uuid4().hex |
| 132 | + table_name = f"iceberg.default.iceberg_res_{suffix}" |
| 133 | + short_name = f"iceberg_res_{suffix}" |
| 134 | + multi_ns_table = f"iceberg.ns1.ns2.iceberg_res_{suffix}_ns" |
| 135 | + |
| 136 | + self._create_insert_count( |
| 137 | + self.rumble, |
| 138 | + f'create collection iceberg-table("{table_name}") with {{"k": 1}}', |
| 139 | + f'insert {{"k": 2}} last into collection iceberg-table("{table_name}")', |
| 140 | + f'count(iceberg-table("{table_name}"))' |
| 141 | + ) |
| 142 | + |
| 143 | + # catalog.table -> catalog.currentNamespace.table |
| 144 | + self._create_insert_count( |
| 145 | + self.rumble, |
| 146 | + f'create collection iceberg-table("iceberg.{short_name}_2") with {{"k": 1}}', |
| 147 | + f'insert {{"k": 2}} last into collection iceberg-table("iceberg.{short_name}_2")', |
| 148 | + f'count(iceberg-table("iceberg.{short_name}_2"))' |
| 149 | + ) |
| 150 | + |
| 151 | + # catalog.namespace1.namespace2.table -> catalog.namespace1.namespace2.table |
| 152 | + self._create_insert_count( |
| 153 | + self.rumble, |
| 154 | + f'create collection iceberg-table("{multi_ns_table}") with {{"k": 1}}', |
| 155 | + f'insert {{"k": 2}} last into collection iceberg-table("{multi_ns_table}")', |
| 156 | + f'count(iceberg-table("{multi_ns_table}"))' |
| 157 | + ) |
| 158 | + |
| 159 | + # namespace.table (current catalog) should fail because spark_catalog is not Iceberg here. |
| 160 | + self._assert_query_fails( |
| 161 | + self.rumble, |
| 162 | + f'iceberg-table("default.{short_name}")' |
| 163 | + ) |
| 164 | + # table (current catalog + namespace) should also fail for the same reason. |
| 165 | + self._assert_query_fails( |
| 166 | + self.rumble, |
| 167 | + f'iceberg-table("{short_name}")' |
| 168 | + ) |
0 commit comments