@@ -276,23 +276,29 @@ impl PySessionContext {
276
276
fn create_dataframe (
277
277
& mut self ,
278
278
partitions : PyArrowType < Vec < Vec < RecordBatch > > > ,
279
+ name : Option < & str > ,
279
280
py : Python ,
280
281
) -> PyResult < PyDataFrame > {
281
282
let schema = partitions. 0 [ 0 ] [ 0 ] . schema ( ) ;
282
283
let table = MemTable :: try_new ( schema, partitions. 0 ) . map_err ( DataFusionError :: from) ?;
283
284
284
- // generate a random (unique) name for this table
285
+ // generate a random (unique) name for this table if none is provided
285
286
// table name cannot start with numeric digit
286
- let name = "c" . to_owned ( )
287
- + Uuid :: new_v4 ( )
288
- . simple ( )
289
- . encode_lower ( & mut Uuid :: encode_buffer ( ) ) ;
287
+ let table_name = match name {
288
+ Some ( val) => val. to_owned ( ) ,
289
+ None => {
290
+ "c" . to_owned ( )
291
+ + Uuid :: new_v4 ( )
292
+ . simple ( )
293
+ . encode_lower ( & mut Uuid :: encode_buffer ( ) )
294
+ }
295
+ } ;
290
296
291
297
self . ctx
292
- . register_table ( & * name , Arc :: new ( table) )
298
+ . register_table ( & * table_name , Arc :: new ( table) )
293
299
. map_err ( DataFusionError :: from) ?;
294
300
295
- let table = wait_for_future ( py, self . _table ( & name ) ) . map_err ( DataFusionError :: from) ?;
301
+ let table = wait_for_future ( py, self . _table ( & table_name ) ) . map_err ( DataFusionError :: from) ?;
296
302
297
303
let df = PyDataFrame :: new ( table) ;
298
304
Ok ( df)
@@ -305,37 +311,52 @@ impl PySessionContext {
305
311
306
312
/// Construct datafusion dataframe from Python list
307
313
#[ allow( clippy:: wrong_self_convention) ]
308
- fn from_pylist ( & mut self , data : PyObject , _py : Python ) -> PyResult < PyDataFrame > {
314
+ fn from_pylist (
315
+ & mut self ,
316
+ data : PyObject ,
317
+ name : Option < & str > ,
318
+ _py : Python ,
319
+ ) -> PyResult < PyDataFrame > {
309
320
Python :: with_gil ( |py| {
310
321
// Instantiate pyarrow Table object & convert to Arrow Table
311
322
let table_class = py. import ( "pyarrow" ) ?. getattr ( "Table" ) ?;
312
323
let args = PyTuple :: new ( py, & [ data] ) ;
313
324
let table = table_class. call_method1 ( "from_pylist" , args) ?. into ( ) ;
314
325
315
326
// Convert Arrow Table to datafusion DataFrame
316
- let df = self . from_arrow_table ( table, py) ?;
327
+ let df = self . from_arrow_table ( table, name , py) ?;
317
328
Ok ( df)
318
329
} )
319
330
}
320
331
321
332
/// Construct datafusion dataframe from Python dictionary
322
333
#[ allow( clippy:: wrong_self_convention) ]
323
- fn from_pydict ( & mut self , data : PyObject , _py : Python ) -> PyResult < PyDataFrame > {
334
+ fn from_pydict (
335
+ & mut self ,
336
+ data : PyObject ,
337
+ name : Option < & str > ,
338
+ _py : Python ,
339
+ ) -> PyResult < PyDataFrame > {
324
340
Python :: with_gil ( |py| {
325
341
// Instantiate pyarrow Table object & convert to Arrow Table
326
342
let table_class = py. import ( "pyarrow" ) ?. getattr ( "Table" ) ?;
327
343
let args = PyTuple :: new ( py, & [ data] ) ;
328
344
let table = table_class. call_method1 ( "from_pydict" , args) ?. into ( ) ;
329
345
330
346
// Convert Arrow Table to datafusion DataFrame
331
- let df = self . from_arrow_table ( table, py) ?;
347
+ let df = self . from_arrow_table ( table, name , py) ?;
332
348
Ok ( df)
333
349
} )
334
350
}
335
351
336
352
/// Construct datafusion dataframe from Arrow Table
337
353
#[ allow( clippy:: wrong_self_convention) ]
338
- fn from_arrow_table ( & mut self , data : PyObject , _py : Python ) -> PyResult < PyDataFrame > {
354
+ fn from_arrow_table (
355
+ & mut self ,
356
+ data : PyObject ,
357
+ name : Option < & str > ,
358
+ _py : Python ,
359
+ ) -> PyResult < PyDataFrame > {
339
360
Python :: with_gil ( |py| {
340
361
// Instantiate pyarrow Table object & convert to batches
341
362
let table = data. call_method0 ( py, "to_batches" ) ?;
@@ -345,34 +366,44 @@ impl PySessionContext {
345
366
// here we need to wrap the vector of record batches in an additional vector
346
367
let batches = table. extract :: < PyArrowType < Vec < RecordBatch > > > ( py) ?;
347
368
let list_of_batches = PyArrowType :: try_from ( vec ! [ batches. 0 ] ) ?;
348
- self . create_dataframe ( list_of_batches, py)
369
+ self . create_dataframe ( list_of_batches, name , py)
349
370
} )
350
371
}
E377
351
372
352
373
/// Construct datafusion dataframe from pandas
353
374
#[ allow( clippy:: wrong_self_convention) ]
354
- fn from_pandas ( & mut self , data : PyObject , _py : Python ) -> PyResult < PyDataFrame > {
375
+ fn from_pandas (
376
+ & mut self ,
377
+ data : PyObject ,
378
+ name : Option < & str > ,
379
+ _py : Python ,
380
+ ) -> PyResult < PyDataFrame > {
355
381
Python :: with_gil ( |py| {
356
382
// Instantiate pyarrow Table object & convert to Arrow Table
357
383
let table_class = py. import ( "pyarrow" ) ?. getattr ( "Table" ) ?;
358
384
let args = PyTuple :: new ( py, & [ data] ) ;
359
385
let table = table_class. call_method1 ( "from_pandas" , args) ?. into ( ) ;
360
386
361
387
// Convert Arrow Table to datafusion DataFrame
362
- let df = self . from_arrow_table ( table, py) ?;
388
+ let df = self . from_arrow_table ( table, name , py) ?;
363
389
Ok ( df)
364
390
} )
365
391
}
366
392
367
393
/// Construct datafusion dataframe from polars
368
394
#[ allow( clippy:: wrong_self_convention) ]
369
- fn from_polars ( & mut self , data : PyObject , _py : Python ) -> PyResult < PyDataFrame > {
395
+ fn from_polars (
396
+ & mut self ,
397
+ data : PyObject ,
398
+ name : Option < & str > ,
399
+ _py : Python ,
400
+ ) -> PyResult < PyDataFrame > {
370
401
Python :: with_gil ( |py| {
371
402
// Convert Polars dataframe to Arrow Table
372
403
let table = data. call_method0 ( py, "to_arrow" ) ?;
373
404
374
405
// Convert Arrow Table to datafusion DataFrame
375
- let df = self . from_arrow_table ( table, py) ?;
406
+ let df = self . from_arrow_table ( table, name , py) ?;
2851
376
407
Ok ( df)
377
408
} )
378
409
}
0 commit comments