sql-cli 1.73.1

SQL query tool for CSV/JSON with both interactive TUI and non-interactive CLI modes - perfect for exploration and automation
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
# Debugging SQL Transformers

## Overview

SQL CLI provides powerful debugging tools to understand how queries are transformed before execution. This is critical when adding new transformers or debugging unexpected query behavior.

## Quick Start

```bash
# Show SQL transformations (recommended for understanding transformers)
./target/release/sql-cli -q "SELECT UPPER(region) AS r FROM sales GROUP BY r" \
    -d data/sales_data.csv \
    --show-transformations

# Works in script mode too
./target/release/sql-cli -f examples/expander_rewriters.sql --show-transformations
```

## Available Debugging Flags

### `--show-transformations` (NEW - Recommended)

**Purpose:** Shows the SQL before and after each transformation in a readable format.

**Output:**  Beautiful boxed display showing exactly what each transformer did.

**Example:**
```bash
./target/release/sql-cli -q "SELECT sales_amount * 1.1 AS adj FROM sales WHERE adj > 20000" \
    -d data/sales_data.csv \
    --show-transformations
```

**Output:**
```
╔════════════════════════════════════════════════════════════════╗
║ Transformer: WhereAliasExpander                                  ║
╠════════════════════════════════════════════════════════════════╣
║ BEFORE:                                                        ║
╠════════════════════════════════════════════════════════════════╣
  SELECT sales_amount * 1.1 AS adjusted
  FROM sales
  WHERE adjusted > 20000
╠════════════════════════════════════════════════════════════════╣
║ AFTER:                                                         ║
╠════════════════════════════════════════════════════════════════╣
  SELECT sales_amount * 1.1 AS adjusted
  FROM sales
  WHERE sales_amount * 1.1 > 20000
╚════════════════════════════════════════════════════════════════╝
```

**When to use:**
- ✅ Understanding what transformers are doing
- ✅ Debugging unexpected query behavior
- ✅ Learning how the preprocessor works
- ✅ Verifying transformer implementation

**When NOT to use:**
- ❌ Production queries (performance overhead)
- ❌ When output needs to be clean (debug output goes to stderr)

---

### `--show-preprocessing` (Existing)

**Purpose:** Shows internal verbose logging about the preprocessing pipeline.

**Output:** Log messages about which transformers are running and statistics.

**Example:**
```bash
./target/release/sql-cli -q "SELECT * FROM sales" \
    -d data/sales_data.csv \
    --show-preprocessing
```

**When to use:**
- Debugging pipeline configuration
- Seeing transformer statistics
- Understanding pipeline flow

---

### `--query-plan` (Existing)

**Purpose:** Shows the parsed AST structure before any transformations.

**Output:** Rust Debug format of the AST.

**Example:**
```bash
./target/release/sql-cli -q "SELECT * FROM sales WHERE region = 'North'" \
    -d data/sales_data.csv \
    --query-plan
```

**When to use:**
- Debugging parser issues
- Understanding AST structure
- Verifying query parsing

---

## Common Debugging Workflows

### Workflow 1: Understanding a Query Transformation

**Problem:** "Why is my query behaving differently than expected?"

**Solution:**
```bash
# Step 1: See what transformers are doing
./target/release/sql-cli -q "YOUR_QUERY" -d data.csv --show-transformations

# Step 2: If you need AST details
./target/release/sql-cli -q "YOUR_QUERY" -d data.csv --query-plan

# Step 3: Isolate which transformer is causing the issue
./target/release/sql-cli -q "YOUR_QUERY" -d data.csv --show-transformations \
    --no-expression-lifter \
    --no-where-expansion \
    # ... disable one at a time
```

### Workflow 2: Developing a New Transformer

**Problem:** "I'm adding a new transformer. How do I verify it works?"

**Solution:**
```bash
# Step 1: Write test query that should trigger your transformer
./target/release/sql-cli -q "YOUR_TEST_QUERY" -d data.csv --show-transformations

# Step 2: Verify the BEFORE/AFTER output shows your transformation

# Step 3: Test with complex queries
./target/release/sql-cli -f examples/expander_rewriters.sql --show-transformations

# Step 4: Add to examples file for regression testing
```

### Workflow 3: Debugging Transformer Interactions

**Problem:** "Transformers are interfering with each other"

**Solution:**
```bash
# Run with all transformers, note the order
./target/release/sql-cli -q "COMPLEX_QUERY" -d data.csv --show-transformations

# Disable transformers one by one to isolate
./target/release/sql-cli -q "COMPLEX_QUERY" -d data.csv --show-transformations --no-expression-lifter
./target/release/sql-cli -q "COMPLEX_QUERY" -d data.csv --show-transformations --no-where-expansion

# Check transformer order in pipeline (docs/PREPROCESSOR_TRANSFORMERS.md)
```

---

## Transformer Execution Order

Transformers run in this order (see pipeline.rs):

1. **ExpressionLifter** - Lifts complex expressions to CTEs
2. **WhereAliasExpander** - Expands aliases in WHERE
3. **GroupByAliasExpander** - Expands aliases in GROUP BY
4. **HavingAliasTransformer** - Auto-aliases aggregates in HAVING
5. **CTEHoister** - Hoists nested CTEs
6. **InOperatorLifter** - Optimizes IN lists

**Order matters!** ExpressionLifter must run before CTEHoister because it creates CTEs.

---

## Examples

### Example 1: WHERE Alias Expansion

```bash
./target/release/sql-cli -q "SELECT sales_amount * 1.1 AS adj FROM sales WHERE adj > 15000" \
    -d data/sales_data.csv \
    --show-transformations
```

**Shows:**
- Original query with `WHERE adj > 15000`
- Transformed to `WHERE sales_amount * 1.1 > 15000`

### Example 2: GROUP BY Alias Expansion

```bash
./target/release/sql-cli -q "SELECT UPPER(region) AS r, SUM(sales_amount) FROM sales GROUP BY r" \
    -d data/sales_data.csv \
    --show-transformations
```

**Shows:**
- Original query with `GROUP BY r`
- Transformed to `GROUP BY UPPER(region)`

### Example 3: HAVING Auto-Aliasing

```bash
./target/release/sql-cli -q "SELECT region, SUM(sales_amount) FROM sales GROUP BY region HAVING SUM(sales_amount) > 50000" \
    -d data/sales_data.csv \
    --show-transformations
```

**Shows:**
- Original query with `HAVING SUM(sales_amount) > 50000`
- Transformed to add alias to SELECT and use in HAVING

### Example 4: Expression Lifter (Window Functions)

```bash
./target/release/sql-cli -q "SELECT region, ROW_NUMBER() OVER (ORDER BY sales_amount) AS rn FROM sales WHERE ROW_NUMBER() OVER (ORDER BY sales_amount) <= 5" \
    -d data/sales_data.csv \
    --show-transformations
```

**Shows:**
- Original query with window function in WHERE
- Transformed to CTE with lifted expression

### Example 5: All Transformers

```bash
./target/release/sql-cli -f examples/expander_rewriters.sql --show-transformations
```

**Shows:**
- All transformers in action
- Multiple queries showcasing each feature

---

## Disabling Transformers for Debugging

Sometimes you need to isolate which transformer is causing an issue:

```bash
# Disable expression lifter
--no-expression-lifter

# Disable WHERE expansion
--no-where-expansion

# Disable GROUP BY expansion
--no-group-by-expansion

# Disable HAVING auto-aliasing
--no-having-expansion

# Disable CTE hoisting
--no-cte-hoister

# Disable IN operator lifting
--no-in-lifter

# Disable all (useful for baseline testing)
--no-expression-lifter \
--no-where-expansion \
--no-group-by-expansion \
--no-having-expansion \
--no-cte-hoister \
--no-in-lifter
```

**Example:**
```bash
# Test query without WHERE expansion
./target/release/sql-cli -q "SELECT sales_amount * 1.1 AS adj FROM sales WHERE adj > 15000" \
    -d data/sales_data.csv \
    --no-where-expansion
# Should fail with "Column 'adj' not found"
```

---

## Performance Considerations

### Overhead of Debugging Flags

| Flag | Overhead | When to Use |
|------|----------|-------------|
| `--show-transformations` | Medium (~5-10ms) | Development/debugging only |
| `--show-preprocessing` | Low (~1-2ms) | Development/debugging only |
| `--query-plan` | Low (~1ms) | Anytime (minimal impact) |
| No flags | None | Production |

**Recommendation:** Only use debugging flags during development. Disable for production queries.

---

## Comparing Before and After

Sometimes you want to see the final SQL that's executed:

```bash
# Step 1: See transformations
./target/release/sql-cli -q "SELECT UPPER(region) AS r FROM sales GROUP BY r" \
    -d data/sales_data.csv \
    --show-transformations

# Step 2: Copy the "AFTER" SQL from the last transformer

# Step 3: Run directly to verify
./target/release/sql-cli -q "SELECT UPPER(region) AS r FROM sales GROUP BY UPPER(region)" \
    -d data/sales_data.csv

# Should produce identical results
```

---

## Troubleshooting Common Issues

### Issue 1: Transformation Not Showing

**Problem:** Expect a transformer to run, but don't see output.

**Cause:** Transformer didn't detect a pattern to transform.

**Solution:**
```bash
# Verify transformer is enabled (default: all enabled)
./target/release/sql-cli -q "YOUR_QUERY" -d data.csv --show-preprocessing

# Check if query matches transformer's pattern
# See docs/PREPROCESSOR_TRANSFORMERS.md for patterns
```

### Issue 2: Too Much Output

**Problem:** `--show-transformations` shows many transformers, hard to read.

**Solution:**
```bash
# Disable transformers you don't care about
./target/release/sql-cli -q "YOUR_QUERY" -d data.csv --show-transformations \
    --no-cte-hoister \
    --no-in-lifter

# Or pipe through less for scrolling
./target/release/sql-cli -f examples/expander_rewriters.sql --show-transformations 2>&1 | less
```

### Issue 3: Understanding AST Format

**Problem:** `--query-plan` output is hard to read (Rust Debug format).

**Solution:**
Use `--show-transformations` instead - it shows formatted SQL which is much more readable.

---

## Integration with Testing

### Manual Testing

```bash
# Run examples with transformations to see all features
./target/release/sql-cli -f examples/expander_rewriters.sql --show-transformations

# Test specific feature
./target/release/sql-cli -q "YOUR_TEST_QUERY" -d data.csv --show-transformations
```

### Automated Testing

The transformation output goes to `stderr`, so it won't interfere with automated tests that check `stdout`:

```bash
# Test still works (stdout is clean)
./target/release/sql-cli -q "SELECT * FROM sales" -d data/sales_data.csv -o csv > output.csv

# Debug output goes to stderr
./target/release/sql-cli -q "SELECT * FROM sales" -d data/sales_data.csv -o csv --show-transformations 2> debug.log
```

---

## Best Practices

1. **Always use `--show-transformations` when debugging** - It's the most useful flag
2. **Check transformation order** - Transformers run in a specific order
3. **Disable unnecessary transformers** - Reduces noise in output
4. **Copy-paste transformed SQL** - Verify it works directly
5. **Add examples** - If you find a good test case, add to `examples/expander_rewriters.sql`

---

## Summary

| Need | Use This Flag | Example |
|------|--------------|---------|
| See what transformers did | `--show-transformations` | See SQL before/after each step |
| Debug parser | `--query-plan` | See raw AST |
| Pipeline stats | `--show-preprocessing` | See timing/stats |
| Disable specific transformer | `--no-where-expansion` etc. | Test without a transformer |
| Production query | (no flags) | No overhead |

**The `--show-transformations` flag is your best friend when working with transformers!**

---

## Related Documentation

- **Transformer Guide:** `docs/PREPROCESSOR_TRANSFORMERS.md`
- **Feature Roadmap:** `docs/SQL_FEATURE_GAPS_AND_ROADMAP.md`
- **Examples:** `examples/expander_rewriters.sql`
- **Main Guide:** `CLAUDE.md`