ggsql 0.4.0

A declarative visualization language that extends SQL with powerful data visualization capabilities.
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
//! Position adjustment dispatch for layers
//!
//! This module applies position adjustments to layers after DataFrame materialization
//! but before scale training. This ensures scales see the adjusted values.
//!
//! The actual position adjustment algorithms are implemented in the position module
//! (`src/plot/layer/position/`). This module provides the dispatch logic.

use crate::plot::{Plot, PositionType};
use crate::{DataFrame, Result};
use std::collections::HashMap;

/// Apply position adjustments to all layers in the spec.
///
/// For each layer with a non-identity position:
/// - Stack: modifies pos2/pos2end columns with cumulative sums
/// - Dodge: creates pos1offset column for horizontal displacement, adjusts bar width
/// - Jitter: creates pos1offset/pos2offset columns with random displacement
///
/// Must be called after resolve_aesthetics() but before resolve_scales().
pub fn apply_position_adjustments(
    spec: &mut Plot,
    data_map: &mut HashMap<String, DataFrame>,
) -> Result<()> {
    for idx in 0..spec.layers.len() {
        // Skip identity position (no adjustment needed)
        if spec.layers[idx].position.position_type() == PositionType::Identity {
            continue;
        }

        let Some(key) = spec.layers[idx].data_key.clone() else {
            continue;
        };

        let Some(df) = data_map.remove(&key) else {
            continue;
        };

        // Delegate to the position's apply_adjustment implementation
        // Each position validates its own requirements internally
        let (adjusted_df, adjusted_width) =
            spec.layers[idx]
                .position
                .apply_adjustment(df, &spec.layers[idx], spec)?;

        data_map.insert(key.clone(), adjusted_df);

        // Store adjusted width on layer (for writers that need it)
        // This does NOT override the user's width parameter
        if let Some(width) = adjusted_width {
            spec.layers[idx].adjusted_width = Some(width);
        }
    }

    Ok(())
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::array_util::as_f64;
    use crate::df;
    use crate::plot::facet::{Facet, FacetLayout};
    use crate::plot::layer::{Geom, Position};
    use crate::plot::{AestheticValue, Mappings, ParameterValue, Scale, ScaleType};
    use arrow::array::Array;

    fn make_continuous_scale(aesthetic: &str) -> Scale {
        let mut scale = Scale::new(aesthetic);
        scale.scale_type = Some(ScaleType::continuous());
        scale
    }

    fn make_discrete_scale(aesthetic: &str) -> Scale {
        let mut scale = Scale::new(aesthetic);
        scale.scale_type = Some(ScaleType::discrete());
        scale
    }

    fn make_test_df() -> DataFrame {
        df! {
            "__ggsql_aes_pos1__" => vec!["A", "A", "B", "B"],
            "__ggsql_aes_pos2__" => vec![10.0, 20.0, 15.0, 25.0],
            "__ggsql_aes_pos2end__" => vec![0.0, 0.0, 0.0, 0.0],
            "__ggsql_aes_fill__" => vec!["X", "Y", "X", "Y"],
        }
        .unwrap()
    }

    fn make_test_layer() -> crate::plot::Layer {
        let mut layer = crate::plot::Layer::new(Geom::bar());
        layer.mappings = {
            let mut m = Mappings::new();
            m.insert(
                "pos1",
                AestheticValue::standard_column("__ggsql_aes_pos1__"),
            );
            m.insert(
                "pos2",
                AestheticValue::standard_column("__ggsql_aes_pos2__"),
            );
            m.insert(
                "pos2end",
                AestheticValue::standard_column("__ggsql_aes_pos2end__"),
            );
            m.insert(
                "fill",
                AestheticValue::standard_column("__ggsql_aes_fill__"),
            );
            m
        };
        // Add fill to partition_by (simulates what add_discrete_columns_to_partition_by does)
        layer.partition_by = vec!["__ggsql_aes_fill__".to_string()];
        layer
    }

    #[test]
    fn test_identity_no_change() {
        let df = make_test_df();
        let mut layer = make_test_layer();
        layer.position = Position::identity();

        let spec = Plot::new();
        let mut data_map = HashMap::new();
        layer.data_key = Some("__ggsql_layer_0__".to_string());
        data_map.insert("__ggsql_layer_0__".to_string(), df.clone());

        let mut spec_with_layer = spec;
        spec_with_layer.layers.push(layer);

        apply_position_adjustments(&mut spec_with_layer, &mut data_map).unwrap();

        // Data should be unchanged
        let result_df = data_map.get("__ggsql_layer_0__").unwrap();
        assert_eq!(result_df.height(), 4);
    }

    #[test]
    fn test_stack_cumsum() {
        let df = make_test_df();
        let mut layer = make_test_layer();
        layer.position = Position::stack();

        let spec = Plot::new();
        let mut data_map = HashMap::new();
        layer.data_key = Some("__ggsql_layer_0__".to_string());
        data_map.insert("__ggsql_layer_0__".to_string(), df);

        let mut spec_with_layer = spec;
        spec_with_layer.layers.push(layer);

        apply_position_adjustments(&mut spec_with_layer, &mut data_map).unwrap();

        let result_df = data_map.get("__ggsql_layer_0__").unwrap();
        let pos2_col = result_df.column("__ggsql_aes_pos2__").unwrap();
        let pos2end_col = result_df.column("__ggsql_aes_pos2end__").unwrap();

        // Verify stacking was applied (column should be numeric)
        assert!(
            matches!(
                pos2_col.data_type(),
                arrow::datatypes::DataType::Float64
                    | arrow::datatypes::DataType::Int64
                    | arrow::datatypes::DataType::Int32
            ),
            "pos2 should be numeric"
        );
        assert!(
            matches!(
                pos2end_col.data_type(),
                arrow::datatypes::DataType::Float64
                    | arrow::datatypes::DataType::Int64
                    | arrow::datatypes::DataType::Int32
            ),
            "pos2end should be numeric"
        );
    }

    #[test]
    fn test_dodge_offset() {
        let df = make_test_df();
        let mut layer = make_test_layer();
        layer.position = Position::dodge();

        // Create spec with pos1 as discrete and pos2 as continuous
        let mut spec = Plot::new();
        spec.scales.push(make_discrete_scale("pos1"));
        spec.scales.push(make_continuous_scale("pos2"));

        let mut data_map = HashMap::new();
        layer.data_key = Some("__ggsql_layer_0__".to_string());
        data_map.insert("__ggsql_layer_0__".to_string(), df);

        let mut spec_with_layer = spec;
        spec_with_layer.layers.push(layer);

        apply_position_adjustments(&mut spec_with_layer, &mut data_map).unwrap();

        let result_df = data_map.get("__ggsql_layer_0__").unwrap();

        // Verify pos1offset column was created
        let offset_col = result_df.column("__ggsql_aes_pos1offset__");
        assert!(offset_col.is_ok(), "pos1offset column should be created");

        let offset = as_f64(offset_col.unwrap()).unwrap();

        // With 2 groups (X, Y) and default width 0.9:
        // - adjusted_width = 0.9 / 2 = 0.45
        // - center_offset = 0.5
        // - Group X: center = (0 - 0.5) * 0.45 = -0.225
        // - Group Y: center = (1 - 0.5) * 0.45 = +0.225
        let offsets: Vec<f64> = (0..offset.len())
            .filter(|&i| !offset.is_null(i))
            .map(|i| offset.value(i))
            .collect();
        assert!(
            offsets.iter().any(|&v| (v - (-0.225)).abs() < 0.001),
            "Should have offset -0.225 for group X, got {:?}",
            offsets
        );
        assert!(
            offsets.iter().any(|&v| (v - 0.225).abs() < 0.001),
            "Should have offset +0.225 for group Y, got {:?}",
            offsets
        );

        // Verify adjusted_width was set
        let adjusted = spec_with_layer.layers[0].adjusted_width;
        assert!(adjusted.is_some());
        assert!((adjusted.unwrap() - 0.45).abs() < 0.001);
    }

    #[test]
    fn test_dodge_custom_width() {
        let df = make_test_df();
        let mut layer = make_test_layer();
        layer.position = Position::dodge();
        layer
            .parameters
            .insert("width".to_string(), ParameterValue::Number(0.6));

        // Create spec with pos1 as discrete and pos2 as continuous
        let mut spec = Plot::new();
        spec.scales.push(make_discrete_scale("pos1"));
        spec.scales.push(make_continuous_scale("pos2"));

        let mut data_map = HashMap::new();
        layer.data_key = Some("__ggsql_layer_0__".to_string());
        data_map.insert("__ggsql_layer_0__".to_string(), df);

        let mut spec_with_layer = spec;
        spec_with_layer.layers.push(layer);

        apply_position_adjustments(&mut spec_with_layer, &mut data_map).unwrap();

        let result_df = data_map.get("__ggsql_layer_0__").unwrap();
        let offset_col = result_df.column("__ggsql_aes_pos1offset__").unwrap();
        let offset = as_f64(offset_col).unwrap();

        // With 2 groups and custom width 0.6:
        // - adjusted_width = 0.6 / 2 = 0.3
        let offsets: Vec<f64> = (0..offset.len())
            .filter(|&i| !offset.is_null(i))
            .map(|i| offset.value(i))
            .collect();
        assert!(offsets.iter().any(|&v| (v - (-0.15)).abs() < 0.001));
        assert!(offsets.iter().any(|&v| (v - 0.15).abs() < 0.001));

        let adjusted = spec_with_layer.layers[0].adjusted_width;
        assert!((adjusted.unwrap() - 0.3).abs() < 0.001);
    }

    #[test]
    fn test_jitter_offset() {
        let df = make_test_df();
        let mut layer = make_test_layer();
        layer.position = Position::jitter();

        // Create spec with pos1 as discrete and pos2 as continuous
        let mut spec = Plot::new();
        spec.scales.push(make_discrete_scale("pos1"));
        spec.scales.push(make_continuous_scale("pos2"));

        let mut data_map = HashMap::new();
        layer.data_key = Some("__ggsql_layer_0__".to_string());
        data_map.insert("__ggsql_layer_0__".to_string(), df);

        let mut spec_with_layer = spec;
        spec_with_layer.layers.push(layer);

        apply_position_adjustments(&mut spec_with_layer, &mut data_map).unwrap();

        let result_df = data_map.get("__ggsql_layer_0__").unwrap();

        // Verify pos1offset column was created
        let offset_col = result_df.column("__ggsql_aes_pos1offset__");
        assert!(offset_col.is_ok());

        let offset = as_f64(offset_col.unwrap()).unwrap();
        let offsets: Vec<f64> = (0..offset.len())
            .filter(|&i| !offset.is_null(i))
            .map(|i| offset.value(i))
            .collect();

        // With default width 0.9, offsets should be in range [-0.45, 0.45]
        for &v in &offsets {
            assert!((-0.45..=0.45).contains(&v));
        }

        // No adjusted_width for jitter
        assert!(spec_with_layer.layers[0].adjusted_width.is_none());
    }

    #[test]
    fn test_jitter_custom_width() {
        let df = make_test_df();
        let mut layer = make_test_layer();
        layer.position = Position::jitter();
        layer
            .parameters
            .insert("width".to_string(), ParameterValue::Number(0.6));

        // Create spec with pos1 as discrete and pos2 as continuous
        let mut spec = Plot::new();
        spec.scales.push(make_discrete_scale("pos1"));
        spec.scales.push(make_continuous_scale("pos2"));

        let mut data_map = HashMap::new();
        layer.data_key = Some("__ggsql_layer_0__".to_string());
        data_map.insert("__ggsql_layer_0__".to_string(), df);

        let mut spec_with_layer = spec;
        spec_with_layer.layers.push(layer);

        apply_position_adjustments(&mut spec_with_layer, &mut data_map).unwrap();

        let result_df = data_map.get("__ggsql_layer_0__").unwrap();
        let offset_col = result_df.column("__ggsql_aes_pos1offset__").unwrap();
        let offset = as_f64(offset_col).unwrap();
        let offsets: Vec<f64> = (0..offset.len())
            .filter(|&i| !offset.is_null(i))
            .map(|i| offset.value(i))
            .collect();

        // With custom width 0.6, offsets should be in range [-0.3, 0.3]
        for &v in &offsets {
            assert!((-0.3..=0.3).contains(&v));
        }
    }

    #[test]
    fn test_stack_resets_per_facet_panel() {
        // Stacking should compute independently within each facet panel.
        // Without this, bars in the second facet panel stack on top of
        // cumulative values from the first panel (see issue #244).
        //
        // Two facet panels (F1, F2) each with the same x="A" and two
        // fill groups (X, Y). Stacking within each panel should start from 0.
        let df = df! {
            "__ggsql_aes_pos1__" => vec!["A", "A", "A", "A"],
            "__ggsql_aes_pos2__" => vec![10.0, 20.0, 30.0, 40.0],
            "__ggsql_aes_pos2end__" => vec![0.0, 0.0, 0.0, 0.0],
            "__ggsql_aes_fill__" => vec!["X", "Y", "X", "Y"],
            "__ggsql_aes_facet1__" => vec!["F1", "F1", "F2", "F2"],
        }
        .unwrap();

        let mut layer = crate::plot::Layer::new(Geom::bar());
        layer.mappings = {
            let mut m = Mappings::new();
            m.insert(
                "pos1",
                AestheticValue::standard_column("__ggsql_aes_pos1__"),
            );
            m.insert(
                "pos2",
                AestheticValue::standard_column("__ggsql_aes_pos2__"),
            );
            m.insert(
                "pos2end",
                AestheticValue::standard_column("__ggsql_aes_pos2end__"),
            );
            m.insert(
                "fill",
                AestheticValue::standard_column("__ggsql_aes_fill__"),
            );
            m.insert(
                "facet1",
                AestheticValue::standard_column("__ggsql_aes_facet1__"),
            );
            m
        };
        layer.partition_by = vec![
            "__ggsql_aes_fill__".to_string(),
            "__ggsql_aes_facet1__".to_string(),
        ];
        layer.position = Position::stack();
        layer.data_key = Some("__ggsql_layer_0__".to_string());

        let mut spec = Plot::new();
        spec.scales.push(make_discrete_scale("pos1"));
        spec.scales.push(make_continuous_scale("pos2"));
        spec.facet = Some(Facet::new(FacetLayout::Wrap {
            variables: vec!["facet_var".to_string()],
        }));
        let mut data_map = HashMap::new();
        data_map.insert("__ggsql_layer_0__".to_string(), df);

        let mut spec_with_layer = spec;
        spec_with_layer.layers.push(layer);

        apply_position_adjustments(&mut spec_with_layer, &mut data_map).unwrap();

        let result_df = data_map.get("__ggsql_layer_0__").unwrap();

        // Sort by facet then fill so we can assert in predictable order
        // Build sort indices based on (facet, fill) lexicographic order
        let facet_col =
            crate::array_util::as_str(result_df.column("__ggsql_aes_facet1__").unwrap()).unwrap();
        let fill_col =
            crate::array_util::as_str(result_df.column("__ggsql_aes_fill__").unwrap()).unwrap();
        let mut indices: Vec<usize> = (0..result_df.height()).collect();
        indices.sort_by(|&a, &b| {
            let fa = facet_col.value(a);
            let fb = facet_col.value(b);
            let cmp1 = fa.cmp(fb);
            if cmp1 != std::cmp::Ordering::Equal {
                return cmp1;
            }
            fill_col.value(a).cmp(fill_col.value(b))
        });

        let pos2_arr = as_f64(result_df.column("__ggsql_aes_pos2__").unwrap()).unwrap();
        let pos2end_arr = as_f64(result_df.column("__ggsql_aes_pos2end__").unwrap()).unwrap();

        let pos2_vals: Vec<f64> = indices.iter().map(|&i| pos2_arr.value(i)).collect();
        let pos2end_vals: Vec<f64> = indices.iter().map(|&i| pos2end_arr.value(i)).collect();

        // Expected (sorted by facet, fill):
        // F1/X: pos2end=0,  pos2=10  (first in panel, starts at 0)
        // F1/Y: pos2end=10, pos2=30  (stacks on X)
        // F2/X: pos2end=0,  pos2=30  (first in panel, should reset to 0)
        // F2/Y: pos2end=30, pos2=70  (stacks on X)
        assert_eq!(
            pos2end_vals[2], 0.0,
            "F2 panel first bar should start at 0, not carry over from F1. pos2end={:?}, pos2={:?}",
            pos2end_vals, pos2_vals
        );
    }

    #[test]
    fn test_stack_groups_by_facet_not_fill_order() {
        // Regression: when partition_by listed fill before facet, the sort order
        // put fill first, interleaving facet panels. compute_group_ids then
        // treated each row as its own group and stacking had no effect.
        //
        // Data is pre-sorted by (fill, facet) — the worst case for the old code.
        // Three facet panels (F1, F2, F3) each with fill groups (X, Y).
        let df = df! {
            "__ggsql_aes_pos1__" => vec!["A", "A", "A", "A", "A", "A"],
            "__ggsql_aes_pos2__" => vec![10.0, 20.0, 30.0, 40.0, 50.0, 60.0],
            "__ggsql_aes_pos2end__" => vec![0.0, 0.0, 0.0, 0.0, 0.0, 0.0],
            "__ggsql_aes_fill__" => vec!["X", "X", "X", "Y", "Y", "Y"],
            "__ggsql_aes_facet1__" => vec!["F1", "F2", "F3", "F1", "F2", "F3"],
        }
        .unwrap();

        let mut layer = crate::plot::Layer::new(Geom::bar());
        layer.mappings = {
            let mut m = Mappings::new();
            m.insert(
                "pos1",
                AestheticValue::standard_column("__ggsql_aes_pos1__"),
            );
            m.insert(
                "pos2",
                AestheticValue::standard_column("__ggsql_aes_pos2__"),
            );
            m.insert(
                "pos2end",
                AestheticValue::standard_column("__ggsql_aes_pos2end__"),
            );
            m.insert(
                "fill",
                AestheticValue::standard_column("__ggsql_aes_fill__"),
            );
            m.insert(
                "facet1",
                AestheticValue::standard_column("__ggsql_aes_facet1__"),
            );
            m
        };
        // fill before facet — the order that triggered the bug
        layer.partition_by = vec![
            "__ggsql_aes_fill__".to_string(),
            "__ggsql_aes_facet1__".to_string(),
        ];
        layer.position = Position::stack();
        layer.data_key = Some("__ggsql_layer_0__".to_string());

        let mut spec = Plot::new();
        spec.scales.push(make_discrete_scale("pos1"));
        spec.scales.push(make_continuous_scale("pos2"));
        spec.facet = Some(Facet::new(FacetLayout::Wrap {
            variables: vec!["facet_var".to_string()],
        }));
        let mut data_map = HashMap::new();
        data_map.insert("__ggsql_layer_0__".to_string(), df);
        spec.layers.push(layer);

        apply_position_adjustments(&mut spec, &mut data_map).unwrap();

        let result_df = data_map.get("__ggsql_layer_0__").unwrap();

        let facet_col =
            crate::array_util::as_str(result_df.column("__ggsql_aes_facet1__").unwrap()).unwrap();
        let fill_col =
            crate::array_util::as_str(result_df.column("__ggsql_aes_fill__").unwrap()).unwrap();
        let pos2_arr = as_f64(result_df.column("__ggsql_aes_pos2__").unwrap()).unwrap();
        let pos2end_arr = as_f64(result_df.column("__ggsql_aes_pos2end__").unwrap()).unwrap();

        // Collect (facet, fill) → (pos2, pos2end)
        let mut by_key: std::collections::HashMap<(&str, &str), (f64, f64)> =
            std::collections::HashMap::new();
        for i in 0..result_df.height() {
            by_key.insert(
                (facet_col.value(i), fill_col.value(i)),
                (pos2_arr.value(i), pos2end_arr.value(i)),
            );
        }

        // Within each facet the two fill groups must stack:
        // F1: X=10 → [0,10], Y=40 → [10,50]
        // F2: X=20 → [0,20], Y=50 → [20,70]
        // F3: X=30 → [0,30], Y=60 → [30,90]
        assert_eq!(by_key[&("F1", "X")], (10.0, 0.0));
        assert_eq!(by_key[&("F1", "Y")], (50.0, 10.0));
        assert_eq!(by_key[&("F2", "X")], (20.0, 0.0));
        assert_eq!(by_key[&("F2", "Y")], (70.0, 20.0));
        assert_eq!(by_key[&("F3", "X")], (30.0, 0.0));
        assert_eq!(by_key[&("F3", "Y")], (90.0, 30.0));
    }

    #[test]
    fn test_dodge_ignores_facet_columns_in_group_count() {
        // Dodge should compute n_groups per facet panel, not globally.
        // With fill=["X","Y"] and facet=["F1","F2"], dodge should see
        // 2 groups (X, Y) not 4 (X-F1, X-F2, Y-F1, Y-F2).
        //
        // With 2 groups and default width 0.9:
        //   adjusted_width = 0.9 / 2 = 0.45
        //   offsets: -0.225 (group X), +0.225 (group Y)
        //
        // If facet columns incorrectly inflate n_groups to 4:
        //   adjusted_width = 0.9 / 4 = 0.225
        //   offsets would be different (spread across 4 positions)
        let df = df! {
            "__ggsql_aes_pos1__" => vec!["A", "A", "A", "A"],
            "__ggsql_aes_pos2__" => vec![10.0, 20.0, 30.0, 40.0],
            "__ggsql_aes_pos2end__" => vec![0.0, 0.0, 0.0, 0.0],
            "__ggsql_aes_fill__" => vec!["X", "Y", "X", "Y"],
            "__ggsql_aes_facet1__" => vec!["F1", "F1", "F2", "F2"],
        }
        .unwrap();

        let mut layer = crate::plot::Layer::new(Geom::bar());
        layer.mappings = {
            let mut m = Mappings::new();
            m.insert(
                "pos1",
                AestheticValue::standard_column("__ggsql_aes_pos1__"),
            );
            m.insert(
                "pos2",
                AestheticValue::standard_column("__ggsql_aes_pos2__"),
            );
            m.insert(
                "pos2end",
                AestheticValue::standard_column("__ggsql_aes_pos2end__"),
            );
            m.insert(
                "fill",
                AestheticValue::standard_column("__ggsql_aes_fill__"),
            );
            m.insert(
                "facet1",
                AestheticValue::standard_column("__ggsql_aes_facet1__"),
            );
            m
        };
        layer.partition_by = vec![
            "__ggsql_aes_fill__".to_string(),
            "__ggsql_aes_facet1__".to_string(),
        ];
        layer.position = Position::dodge();
        layer.data_key = Some("__ggsql_layer_0__".to_string());

        let mut spec = Plot::new();
        spec.scales.push(make_discrete_scale("pos1"));
        spec.scales.push(make_continuous_scale("pos2"));
        spec.facet = Some(Facet::new(FacetLayout::Wrap {
            variables: vec!["facet_var".to_string()],
        }));
        let mut data_map = HashMap::new();
        data_map.insert("__ggsql_layer_0__".to_string(), df);

        spec.layers.push(layer);

        apply_position_adjustments(&mut spec, &mut data_map).unwrap();

        // With 2 groups (X, Y), adjusted_width should be 0.45
        let adjusted = spec.layers[0].adjusted_width.unwrap();
        assert!(
            (adjusted - 0.45).abs() < 0.001,
            "adjusted_width should be 0.45 (2 groups), got {} (facet columns inflated group count)",
            adjusted
        );
    }
}