Skip to content

Commit 8b0b2a6

Browse files
committed
- refactor: streamlines mode dispatch logic in save operations by replacing the function dispatch dict with direct conditional handling, improving clarity and maintainability
- docs: expands and clarifies docstring to explain available save modes and their effects, referencing Spark semantics for familiarity Signed-off-by: gitgud5000 <[email protected]>
1 parent 523c7aa commit 8b0b2a6

File tree

1 file changed

+19
-23
lines changed

1 file changed

+19
-23
lines changed

kedro-datasets/kedro_datasets/ibis/table_dataset.py

Lines changed: 19 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -105,18 +105,25 @@ def __init__( # noqa: PLR0913
105105
(e.g. `("catalog", "database")`) or a dotted string path
106106
(e.g. `"catalog.database"`) to reference a table or view
107107
in a multi-level table hierarchy.
108-
credentials: (Preferred) Connection information for the Ibis backend. Can be a connection string or a dict of parameters. Supersedes `connection`.
109-
connection: (Deprecated) Configuration for connecting to an Ibis backend. Use `credentials` instead.
108+
credentials: (Preferred) Connection information for the Ibis
109+
backend. Can be a connection string or a dict of parameters.
110+
Supersedes `connection`.
111+
connection: (Deprecated) Configuration for connecting to an
112+
Ibis backend. Use `credentials` instead.
110113
load_args: Additional arguments passed to the Ibis backend's
111114
`read_{file_format}` method.
112115
save_args: Additional arguments passed to the Ibis backend's
113116
`create_{materialized}` method. By default, ``ir.Table``
114117
objects are materialized as views. To save a table using
115118
a different materialization strategy, supply a value for
116-
`materialized` in `save_args`. You can also include a
117-
`mode` parameter ("append", "overwrite", "error", "ignore")
118-
to control write behavior.
119-
metadata: Any arbitrary metadata. This is ignored by Kedro,
119+
`materialized` in `save_args`. The `mode` parameter controls
120+
the behavior when saving data:
121+
- _"overwrite"_: Overwrite existing data in the table.
122+
- _"append"_: Append contents of the new data to the existing table (does not overwrite).
123+
- _"error"_ or _"errorifexists"_: Throw an exception if the table already exists.
124+
- _"ignore"_: Silently ignore the operation if the table already exists.
125+
These options are similar to those in Spark's DataFrameWriter (see: https://spark.apache.org/docs/latest/api/python/reference/pyspark.sql/api/pyspark.sql.DataFrameWriter.mode.html).
126+
metadata: Any arbitrary metadata. This is ignored by Kedro,
120127
but may be consumed by users or external plugins.
121128
"""
122129

@@ -139,13 +146,13 @@ def __init__( # noqa: PLR0913
139146

140147
# Set load and save arguments, overwriting defaults if provided.
141148
self._load_args = deepcopy(self.DEFAULT_LOAD_ARGS)
142-
if load_args:
149+
if load_args is not None:
143150
self._load_args.update(load_args)
144151
if database is not None:
145152
self._load_args["database"] = database
146153

147154
self._save_args = deepcopy(self.DEFAULT_SAVE_ARGS)
148-
if save_args:
155+
if save_args is not None:
149156
self._save_args.update(save_args)
150157
if database is not None:
151158
self._save_args["database"] = database
@@ -196,26 +203,15 @@ def save(self, data: ir.Table) -> None:
196203

197204
writer = getattr(self.connection, f"create_{self._materialized}")
198205

199-
def overwrite():
206+
if self._mode == "overwrite":
200207
writer(self._table_name, data, overwrite=True, **self._save_args)
201-
202-
def error():
208+
elif self._mode == "error":
203209
writer(self._table_name, data, overwrite=False, **self._save_args)
204-
205-
def ignore():
210+
elif self._mode == "ignore":
206211
if self._exists():
207212
return
208213
writer(self._table_name, data, overwrite=False, **self._save_args)
209-
210-
mode_dispatch = {
211-
"overwrite": overwrite,
212-
"error": error,
213-
"ignore": ignore,
214-
}
215-
216-
try:
217-
mode_dispatch[self._mode]()
218-
except KeyError:
214+
else:
219215
raise ValueError(f"Unknown mode: {self._mode!r}")
220216

221217
def _get_backend_name(self) -> str | None:

0 commit comments

Comments
 (0)