cqlite-cli 0.11.0

Command-line interface for CQLite — read Apache Cassandra 5.0 SSTables without a cluster
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
//! Query executor for running SELECT queries against real SSTable data
//!
//! This module provides functionality to execute CQL SELECT queries directly
//! against SSTable files, showing live table data instead of mocked data.

use anyhow::{Context, Result};
use cqlite_core::{
    schema::TableSchema,
    storage::sstable::{reader::SSTableReader, bulletproof_reader::BulletproofReader},
};
use std::path::Path;
use std::sync::Arc;

use crate::data_parser::{RealDataParser, ParsedRow, ParsedValue};
use crate::formatter::CqlshTableFormatter;

/// Configuration for query executor performance settings
#[derive(Debug, Clone)]
pub struct QueryExecutorConfig {
    /// Pagination configuration
    // pub pagination: PaginationConfig,
    /// Enable streaming processing for large datasets
    pub enable_streaming: bool,
    /// Cache size for frequently accessed data
    pub cache_size_mb: usize,
    /// I/O timeout in milliseconds
    pub io_timeout_ms: u64,
}

impl Default for QueryExecutorConfig {
    fn default() -> Self {
        Self {
            // pagination: PaginationConfig::default(),
            enable_streaming: true,
            cache_size_mb: 50,
            io_timeout_ms: 30000,
        }
    }
}

/// Query executor for running SELECT statements against SSTable data
pub struct QueryExecutor {
    /// Table schema for data interpretation
    schema: TableSchema,
    /// Bulletproof SSTable reader for data access
    reader: BulletproofReader,
    /// Data parser for converting binary data to readable format
    parser: RealDataParser,
    /// Performance configuration
    config: QueryExecutorConfig,
}

/// Query result containing parsed rows
pub struct QueryResult {
    /// Column names in order
    pub columns: Vec<String>,
    /// Parsed rows
    pub rows: Vec<ParsedRow>,
    /// Total rows found before limit
    pub total_rows: usize,
    /// Execution time in milliseconds
    pub execution_time_ms: u64,
}

impl QueryExecutor {
    /// Create a new query executor
    pub async fn new(sstable_path: &Path, schema: TableSchema) -> Result<Self> {
        Self::with_config(sstable_path, schema, QueryExecutorConfig::default()).await
    }

    /// Create a new query executor with custom configuration
    pub async fn with_config(sstable_path: &Path, schema: TableSchema, executor_config: QueryExecutorConfig) -> Result<Self> {
        let reader = BulletproofReader::open(sstable_path)
            .with_context(|| format!("Failed to open SSTable with bulletproof reader: {}", sstable_path.display()))?;

        let parser = RealDataParser::new(schema.clone());

        Ok(Self {
            schema,
            reader,
            parser,
            config: executor_config,
        })
    }

    /// Execute a SELECT query with streaming for better performance (simplified version)
    pub async fn execute_select_streaming(&mut self, query: &str) -> Result<QueryResult> {
        // For now, just use the regular execute_select method
        // Streaming features will be re-enabled when pagination is implemented
        self.execute_select(query).await
    }

    /// Execute a SELECT query against the SSTable data using bulletproof reader
    pub async fn execute_select(&mut self, query: &str) -> Result<QueryResult> {
        let start_time = std::time::Instant::now();
        
        // Parse the query (simplified - just handle basic SELECT queries)
        let query_info = self.parse_select_query(query)?;
        
        println!("🔍 Executing query against live SSTable data...");
        println!("📝 Query: {}", query);
        println!("📊 Columns requested: {}", query_info.columns.join(", "));
        
        // Get all entries from SSTable using bulletproof reader
        let entries = self.reader.parse_sstable_data()?;
        let mut matching_rows = Vec::new();
        let mut processed = 0;

        println!("📋 Scanning {} entries in SSTable...", entries.len());

        for entry in entries {
            processed += 1;
            
            // Parse the entry using bulletproof reader data
            match self.parser.parse_bulletproof_entry(&entry) {
                Ok(parsed_row) => {
                    // Apply WHERE clause if present
                    if query_info.where_clause.is_none() || self.matches_where(&parsed_row, &query_info.where_clause)? {
                        matching_rows.push(parsed_row);
                    }
                }
                Err(e) => {
                    eprintln!("⚠️  Failed to parse row {}: {}", processed, e);
                }
            }

            // Apply LIMIT if specified
            if let Some(limit) = query_info.limit {
                if matching_rows.len() >= limit {
                    break;
                }
            }
        }

        let execution_time = start_time.elapsed();
        
        println!("✅ Query completed in {:.2}ms", execution_time.as_millis());
        println!("   Processed {} entries, found {} matching rows", processed, matching_rows.len());

        Ok(QueryResult {
            columns: query_info.columns,
            rows: matching_rows,
            total_rows: processed,
            execution_time_ms: execution_time.as_millis() as u64,
        })
    }

    /// Get table schema
    pub fn schema(&self) -> &TableSchema {
        &self.schema
    }

    /// Process entries using streaming for memory efficiency
    /* Temporarily disabled - streaming pending
    async fn process_entries_streaming(
        &self,
        entries: Vec<(u64, Vec<u8>, Vec<u8>)>,
        query_info: &QueryInfo,
        progress: &PaginationProgress,
    ) -> Result<Vec<ParsedRow>> {
        // Create streaming processor
        let processor = StreamingProcessor::new(
            |entry: &(u64, Vec<u8>, Vec<u8>)| -> Result<Option<ParsedRow>> {
                let (_table_id, key, value) = entry;
                
                // Parse the entry
                match self.parser.parse_entry(key, value) {
                    Ok(parsed_row) => {
                        // Apply WHERE clause if present
                        if query_info.where_clause.is_none() || 
                           self.matches_where(&parsed_row, &query_info.where_clause)? {
                            Ok(Some(parsed_row))
                        } else {
                            Ok(None)
                        }
                    }
                    Err(_) => Ok(None), // Skip failed rows
                }
            },
            self.config.pagination.clone(),
        );

        // Process entries in stream
        let results = processor.process_stream(entries).await?;
        
        // Filter out None results and apply LIMIT
        let mut matching_rows: Vec<ParsedRow> = results.into_iter().filter_map(|r| r).collect();
        
        // Apply pagination limits
        let skip = self.config.pagination.skip;
        let limit = query_info.limit.or(self.config.pagination.limit);
        
        if skip > 0 {
            matching_rows = matching_rows.into_iter().skip(skip).collect();
        }
        
        if let Some(limit_count) = limit {
            matching_rows.truncate(limit_count);
        }

        Ok(matching_rows)
    }

    /// Process entries sequentially (fallback method)
    async fn process_entries_sequential(
        &self,
        entries: Vec<(u64, Vec<u8>, Vec<u8>)>,
        query_info: &QueryInfo,
        progress: &PaginationProgress,
    ) -> Result<Vec<ParsedRow>> {
        let mut matching_rows = Vec::new();
        let mut processed = 0;
        let skip = self.config.pagination.skip;
        let limit = query_info.limit.or(self.config.pagination.limit);

        for (_table_id, key, value) in entries {
            progress.update(processed as u64, None);
            processed += 1;

            // Parse the entry
            match self.parser.parse_entry(&key, &value) {
                Ok(parsed_row) => {
                    // Apply WHERE clause if present
                    if query_info.where_clause.is_none() || 
                       self.matches_where(&parsed_row, &query_info.where_clause)? {
                        
                        // Apply skip
                        if matching_rows.len() < skip {
                            continue;
                        }
                        
                        matching_rows.push(parsed_row);
                        
                        // Apply LIMIT if specified
                        if let Some(limit_count) = limit {
                            if matching_rows.len() >= limit_count + skip {
                                break;
                            }
                        }
                    }
                }
                Err(e) => {
                    eprintln!("⚠️  Failed to parse row {}: {}", processed, e);
                }
            }
        }

        // Remove skipped rows from the result
        if skip > 0 && matching_rows.len() > skip {
            matching_rows = matching_rows.into_iter().skip(skip).collect();
        }

        Ok(matching_rows)
    }
    */ // End streaming methods

    /// Parse a SELECT query (simplified implementation)
    fn parse_select_query(&self, query: &str) -> Result<QueryInfo> {
        let query = query.trim();
        let query_upper = query.to_uppercase();

        if !query_upper.starts_with("SELECT") {
            return Err(anyhow::anyhow!("Only SELECT queries are supported"));
        }

        // Extract columns (simplified parsing)
        let columns = if query_upper.contains("SELECT *") {
            self.parser.get_column_names()
        } else {
            // Try to extract column names between SELECT and FROM
            let select_part = query_upper
                .split("FROM")
                .next()
                .unwrap_or("")
                .strip_prefix("SELECT")
                .unwrap_or("")
                .trim();
            
            if select_part.is_empty() || select_part == "*" {
                self.parser.get_column_names()
            } else {
                select_part
                    .split(',')
                    .map(|col| col.trim().to_lowercase())
                    .collect()
            }
        };

        // Extract WHERE clause (simplified)
        let where_clause = if query_upper.contains("WHERE") {
            let where_part = query
                .split_whitespace()
                .skip_while(|&word| word.to_uppercase() != "WHERE")
                .skip(1)
                .collect::<Vec<_>>()
                .join(" ");
            
            if where_part.is_empty() {
                None
            } else {
                Some(where_part)
            }
        } else {
            None
        };

        // Extract LIMIT (simplified)
        let limit = if query_upper.contains("LIMIT") {
            let limit_part = query
                .split_whitespace()
                .skip_while(|&word| word.to_uppercase() != "LIMIT")
                .nth(1)
                .and_then(|s| s.parse::<usize>().ok());
            limit_part
        } else {
            None
        };

        Ok(QueryInfo {
            columns,
            where_clause,
            limit,
        })
    }

    /* Temporarily disabled - scanner integration pending
    /// Create a ScanFilter from a WHERE clause
    fn create_scan_filter(&self, where_clause: &Option<String>) -> Result<ScanFilter> {
        match where_clause {
            None => {
                // Empty filter that matches everything
                Ok(ScanFilter {
                    column_filters: std::collections::HashMap::new(),
                    operator: FilterOperator::And,
                })
            }
            Some(clause) => {
                use std::collections::HashMap;
                let mut column_filters = HashMap::new();
                
                // Very simplified WHERE clause parsing
                if clause.contains('=') {
                    let parts: Vec<&str> = clause.split('=').collect();
                    if parts.len() == 2 {
                        let column_name = parts[0].trim().to_lowercase();
                        let expected_value = parts[1].trim().trim_matches('\'').trim_matches('"');
                        
                        // Try to parse the value as appropriate type
                        let parsed_value = if let Ok(int_val) = expected_value.parse::<i64>() {
                            ParsedValue::Integer(int_val)
                        } else if let Ok(float_val) = expected_value.parse::<f64>() {
                            ParsedValue::Float(float_val)
                        } else if expected_value.eq_ignore_ascii_case("true") {
                            ParsedValue::Boolean(true)
                        } else if expected_value.eq_ignore_ascii_case("false") {
                            ParsedValue::Boolean(false)
                        } else if expected_value.eq_ignore_ascii_case("null") {
                            ParsedValue::Null
                        } else {
                            ParsedValue::Text(expected_value.to_string())
                        };
                        
                        column_filters.insert(column_name, ColumnFilter {
                            operator: ComparisonOperator::Equals,
                            value: parsed_value,
                        });
                    }
                }
                
                Ok(ScanFilter {
                    column_filters,
                    operator: FilterOperator::And,
                })
            }
        }
    }
    */ // End scanner integration

    /// Check if a row matches the WHERE clause (simplified implementation)
    fn matches_where(&self, row: &ParsedRow, where_clause: &Option<String>) -> Result<bool> {
        match where_clause {
            None => Ok(true),
            Some(clause) => {
                // Very simplified WHERE clause evaluation
                // In a real implementation, you'd parse this properly
                
                if clause.contains('=') {
                    let parts: Vec<&str> = clause.split('=').collect();
                    if parts.len() == 2 {
                        let column_name = parts[0].trim().to_lowercase();
                        let expected_value = parts[1].trim().trim_matches('\'').trim_matches('"');
                        
                        if let Some(actual_value) = row.get(&column_name) {
                            return Ok(actual_value.to_string() == expected_value);
                        }
                    }
                }
                
                // If we can't parse the WHERE clause, just return true
                println!("⚠️  Complex WHERE clauses not yet supported: {}", clause);
                Ok(true)
            }
        }
    }
}

/// Parsed query information
#[derive(Debug)]
struct QueryInfo {
    /// Columns to select
    columns: Vec<String>,
    /// WHERE clause if present
    where_clause: Option<String>,
    /// LIMIT if specified
    limit: Option<usize>,
}

impl QueryResult {
    /// Display the results in table format (cqlsh-compatible)
    pub fn display_table(&self) {
        let mut formatter = CqlshTableFormatter::new();
        formatter.set_headers(self.columns.clone());
        
        // Convert ParsedRow to Vec<Vec<String>>
        let string_rows: Vec<Vec<String>> = self.rows.iter()
            .map(|row| row.to_string_vec(&self.columns))
            .collect();
        formatter.add_rows(string_rows);
        
        let table_output = formatter.format();
        
        println!("{}", table_output);
        println!("✅ Query completed in {}ms", self.execution_time_ms);
    }

    /// Display the results in JSON format
    pub fn display_json(&self) -> Result<()> {
        let json_rows: Vec<serde_json::Value> = self.rows
            .iter()
            .map(|row| row.to_json())
            .collect();

        println!("{}", serde_json::to_string_pretty(&json_rows)?);
        println!("\n{} rows returned in {}ms", self.rows.len(), self.execution_time_ms);
        
        Ok(())
    }

    /// Display the results in CSV format
    pub fn display_csv(&self) -> Result<()> {
        let mut wtr = csv::Writer::from_writer(std::io::stdout());

        // Write header
        wtr.write_record(&self.columns)?;

        // Write data rows
        for parsed_row in &self.rows {
            let mut record = Vec::new();
            for column in &self.columns {
                let cell_value = parsed_row
                    .get(column)
                    .map(|v| v.to_string())
                    .unwrap_or_else(|| "NULL".to_string());
                record.push(cell_value);
            }
            wtr.write_record(&record)?;
        }

        wtr.flush()?;
        eprintln!("\n{} rows returned in {}ms", self.rows.len(), self.execution_time_ms);
        
        Ok(())
    }
}