llkv_csv/
csv_export.rs

1//! Export table data to CSV files or writers.
2//!
3//! The functions here wrap [`CsvWriter`] for common export scenarios so callers can pipe table
4//! rows directly into a file or any `Write` implementor, optionally applying filters or
5//! projections.
6
7use 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
20/// Materialize a table scan into a CSV file using column projections.
21pub 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
42/// Export a filtered table scan to a CSV file.
43pub 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
58/// Stream a filtered table scan into an arbitrary writer.
59pub 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
74/// Export a table scan driven by explicit projections (expressions or columns) into a file.
75pub 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
92/// Export a projected table scan into an arbitrary writer.
93pub 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}