duckdb-slt 0.1.0

Command-line sqllogictest runner for DuckDB.
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
mod duckdb_driver;
mod extensions;

use std::io::{IsTerminal, Write};
use std::path::{Path, PathBuf};
use std::process::ExitCode;

use anyhow::{Context, Result};
use clap::{ArgAction, Parser, error::ErrorKind};
use duckdb::{Config, Connection};
use sqllogictest::runner::TestErrorKind;
use sqllogictest::{QueryExpect, Record, Runner};

use crate::duckdb_driver::DuckdbDriver;
use crate::extensions::ExtensionActions;

const EXIT_OK: u8 = 0;
const EXIT_RUNTIME_ERROR: u8 = 1;
const EXIT_TEST_FAIL: u8 = 2;

#[derive(Parser, Debug)]
#[command(name = "duckdb-slt", about = "DuckDB sqllogictest runner", version)]
struct Cli {
    /// Path to the DuckDB database file. Defaults to an in-memory database.
    #[arg(long, value_name = "PATH")]
    db: Option<PathBuf>,

    /// Allow loading unsigned DuckDB extensions (risky; opt-in).
    #[arg(short = 'u', long)]
    allow_unsigned_extensions: bool,

    /// DuckDB extensions to enable (repeatable). Each entry runs INSTALL then LOAD.
    #[arg(short = 'e', long, value_name = "EXT")]
    extensions: Vec<String>,

    /// Working directory to apply before resolving relative paths.
    #[arg(short = 'w', long, value_name = "DIR")]
    workdir: Option<PathBuf>,

    /// Stop after the first test mismatch.
    #[arg(long, default_value_t = false, action = ArgAction::SetTrue)]
    fail_fast: bool,

    /// One or more sqllogictest input files.
    #[arg(value_name = "FILES", required = true)]
    files: Vec<PathBuf>,
}

fn main() -> ExitCode {
    let cli = match Cli::try_parse() {
        Ok(cli) => cli,
        Err(err) => {
            return match err.kind() {
                ErrorKind::DisplayHelp | ErrorKind::DisplayVersion => {
                    print!("{err}");
                    ExitCode::SUCCESS
                }
                _ => {
                    eprintln!("{err}");
                    ExitCode::from(EXIT_RUNTIME_ERROR)
                }
            };
        }
    };

    match run(cli) {
        Ok(code) => ExitCode::from(code),
        Err(err) => {
            eprintln!("error: {err:?}");
            ExitCode::from(EXIT_RUNTIME_ERROR)
        }
    }
}

fn run(cli: Cli) -> Result<u8> {
    if let Some(workdir) = &cli.workdir {
        std::env::set_current_dir(workdir)
            .with_context(|| format!("failed to set workdir: {}", workdir.display()))?;
    }

    let base_dir = std::env::current_dir()?;

    let files = expand_files(&cli.files)?
        .into_iter()
        .map(|p| normalize_path(&p))
        .collect::<Result<Vec<_>>>()?;

    println!("running {} tests", files.len());
    let started = std::time::Instant::now();
    let use_color = std::io::stdout().is_terminal();

    let mut files_passed = 0usize;
    let mut files_failed = 0usize;
    let mut files_errored = 0usize;

    let mut failed_files: Vec<String> = Vec::new();
    let mut errored_files: Vec<String> = Vec::new();

    for path in files {
        let display_path = format_user_path(&base_dir, &path);
        let res = run_one_file(&cli, &base_dir, &path);
        match res {
            Ok(()) => {
                files_passed += 1;
                println!("test {display_path} ... {}", format_ok(use_color));
            }
            Err(FileRunError::TestFailure(e)) => {
                files_failed += 1;
                failed_files.push(display_path.clone());
                println!("test {display_path} ... {}", format_failed(use_color));
                // Ensure the FAILED line is emitted before the detailed report.
                let _ = std::io::stdout().flush();
                eprintln!("{e}");

                if cli.fail_fast {
                    break;
                }
            }
            Err(FileRunError::Runtime(e)) => {
                files_errored += 1;
                errored_files.push(display_path.clone());
                println!("test {display_path} ... {}", format_error(use_color));
                let _ = std::io::stdout().flush();
                eprintln!("{e:?}");
                // Runtime errors are not recoverable for a single run.
                break;
            }
        }
    }

    if !failed_files.is_empty() || !errored_files.is_empty() {
        println!("\nfailures:\n");
        for f in &failed_files {
            println!("    {f}");
        }
        for f in &errored_files {
            println!("    {f}");
        }
        println!();
    }

    let exit_code = if files_errored > 0 {
        EXIT_RUNTIME_ERROR
    } else if files_failed > 0 {
        EXIT_TEST_FAIL
    } else {
        EXIT_OK
    };

    let status = if files_failed == 0 && files_errored == 0 {
        format_ok(use_color)
    } else {
        format_failed(use_color)
    };
    println!(
        "test result: {status}. {files_passed} passed; {files_failed} failed; {files_errored} errored; 0 ignored; 0 measured; 0 filtered out; finished in {:.2}s",
        started.elapsed().as_secs_f64()
    );

    Ok(exit_code)
}

fn format_ok(use_color: bool) -> &'static str {
    if use_color { "\x1b[32mok\x1b[0m" } else { "ok" }
}

fn format_failed(use_color: bool) -> &'static str {
    if use_color {
        "\x1b[31mFAILED\x1b[0m"
    } else {
        "FAILED"
    }
}

fn format_error(use_color: bool) -> &'static str {
    if use_color {
        "\x1b[31mERROR\x1b[0m"
    } else {
        "ERROR"
    }
}

fn expand_files(files: &[PathBuf]) -> Result<Vec<PathBuf>> {
    let mut out: Vec<PathBuf> = Vec::new();

    for p in files {
        if looks_like_glob_pattern(p) {
            let pattern = normalize_glob_pattern(p);
            let mut matches: Vec<PathBuf> = glob::glob(&pattern)
                .with_context(|| format!("invalid glob pattern: {pattern}"))?
                .map(|res| res.with_context(|| format!("failed to expand glob: {pattern}")))
                .collect::<Result<Vec<_>>>()?;

            matches.sort_by(|a, b| a.to_string_lossy().cmp(&b.to_string_lossy()));

            if matches.is_empty() {
                anyhow::bail!("glob pattern matched no files: {pattern}");
            }

            out.extend(matches);
        } else {
            out.push(p.clone());
        }
    }

    Ok(out)
}

fn looks_like_glob_pattern(path: &Path) -> bool {
    let s = path.to_string_lossy();
    s.contains('*')
        || s.contains('?')
        || s.contains('[')
        || s.contains(']')
        || s.contains('{')
        || s.contains('}')
}

fn normalize_glob_pattern(path: &Path) -> String {
    let s = path.to_string_lossy();
    if cfg!(windows) {
        s.replace('\\', "/")
    } else {
        s.to_string()
    }
}

fn normalize_path(path: &Path) -> Result<PathBuf> {
    let path = if path.is_absolute() {
        path.to_path_buf()
    } else {
        std::env::current_dir()?.join(path)
    };

    Ok(path)
}

fn format_user_path(base_dir: &Path, path: &Path) -> String {
    path.strip_prefix(base_dir)
        .unwrap_or(path)
        .display()
        .to_string()
}

fn format_user_path_str(base_dir: &Path, raw: &str) -> String {
    let p = Path::new(raw);
    if p.is_absolute() {
        format_user_path(base_dir, p)
    } else {
        raw.replace(['/', '\\'], std::path::MAIN_SEPARATOR_STR)
    }
}

enum FileRunError {
    TestFailure(String),
    Runtime(anyhow::Error),
}

#[derive(Debug, Clone)]
struct RecordId {
    index_1_based: usize,
    name: Option<String>,
}

fn find_record_id(main_file: &Path, loc: &sqllogictest::Location) -> Option<RecordId> {
    let file_hint = PathBuf::from(loc.file());
    let candidate = if file_hint.is_absolute() {
        file_hint
    } else {
        main_file
            .parent()
            .unwrap_or_else(|| Path::new("."))
            .join(file_hint)
    };

    let records = sqllogictest::parse_file::<sqllogictest::DefaultColumnType>(&candidate).ok()?;
    let mut index = 0usize;

    for r in records {
        match r {
            Record::Statement { loc: rloc, .. } => {
                index += 1;
                if rloc.file() == loc.file() && rloc.line() == loc.line() {
                    return Some(RecordId {
                        index_1_based: index,
                        name: None,
                    });
                }
            }
            Record::System { loc: rloc, .. } => {
                index += 1;
                if rloc.file() == loc.file() && rloc.line() == loc.line() {
                    return Some(RecordId {
                        index_1_based: index,
                        name: None,
                    });
                }
            }
            Record::Query {
                loc: rloc,
                expected,
                ..
            } => {
                index += 1;
                if rloc.file() == loc.file() && rloc.line() == loc.line() {
                    let name = match expected {
                        QueryExpect::Results { label, .. } => label,
                        QueryExpect::Error(_) => None,
                    };
                    return Some(RecordId {
                        index_1_based: index,
                        name,
                    });
                }
            }
            _ => {}
        }
    }

    None
}

fn render_failure_report(
    main_file: &Path,
    base_dir: &Path,
    test_err: &sqllogictest::TestError,
) -> String {
    use std::fmt::Write;

    let kind = test_err.kind();
    let loc = test_err.location();
    let record_id = find_record_id(main_file, &loc);

    let mut out = String::new();

    // writeln!(out, "test mismatch").expect("writing to String should not fail");
    // writeln!(out, "file: {}", format_user_path(base_dir, main_file))
    //     .expect("writing to String should not fail");
    writeln!(
        out,
        "  at: {}:{}",
        format_user_path_str(base_dir, loc.file()),
        loc.line()
    )
    .expect("writing to String should not fail");
    if let Some(id) = &record_id {
        writeln!(
            out,
            "  record: {}{}",
            id.index_1_based,
            id.name
                .as_deref()
                .map(|n| format!(" name={n}"))
                .unwrap_or_default()
        )
        .expect("writing to String should not fail");
    }

    let sql = match &kind {
        TestErrorKind::Ok { sql, .. }
        | TestErrorKind::Fail { sql, .. }
        | TestErrorKind::ErrorMismatch { sql, .. }
        | TestErrorKind::StatementResultMismatch { sql, .. }
        | TestErrorKind::QueryResultMismatch { sql, .. }
        | TestErrorKind::QueryResultColumnsMismatch { sql, .. } => Some(sql.as_str()),
        TestErrorKind::ParseError(_)
        | TestErrorKind::SystemFail { .. }
        | TestErrorKind::SystemStdoutMismatch { .. } => None,
        _ => None,
    };

    if let Some(sql) = sql {
        writeln!(out, "sql:\n{sql}").expect("writing to String should not fail");
    }

    match &kind {
        TestErrorKind::QueryResultMismatch {
            expected, actual, ..
        } => {
            writeln!(out, "expected: {expected}").expect("writing to String should not fail");
            writeln!(out, "actual: {actual}").expect("writing to String should not fail");
        }
        TestErrorKind::QueryResultColumnsMismatch {
            expected, actual, ..
        } => {
            writeln!(out, "expected_columns: {expected}")
                .expect("writing to String should not fail");
            writeln!(out, "actual_columns: {actual}").expect("writing to String should not fail");
        }
        TestErrorKind::ErrorMismatch {
            expected_err,
            err,
            actual_sqlstate,
            ..
        } => {
            writeln!(out, "expected_error: {expected_err}")
                .expect("writing to String should not fail");
            if let Some(sqlstate) = actual_sqlstate {
                writeln!(out, "actual_sqlstate: {sqlstate}")
                    .expect("writing to String should not fail");
            }
            writeln!(out, "actual_error: {err}").expect("writing to String should not fail");
        }
        TestErrorKind::StatementResultMismatch {
            expected, actual, ..
        } => {
            writeln!(out, "expected_rows: {expected}").expect("writing to String should not fail");
            writeln!(out, "actual_rows: {actual}").expect("writing to String should not fail");
        }
        TestErrorKind::Ok { .. }
        | TestErrorKind::Fail { .. }
        | TestErrorKind::SystemFail { .. }
        | TestErrorKind::SystemStdoutMismatch { .. }
        | TestErrorKind::ParseError(_)
        | _ => {
            // Fallback: still include the underlying library error message.
            writeln!(out, "details: {}", test_err.display(false))
                .expect("writing to String should not fail");
        }
    }

    out.trim_end_matches('\n').to_string()
}

fn run_one_file(cli: &Cli, base_dir: &Path, path: &Path) -> std::result::Result<(), FileRunError> {
    if !path.exists() {
        return Err(FileRunError::Runtime(anyhow::anyhow!(
            "file does not exist: {}",
            path.display()
        )));
    }

    let db = cli.db.clone();
    let allow_unsigned_extensions = cli.allow_unsigned_extensions;
    let extensions = cli
        .extensions
        .iter()
        .map(|raw| crate::extensions::compile_extension_actions(raw))
        .collect::<Result<Vec<ExtensionActions>>>()
        .map_err(FileRunError::Runtime)?;
    let mut runner = Runner::new(move || {
        let db = db.clone();
        let extensions = extensions.clone();

        async move {
            let conn = open_duckdb_connection(db.as_deref(), allow_unsigned_extensions)?;

            for ext in &extensions {
                conn.execute_batch(&ext.install_sql)?;
                conn.execute_batch(&ext.load_sql)?;
            }

            Ok(DuckdbDriver::new(conn))
        }
    });

    match runner.run_file(path) {
        Ok(()) => Ok(()),
        Err(test_err) => match test_err.kind() {
            TestErrorKind::ParseError(_) => Err(FileRunError::Runtime(anyhow::anyhow!(
                "parse error in {}: {}",
                path.display(),
                test_err.display(false)
            ))),
            _ => Err(FileRunError::TestFailure(render_failure_report(
                path, base_dir, &test_err,
            ))),
        },
    }
}

fn open_duckdb_connection(
    db: Option<&Path>,
    allow_unsigned_extensions: bool,
) -> duckdb::Result<Connection> {
    let mut config = Config::default();
    if allow_unsigned_extensions {
        config = config.allow_unsigned_extensions()?;
    }

    let conn = match db {
        Some(p) => Connection::open_with_flags(p, config)?,
        None => Connection::open_in_memory_with_flags(config)?,
    };

    Ok(conn)
}