Skip to content

Commit a882392

Browse files
committed
can override table location
1 parent 8fa8d1b commit a882392

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
@@ -33,6 +33,7 @@
3333
NoSuchTableError,
3434
TableAlreadyExistsError,
3535
)
36+
from pyiceberg.io import WAREHOUSE
3637
from pyiceberg.partitioning import PartitionField, PartitionSpec
3738
from pyiceberg.schema import Schema
3839
from pyiceberg.table import (
@@ -49,9 +50,7 @@
4950

5051
@pytest.fixture
5152
def catalog(tmp_path: PosixPath) -> InMemoryCatalog:
52-
return InMemoryCatalog(
53-
"test.in.memory.catalog", warehouse_location=tmp_path.absolute().as_posix(), **{"test.key": "test.value"}
54-
)
53+
return InMemoryCatalog("test.in.memory.catalog", **{WAREHOUSE: tmp_path.absolute().as_posix(), "test.key": "test.value"})
5554

5655

5756
TEST_TABLE_IDENTIFIER = ("com", "organization", "department", "my_table")
@@ -76,7 +75,6 @@ def given_catalog_has_a_table(catalog: InMemoryCatalog) -> Table:
7675
return catalog.create_table(
7776
identifier=TEST_TABLE_IDENTIFIER,
7877
schema=TEST_TABLE_SCHEMA,
79-
location=TEST_TABLE_LOCATION,
8078
partition_spec=TEST_TABLE_PARTITION_SPEC,
8179
properties=TEST_TABLE_PROPERTIES,
8280
)
@@ -119,6 +117,16 @@ def test_name_from_str() -> None:
119117

120118

121119
def test_create_table(catalog: InMemoryCatalog) -> None:
120+
table = catalog.create_table(
121+
identifier=TEST_TABLE_IDENTIFIER,
122+
schema=TEST_TABLE_SCHEMA,
123+
partition_spec=TEST_TABLE_PARTITION_SPEC,
124+
properties=TEST_TABLE_PROPERTIES,
125+
)
126+
assert catalog.load_table(TEST_TABLE_IDENTIFIER) == table
127+
128+
129+
def test_create_table_override(catalog: InMemoryCatalog) -> None:
122130
table = catalog.create_table(
123131
identifier=TEST_TABLE_IDENTIFIER,
124132
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)