datapress-core 0.5.6

Backend-agnostic core types, config, routing, and HTTP handlers for the datapress dataset server.
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
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
//! Shared safety gate for the raw-SQL endpoint (`POST /api/v1/sql`).
//!
//! Raw SQL is a much larger attack surface than the structured `/query`
//! endpoint, so every statement is parsed and validated *before* it is
//! handed to a backend engine. The same gate runs for DuckDB and
//! DataFusion, giving both backends identical safety semantics — and
//! keeping the "which tables may this query touch?" policy in one place.
//!
//! Guarantees enforced by [`validate`]:
//! - exactly one statement, and it is a read-only `SELECT` / `WITH … SELECT`
//!   or a `DESCRIBE` / `DESC <table>` schema lookup,
//! - every referenced table is a registered dataset — no file-reading
//!   table functions (`read_parquet`, `read_csv`, …), no unknown tables,
//! - no file-reading scalar functions (`read_text`, `read_blob`, …),
//! - at most `max_datasets` distinct datasets are referenced. Phase 1
//!   passes `1`, enforcing the single-dataset rule; raising this bound is
//!   all that's needed to allow cross-dataset joins later.
//!
//! CTE-defined names are tracked per query scope and excluded from the
//! dataset allowlist check, so `WITH t AS (SELECT … FROM events) SELECT …`
//! is accepted (it still only touches `events`).

use std::collections::{HashMap, HashSet};
use std::ops::ControlFlow;

use sqlparser::ast::{
    DescribeAlias, Expr, Ident, ObjectName, ObjectNamePart, Query, SelectItem, SetExpr, Statement,
    TableFactor, Visit, VisitMut, Visitor, VisitorMut,
};
use sqlparser::dialect::GenericDialect;
use sqlparser::parser::Parser;

use crate::errors::AppError;
use crate::schema::DatasetSchema;

/// File-reading / external-access functions that must never run through
/// the SQL endpoint, in either table or scalar position. Table-position
/// functions are already blocked by the relation allowlist; this list
/// closes the scalar-position gap (e.g. `SELECT read_text('/etc/passwd')`).
const DENIED_FUNCTIONS: &[&str] = &[
    "read_text",
    "read_blob",
    "read_csv",
    "read_csv_auto",
    "read_parquet",
    "parquet_scan",
    "read_json",
    "read_json_auto",
    "read_json_objects",
    "read_ndjson",
    "read_ndjson_auto",
    "read_ndjson_objects",
    "sniff_csv",
    "glob",
];

/// A validated, ready-to-execute SQL query.
#[derive(Debug)]
pub struct ValidatedSql {
    /// The trimmed, semicolon-free SQL string, safe to wrap and execute.
    pub sql: String,
    /// The distinct dataset names the query references (lowercased). Empty
    /// for table-less queries such as `SELECT 1`.
    pub datasets: Vec<String>,
}

/// Validate `sql` for the raw-SQL endpoint.
///
/// Accepts a single read-only `SELECT` / `WITH … SELECT` or a `DESCRIBE` /
/// `DESC <table>` statement. `allowed` is the set of registered dataset
/// caller (matching is case-insensitive). `max_datasets` caps how many
/// distinct datasets a single statement may touch (phase 1 = `1`).
///
/// On success returns the cleaned SQL ready to be wrapped in an outer
/// `LIMIT` and executed by the backend (DESCRIBE is run as-is; see
/// [`is_describe`]).
pub fn validate(
    sql: &str,
    allowed: &HashSet<String>,
    max_datasets: usize,
) -> Result<ValidatedSql, AppError> {
    let trimmed = sql.trim().trim_end_matches(';').trim();
    if trimmed.is_empty() {
        return Err(AppError::InvalidValue("sql must not be empty".into()));
    }

    let statements = Parser::parse_sql(&GenericDialect {}, trimmed)
        .map_err(|e| AppError::InvalidValue(format!("could not parse SQL: {e}")))?;
    if statements.len() != 1 {
        return Err(AppError::InvalidValue(
            "exactly one SQL statement is allowed".into(),
        ));
    }
    let stmt = &statements[0];
    // Read-only statements only: a `SELECT` / `WITH … SELECT`, or a
    // `DESCRIBE`/`DESC <table>` schema lookup. `DESCRIBE` still flows through
    // the visitor below, so its target table is subject to the same dataset
    // allowlist as a query. The `EXPLAIN` alias is deliberately excluded.
    match stmt {
        Statement::Query(_) => {}
        Statement::ExplainTable {
            describe_alias: DescribeAlias::Describe | DescribeAlias::Desc,
            ..
        } => {}
        _ => {
            return Err(AppError::InvalidValue(
                "only read-only SELECT and DESCRIBE statements are allowed".into(),
            ));
        }
    }

    let mut checker = ScopeCheck {
        allowed,
        cte_names: HashSet::new(),
        referenced: HashSet::new(),
        violation: None,
    };
    let _ = stmt.visit(&mut checker);
    if let Some(err) = checker.violation {
        return Err(AppError::InvalidValue(err));
    }

    let mut datasets: Vec<String> = checker.referenced.into_iter().collect();
    datasets.sort();
    if datasets.len() > max_datasets {
        return Err(AppError::InvalidValue(format!(
            "this endpoint allows at most {max_datasets} dataset(s) per query; \
             the statement references {}",
            datasets.len()
        )));
    }

    Ok(ValidatedSql {
        sql: trimmed.to_string(),
        datasets,
    })
}

/// Returns `true` if `sql` is a single `DESCRIBE` / `DESC <table>` statement.
///
/// `DESCRIBE` yields a schema listing rather than a row stream and cannot be
/// nested inside an outer `SELECT … LIMIT` subquery on every backend (notably
/// DataFusion), so callers run it directly instead of through the bounded
/// wrapper. Returns `false` for anything that does not parse to exactly one
/// `DESCRIBE`/`DESC` statement.
pub fn is_describe(sql: &str) -> bool {
    let trimmed = sql.trim().trim_end_matches(';').trim();
    matches!(
        Parser::parse_sql(&GenericDialect {}, trimmed).as_deref(),
        Ok([Statement::ExplainTable {
            describe_alias: DescribeAlias::Describe | DescribeAlias::Desc,
            ..
        }])
    )
}

/// Rewrite references to registered tables and their columns so they
/// match **case-insensitively**, the way DuckDB does.
///
/// DataFusion lowercases unquoted identifiers by default, so a query like
/// `SELECT State FROM accidents` is looked up as `state` and fails against
/// a case-sensitive Parquet column literally named `State`. Rather than
/// disable normalization globally (which would also make *aliases* and
/// *CTE names* case-sensitive), we rewrite only the identifiers that name
/// a known dataset or column into their **canonical casing, quoted**.
/// Quoted identifiers bypass the engine's lowercasing and match the stored
/// name exactly, while every other identifier (aliases, CTE names, …) is
/// left untouched so the engine's normal case-insensitive handling still
/// applies.
///
/// `tables` and `columns` map a **lowercased** name to its canonical
/// spelling. On any parse failure the input is returned unchanged so the
/// backend can surface a meaningful error.
pub fn canonicalize_identifiers(
    sql: &str,
    tables: &HashMap<String, String>,
    columns: &HashMap<String, String>,
) -> String {
    let mut statements = match Parser::parse_sql(&GenericDialect {}, sql) {
        Ok(s) if s.len() == 1 => s,
        _ => return sql.to_string(),
    };
    let mut canon = Canonicalizer { tables, columns };
    let _ = VisitMut::visit(&mut statements[0], &mut canon);
    statements[0].to_string()
}

struct Canonicalizer<'a> {
    tables: &'a HashMap<String, String>,
    columns: &'a HashMap<String, String>,
}

impl Canonicalizer<'_> {
    /// If `ident` (case-folded) names something in `map`, replace it with
    /// the canonical spelling and force double-quoting so the engine keeps
    /// the exact case.
    fn rewrite(ident: &mut Ident, map: &HashMap<String, String>) {
        if let Some(canonical) = map.get(&ident.value.to_lowercase()) {
            ident.value = canonical.clone();
            ident.quote_style = Some('"');
        }
    }
}

impl VisitorMut for Canonicalizer<'_> {
    type Break = ();

    fn pre_visit_relation(&mut self, relation: &mut ObjectName) -> ControlFlow<Self::Break> {
        for part in relation.0.iter_mut() {
            if let ObjectNamePart::Identifier(ident) = part {
                Self::rewrite(ident, self.tables);
            }
        }
        ControlFlow::Continue(())
    }

    fn pre_visit_expr(&mut self, expr: &mut Expr) -> ControlFlow<Self::Break> {
        match expr {
            // Bare column reference: `State`.
            Expr::Identifier(ident) => Self::rewrite(ident, self.columns),
            // Qualified reference: `accidents.State` / `a.State`. The last
            // part is the column; earlier parts qualify it with a table or
            // alias (only real table names are rewritten).
            Expr::CompoundIdentifier(idents) => {
                if let Some((column, qualifiers)) = idents.split_last_mut() {
                    Self::rewrite(column, self.columns);
                    for qualifier in qualifiers {
                        Self::rewrite(qualifier, self.tables);
                    }
                }
            }
            _ => {}
        }
        ControlFlow::Continue(())
    }
}

struct ScopeCheck<'a> {
    allowed: &'a HashSet<String>,
    cte_names: HashSet<String>,
    referenced: HashSet<String>,
    violation: Option<String>,
}

impl Visitor for ScopeCheck<'_> {
    type Break = ();

    fn pre_visit_query(&mut self, query: &Query) -> ControlFlow<Self::Break> {
        // Record CTE names *before* visiting the query body so references
        // to them inside the body are recognised and not mistaken for
        // unknown tables. Nested `WITH` clauses are handled the same way
        // as the visitor descends into subqueries.
        if let Some(with) = &query.with {
            for cte in &with.cte_tables {
                self.cte_names.insert(cte.alias.name.value.to_lowercase());
            }
        }
        ControlFlow::Continue(())
    }

    fn pre_visit_relation(&mut self, relation: &ObjectName) -> ControlFlow<Self::Break> {
        let ident = relation
            .0
            .last()
            .and_then(|p| p.as_ident())
            .map(|i| i.value.to_lowercase())
            .unwrap_or_default();

        if self.cte_names.contains(&ident) {
            return ControlFlow::Continue(());
        }
        if let Some(name) = self.allowed.get(&ident) {
            self.referenced.insert(name.clone());
            return ControlFlow::Continue(());
        }
        self.violation = Some(format!(
            "table '{ident}' is not a registered dataset accessible from the SQL endpoint"
        ));
        ControlFlow::Break(())
    }

    fn pre_visit_expr(&mut self, expr: &Expr) -> ControlFlow<Self::Break> {
        if let Expr::Function(func) = expr {
            let fname = func
                .name
                .0
                .last()
                .and_then(|p| p.as_ident())
                .map(|i| i.value.to_lowercase())
                .unwrap_or_default();
            if DENIED_FUNCTIONS.contains(&fname.as_str()) {
                self.violation =
                    Some(format!("function '{fname}' is not allowed in the SQL endpoint"));
                return ControlFlow::Break(());
            }
        }
        ControlFlow::Continue(())
    }
}

/// Enforce a dataset's column-level access filters on a raw-SQL statement.
///
/// The structured `/query` endpoint can tell a projection column from a
/// filter column, so it distinguishes the two filters precisely. Raw SQL
/// cannot be re-planned that cheaply, so the SQL endpoint applies a single
/// conservative rule: **a column that is hidden from projection or blocked
/// from predicates may not be referenced at all**, and `SELECT *` is
/// rejected whenever any column is hidden (it would otherwise expand to
/// include the hidden ones). A column reference that does not name a real
/// schema column (an alias, CTE column, or function) is ignored — only the
/// engine can resolve those.
///
/// `schema` is the referenced dataset's schema. A no-op when the schema
/// carries no active filters. On a parse failure the statement is passed
/// through unchanged so the backend surfaces the real parse error.
pub fn enforce_column_access(sql: &str, schema: &DatasetSchema) -> Result<(), AppError> {
    if !schema.has_column_filters() {
        return Ok(());
    }
    let trimmed = sql.trim().trim_end_matches(';').trim();
    let statements = match Parser::parse_sql(&GenericDialect {}, trimmed) {
        Ok(s) if s.len() == 1 => s,
        _ => return Ok(()),
    };
    let stmt = &statements[0];

    if schema.projection_filter.is_active() && statement_has_wildcard(stmt) {
        return Err(AppError::Forbidden(format!(
            "SELECT * is not allowed on dataset '{}' because it hides columns; \
             list the columns explicitly",
            schema.name
        )));
    }

    let mut refs = ColumnRefCollector {
        columns: HashSet::new(),
    };
    let _ = stmt.visit(&mut refs);
    for lc in &refs.columns {
        // Only real schema columns are subject to the filters; aliases,
        // CTE columns and function names are left for the engine.
        let Some(col) = schema.by_name.get(lc).map(|&i| &schema.columns[i]) else {
            continue;
        };
        if !schema.projection_filter.allows(&col.name) {
            return Err(AppError::UnknownColumn(col.name.clone()));
        }
        if !schema.predicate_filter.allows(&col.name) {
            return Err(AppError::Forbidden(format!(
                "column '{}' may not be used on the SQL endpoint for dataset '{}'",
                col.name, schema.name
            )));
        }
    }
    Ok(())
}

/// Collects every identifier used in column position (bare `col` or the
/// trailing part of a qualified `t.col`) so it can be matched against the
/// schema's access filters.
struct ColumnRefCollector {
    columns: HashSet<String>,
}

impl Visitor for ColumnRefCollector {
    type Break = ();

    fn pre_visit_expr(&mut self, expr: &Expr) -> ControlFlow<Self::Break> {
        match expr {
            Expr::Identifier(ident) => {
                self.columns.insert(ident.value.to_lowercase());
            }
            Expr::CompoundIdentifier(idents) => {
                if let Some(last) = idents.last() {
                    self.columns.insert(last.value.to_lowercase());
                }
            }
            _ => {}
        }
        ControlFlow::Continue(())
    }
}

/// Whether any `SELECT` projection in the statement uses an (optionally
/// qualified) wildcard. Walks the set-expression tree, CTEs and derived
/// tables — the positions whose columns are returned to the caller.
fn statement_has_wildcard(stmt: &Statement) -> bool {
    match stmt {
        Statement::Query(q) => query_has_wildcard(q),
        _ => false,
    }
}

fn query_has_wildcard(query: &Query) -> bool {
    if let Some(with) = &query.with
        && with
            .cte_tables
            .iter()
            .any(|cte| query_has_wildcard(&cte.query))
    {
        return true;
    }
    set_expr_has_wildcard(&query.body)
}

fn set_expr_has_wildcard(set: &SetExpr) -> bool {
    match set {
        SetExpr::Select(select) => {
            let proj_wildcard = select.projection.iter().any(|item| {
                matches!(
                    item,
                    SelectItem::Wildcard(_) | SelectItem::QualifiedWildcard(_, _)
                )
            });
            proj_wildcard
                || select
                    .from
                    .iter()
                    .any(|twj| table_factor_has_wildcard(&twj.relation))
        }
        SetExpr::Query(q) => query_has_wildcard(q),
        SetExpr::SetOperation { left, right, .. } => {
            set_expr_has_wildcard(left) || set_expr_has_wildcard(right)
        }
        _ => false,
    }
}

fn table_factor_has_wildcard(factor: &TableFactor) -> bool {
    match factor {
        TableFactor::Derived { subquery, .. } => query_has_wildcard(subquery),
        TableFactor::NestedJoin {
            table_with_joins, ..
        } => table_factor_has_wildcard(&table_with_joins.relation),
        _ => false,
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    fn allowed(names: &[&str]) -> HashSet<String> {
        names.iter().map(|s| s.to_lowercase()).collect()
    }

    #[test]
    fn accepts_single_dataset_select() {
        let v = validate("SELECT a, b FROM events WHERE a > 1", &allowed(&["events"]), 1).unwrap();
        assert_eq!(v.datasets, vec!["events".to_string()]);
    }

    #[test]
    fn case_insensitive_table_match() {
        let v = validate("SELECT * FROM Events", &allowed(&["events"]), 1).unwrap();
        assert_eq!(v.datasets, vec!["events".to_string()]);
    }

    #[test]
    fn strips_trailing_semicolon() {
        let v = validate("SELECT 1 FROM events;", &allowed(&["events"]), 1).unwrap();
        assert_eq!(v.sql, "SELECT 1 FROM events");
    }

    #[test]
    fn allows_cte_over_single_dataset() {
        let sql = "WITH t AS (SELECT * FROM events) SELECT count(*) FROM t";
        let v = validate(sql, &allowed(&["events"]), 1).unwrap();
        assert_eq!(v.datasets, vec!["events".to_string()]);
    }

    #[test]
    fn allows_tableless_select() {
        let v = validate("SELECT 1 + 1", &allowed(&["events"]), 1).unwrap();
        assert!(v.datasets.is_empty());
    }

    #[test]
    fn rejects_unknown_table() {
        let err = validate("SELECT * FROM secrets", &allowed(&["events"]), 1).unwrap_err();
        assert!(matches!(err, AppError::InvalidValue(_)));
    }

    #[test]
    fn rejects_second_dataset_join() {
        let err = validate(
            "SELECT * FROM events e JOIN other o ON e.id = o.id",
            &allowed(&["events", "other"]),
            1,
        )
        .unwrap_err();
        assert!(matches!(err, AppError::InvalidValue(_)));
    }

    #[test]
    fn allows_two_datasets_when_limit_raised() {
        let v = validate(
            "SELECT * FROM events e JOIN other o ON e.id = o.id",
            &allowed(&["events", "other"]),
            2,
        )
        .unwrap();
        assert_eq!(v.datasets.len(), 2);
    }

    #[test]
    fn rejects_non_select() {
        let err = validate("DELETE FROM events", &allowed(&["events"]), 1).unwrap_err();
        assert!(matches!(err, AppError::InvalidValue(_)));
    }

    #[test]
    fn accepts_describe_table() {
        let v = validate("DESCRIBE events", &allowed(&["events"]), 1).unwrap();
        assert_eq!(v.datasets, vec!["events".to_string()]);
        assert!(is_describe(&v.sql));
    }

    #[test]
    fn accepts_desc_table_case_insensitive() {
        let v = validate("DESC Events", &allowed(&["events"]), 1).unwrap();
        assert_eq!(v.datasets, vec!["events".to_string()]);
        assert!(is_describe(&v.sql));
    }

    #[test]
    fn describe_rejects_unknown_table() {
        let err = validate("DESCRIBE secrets", &allowed(&["events"]), 1).unwrap_err();
        assert!(matches!(err, AppError::InvalidValue(_)));
    }

    #[test]
    fn is_describe_false_for_select() {
        assert!(!is_describe("SELECT * FROM events"));
        assert!(!is_describe("SELECT 1"));
    }

    #[test]
    fn rejects_multiple_statements() {
        let err = validate("SELECT 1 FROM events; SELECT 2 FROM events", &allowed(&["events"]), 1)
            .unwrap_err();
        assert!(matches!(err, AppError::InvalidValue(_)));
    }

    #[test]
    fn rejects_file_table_function() {
        let err = validate("SELECT * FROM read_parquet('/etc/passwd')", &allowed(&["events"]), 1)
            .unwrap_err();
        assert!(matches!(err, AppError::InvalidValue(_)));
    }

    #[test]
    fn rejects_file_scalar_function() {
        let err = validate(
            "SELECT read_text('/etc/passwd') FROM events",
            &allowed(&["events"]),
            1,
        )
        .unwrap_err();
        assert!(matches!(err, AppError::InvalidValue(_)));
    }

    #[test]
    fn rejects_empty_sql() {
        let err = validate("   ", &allowed(&["events"]), 1).unwrap_err();
        assert!(matches!(err, AppError::InvalidValue(_)));
    }

    fn maps(
        tables: &[(&str, &str)],
        columns: &[(&str, &str)],
    ) -> (HashMap<String, String>, HashMap<String, String>) {
        let t = tables
            .iter()
            .map(|(k, v)| (k.to_string(), v.to_string()))
            .collect();
        let c = columns
            .iter()
            .map(|(k, v)| (k.to_string(), v.to_string()))
            .collect();
        (t, c)
    }

    #[test]
    fn canonicalizes_mixed_case_column_and_table() {
        let (t, c) = maps(
            &[("accidents", "accidents")],
            &[("state", "State"), ("id", "ID")],
        );
        let out = canonicalize_identifiers(
            "SELECT state, COUNT(*) AS n FROM Accidents GROUP BY STATE ORDER BY n DESC",
            &t,
            &c,
        );
        // Column refs become quoted canonical names; the table name is
        // quoted canonical; the alias `n` is left untouched.
        assert!(out.contains("\"State\""), "got: {out}");
        assert!(out.contains("FROM \"accidents\""), "got: {out}");
        assert!(out.contains("AS n"), "got: {out}");
        assert!(!out.contains("\"n\""), "alias must not be quoted: {out}");
    }

    #[test]
    fn canonicalizes_qualified_column() {
        let (t, c) = maps(&[("accidents", "accidents")], &[("state", "State")]);
        let out = canonicalize_identifiers("SELECT a.state FROM accidents AS a", &t, &c);
        // The column part is canonicalized; the table alias `a` is not a
        // registered table, so it is left alone.
        assert!(out.contains("a.\"State\""), "got: {out}");
    }

    #[test]
    fn leaves_unknown_identifiers_untouched() {
        let (t, c) = maps(&[("events", "events")], &[("id", "id")]);
        let out = canonicalize_identifiers("SELECT foo, bar FROM events", &t, &c);
        assert!(out.contains("foo"), "got: {out}");
        assert!(out.contains("bar"), "got: {out}");
        assert!(!out.contains("\"foo\""), "got: {out}");
    }

    #[test]
    fn returns_input_unchanged_on_parse_error() {
        let (t, c) = maps(&[], &[]);
        let garbage = "SELECT FROM WHERE";
        assert_eq!(canonicalize_identifiers(garbage, &t, &c), garbage);
    }

    // ---- enforce_column_access ---------------------------------------------

    fn filtered_schema(pred_excl: &[&str], proj_excl: &[&str]) -> DatasetSchema {
        use crate::schema::{ColumnInfo, LogicalType};
        let col = |name: &str| ColumnInfo {
            name: name.into(),
            logical: LogicalType::Int,
            sql_type: "BIGINT".into(),
            nullable: true,
        };
        let excl = |cols: &[&str]| crate::config::ColumnFilter {
            include: vec![],
            exclude: cols.iter().map(|s| s.to_string()).collect(),
        };
        DatasetSchema::new("events", vec![col("id"), col("email"), col("ts")])
            .with_filters(excl(pred_excl), excl(proj_excl))
            .unwrap()
    }

    #[test]
    fn access_noop_without_filters() {
        use crate::schema::{ColumnInfo, LogicalType};
        let sch = DatasetSchema::new(
            "events",
            vec![ColumnInfo {
                name: "id".into(),
                logical: LogicalType::Int,
                sql_type: "BIGINT".into(),
                nullable: false,
            }],
        );
        assert!(enforce_column_access("SELECT * FROM events", &sch).is_ok());
    }

    #[test]
    fn access_rejects_wildcard_when_projection_hides() {
        let sch = filtered_schema(&[], &["email"]);
        let err = enforce_column_access("SELECT * FROM events", &sch).unwrap_err();
        assert!(matches!(err, AppError::Forbidden(_)));
    }

    #[test]
    fn access_allows_wildcard_when_only_predicate_filter() {
        let sch = filtered_schema(&["email"], &[]);
        assert!(enforce_column_access("SELECT * FROM events", &sch).is_ok());
    }

    #[test]
    fn access_rejects_hidden_column_reference() {
        let sch = filtered_schema(&[], &["email"]);
        let err = enforce_column_access("SELECT id, email FROM events", &sch).unwrap_err();
        assert!(matches!(err, AppError::UnknownColumn(_)));
    }

    #[test]
    fn access_rejects_predicate_restricted_column_reference() {
        let sch = filtered_schema(&["email"], &[]);
        let err =
            enforce_column_access("SELECT id FROM events WHERE email = 'x'", &sch).unwrap_err();
        assert!(matches!(err, AppError::Forbidden(_)));
    }

    #[test]
    fn access_allows_visible_columns() {
        let sch = filtered_schema(&[], &["email"]);
        assert!(enforce_column_access("SELECT id, ts FROM events WHERE id > 1", &sch).is_ok());
    }

    #[test]
    fn access_ignores_aliases_and_functions() {
        let sch = filtered_schema(&[], &["email"]);
        // `total` is an alias, not a schema column, so it is ignored.
        assert!(
            enforce_column_access("SELECT count(id) AS total FROM events", &sch).is_ok()
        );
    }

    #[test]
    fn access_matches_qualified_column() {
        let sch = filtered_schema(&[], &["email"]);
        let err =
            enforce_column_access("SELECT e.email FROM events e", &sch).unwrap_err();
        assert!(matches!(err, AppError::UnknownColumn(_)));
    }
}