datu 0.3.4

datu - a data file utility
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
//! Parse flt [`Expr`] trees into [`PipelineStage`] lists and validate order.

use flt::ast::BinaryOp;
use flt::ast::Expr;
use flt::ast::Literal;

use super::stage::ReplPipelineStage;
use super::stage::repl_pipeline_last_select_is_terminal;
use crate::Error;
use crate::pipeline::ColumnSpec;
use crate::pipeline::SelectItem;
use crate::pipeline::SelectSpec;

/// Collects pipeline stages from a pipe expression (e.g. a |> b |> c -> [a, b, c]).
pub(super) fn collect_pipe_stages(expr: Expr, out: &mut Vec<Expr>) {
    match expr {
        Expr::BinaryExpr(l, BinaryOp::Pipe, r) => {
            collect_pipe_stages(*l, out);
            collect_pipe_stages(*r, out);
        }
        other => out.push(other),
    }
}

fn expr_is_group_by_call(expr: &Expr) -> bool {
    matches!(
        expr,
        Expr::FunctionCall(name, _) if name.to_string().as_str() == "group_by"
    )
}

fn expr_is_select_call(expr: &Expr) -> bool {
    matches!(
        expr,
        Expr::FunctionCall(name, _) if name.to_string().as_str() == "select"
    )
}

/// True when the accumulated pipe stages form a complete REPL statement (flush and execute).
pub(super) fn is_statement_complete(pending_exprs: &[Expr]) -> bool {
    let Some(last) = pending_exprs.last() else {
        return false;
    };
    match last {
        Expr::FunctionCall(name, args) => match name.to_string().as_str() {
            "count" | "head" | "tail" | "sample" | "schema" | "write" => true,
            "group_by" => pending_exprs[..pending_exprs.len().saturating_sub(1)]
                .iter()
                .any(expr_is_select_call),
            "select" => {
                if select_args_are_all_aggregates(args) {
                    return true;
                }
                pending_exprs[..pending_exprs.len().saturating_sub(1)]
                    .iter()
                    .any(expr_is_group_by_call)
            }
            _ => false,
        },
        _ => false,
    }
}

fn select_args_are_all_aggregates(args: &[Expr]) -> bool {
    !args.is_empty()
        && args.iter().all(|e| {
            matches!(
                e,
                Expr::FunctionCall(n, a)
                    if matches!(n.to_string().as_str(), "sum" | "avg" | "min" | "max")
                        && a.len() == 1
            )
        })
}

/// Extracts a single path string from a function's argument list.
pub(super) fn extract_path_from_args(func_name: &str, args: &[Expr]) -> crate::Result<String> {
    match args {
        [Expr::Literal(Literal::String(s))] => Ok(s.clone()),
        _ => Err(Error::UnsupportedFunctionCall(format!(
            "{func_name} expects a single string argument, got {args:?}"
        ))),
    }
}

fn extract_one_column_spec(expr: &Expr) -> crate::Result<ColumnSpec> {
    match expr {
        Expr::Literal(Literal::Symbol(s)) => Ok(ColumnSpec::CaseInsensitive(s.clone())),
        Expr::Literal(Literal::String(s)) => Ok(ColumnSpec::Exact(s.clone())),
        Expr::Ident(s) => Ok(ColumnSpec::CaseInsensitive(s.clone())),
        _ => Err(Error::UnsupportedFunctionCall(format!(
            "expected a column (symbol, string, or identifier), got {expr:?}"
        ))),
    }
}

fn select_aggregate_item(name: &str, col: ColumnSpec) -> SelectItem {
    match name {
        "sum" => SelectItem::Sum(col),
        "avg" => SelectItem::Avg(col),
        "min" => SelectItem::Min(col),
        "max" => SelectItem::Max(col),
        _ => unreachable!("select_aggregate_item only called for sum, avg, min, max"),
    }
}

/// Extracts select items: column refs or `sum(column)` / `avg(column)` / `min(column)` / `max(column)`.
pub(super) fn extract_select_items(args: &[Expr]) -> crate::Result<Vec<SelectItem>> {
    const SELECT_AGG_EXPECTED: &str =
        "select expects column names, sum(column), avg(column), min(column), or max(column)";
    args.iter()
        .map(|expr| match expr {
            Expr::FunctionCall(name, inner) => {
                let name_str = name.to_string();
                match name_str.as_str() {
                    "sum" | "avg" | "min" | "max" => match inner.as_slice() {
                        [one] => Ok(select_aggregate_item(
                            name_str.as_str(),
                            extract_one_column_spec(one)?,
                        )),
                        _ => Err(Error::UnsupportedFunctionCall(format!(
                            "{name_str}() expects exactly one column argument"
                        ))),
                    },
                    _ => Err(Error::UnsupportedFunctionCall(format!(
                        "{SELECT_AGG_EXPECTED}, got {expr:?}"
                    ))),
                }
            }
            Expr::Literal(Literal::Symbol(s)) => {
                Ok(SelectItem::Column(ColumnSpec::CaseInsensitive(s.clone())))
            }
            Expr::Literal(Literal::String(s)) => {
                Ok(SelectItem::Column(ColumnSpec::Exact(s.clone())))
            }
            Expr::Ident(s) => Ok(SelectItem::Column(ColumnSpec::CaseInsensitive(s.clone()))),
            _ => Err(Error::UnsupportedFunctionCall(format!(
                "{SELECT_AGG_EXPECTED}, got {expr:?}"
            ))),
        })
        .collect()
}

#[cfg(test)]
pub(super) fn is_head_call(expr: Option<&Expr>) -> bool {
    if let Some(Expr::FunctionCall(name, _)) = expr {
        *name == "head"
    } else {
        false
    }
}

/// Plans a single pipeline stage from an AST expression.
pub(super) fn plan_stage(expr: Expr) -> crate::Result<ReplPipelineStage> {
    match expr {
        Expr::FunctionCall(name, args) => {
            let name_str = name.to_string();
            match name_str.as_str() {
                "read" => {
                    let path = extract_path_from_args("read", &args)?;
                    Ok(ReplPipelineStage::Read { path })
                }
                "group_by" => {
                    if args.is_empty() {
                        return Err(Error::UnsupportedFunctionCall(
                            "group_by expects at least one column".to_string(),
                        ));
                    }
                    let columns = args
                        .iter()
                        .map(extract_one_column_spec)
                        .collect::<crate::Result<Vec<_>>>()?;
                    Ok(ReplPipelineStage::GroupBy { columns })
                }
                "select" => {
                    let columns = extract_select_items(&args)?;
                    if columns.is_empty() {
                        return Err(Error::UnsupportedFunctionCall(
                            "select expects at least one column name".to_string(),
                        ));
                    }
                    Ok(ReplPipelineStage::Select { columns })
                }
                "head" => {
                    let n = extract_head_n(&args)?;
                    Ok(ReplPipelineStage::Head { n })
                }
                "tail" => {
                    let n = extract_tail_n(&args)?;
                    Ok(ReplPipelineStage::Tail { n })
                }
                "sample" => {
                    let n = extract_sample_n(&args)?;
                    Ok(ReplPipelineStage::Sample { n })
                }
                "count" => {
                    if !args.is_empty() {
                        return Err(Error::UnsupportedFunctionCall(
                            "count takes no arguments".to_string(),
                        ));
                    }
                    Ok(ReplPipelineStage::Count)
                }
                "schema" => {
                    if !args.is_empty() {
                        return Err(Error::UnsupportedFunctionCall(
                            "schema takes no arguments".to_string(),
                        ));
                    }
                    Ok(ReplPipelineStage::Schema)
                }
                "write" => {
                    let path = extract_path_from_args("write", &args)?;
                    Ok(ReplPipelineStage::Write { path })
                }
                _ => Err(Error::UnsupportedFunctionCall(name_str)),
            }
        }
        _ => Err(Error::UnsupportedExpression(expr.to_string())),
    }
}

/// Plans a full pipeline and returns whether the statement is incomplete
/// (the final explicit stage is non-terminal).
pub(super) fn plan_pipeline_with_state(
    exprs: Vec<Expr>,
) -> crate::Result<(Vec<ReplPipelineStage>, bool)> {
    // Collect all stages from the expressions.
    let mut stages: Vec<ReplPipelineStage> = exprs
        .into_iter()
        .map(plan_stage)
        .collect::<crate::Result<Vec<_>>>()?;
    // Check if the statement is incomplete (before implicit print).
    let statement_incomplete = if repl_pipeline_last_select_is_terminal(&stages) {
        false
    } else {
        stages
            .last()
            .is_some_and(ReplPipelineStage::is_non_terminal)
    };
    // Add any implicit followup stage.
    if let Some(implicit_stage) = stages
        .last()
        .and_then(ReplPipelineStage::get_implicit_followup_stage)
    {
        stages.push(implicit_stage);
    }
    Ok((stages, statement_incomplete))
}

fn validate_grouped_select(keys: &[ColumnSpec], items: &[SelectItem]) -> crate::Result<()> {
    for key in keys {
        let mut found = false;
        for item in items {
            if let SelectItem::Column(c) = item
                && c == key
            {
                found = true;
                break;
            }
        }
        if !found {
            return Err(Error::InvalidReplPipeline(
                "every group_by column must appear in select() as a plain column".to_string(),
            ));
        }
    }
    for item in items {
        match item {
            SelectItem::Column(c) => {
                if !keys.iter().any(|k| k == c) {
                    return Err(Error::InvalidReplPipeline(
                        "select with group_by: non-key columns must use an aggregate (sum, avg, min, or max), not plain columns"
                            .to_string(),
                    ));
                }
            }
            SelectItem::Sum(_) | SelectItem::Avg(_) | SelectItem::Min(_) | SelectItem::Max(_) => {}
        }
    }
    Ok(())
}

/// Validates that stages match `read` → optional `group_by` / `select` (either order) → optional slice or `schema`/`count` → optional `write`,
/// with optional trailing `print` only after head/tail/sample.
pub(super) fn validate_repl_pipeline_stages(stages: &[ReplPipelineStage]) -> crate::Result<()> {
    if stages.is_empty() {
        return Err(Error::InvalidReplPipeline("empty pipeline".to_string()));
    }

    let body = match stages.last() {
        Some(ReplPipelineStage::Print) if stages.len() >= 2 => &stages[..stages.len() - 1],
        _ => stages,
    };

    if !matches!(body.first(), Some(ReplPipelineStage::Read { .. })) {
        return Err(Error::InvalidReplPipeline(
            "pipeline must start with read(path)".to_string(),
        ));
    }

    let mut i = 1usize;
    let mut group_by_cols: Option<&Vec<ColumnSpec>> = None;
    let mut select_items: Option<&Vec<SelectItem>> = None;

    for _ in 0..2 {
        match body.get(i) {
            Some(ReplPipelineStage::GroupBy { columns }) => {
                if group_by_cols.is_some() {
                    return Err(Error::InvalidReplPipeline(
                        "only one group_by(...) is allowed in a pipeline".to_string(),
                    ));
                }
                group_by_cols = Some(columns);
                i += 1;
            }
            Some(ReplPipelineStage::Select { columns }) => {
                if select_items.is_some() {
                    return Err(Error::InvalidReplPipeline(
                        "only one select(...) is allowed in a pipeline".to_string(),
                    ));
                }
                select_items = Some(columns);
                i += 1;
            }
            _ => break,
        }
    }

    if group_by_cols.is_some() && select_items.is_none() {
        return Err(Error::InvalidReplPipeline(
            "group_by(...) requires select(...)".to_string(),
        ));
    }

    if let Some(keys) = group_by_cols {
        let items = select_items.expect("select when group_by");
        validate_grouped_select(keys, items)?;
    } else if let Some(items) = select_items {
        let spec = SelectSpec {
            columns: items.to_vec(),
            group_by: None,
        };
        if spec.has_aggregates() && !spec.is_aggregate_only() {
            return Err(Error::InvalidReplPipeline(
                "mixed column projections and aggregates in select require group_by(); \
                 put every group key in group_by() and list them as columns in select()"
                    .to_string(),
            ));
        }
    }

    match body.get(i) {
        Some(
            ReplPipelineStage::Head { .. }
            | ReplPipelineStage::Tail { .. }
            | ReplPipelineStage::Sample { .. },
        ) => {
            i += 1;
            if matches!(
                body.get(i),
                Some(
                    ReplPipelineStage::Head { .. }
                        | ReplPipelineStage::Tail { .. }
                        | ReplPipelineStage::Sample { .. },
                ),
            ) {
                return Err(Error::InvalidReplPipeline(
                    "only one of head(...), tail(...), or sample(...) is allowed".to_string(),
                ));
            }
            if matches!(body.get(i), Some(ReplPipelineStage::Write { .. })) {
                i += 1;
            }
        }
        Some(ReplPipelineStage::Schema) | Some(ReplPipelineStage::Count) => {
            i += 1;
        }
        Some(ReplPipelineStage::Write { .. }) => {
            i += 1;
        }
        None => {
            if !repl_pipeline_last_select_is_terminal(body) {
                return Err(Error::InvalidReplPipeline(
                    "pipeline must end with write, head, tail, sample, schema, count, or a complete grouped select()"
                        .to_string(),
                ));
            }
        }
        _ => {
            return Err(Error::InvalidReplPipeline(
                "invalid pipeline stage order (expected read, optional group_by and select, then head|tail|sample|schema|count, or write)".to_string(),
            ));
        }
    }

    if i != body.len() {
        return Err(Error::InvalidReplPipeline(
            "invalid pipeline stage order (expected read, optional group_by/select, optional head|tail|sample|schema|count, optional write)".to_string(),
        ));
    }

    if matches!(stages.last(), Some(ReplPipelineStage::Print)) {
        if !matches!(
            body.last(),
            Some(
                ReplPipelineStage::Head { .. }
                    | ReplPipelineStage::Tail { .. }
                    | ReplPipelineStage::Sample { .. },
            )
        ) {
            return Err(Error::InvalidReplPipeline(
                "print may only follow head, tail, or sample".to_string(),
            ));
        }
    } else {
        let ends_ok = match body.last() {
            Some(ReplPipelineStage::Write { .. })
            | Some(
                ReplPipelineStage::Head { .. }
                | ReplPipelineStage::Tail { .. }
                | ReplPipelineStage::Sample { .. },
            )
            | Some(ReplPipelineStage::Schema)
            | Some(ReplPipelineStage::Count) => true,
            _ => repl_pipeline_last_select_is_terminal(body),
        };
        if !ends_ok {
            return Err(Error::InvalidReplPipeline(
                "pipeline must end with write, head, tail, sample, schema, count, or a complete grouped select()"
                    .to_string(),
            ));
        }
    }

    Ok(())
}

/// Extracts a single positive integer argument from a function call's args.
fn extract_usize_arg(func_name: &str, args: &[Expr]) -> crate::Result<usize> {
    match args {
        [Expr::Literal(Literal::Number(num))] => {
            let s = num.to_string();
            s.parse::<usize>().map_err(|_| {
                Error::UnsupportedFunctionCall(format!(
                    "{func_name} expects a positive integer argument, got {s}"
                ))
            })
        }
        _ => Err(Error::UnsupportedFunctionCall(format!(
            "{func_name} expects a single integer argument, got {args:?}"
        ))),
    }
}

/// Extracts the integer argument N from a head() call's args.
pub(super) fn extract_head_n(args: &[Expr]) -> crate::Result<usize> {
    extract_usize_arg("head", args)
}

/// Extracts the integer argument N from a tail() call's args.
pub(super) fn extract_tail_n(args: &[Expr]) -> crate::Result<usize> {
    extract_usize_arg("tail", args)
}

/// Extracts the optional integer argument N from a sample() call's args (default 10).
pub(super) fn extract_sample_n(args: &[Expr]) -> crate::Result<usize> {
    if args.is_empty() {
        return Ok(10);
    }
    extract_usize_arg("sample", args)
}