Skip to content

Commit

Permalink
feat: write_csv
Browse files Browse the repository at this point in the history
  • Loading branch information
Desdaemon committed Jan 17, 2024
1 parent ad49146 commit 8357b0b
Show file tree
Hide file tree
Showing 7 changed files with 234 additions and 0 deletions.
66 changes: 66 additions & 0 deletions packages/polars/lib/src/frb_generated.dart
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,16 @@ abstract class RustLibApi extends BaseApi {
int? offset,
dynamic hint});

Future<void> dataFrameWriteCsv(
{required DataFrame that,
required String path,
bool includeBom = false,
bool includeHeader = true,
bool append = false,
bool createNew = false,
String? nullValue,
dynamic hint});

LazyFrame lazyFrameCache({required LazyFrame that, dynamic hint});

Future<DataFrame> lazyFrameCollect(
Expand Down Expand Up @@ -1915,6 +1925,62 @@ class RustLibApiImpl extends RustLibApiImplPlatform implements RustLibApi {
argNames: ["that", "name", "offset"],
);

@override
Future<void> dataFrameWriteCsv(
{required DataFrame that,
required String path,
bool includeBom = false,
bool includeHeader = true,
bool append = false,
bool createNew = false,
String? nullValue,
dynamic hint}) {
return handler.executeNormal(NormalTask(
callFfi: (port_) {
var arg0 =
cst_encode_Auto_RefMut_RustOpaque_flutter_rust_bridgefor_generatedrust_asyncRwLockDataFrame(
that);
var arg1 = cst_encode_String(path);
var arg2 = cst_encode_bool(includeBom);
var arg3 = cst_encode_bool(includeHeader);
var arg4 = cst_encode_bool(append);
var arg5 = cst_encode_bool(createNew);
var arg6 = cst_encode_opt_String(nullValue);
return wire.wire_DataFrame_write_csv(
port_, arg0, arg1, arg2, arg3, arg4, arg5, arg6);
},
codec: DcoCodec(
decodeSuccessData: dco_decode_unit,
decodeErrorData: dco_decode_AnyhowException,
),
constMeta: kDataFrameWriteCsvConstMeta,
argValues: [
that,
path,
includeBom,
includeHeader,
append,
createNew,
nullValue
],
apiImpl: this,
hint: hint,
));
}

TaskConstMeta get kDataFrameWriteCsvConstMeta => const TaskConstMeta(
debugName: "DataFrame_write_csv",
argNames: [
"that",
"path",
"includeBom",
"includeHeader",
"append",
"createNew",
"nullValue"
],
);

@override
LazyFrame lazyFrameCache({required LazyFrame that, dynamic hint}) {
return handler.executeSync(SyncTask(
Expand Down
46 changes: 46 additions & 0 deletions packages/polars/lib/src/frb_generated.io.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2834,6 +2834,52 @@ class RustLibWire implements BaseWire {
ffi.Pointer<wire_cst_list_prim_u_8_strict>,
ffi.Pointer<ffi.Uint32>)>();

void wire_DataFrame_write_csv(
int port_,
int that,
ffi.Pointer<wire_cst_list_prim_u_8_strict> path,
bool include_bom,
bool include_header,
bool append,
bool create_new,
ffi.Pointer<wire_cst_list_prim_u_8_strict> null_value,
) {
return _wire_DataFrame_write_csv(
port_,
that,
path,
include_bom,
include_header,
append,
create_new,
null_value,
);
}

late final _wire_DataFrame_write_csvPtr = _lookup<
ffi.NativeFunction<
ffi.Void Function(
ffi.Int64,
ffi.UintPtr,
ffi.Pointer<wire_cst_list_prim_u_8_strict>,
ffi.Bool,
ffi.Bool,
ffi.Bool,
ffi.Bool,
ffi.Pointer<wire_cst_list_prim_u_8_strict>)>>(
'frbgen_polars_wire_DataFrame_write_csv');
late final _wire_DataFrame_write_csv =
_wire_DataFrame_write_csvPtr.asFunction<
void Function(
int,
int,
ffi.Pointer<wire_cst_list_prim_u_8_strict>,
bool,
bool,
bool,
bool,
ffi.Pointer<wire_cst_list_prim_u_8_strict>)>();

WireSyncRust2DartDco wire_LazyFrame_cache(
int that,
) {
Expand Down
18 changes: 18 additions & 0 deletions packages/polars/lib/src/wrapper/df.dart
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,24 @@ class DataFrame extends RustOpaque {
name: name,
offset: offset,
);

Future<void> writeCsv(
{required String path,
bool includeBom = false,
bool includeHeader = true,
bool append = false,
bool createNew = false,
String? nullValue,
dynamic hint}) =>
RustLib.instance.api.dataFrameWriteCsv(
that: this,
path: path,
includeBom: includeBom,
includeHeader: includeHeader,
append: append,
createNew: createNew,
nullValue: nullValue,
);
}

// Rust type: RustOpaqueNom<flutter_rust_bridge::for_generated::rust_async::RwLock<LazyFrame>>
Expand Down
10 changes: 10 additions & 0 deletions polars-wrapper/include/polars_wrapper.h
Original file line number Diff line number Diff line change
Expand Up @@ -648,6 +648,15 @@ WireSyncRust2DartDco frbgen_polars_wire_DataFrame_with_row_count(uintptr_t that,
struct wire_cst_list_prim_u_8_strict *name,
uint32_t *offset);

void frbgen_polars_wire_DataFrame_write_csv(int64_t port_,
uintptr_t that,
struct wire_cst_list_prim_u_8_strict *path,
bool include_bom,
bool include_header,
bool append,
bool create_new,
struct wire_cst_list_prim_u_8_strict *null_value);

WireSyncRust2DartDco frbgen_polars_wire_LazyFrame_cache(uintptr_t that);

void frbgen_polars_wire_LazyFrame_collect(int64_t port_, uintptr_t that, bool streaming);
Expand Down Expand Up @@ -1617,6 +1626,7 @@ static int64_t dummy_method_to_enforce_bundling(void) {
dummy_var ^= ((int64_t) (void*) frbgen_polars_wire_DataFrame_tail);
dummy_var ^= ((int64_t) (void*) frbgen_polars_wire_DataFrame_width);
dummy_var ^= ((int64_t) (void*) frbgen_polars_wire_DataFrame_with_row_count);
dummy_var ^= ((int64_t) (void*) frbgen_polars_wire_DataFrame_write_csv);
dummy_var ^= ((int64_t) (void*) frbgen_polars_wire_Expr_abs);
dummy_var ^= ((int64_t) (void*) frbgen_polars_wire_Expr_all);
dummy_var ^= ((int64_t) (void*) frbgen_polars_wire_Expr_any);
Expand Down
23 changes: 23 additions & 0 deletions polars-wrapper/src/bridge.io.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1246,6 +1246,29 @@ pub extern "C" fn frbgen_polars_wire_DataFrame_with_row_count(
wire_DataFrame_with_row_count_impl(that, name, offset)
}

#[no_mangle]
pub extern "C" fn frbgen_polars_wire_DataFrame_write_csv(
port_: i64,
that: usize,
path: *mut wire_cst_list_prim_u_8_strict,
include_bom: bool,
include_header: bool,
append: bool,
create_new: bool,
null_value: *mut wire_cst_list_prim_u_8_strict,
) {
wire_DataFrame_write_csv_impl(
port_,
that,
path,
include_bom,
include_header,
append,
create_new,
null_value,
)
}

#[no_mangle]
pub extern "C" fn frbgen_polars_wire_LazyFrame_cache(
that: usize,
Expand Down
43 changes: 43 additions & 0 deletions polars-wrapper/src/bridge.rs
Original file line number Diff line number Diff line change
Expand Up @@ -786,6 +786,49 @@ fn wire_DataFrame_with_row_count_impl(
},
)
}
fn wire_DataFrame_write_csv_impl(
port_: flutter_rust_bridge::for_generated::MessagePort,
that: impl CstDecode<
RustOpaqueNom<flutter_rust_bridge::for_generated::rust_async::RwLock<DataFrame>>,
>,
path: impl CstDecode<String>,
include_bom: impl CstDecode<bool>,
include_header: impl CstDecode<bool>,
append: impl CstDecode<bool>,
create_new: impl CstDecode<bool>,
null_value: impl CstDecode<Option<String>>,
) {
FLUTTER_RUST_BRIDGE_HANDLER.wrap_normal::<flutter_rust_bridge::for_generated::DcoCodec, _, _>(
flutter_rust_bridge::for_generated::TaskInfo {
debug_name: "DataFrame_write_csv",
port: Some(port_),
mode: flutter_rust_bridge::for_generated::FfiCallMode::Normal,
},
move || {
let api_that = that.cst_decode();
let api_path = path.cst_decode();
let api_include_bom = include_bom.cst_decode();
let api_include_header = include_header.cst_decode();
let api_append = append.cst_decode();
let api_create_new = create_new.cst_decode();
let api_null_value = null_value.cst_decode();
move |context| {
transform_result_dco((move || {
let mut api_that = api_that.rust_auto_opaque_decode_sync_ref_mut();
crate::wrapper::df::DataFrame::write_csv(
&mut api_that,
api_path,
api_include_bom,
api_include_header,
api_append,
api_create_new,
api_null_value,
)
})())
}
},
)
}
fn wire_LazyFrame_cache_impl(
that: impl CstDecode<
RustOpaqueNom<flutter_rust_bridge::for_generated::rust_async::RwLock<LazyFrame>>,
Expand Down
28 changes: 28 additions & 0 deletions polars-wrapper/src/wrapper/df.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ pub(crate) use super::prelude::UniqueKeepStrategy;
pub(crate) use super::{expr::PExpr, prelude::*};
use chrono::Duration;
pub(crate) use core::panic::AssertUnwindSafe;
use std::fs::File;

/// A contiguous growable collection of [Series] that have the same length.
///
Expand Down Expand Up @@ -102,6 +103,33 @@ impl LazyFrame {
}

impl DataFrame {
#[frb]
pub fn write_csv(
&mut self,
path: String,
#[frb(default = false)] include_bom: bool,
#[frb(default = true)] include_header: bool,
#[frb(default = false)] append: bool,
#[frb(default = false)] create_new: bool,
null_value: Option<String>,
) -> Result<()> {
let file = if append {
File::options().create_new(create_new).append(true)
} else {
File::options()
.write(true)
.truncate(true)
.create_new(create_new)
};
let mut writer = CsvWriter::new(file.open(path)?)
.include_bom(include_bom)
.include_header(include_header);
if let Some(null_value) = null_value {
writer = writer.with_null_value(null_value);
}
writer.finish(&mut self.0)?;
Ok(())
}
/// Returns a new, empty dataframe.
#[frb(sync)]
pub fn of_lits(series: Option<Vec<(String, Literals)>>) -> Result<DataFrame> {
Expand Down

0 comments on commit 8357b0b

Please sign in to comment.