polars.PolarsDatabaseDataset
kedro_datasets_experimental.polars.PolarsDatabaseDataset ¶
PolarsDatabaseDataset(
*,
sql=None,
credentials=None,
load_args=None,
fs_args=None,
filepath=None,
table_name=None,
save_args=None,
metadata=None
)
Bases: AbstractDataset[None, DataFrame]
PolarsDatabaseDataset loads data from a provided SQL query or write data to a table.
It supports all allowed polars options on read_database and write_database.
Since Polars uses SQLAlchemy behind the scenes, when instantiating PolarsDatabaseDataset one needs to pass
a compatible connection string either in credentials (see the example
code snippet below) or in load_args. Connection string formats supported
by SQLAlchemy can be found here:
https://docs.sqlalchemy.org/core/engines.html#database-urls
Provide at least one of sql, filepath, or table_name (sql
and filepath are mutually exclusive). load uses sql or
filepath when given, otherwise SELECT * FROM <table_name>.
save always writes to table_name.
Schema-qualified tables can be passed directly to table_name using the
schema_name.table_name form; there is no separate schema argument.
Example usage for the YAML API:¶
Load-and-save against a single table:
shuttles_table:
type: polars.PolarsDatabaseDataset
table_name: shuttles
credentials: db_credentials
Load via a custom (here schema-qualified) SQL query:
shuttle_id_dataset:
type: polars.PolarsDatabaseDataset
sql: "SELECT shuttle, shuttle_id FROM spaceflights.shuttles;"
credentials: db_credentials
Pass extra arguments to the underlying polars methods via load_args and
save_args:
shuttles_configured:
type: polars.PolarsDatabaseDataset
table_name: shuttles
credentials: db_credentials
load_args:
batch_size: 10000
schema_overrides:
shuttle_id: Int64
save_args:
if_table_exists: append
Sample database credentials entry in credentials.yml:
db_credentials:
con: postgresql://scott:tiger@localhost/test # pragma: allowlist secret
pool_size: 10 # additional parameters
Example usage for the Python API:¶
>>> from pathlib import Path
>>> import polars as pl
>>> import sqlite3
>>>
>>> from kedro_datasets_experimental.polars import PolarsDatabaseDataset
>>>
>>> data = pl.DataFrame({"col1": [1, 2], "col2": [4, 5], "col3": [5, 6]})
>>> sql = "SELECT * FROM table_a"
>>> tmp_path = Path.cwd() / "tmp"
>>> tmp_path.mkdir(parents=True, exist_ok=True)
>>> credentials = {"con": f"sqlite:///{tmp_path / 'test.db'}"}
>>> dataset = PolarsDatabaseDataset(sql=sql, credentials=credentials, table_name="table_a")
>>>
>>> dataset.save(data)
>>> reloaded = dataset.load()
>>>
>>> assert data.equals(reloaded)
Parameters:
-
sql(str | None, default:None) –SQL query to execute on load. Mutually exclusive with
filepath. If neither is given,table_namemust be provided and the dataset will load viaSELECT * FROM <table_name>. -
credentials(dict[str, Any] | None, default:None) –A dictionary with a
SQLAlchemyconnection string. Users are supposed to provide the connection stringconthrough credentials. To find all supported connection string formats, see here: https://docs.sqlalchemy.org/core/engines.html#database-urls Additional parameters for the sqlalchemy engine can be provided alongside theconparameter. -
load_args(dict[str, Any] | None, default:None) –Provided to the underlying
polars.read_databasefunction along with the connection. To find all supported arguments, see here: https://docs.pola.rs/api/python/stable/reference/api/polars.read_database.html -
fs_args(dict[str, Any] | None, default:None) –Extra arguments passed to
fsspecwhenfilepathpoints to a SQL file (e.g. credentials for remote storage). -
filepath(str | None, default:None) –Path to a
.sqlfile containing the query to execute on load. Mutually exclusive withsql. -
table_name(str | None, default:None) –Target table for
save. Whensqlandfilepathare not provided, also used as the load source viaSELECT * FROM <table_name>. Schema-qualified tables can be passed asschema_name.table_name. -
save_args(dict[str, Any] | None, default:None) –Provided to the underlying
polars.DataFrame.write_databasemethod along with the connection string. To find all supported arguments, see here: https://docs.pola.rs/api/python/stable/reference/api/polars.DataFrame.write_database.html Defaults to{"if_table_exists": "replace"}— writes overwrite the target table unless overridden (e.g.if_table_exists: append). -
metadata(dict[str, Any] | None, default:None) –Any arbitrary metadata. This is ignored by Kedro, but may be consumed by users or external plugins.
Raises:
-
DatasetError–When both
sqlandfilepathare provided, when none ofsql/filepath/table_nameis provided, or whenconis missing fromcredentials.
Source code in kedro_datasets_experimental/polars/polars_database_dataset.py
175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 | |
_connection_args
instance-attribute
¶
_connection_args = {
k: (credentials[k])
for k in (credentials.keys())
if k != "con"
}
_load_args
instance-attribute
¶
_load_args = (
{None: default_load_args, None: load_args}
if load_args is not None
else default_load_args
)
_save_args
instance-attribute
¶
_save_args = (
{None: default_save_args, None: save_args}
if save_args is not None
else default_save_args
)
_describe ¶
_describe()
Source code in kedro_datasets_experimental/polars/polars_database_dataset.py
312 313 314 315 316 317 318 319 320 | |
create_connection
classmethod
¶
create_connection(connection_str, connection_args=None)
Given a connection string, create a singleton Engine
to be used across all instances of PolarsDatabaseDataset that
need to connect to the same source.
Source code in kedro_datasets_experimental/polars/polars_database_dataset.py
284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 | |
load ¶
load()
Source code in kedro_datasets_experimental/polars/polars_database_dataset.py
322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 | |
save ¶
save(data)
Source code in kedro_datasets_experimental/polars/polars_database_dataset.py
340 341 342 343 344 345 346 347 348 349 350 | |