Skip to content

Commit 21b1e50

Browse files
committed
can override table location
1 parent 3c6e06a commit 21b1e50

File tree

3 files changed

+45
-12
lines changed

3 files changed

+45
-12
lines changed

pyiceberg/catalog/memory.py

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
NoSuchTableError,
2121
TableAlreadyExistsError,
2222
)
23+
from pyiceberg.io import WAREHOUSE
2324
from pyiceberg.partitioning import UNPARTITIONED_PARTITION_SPEC, PartitionSpec
2425
from pyiceberg.schema import Schema
2526
from pyiceberg.table import (
@@ -41,12 +42,11 @@ class InMemoryCatalog(Catalog):
4142
__tables: Dict[Identifier, Table]
4243
__namespaces: Dict[Identifier, Properties]
4344

44-
def __init__(self, name: str, warehouse_location: Optional[str] = None, **properties: str) -> None:
45+
def __init__(self, name: str, **properties: str) -> None:
4546
super().__init__(name, **properties)
46-
47-
self._warehouse_location = warehouse_location or DEFAULT_WAREHOUSE_LOCATION
4847
self.__tables = {}
4948
self.__namespaces = {}
49+
self._warehouse_location = properties.get(WAREHOUSE, None) or DEFAULT_WAREHOUSE_LOCATION
5050

5151
def create_table(
5252
self,
@@ -67,10 +67,9 @@ def create_table(
6767
if namespace not in self.__namespaces:
6868
self.__namespaces[namespace] = {}
6969

70-
# if not location:
71-
location = f'{self._warehouse_location}/{"/".join(identifier)}'
70+
if not location:
71+
location = f'{self._warehouse_location}/{"/".join(identifier)}'
7272

73-
# _get_default_warehouse_location
7473
metadata_location = f'{self._warehouse_location}/{"/".join(identifier)}/metadata/metadata.json'
7574

7675
metadata = new_table_metadata(

tests/catalog/test_base.py

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
NoSuchTableError,
3636
TableAlreadyExistsError,
3737
)
38+
from pyiceberg.io import WAREHOUSE
3839
from pyiceberg.partitioning import PartitionField, PartitionSpec
3940
from pyiceberg.schema import Schema
4041
from pyiceberg.table import (
@@ -51,9 +52,7 @@
5152

5253
@pytest.fixture
5354
def catalog(tmp_path: PosixPath) -> InMemoryCatalog:
54-
return InMemoryCatalog(
55-
"test.in.memory.catalog", warehouse_location=tmp_path.absolute().as_posix(), **{"test.key": "test.value"}
56-
)
55+
return InMemoryCatalog("test.in.memory.catalog", **{WAREHOUSE: tmp_path.absolute().as_posix(), "test.key": "test.value"})
5756

5857

5958
TEST_TABLE_IDENTIFIER = ("com", "organization", "department", "my_table")
@@ -78,7 +77,6 @@ def given_catalog_has_a_table(catalog: InMemoryCatalog) -> Table:
7877
return catalog.create_table(
7978
identifier=TEST_TABLE_IDENTIFIER,
8079
schema=TEST_TABLE_SCHEMA,
81-
location=TEST_TABLE_LOCATION,
8280
partition_spec=TEST_TABLE_PARTITION_SPEC,
8381
properties=TEST_TABLE_PROPERTIES,
8482
)
@@ -121,6 +119,16 @@ def test_name_from_str() -> None:
121119

122120

123121
def test_create_table(catalog: InMemoryCatalog) -> None:
122+
table = catalog.create_table(
123+
identifier=TEST_TABLE_IDENTIFIER,
124+
schema=TEST_TABLE_SCHEMA,
125+
partition_spec=TEST_TABLE_PARTITION_SPEC,
126+
properties=TEST_TABLE_PROPERTIES,
127+
)
128+
assert catalog.load_table(TEST_TABLE_IDENTIFIER) == table
129+
130+
131+
def test_create_table_override(catalog: InMemoryCatalog) -> None:
124132
table = catalog.create_table(
125133
identifier=TEST_TABLE_IDENTIFIER,
126134
schema=TEST_TABLE_SCHEMA,

tests/cli/test_console.py

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -264,7 +264,6 @@ def test_location(catalog: InMemoryCatalog) -> None:
264264
catalog.create_table(
265265
identifier=TEST_TABLE_IDENTIFIER,
266266
schema=TEST_TABLE_SCHEMA,
267-
location=TEST_TABLE_LOCATION,
268267
partition_spec=TEST_TABLE_PARTITION_SPEC,
269268
)
270269

@@ -274,6 +273,20 @@ def test_location(catalog: InMemoryCatalog) -> None:
274273
assert result.output == f"""{DEFAULT_WAREHOUSE_LOCATION}/default/my_table\n"""
275274

276275

276+
def test_location_override(catalog: InMemoryCatalog) -> None:
277+
catalog.create_table(
278+
identifier=TEST_TABLE_IDENTIFIER,
279+
schema=TEST_TABLE_SCHEMA,
280+
location=TEST_TABLE_LOCATION,
281+
partition_spec=TEST_TABLE_PARTITION_SPEC,
282+
)
283+
284+
runner = CliRunner()
285+
result = runner.invoke(run, ["location", "default.my_table"])
286+
assert result.exit_code == 0
287+
assert result.output == f"""{TEST_TABLE_LOCATION}\n"""
288+
289+
277290
def test_location_does_not_exists(catalog: InMemoryCatalog) -> None:
278291
# pylint: disable=unused-argument
279292

@@ -674,7 +687,6 @@ def test_json_location(catalog: InMemoryCatalog) -> None:
674687
catalog.create_table(
675688
identifier=TEST_TABLE_IDENTIFIER,
676689
schema=TEST_TABLE_SCHEMA,
677-
location=TEST_TABLE_LOCATION,
678690
partition_spec=TEST_TABLE_PARTITION_SPEC,
679691
)
680692

@@ -684,6 +696,20 @@ def test_json_location(catalog: InMemoryCatalog) -> None:
684696
assert result.output == f'"{DEFAULT_WAREHOUSE_LOCATION}/default/my_table"\n'
685697

686698

699+
def test_json_location_override(catalog: InMemoryCatalog) -> None:
700+
catalog.create_table(
701+
identifier=TEST_TABLE_IDENTIFIER,
702+
schema=TEST_TABLE_SCHEMA,
703+
location=TEST_TABLE_LOCATION,
704+
partition_spec=TEST_TABLE_PARTITION_SPEC,
705+
)
706+
707+
runner = CliRunner()
708+
result = runner.invoke(run, ["--output=json", "location", "default.my_table"])
709+
assert result.exit_code == 0
710+
assert result.output == f'"{TEST_TABLE_LOCATION}"\n'
711+
712+
687713
def test_json_location_does_not_exists(catalog: InMemoryCatalog) -> None:
688714
# pylint: disable=unused-argument
689715

0 commit comments

Comments
 (0)