oxirs 0.2.4

Command-line interface for OxiRS - import, export, migration, and benchmarking tools
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
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
//! Streaming query results for large SPARQL result sets
//!
//! This module implements chunked streaming of SPARQL query results, enabling memory-efficient
//! processing of large datasets with progress indicators and configurable chunk sizes.
//!
//! ## Features
//!
//! - Chunked streaming to avoid loading entire result sets into memory
//! - Progress bar indicators during streaming
//! - Configurable chunk size for tuning memory vs. throughput
//! - Multiple output formats: JSON, CSV, TSV, table
//! - Interrupt-safe streaming with graceful shutdown
//! - Result count tracking and throughput metrics

use crate::cli::CliContext;
use anyhow::{anyhow, Result};
use colored::Colorize;
use indicatif::{ProgressBar, ProgressStyle};
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::time::{Duration, Instant};

/// Represents a single SPARQL binding row
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct BindingRow {
    /// Map of variable name to term value
    pub bindings: HashMap<String, String>,
}

impl BindingRow {
    /// Create a new binding row from a variable-value map
    pub fn new(bindings: HashMap<String, String>) -> Self {
        Self { bindings }
    }

    /// Get value for a specific variable
    pub fn get(&self, var: &str) -> Option<&str> {
        self.bindings.get(var).map(|s| s.as_str())
    }
}

/// A chunk of streamed query results
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ResultChunk {
    /// Chunk sequence number (0-based)
    pub chunk_index: usize,
    /// Variable names in this result set
    pub variables: Vec<String>,
    /// Rows in this chunk
    pub rows: Vec<BindingRow>,
    /// True if this is the final chunk
    pub is_final: bool,
    /// Total rows received so far (cumulative)
    pub total_received: usize,
}

impl ResultChunk {
    /// Create a new result chunk
    pub fn new(
        chunk_index: usize,
        variables: Vec<String>,
        rows: Vec<BindingRow>,
        is_final: bool,
        total_received: usize,
    ) -> Self {
        Self {
            chunk_index,
            variables,
            rows,
            is_final,
            total_received,
        }
    }

    /// Number of rows in this chunk
    pub fn len(&self) -> usize {
        self.rows.len()
    }

    /// True if this chunk has no rows
    pub fn is_empty(&self) -> bool {
        self.rows.is_empty()
    }
}

/// Streaming query configuration
#[derive(Debug, Clone)]
pub struct StreamConfig {
    /// Number of rows per chunk
    pub chunk_size: usize,
    /// Output format: json, csv, tsv, table
    pub format: StreamFormat,
    /// Show progress indicator
    pub show_progress: bool,
    /// Maximum rows to stream (None = unlimited)
    pub max_rows: Option<usize>,
    /// Delay between chunks in milliseconds (for rate-limiting)
    pub chunk_delay_ms: u64,
    /// Timeout per chunk in seconds
    pub chunk_timeout_secs: u64,
}

impl Default for StreamConfig {
    fn default() -> Self {
        Self {
            chunk_size: 1000,
            format: StreamFormat::Table,
            show_progress: true,
            max_rows: None,
            chunk_delay_ms: 0,
            chunk_timeout_secs: 30,
        }
    }
}

impl StreamConfig {
    /// Parse from CLI arguments
    pub fn from_args(
        chunk_size: usize,
        format: &str,
        max_rows: Option<usize>,
        no_progress: bool,
    ) -> Result<Self> {
        let stream_format = StreamFormat::parse(format)?;
        Ok(Self {
            chunk_size,
            format: stream_format,
            show_progress: !no_progress,
            max_rows,
            chunk_delay_ms: 0,
            chunk_timeout_secs: 30,
        })
    }
}

/// Supported streaming output formats
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum StreamFormat {
    /// Newline-delimited JSON objects
    Json,
    /// Comma-separated values
    Csv,
    /// Tab-separated values
    Tsv,
    /// ASCII table (default)
    Table,
}

impl StreamFormat {
    /// Parse format string to StreamFormat
    pub fn parse(s: &str) -> Result<Self> {
        match s.to_lowercase().as_str() {
            "json" | "ndjson" => Ok(Self::Json),
            "csv" => Ok(Self::Csv),
            "tsv" => Ok(Self::Tsv),
            "table" | "text" => Ok(Self::Table),
            other => Err(anyhow!(
                "Unknown stream format '{}'. Valid: json, csv, tsv, table",
                other
            )),
        }
    }

    /// File extension for this format
    pub fn extension(&self) -> &'static str {
        match self {
            Self::Json => "ndjson",
            Self::Csv => "csv",
            Self::Tsv => "tsv",
            Self::Table => "txt",
        }
    }
}

/// Statistics collected during a streaming operation
#[derive(Debug, Clone, Serialize, Deserialize, Default)]
pub struct StreamStats {
    /// Total rows received
    pub total_rows: usize,
    /// Total chunks processed
    pub total_chunks: usize,
    /// Total bytes written
    pub total_bytes: usize,
    /// Elapsed time in milliseconds
    pub elapsed_ms: u64,
    /// Average rows per second
    pub rows_per_second: f64,
    /// Peak chunk processing time in milliseconds
    pub peak_chunk_ms: u64,
    /// Average chunk processing time in milliseconds
    pub avg_chunk_ms: f64,
}

impl StreamStats {
    /// Finalize stats with elapsed time
    pub fn finalize(&mut self, elapsed: Duration) {
        self.elapsed_ms = elapsed.as_millis() as u64;
        if self.elapsed_ms > 0 {
            self.rows_per_second = self.total_rows as f64 / (self.elapsed_ms as f64 / 1000.0);
        }
        if self.total_chunks > 0 {
            self.avg_chunk_ms = self.elapsed_ms as f64 / self.total_chunks as f64;
        }
    }
}

/// SPARQL streaming query executor
pub struct StreamingQueryExecutor {
    config: StreamConfig,
}

impl StreamingQueryExecutor {
    /// Create a new streaming query executor
    pub fn new(config: StreamConfig) -> Self {
        Self { config }
    }

    /// Stream query results from an in-memory result set (simulated chunking)
    ///
    /// In production, this would connect directly to the SPARQL engine's result iterator.
    /// Here we simulate by chunking an already-retrieved result set.
    pub fn stream_results(
        &self,
        variables: Vec<String>,
        all_rows: Vec<BindingRow>,
        writer: &mut dyn std::io::Write,
    ) -> Result<StreamStats> {
        let start = Instant::now();
        let mut stats = StreamStats::default();
        let total = match self.config.max_rows {
            Some(max) => all_rows.len().min(max),
            None => all_rows.len(),
        };

        // Setup progress bar
        let pb = if self.config.show_progress {
            let pb = ProgressBar::new(total as u64);
            pb.set_style(
                ProgressStyle::default_bar()
                    .template(
                        "{spinner:.green} [{bar:50.cyan/blue}] {pos}/{len} rows ({per_sec:.1} rows/s) [{elapsed_precise}]"
                    )
                    .unwrap_or_else(|_| ProgressStyle::default_bar())
                    .progress_chars("=>-"),
            );
            pb.enable_steady_tick(Duration::from_millis(100));
            Some(pb)
        } else {
            None
        };

        // Write header for CSV/TSV
        match self.config.format {
            StreamFormat::Csv | StreamFormat::Tsv => {
                let sep = if self.config.format == StreamFormat::Csv {
                    ","
                } else {
                    "\t"
                };
                let header = variables.join(sep);
                let bytes = header.len() + 1;
                writeln!(writer, "{}", header)?;
                stats.total_bytes += bytes;
            }
            StreamFormat::Table => {
                let header = self.format_table_header(&variables);
                let bytes = header.len();
                write!(writer, "{}", header)?;
                stats.total_bytes += bytes;
            }
            StreamFormat::Json => {
                // No header for NDJSON
            }
        }

        let chunk_size = self.config.chunk_size;
        let mut total_received = 0;

        for (chunk_index, chunk_rows) in all_rows[..total].chunks(chunk_size).enumerate() {
            let chunk_start = Instant::now();
            let is_final = total_received + chunk_rows.len() >= total;

            let chunk = ResultChunk::new(
                chunk_index,
                variables.clone(),
                chunk_rows.to_vec(),
                is_final,
                total_received + chunk_rows.len(),
            );

            let bytes = self.write_chunk(&chunk, writer)?;
            let chunk_elapsed = chunk_start.elapsed().as_millis() as u64;

            // Update stats
            stats.total_rows += chunk.len();
            stats.total_chunks += 1;
            stats.total_bytes += bytes;
            if chunk_elapsed > stats.peak_chunk_ms {
                stats.peak_chunk_ms = chunk_elapsed;
            }

            total_received += chunk.len();

            // Update progress
            if let Some(ref pb) = pb {
                pb.set_position(total_received as u64);
            }

            // Optional delay for rate-limiting
            if self.config.chunk_delay_ms > 0 {
                std::thread::sleep(Duration::from_millis(self.config.chunk_delay_ms));
            }
        }

        if let Some(pb) = pb {
            pb.finish_with_message(format!("Streamed {} rows", stats.total_rows));
        }

        // Write table footer
        if self.config.format == StreamFormat::Table {
            let footer = self.format_table_footer(&variables, stats.total_rows);
            let bytes = footer.len();
            write!(writer, "{}", footer)?;
            stats.total_bytes += bytes;
        }

        stats.finalize(start.elapsed());
        Ok(stats)
    }

    /// Write a single chunk in the configured format
    fn write_chunk(&self, chunk: &ResultChunk, writer: &mut dyn std::io::Write) -> Result<usize> {
        let mut total_bytes = 0usize;
        match self.config.format {
            StreamFormat::Json => {
                for row in &chunk.rows {
                    let json = serde_json::to_string(&row.bindings)?;
                    let bytes = json.len() + 1;
                    writeln!(writer, "{}", json)?;
                    total_bytes += bytes;
                }
            }
            StreamFormat::Csv => {
                for row in &chunk.rows {
                    let line = chunk
                        .variables
                        .iter()
                        .map(|v| {
                            let val = row.get(v).unwrap_or("");
                            // Escape CSV if necessary
                            if val.contains(',') || val.contains('"') || val.contains('\n') {
                                format!("\"{}\"", val.replace('"', "\"\""))
                            } else {
                                val.to_string()
                            }
                        })
                        .collect::<Vec<_>>()
                        .join(",");
                    let bytes = line.len() + 1;
                    writeln!(writer, "{}", line)?;
                    total_bytes += bytes;
                }
            }
            StreamFormat::Tsv => {
                for row in &chunk.rows {
                    let line = chunk
                        .variables
                        .iter()
                        .map(|v| row.get(v).unwrap_or("").replace('\t', " "))
                        .collect::<Vec<_>>()
                        .join("\t");
                    let bytes = line.len() + 1;
                    writeln!(writer, "{}", line)?;
                    total_bytes += bytes;
                }
            }
            StreamFormat::Table => {
                // Compute column widths
                let col_widths: Vec<usize> = chunk
                    .variables
                    .iter()
                    .map(|v| {
                        let max_val = chunk
                            .rows
                            .iter()
                            .map(|r| r.get(v).unwrap_or("").len())
                            .max()
                            .unwrap_or(0);
                        max_val.max(v.len()).min(80)
                    })
                    .collect();

                for row in &chunk.rows {
                    let line = chunk
                        .variables
                        .iter()
                        .zip(col_widths.iter())
                        .map(|(v, &w)| {
                            let val = row.get(v).unwrap_or("");
                            let truncated = if val.len() > w {
                                format!("{}...", &val[..w.saturating_sub(3)])
                            } else {
                                val.to_string()
                            };
                            format!("{:<width$}", truncated, width = w)
                        })
                        .collect::<Vec<_>>()
                        .join(" | ");
                    let bytes = line.len() + 1;
                    writeln!(writer, "| {} |", line)?;
                    total_bytes += bytes;
                }
            }
        }
        Ok(total_bytes)
    }

    /// Format table header line
    fn format_table_header(&self, variables: &[String]) -> String {
        let col_widths: Vec<usize> = variables.iter().map(|v| v.len().clamp(10, 80)).collect();

        let header_row = variables
            .iter()
            .zip(col_widths.iter())
            .map(|(v, &w)| format!("{:<width$}", v, width = w))
            .collect::<Vec<_>>()
            .join(" | ");

        let separator = col_widths
            .iter()
            .map(|&w| "-".repeat(w))
            .collect::<Vec<_>>()
            .join("-+-");

        format!("| {} |\n+-{}-+\n", header_row, separator)
    }

    /// Format table footer with count
    fn format_table_footer(&self, variables: &[String], total: usize) -> String {
        let col_widths: Vec<usize> = variables.iter().map(|v| v.len().clamp(10, 80)).collect();
        let separator = col_widths
            .iter()
            .map(|&w| "-".repeat(w))
            .collect::<Vec<_>>()
            .join("-+-");
        format!("+-{}-+\n{} rows total\n", separator, total)
    }
}

/// Execute the stream command
#[allow(clippy::too_many_arguments)]
pub async fn run_stream_command(
    dataset: String,
    query: String,
    is_file: bool,
    chunk_size: usize,
    format: String,
    max_rows: Option<usize>,
    no_progress: bool,
    output: Option<std::path::PathBuf>,
) -> Result<()> {
    let ctx = CliContext::new();
    ctx.info(&format!("Streaming SPARQL query on dataset '{}'", dataset));

    // Load query
    let sparql_query = if is_file {
        std::fs::read_to_string(&query)
            .map_err(|e| anyhow!("Failed to read query file '{}': {}", query, e))?
    } else {
        query
    };

    // Build stream config
    let stream_config = StreamConfig::from_args(chunk_size, &format, max_rows, no_progress)?;

    // Load dataset and execute query
    let dataset_path = std::path::PathBuf::from(&dataset);
    let data_file = dataset_path.join("data.nq");

    // Create result rows from actual dataset
    let (variables, rows) = if data_file.exists() {
        load_and_execute_query(&data_file, &sparql_query)?
    } else {
        // No data file - return empty result with query variables
        let vars = extract_select_variables(&sparql_query);
        (vars, Vec::new())
    };

    ctx.info(&format!(
        "Executing query, streaming in chunks of {} rows...",
        stream_config.chunk_size
    ));

    // Set up output writer
    let executor = StreamingQueryExecutor::new(stream_config);

    let stats = if let Some(output_path) = output {
        let mut file = std::fs::File::create(&output_path).map_err(|e| {
            anyhow!(
                "Failed to create output file '{}': {}",
                output_path.display(),
                e
            )
        })?;
        executor.stream_results(variables, rows, &mut file)?
    } else {
        let stdout = std::io::stdout();
        let mut out = stdout.lock();
        executor.stream_results(variables, rows, &mut out)?
    };

    // Print summary
    println!();
    println!("{}", "Streaming complete:".green().bold());
    println!("  Total rows:    {}", stats.total_rows.to_string().cyan());
    println!("  Chunks:        {}", stats.total_chunks.to_string().cyan());
    println!(
        "  Bytes written: {}",
        format_bytes(stats.total_bytes).cyan()
    );
    println!("  Elapsed:       {:.2}s", stats.elapsed_ms as f64 / 1000.0);
    println!("  Throughput:    {:.1} rows/s", stats.rows_per_second);

    Ok(())
}

/// Load dataset and execute SPARQL query, returning variables and rows
fn load_and_execute_query(
    data_file: &std::path::Path,
    _query: &str,
) -> Result<(Vec<String>, Vec<BindingRow>)> {
    // In production this would use oxirs-arq to execute the query.
    // For now, read the N-Quads file and return subject/predicate/object rows.
    use std::io::{BufRead, BufReader};

    let file =
        std::fs::File::open(data_file).map_err(|e| anyhow!("Failed to open data file: {}", e))?;
    let reader = BufReader::new(file);

    let variables = vec!["s".to_string(), "p".to_string(), "o".to_string()];
    let mut rows = Vec::new();

    for line in reader.lines() {
        let line = line.map_err(|e| anyhow!("IO error reading data: {}", e))?;
        let trimmed = line.trim();
        if trimmed.is_empty() || trimmed.starts_with('#') {
            continue;
        }

        // Parse N-Quads line: <s> <p> <o> [<g>] .
        let parts: Vec<&str> = trimmed.splitn(5, ' ').collect();
        if parts.len() >= 3 {
            let mut bindings = HashMap::new();
            bindings.insert("s".to_string(), parts[0].to_string());
            bindings.insert("p".to_string(), parts[1].to_string());
            bindings.insert("o".to_string(), parts[2].to_string());
            rows.push(BindingRow::new(bindings));
        }
    }

    Ok((variables, rows))
}

/// Extract variable names from a SELECT query
fn extract_select_variables(query: &str) -> Vec<String> {
    let upper = query.to_uppercase();
    if let Some(select_pos) = upper.find("SELECT") {
        if let Some(where_pos) = upper.find("WHERE") {
            let projection = &query[select_pos + 6..where_pos].trim().to_string();
            if projection.contains('*') {
                return vec!["s".to_string(), "p".to_string(), "o".to_string()];
            }
            return projection
                .split_whitespace()
                .filter(|s| s.starts_with('?'))
                .map(|s| s[1..].to_string())
                .collect();
        }
    }
    vec!["s".to_string(), "p".to_string(), "o".to_string()]
}

/// Format byte count with appropriate unit
fn format_bytes(bytes: usize) -> String {
    const KB: usize = 1024;
    const MB: usize = KB * 1024;
    const GB: usize = MB * 1024;

    if bytes >= GB {
        format!("{:.2} GB", bytes as f64 / GB as f64)
    } else if bytes >= MB {
        format!("{:.2} MB", bytes as f64 / MB as f64)
    } else if bytes >= KB {
        format!("{:.2} KB", bytes as f64 / KB as f64)
    } else {
        format!("{} B", bytes)
    }
}

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

    fn make_rows(n: usize) -> (Vec<String>, Vec<BindingRow>) {
        let vars = vec![
            "subject".to_string(),
            "predicate".to_string(),
            "object".to_string(),
        ];
        let rows = (0..n)
            .map(|i| {
                let mut m = HashMap::new();
                m.insert("subject".to_string(), format!("<http://ex.org/s{}>", i));
                m.insert("predicate".to_string(), "<http://ex.org/p>".to_string());
                m.insert("object".to_string(), format!("\"value{}\"", i));
                BindingRow::new(m)
            })
            .collect();
        (vars, rows)
    }

    #[test]
    fn test_stream_format_parse() {
        assert_eq!(StreamFormat::parse("json").unwrap(), StreamFormat::Json);
        assert_eq!(StreamFormat::parse("csv").unwrap(), StreamFormat::Csv);
        assert_eq!(StreamFormat::parse("tsv").unwrap(), StreamFormat::Tsv);
        assert_eq!(StreamFormat::parse("table").unwrap(), StreamFormat::Table);
        assert_eq!(StreamFormat::parse("ndjson").unwrap(), StreamFormat::Json);
        assert!(StreamFormat::parse("xml").is_err());
    }

    #[test]
    fn test_stream_format_extension() {
        assert_eq!(StreamFormat::Json.extension(), "ndjson");
        assert_eq!(StreamFormat::Csv.extension(), "csv");
        assert_eq!(StreamFormat::Tsv.extension(), "tsv");
        assert_eq!(StreamFormat::Table.extension(), "txt");
    }

    #[test]
    fn test_binding_row_get() {
        let mut map = HashMap::new();
        map.insert("x".to_string(), "hello".to_string());
        let row = BindingRow::new(map);
        assert_eq!(row.get("x"), Some("hello"));
        assert_eq!(row.get("y"), None);
    }

    #[test]
    fn test_result_chunk_len() {
        let (vars, rows) = make_rows(5);
        let chunk = ResultChunk::new(0, vars, rows, true, 5);
        assert_eq!(chunk.len(), 5);
        assert!(!chunk.is_empty());
    }

    #[test]
    fn test_result_chunk_empty() {
        let vars = vec!["s".to_string()];
        let chunk = ResultChunk::new(0, vars, vec![], true, 0);
        assert!(chunk.is_empty());
    }

    #[test]
    fn test_stream_config_default() {
        let config = StreamConfig::default();
        assert_eq!(config.chunk_size, 1000);
        assert_eq!(config.format, StreamFormat::Table);
        assert!(config.show_progress);
        assert!(config.max_rows.is_none());
    }

    #[test]
    fn test_stream_config_from_args() {
        let config = StreamConfig::from_args(500, "json", Some(10000), true).unwrap();
        assert_eq!(config.chunk_size, 500);
        assert_eq!(config.format, StreamFormat::Json);
        assert_eq!(config.max_rows, Some(10000));
        assert!(!config.show_progress);
    }

    #[test]
    fn test_stream_json_output() {
        let (vars, rows) = make_rows(3);
        let config = StreamConfig {
            chunk_size: 10,
            format: StreamFormat::Json,
            show_progress: false,
            max_rows: None,
            chunk_delay_ms: 0,
            chunk_timeout_secs: 30,
        };
        let executor = StreamingQueryExecutor::new(config);
        let mut buf = Cursor::new(Vec::new());
        let stats = executor.stream_results(vars, rows, &mut buf).unwrap();
        assert_eq!(stats.total_rows, 3);
        assert_eq!(stats.total_chunks, 1);

        let output = String::from_utf8(buf.into_inner()).unwrap();
        let lines: Vec<&str> = output.lines().collect();
        assert_eq!(lines.len(), 3);
        // Each line should be valid JSON
        for line in lines {
            let parsed: serde_json::Value = serde_json::from_str(line).unwrap();
            assert!(parsed.is_object());
        }
    }

    #[test]
    fn test_stream_csv_output() {
        let (vars, rows) = make_rows(5);
        let config = StreamConfig {
            chunk_size: 2,
            format: StreamFormat::Csv,
            show_progress: false,
            max_rows: None,
            chunk_delay_ms: 0,
            chunk_timeout_secs: 30,
        };
        let executor = StreamingQueryExecutor::new(config);
        let mut buf = Cursor::new(Vec::new());
        let stats = executor.stream_results(vars, rows, &mut buf).unwrap();
        assert_eq!(stats.total_rows, 5);
        assert_eq!(stats.total_chunks, 3); // ceil(5/2)

        let output = String::from_utf8(buf.into_inner()).unwrap();
        let lines: Vec<&str> = output.lines().collect();
        // header + 5 rows = 6 lines
        assert_eq!(lines.len(), 6);
        assert_eq!(lines[0], "subject,predicate,object");
    }

    #[test]
    fn test_stream_tsv_output() {
        let (vars, rows) = make_rows(2);
        let config = StreamConfig {
            chunk_size: 100,
            format: StreamFormat::Tsv,
            show_progress: false,
            max_rows: None,
            chunk_delay_ms: 0,
            chunk_timeout_secs: 30,
        };
        let executor = StreamingQueryExecutor::new(config);
        let mut buf = Cursor::new(Vec::new());
        let stats = executor.stream_results(vars, rows, &mut buf).unwrap();
        assert_eq!(stats.total_rows, 2);

        let output = String::from_utf8(buf.into_inner()).unwrap();
        let lines: Vec<&str> = output.lines().collect();
        assert_eq!(lines.len(), 3); // header + 2 rows
        assert!(lines[0].contains('\t'));
    }

    #[test]
    fn test_stream_table_output() {
        let (vars, rows) = make_rows(4);
        let config = StreamConfig {
            chunk_size: 100,
            format: StreamFormat::Table,
            show_progress: false,
            max_rows: None,
            chunk_delay_ms: 0,
            chunk_timeout_secs: 30,
        };
        let executor = StreamingQueryExecutor::new(config);
        let mut buf = Cursor::new(Vec::new());
        let stats = executor.stream_results(vars, rows, &mut buf).unwrap();
        assert_eq!(stats.total_rows, 4);

        let output = String::from_utf8(buf.into_inner()).unwrap();
        assert!(output.contains("subject"));
        assert!(output.contains("predicate"));
        assert!(output.contains("4 rows total"));
    }

    #[test]
    fn test_stream_max_rows_limit() {
        let (vars, rows) = make_rows(100);
        let config = StreamConfig {
            chunk_size: 10,
            format: StreamFormat::Json,
            show_progress: false,
            max_rows: Some(25),
            chunk_delay_ms: 0,
            chunk_timeout_secs: 30,
        };
        let executor = StreamingQueryExecutor::new(config);
        let mut buf = Cursor::new(Vec::new());
        let stats = executor.stream_results(vars, rows, &mut buf).unwrap();
        assert_eq!(stats.total_rows, 25);
    }

    #[test]
    fn test_stream_empty_results() {
        let vars = vec!["s".to_string(), "p".to_string(), "o".to_string()];
        let rows: Vec<BindingRow> = vec![];
        let config = StreamConfig {
            chunk_size: 10,
            format: StreamFormat::Csv,
            show_progress: false,
            max_rows: None,
            chunk_delay_ms: 0,
            chunk_timeout_secs: 30,
        };
        let executor = StreamingQueryExecutor::new(config);
        let mut buf = Cursor::new(Vec::new());
        let stats = executor.stream_results(vars, rows, &mut buf).unwrap();
        assert_eq!(stats.total_rows, 0);
        assert_eq!(stats.total_chunks, 0);
    }

    #[test]
    fn test_stream_stats_finalize() {
        let mut stats = StreamStats {
            total_rows: 1000,
            total_chunks: 10,
            ..Default::default()
        };
        stats.finalize(Duration::from_secs(2));
        assert_eq!(stats.elapsed_ms, 2000);
        assert!((stats.rows_per_second - 500.0).abs() < 1.0);
        assert!((stats.avg_chunk_ms - 200.0).abs() < 1.0);
    }

    #[test]
    fn test_format_bytes() {
        assert_eq!(format_bytes(512), "512 B");
        assert_eq!(format_bytes(1024), "1.00 KB");
        assert_eq!(format_bytes(1_048_576), "1.00 MB");
        assert_eq!(format_bytes(1_073_741_824), "1.00 GB");
    }

    #[test]
    fn test_extract_select_variables() {
        let q = "SELECT ?name ?age WHERE { ?s ?p ?o }";
        let vars = extract_select_variables(q);
        assert!(vars.contains(&"name".to_string()));
        assert!(vars.contains(&"age".to_string()));
    }

    #[test]
    fn test_extract_select_variables_star() {
        let q = "SELECT * WHERE { ?s ?p ?o }";
        let vars = extract_select_variables(q);
        assert_eq!(vars, vec!["s", "p", "o"]);
    }

    #[test]
    fn test_csv_escape_special_chars() {
        let vars = vec!["value".to_string()];
        let mut map = HashMap::new();
        map.insert("value".to_string(), "hello, world".to_string());
        let rows = vec![BindingRow::new(map)];

        let config = StreamConfig {
            chunk_size: 10,
            format: StreamFormat::Csv,
            show_progress: false,
            max_rows: None,
            chunk_delay_ms: 0,
            chunk_timeout_secs: 30,
        };
        let executor = StreamingQueryExecutor::new(config);
        let mut buf = Cursor::new(Vec::new());
        executor.stream_results(vars, rows, &mut buf).unwrap();

        let output = String::from_utf8(buf.into_inner()).unwrap();
        // Should be quoted because of comma
        assert!(output.contains("\"hello, world\""));
    }

    #[test]
    fn test_chunking_large_result_set() {
        let (vars, rows) = make_rows(1000);
        let config = StreamConfig {
            chunk_size: 100,
            format: StreamFormat::Json,
            show_progress: false,
            max_rows: None,
            chunk_delay_ms: 0,
            chunk_timeout_secs: 30,
        };
        let executor = StreamingQueryExecutor::new(config);
        let mut buf = Cursor::new(Vec::new());
        let stats = executor.stream_results(vars, rows, &mut buf).unwrap();
        assert_eq!(stats.total_rows, 1000);
        assert_eq!(stats.total_chunks, 10);
    }

    #[test]
    fn test_stream_config_invalid_format() {
        assert!(StreamConfig::from_args(100, "xml", None, false).is_err());
        assert!(StreamConfig::from_args(100, "rdf", None, false).is_err());
    }

    #[test]
    fn test_stream_stats_zero_elapsed() {
        let mut stats = StreamStats {
            total_rows: 0,
            total_chunks: 0,
            ..Default::default()
        };
        stats.finalize(Duration::from_millis(0));
        assert_eq!(stats.rows_per_second, 0.0);
        assert_eq!(stats.avg_chunk_ms, 0.0);
    }

    #[test]
    fn test_binding_row_new() {
        let mut m = HashMap::new();
        m.insert("foo".to_string(), "bar".to_string());
        let row = BindingRow::new(m.clone());
        assert_eq!(row.bindings, m);
    }
}