1use std::io::Write;
8use std::path::Path;
9
10use llkv_result::Result as LlkvResult;
11use llkv_storage::pager::Pager;
12use simd_r_drive_entry_handle::EntryHandle;
13
14use llkv_table::expr::Expr;
15use llkv_table::table::ScanProjection;
16use llkv_table::{Table, types::FieldId};
17
18pub use crate::writer::{CsvExportColumn, CsvWriteOptions, CsvWriter};
19
20pub fn export_csv_from_table<P, C>(
22 table: &Table<P>,
23 csv_path: C,
24 columns: &[CsvExportColumn],
25 options: &CsvWriteOptions,
26) -> LlkvResult<()>
27where
28 P: Pager<Blob = EntryHandle> + Send + Sync,
29 C: AsRef<Path>,
30{
31 tracing::trace!(
32 "[CSV_EXPORT] export_csv_from_table called with {} columns",
33 columns.len()
34 );
35 let writer = CsvWriter::with_options(table, options.clone());
36 tracing::trace!("[CSV_EXPORT] About to call write_columns_to_path");
37 let result = writer.write_columns_to_path(csv_path, columns);
38 tracing::trace!("[CSV_EXPORT] write_columns_to_path returned: {:?}", result);
39 result
40}
41
42pub fn export_csv_from_table_with_filter<P, C>(
44 table: &Table<P>,
45 csv_path: C,
46 columns: &[CsvExportColumn],
47 filter_expr: &Expr<'_, FieldId>,
48 options: &CsvWriteOptions,
49) -> LlkvResult<()>
50where
51 P: Pager<Blob = EntryHandle> + Send + Sync,
52 C: AsRef<Path>,
53{
54 let writer = CsvWriter::with_options(table, options.clone());
55 writer.write_columns_to_path_with_filter(csv_path, columns, filter_expr)
56}
57
58pub fn export_csv_to_writer_with_filter<P, W>(
60 table: &Table<P>,
61 writer: W,
62 columns: &[CsvExportColumn],
63 filter_expr: &Expr<'_, FieldId>,
64 options: &CsvWriteOptions,
65) -> LlkvResult<()>
66where
67 P: Pager<Blob = EntryHandle> + Send + Sync,
68 W: Write,
69{
70 let writer_state = CsvWriter::with_options(table, options.clone());
71 writer_state.write_columns_to_writer(writer, columns, filter_expr)
72}
73
74pub fn export_csv_from_table_with_projections<P, C, I, SP>(
76 table: &Table<P>,
77 csv_path: C,
78 projections: I,
79 filter_expr: &Expr<'_, FieldId>,
80 options: &CsvWriteOptions,
81) -> LlkvResult<()>
82where
83 P: Pager<Blob = EntryHandle> + Send + Sync,
84 C: AsRef<Path>,
85 I: IntoIterator<Item = SP>,
86 SP: Into<ScanProjection>,
87{
88 let writer = CsvWriter::with_options(table, options.clone());
89 writer.write_projections_to_path(csv_path, projections, filter_expr)
90}
91
92pub fn export_csv_to_writer_with_projections<P, W, I, SP>(
94 table: &Table<P>,
95 writer: W,
96 projections: I,
97 filter_expr: &Expr<'_, FieldId>,
98 options: &CsvWriteOptions,
99) -> LlkvResult<()>
100where
101 P: Pager<Blob = EntryHandle> + Send + Sync,
102 W: Write,
103 I: IntoIterator<Item = SP>,
104 SP: Into<ScanProjection>,
105{
106 let writer_state = CsvWriter::with_options(table, options.clone());
107 writer_state.write_projections_to_writer(writer, projections, filter_expr)
108}