Add mode to overwrite table if already exists

This commit is contained in:
Chang She
2023-04-19 16:19:18 -07:00
parent ec197b1855
commit d7c5793803
4 changed files with 49 additions and 5 deletions

View File

@@ -55,7 +55,11 @@ class LanceDBConnection:
return self.open_table(name)
def create_table(
self, name: str, data: DATA = None, schema: pa.Schema = None
self,
name: str,
data: DATA = None,
schema: pa.Schema = None,
mode: str = "create",
) -> LanceTable:
"""Create a table in the database.
@@ -67,6 +71,10 @@ class LanceDBConnection:
The data to insert into the table.
schema: pyarrow.Schema; optional
The schema of the table.
mode: str; default "create"
The mode to use when creating the table.
By default, if the table already exists, an exception is raised.
If you want to overwrite the table, use mode="overwrite".
Note
----
@@ -78,7 +86,7 @@ class LanceDBConnection:
A LanceTable object representing the table.
"""
if data is not None:
tbl = LanceTable.create(self, name, data, schema)
tbl = LanceTable.create(self, name, data, schema, mode=mode)
else:
tbl = LanceTable(self, name)
return tbl

View File

@@ -156,10 +156,10 @@ class LanceTable:
return LanceQueryBuilder(self, query)
@classmethod
def create(cls, db, name, data, schema=None):
def create(cls, db, name, data, schema=None, mode="create"):
tbl = LanceTable(db, name)
data = _sanitize_data(data, schema)
lance.write_dataset(data, tbl._dataset_uri, mode="create")
lance.write_dataset(data, tbl._dataset_uri, mode=mode)
return tbl