ggsql 0.4.1

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
620
621
622
623
624
625
626
627
628
629
630
631
632
633
//! Rule geom implementation

use super::{
    densify_edges, needs_projection, project_position_columns, DefaultAesthetics, GeomTrait,
    GeomType, ParamDefinition,
};
use crate::plot::projection::coord::map::clip_boundary_table;
use crate::plot::projection::coord::CoordKind;
use crate::plot::projection::Projection;
use crate::plot::types::{DefaultAestheticValue, ParameterValue, Parameters};
use crate::reader::SqlDialect;
use crate::{naming, Mappings, Result};

/// Rule geom - horizontal and vertical reference lines
#[derive(Debug, Clone, Copy)]
pub struct Rule;

impl GeomTrait for Rule {
    fn geom_type(&self) -> GeomType {
        GeomType::Rule
    }

    fn aesthetics(&self) -> DefaultAesthetics {
        DefaultAesthetics {
            defaults: &[
                ("pos1", DefaultAestheticValue::Required),
                ("slope", DefaultAestheticValue::Number(0.0)),
                ("stroke", DefaultAestheticValue::String("black")),
                ("linewidth", DefaultAestheticValue::Number(1.0)),
                ("opacity", DefaultAestheticValue::Number(1.0)),
                ("linetype", DefaultAestheticValue::String("solid")),
            ],
        }
    }

    fn default_params(&self) -> &'static [ParamDefinition] {
        const PARAMS: &[ParamDefinition] = &[super::types::AGGREGATE_PARAM];
        PARAMS
    }

    fn aggregate_domain_aesthetics(&self) -> Option<&'static [&'static str]> {
        Some(&[])
    }

    fn apply_projection(
        &self,
        query: &str,
        projection: &Projection,
        dialect: &dyn SqlDialect,
        mappings: &mut Mappings,
        partition_by: &mut Vec<String>,
        parameters: &mut std::collections::HashMap<String, crate::plot::types::ParameterValue>,
    ) -> Result<String> {
        if !needs_projection(projection) {
            return Ok(query.to_string());
        }

        // The rule input always has one position column named __ggsql_aes_pos1__
        // regardless of whether the aesthetic key is "pos1" or "pos2" — the executor
        // normalizes it to the pos1 slot.
        let columns = crate::util::set_union(vec![naming::aesthetic_column("pos1")], partition_by);

        let has_pos1 = mappings.contains_key("pos1");
        let bbox_expr = match projection.coord.coord_kind() {
            CoordKind::Map
                if matches!(
                    projection.properties.get("clip"),
                    Some(ParameterValue::Boolean(true))
                ) =>
            {
                let boundary_table = clip_boundary_table();
                dialect.sql_geometry_bbox("geom", &boundary_table)
            }
            // If we don't have a bbox, we cannot expand rule layer
            _ => return project_position_columns(query, projection, dialect, &columns),
        };
        let (expanded, expanded_columns) =
            expand_rule_to_segment(query, &columns, has_pos1, &bbox_expr);

        partition_by.push(naming::DENSIFY_ID_COLUMN.to_string());
        parameters.insert("densified".to_string(), ParameterValue::Boolean(true));

        let expanded_columns = crate::util::set_union(expanded_columns, partition_by);

        let densified = densify_edges(
            &expanded,
            dialect,
            &expanded_columns,
            partition_by,
            Some("__ggsql_vertex__"),
            false,
            1.0,
            360,
        );
        let clipped = match projection.coord.coord_kind() {
            CoordKind::Map => {
                let pos1_q = naming::quote_ident(&naming::aesthetic_column("pos1"));
                let pos2_q = naming::quote_ident(&naming::aesthetic_column("pos2"));
                let clip_table = clip_boundary_table();
                format!(
                    "SELECT * FROM ({densified}) WHERE ST_Contains(\
                     (SELECT geom FROM {clip_table}), ST_Point({pos1_q}, {pos2_q}))"
                )
            }
            _ => densified,
        };

        let projected = project_position_columns(&clipped, projection, dialect, &expanded_columns)?;

        // Both pos1 and pos2 columns exist in the output SQL (the spanning axis
        // was synthesized). Add the new axis to mappings so the writer encodes it.
        if !has_pos1 {
            mappings.aesthetics.remove("pos2");
            mappings.insert_column("pos1", "pos1");
            mappings.insert_column("pos2", "pos2");
        } else {
            mappings.insert_column("pos2", "pos2");
        }

        // After densification both axes are explicit — disable the orientation flip
        // that would otherwise swap the DataFrame columns.
        parameters.insert(
            "orientation".to_string(),
            ParameterValue::String("aligned".to_string()),
        );

        Ok(projected)
    }

    fn validate_aesthetics(
        &self,
        mappings: &crate::Mappings,
        aesthetic_ctx: &Option<crate::plot::aesthetic::AestheticContext>,
        parameters: &std::collections::HashMap<String, crate::plot::types::ParameterValue>,
    ) -> std::result::Result<(), String> {
        // Rule requires exactly one of pos1 or pos2 (XOR logic).
        // After densification both axes are present — skip the check.
        if matches!(
            parameters.get("densified"),
            Some(ParameterValue::Boolean(true))
        ) {
            return Ok(());
        }

        let has_pos1 = mappings.contains_key("pos1");
        let has_pos2 = mappings.contains_key("pos2");

        if has_pos1 && has_pos2 {
            let translate = |aes: &str| match aesthetic_ctx {
                Some(ctx) => ctx.map_internal_to_user(aes),
                None => aes.to_string(),
            };
            return Err(format!(
                "Layer 'rule' requires exactly one of `{}` or `{}`, not both.",
                translate("pos1"),
                translate("pos2")
            ));
        }

        Ok(())
    }

    fn setup_layer(
        &self,
        mappings: &mut crate::plot::layer::Mappings,
        parameters: &mut Parameters,
    ) -> crate::Result<()> {
        use crate::plot::layer::AestheticValue;
        use crate::plot::ParameterValue;

        // For diagonal rules (slope present), convert position aesthetics to AnnotationColumn
        // so they don't participate in scale training. The position value is the intercept,
        // not the actual extent of the line.

        // Check if slope is present and non-zero (in either mappings or parameters)
        let has_diagonal_slope = mappings.get("slope").is_some_and(|mapping| {
            !matches!(mapping, AestheticValue::Literal(ParameterValue::Number(n)) if *n == 0.0)
        }) || parameters.get("slope").is_some_and(|param| {
            !matches!(param, ParameterValue::Number(n) if *n == 0.0)
        });

        if !has_diagonal_slope {
            return Ok(());
        }
        parameters.insert("diagonal".to_string(), ParameterValue::Boolean(true));

        // Determine orientation from which intercept is present.
        // We override the bidirectionality algorithm here since it uses
        // scales to determine orientation. We can't rely on that here,
        // because diagonal lines purposefully use AnnotationColumns to
        // avoid training the scale.
        let orientation = if mappings.contains_key("pos1") {
            // x-intercept → pos2 varies
            crate::plot::layer::orientation::TRANSPOSED
        } else {
            // y-intercept → pos1 varies (or default)
            crate::plot::layer::orientation::ALIGNED
        };

        parameters.insert(
            "orientation".to_string(),
            ParameterValue::String(orientation.to_string()),
        );

        // For diagonal rules, convert pos1/pos2 to AnnotationColumn so they don't participate in scale training
        // The position value is the intercept, not the actual extent of the line
        for aesthetic in ["pos1", "pos2"] {
            if let Some(mapping) = mappings.aesthetics.get_mut(aesthetic) {
                // Convert Column to AnnotationColumn
                if let AestheticValue::Column { name, .. } = &*mapping {
                    let name = name.clone();
                    *mapping = AestheticValue::AnnotationColumn { name };
                }
            }
        }

        Ok(())
    }
}

/// Expand each rule into 2 vertex rows (start + end of the spanning axis).
///
/// The input always has a single position column `__ggsql_aes_pos1__` (the
/// executor normalizes the rule's fixed axis into the pos1 slot). `has_pos1`
/// tells us the semantic meaning:
/// - true (vertical rule): pos1 is longitude (fixed), synthesize pos2 from bbox y.
/// - false (horizontal rule): pos1 is latitude (fixed), rename to pos2, synthesize
///   pos1 from bbox x.
///
/// `bbox_expr` is a SQL expression that yields xmin, ymin, xmax, ymax columns.
fn expand_rule_to_segment(
    query: &str,
    columns: &[String],
    has_pos1: bool,
    bbox_expr: &str,
) -> (String, Vec<String>) {
    let pos1_col = naming::aesthetic_column("pos1");
    let pos2_col = naming::aesthetic_column("pos2");
    let pos1_q = naming::quote_ident(&pos1_col);
    let pos2_q = naming::quote_ident(&pos2_col);

    // The input column is always __ggsql_aes_pos1__. Build the SELECT
    // expressions that produce both pos1 and pos2 in the output.
    let (fixed_expr, span_expr) = if has_pos1 {
        // Vertical rule: input pos1 = longitude (keep as pos1), synthesize pos2 from y-extent
        let fixed = pos1_q.clone();
        let span = format!(
            "CASE \"__ggsql_vertex__\" WHEN 0 THEN (SELECT ymin FROM ({bbox_expr})) \
             WHEN 1 THEN (SELECT ymax FROM ({bbox_expr})) END AS {pos2_q}"
        );
        (fixed, span)
    } else {
        // Horizontal rule: input pos1 = latitude (rename to pos2), synthesize pos1 from x-extent
        let fixed = format!("{pos1_q} AS {pos2_q}");
        let span = format!(
            "CASE \"__ggsql_vertex__\" WHEN 0 THEN (SELECT xmin FROM ({bbox_expr})) \
             WHEN 1 THEN (SELECT xmax FROM ({bbox_expr})) END AS {pos1_q}"
        );
        (fixed, span)
    };

    // Passthrough: columns minus the input pos1 column (we handle it explicitly above)
    let passthrough_cols: Vec<&String> = columns.iter().filter(|c| *c != &pos1_col).collect();
    let passthrough_quoted: Vec<String> = passthrough_cols
        .iter()
        .map(|c| naming::quote_ident(c))
        .collect();

    let densify_id_q = naming::quote_ident(naming::DENSIFY_ID_COLUMN);

    let numbered = format!(
        "SELECT *, ROW_NUMBER() OVER (ORDER BY (SELECT NULL)) \
         AS {densify_id_q} FROM ({query})"
    );

    let vertices_table = "(SELECT 0 AS \"__ggsql_vertex__\" UNION ALL SELECT 1)";

    let mut select_parts: Vec<String> = passthrough_quoted;
    select_parts.push(densify_id_q.to_string());
    select_parts.push("\"__ggsql_vertex__\"".to_string());
    select_parts.push(fixed_expr);
    select_parts.push(span_expr);

    let sql = format!(
        "SELECT {} FROM ({numbered}) \"__ggsql_rule__\" \
         CROSS JOIN {vertices_table} \"__ggsql_vertices__\"",
        select_parts.join(", ")
    );

    let mut out_columns: Vec<String> = passthrough_cols.into_iter().cloned().collect();
    out_columns.push(naming::DENSIFY_ID_COLUMN.to_string());
    out_columns.push(pos1_col);
    out_columns.push(pos2_col);

    (sql, out_columns)
}

impl std::fmt::Display for Rule {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "rule")
    }
}

#[cfg(test)]
mod tests {
    use super::{expand_rule_to_segment, Rule};
    use crate::plot::layer::geom::{densify_edges, GeomTrait};
    use crate::plot::projection::Projection;
    use crate::plot::types::ParameterValue;
    use crate::plot::{AestheticContext, AestheticValue, Geom, Layer};
    use crate::{naming, Mappings};

    fn validate_rule(mappings: &[(&str, &str)]) -> Result<(), String> {
        let mut layer = Layer::new(Geom::rule());
        for (aesthetic, column) in mappings {
            layer.mappings.insert(
                aesthetic.to_string(),
                AestheticValue::standard_column(column.to_string()),
            );
        }
        let ctx = AestheticContext::from_static(&["x", "y"], &[]);
        layer.validate_mapping(&Some(ctx), false)
    }

    #[test]
    fn test_rule_requires_exactly_one_position() {
        // Rule requires exactly one of pos1 or pos2 (XOR logic)

        // Missing both should fail
        let result = validate_rule(&[]);
        assert!(result.is_err(), "Should fail when missing both x and y");

        // Both present should fail
        let result = validate_rule(&[("pos1", "x"), ("pos2", "y")]);
        assert!(result.is_err(), "Should fail when both x and y are present");
    }

    #[test]
    fn test_rule_validates_with_x_only() {
        // Vertical rule with only x
        let result = validate_rule(&[("pos1", "x")]);
        assert!(
            result.is_ok(),
            "Expected validation to pass with only x, got error: {:?}",
            result.err()
        );
    }

    #[test]
    fn test_rule_validates_with_y_only() {
        // Horizontal rule with only y
        let result = validate_rule(&[("pos2", "y")]);
        assert!(
            result.is_ok(),
            "Expected validation to pass with only y, got error: {:?}",
            result.err()
        );
    }

    #[test]
    fn test_apply_projection_no_op_without_map() {
        let rule = Rule;
        let projection = Projection::cartesian();
        let mut mappings = Mappings::new();
        mappings.insert_column("pos1", "pos1");
        let mut partition_by = vec![];

        let result = rule
            .apply_projection(
                "SELECT * FROM t",
                &projection,
                &crate::reader::AnsiDialect,
                &mut mappings,
                &mut partition_by,
                &mut std::collections::HashMap::new(),
            )
            .unwrap();

        assert_eq!(result, "SELECT * FROM t");
        assert!(partition_by.is_empty());
    }

    #[test]
    fn test_apply_projection_without_clip_only_projects() {
        let rule = Rule;
        let mut projection = Projection::map();
        projection.properties.insert(
            "source".to_string(),
            ParameterValue::String("EPSG:4326".to_string()),
        );
        projection.properties.insert(
            "target".to_string(),
            ParameterValue::String("+proj=merc".to_string()),
        );
        projection
            .properties
            .insert("clip".to_string(), ParameterValue::Boolean(false));

        let mut mappings = Mappings::new();
        mappings.insert_column("pos1", "pos1");
        let mut partition_by = vec![];

        let result = rule
            .apply_projection(
                "SELECT * FROM t",
                &projection,
                &crate::reader::AnsiDialect,
                &mut mappings,
                &mut partition_by,
                &mut std::collections::HashMap::new(),
            )
            .unwrap();

        assert!(result.contains("ST_Transform"));
        assert!(!result.contains(naming::DENSIFY_ID_COLUMN));
        assert!(partition_by.is_empty());
    }

    #[test]
    fn test_apply_projection_with_clip_expands_and_densifies() {
        let rule = Rule;
        let mut projection = Projection::map();
        projection.properties.insert(
            "source".to_string(),
            ParameterValue::String("EPSG:4326".to_string()),
        );
        projection.properties.insert(
            "target".to_string(),
            ParameterValue::String("+proj=merc".to_string()),
        );
        projection
            .properties
            .insert("clip".to_string(), ParameterValue::Boolean(true));

        let mut mappings = Mappings::new();
        mappings.insert_column("pos1", "pos1");
        let mut partition_by = vec![];

        let result = rule
            .apply_projection(
                "SELECT * FROM t",
                &projection,
                &crate::reader::AnsiDialect,
                &mut mappings,
                &mut partition_by,
                &mut std::collections::HashMap::new(),
            )
            .unwrap();

        assert!(result.contains(naming::DENSIFY_ID_COLUMN));
        assert!(result.contains("CROSS JOIN"));
        assert!(result.contains("ST_Transform"));
        assert!(partition_by.contains(&naming::DENSIFY_ID_COLUMN.to_string()));
        // pos2 should be added to mappings (the spanning axis)
        assert!(mappings.contains_key("pos2"));
    }

    #[cfg(feature = "duckdb")]
    #[test]
    fn test_densified_rule_produces_intermediate_vertices() {
        use crate::plot::projection::coord::map::clip_boundary_table;
        use crate::reader::{DuckDBReader, Reader};
        use arrow::array::Array;

        let reader = DuckDBReader::from_connection_string("duckdb://memory").unwrap();
        let dialect = reader.dialect();

        for stmt in dialect.sql_spatial_setup() {
            reader.execute_sql(&stmt).unwrap();
        }

        // Create a clip boundary table simulating an orthographic hemisphere
        let boundary_table = clip_boundary_table();
        let create_sql = format!(
            "CREATE TEMP TABLE \"{boundary_table}\" AS \
             SELECT ST_GeomFromText(\
                 'POLYGON ((-90 -60, 90 -60, 90 60, -90 60, -90 -60))'\
             ) AS geom"
        );
        reader.execute_sql(&create_sql).unwrap();

        // A vertical rule at lon = -30
        let input = format!("SELECT -30.0 AS \"{}\"", naming::aesthetic_column("pos1"),);

        let rule = Rule;
        let mut projection = Projection::map();
        projection.properties.insert(
            "source".to_string(),
            ParameterValue::String("EPSG:4326".to_string()),
        );
        projection.properties.insert(
            "target".to_string(),
            ParameterValue::String("+proj=ortho +lat_0=0 +lon_0=0".to_string()),
        );
        projection
            .properties
            .insert("clip".to_string(), ParameterValue::Boolean(true));

        let mut mappings = Mappings::new();
        mappings.insert_column("pos1", "pos1");
        let mut partition_by = vec![];

        let projected_sql = rule
            .apply_projection(
                &input,
                &projection,
                dialect,
                &mut mappings,
                &mut partition_by,
                &mut std::collections::HashMap::new(),
            )
            .unwrap();

        let df = reader.execute_sql(&projected_sql).unwrap();
        let n = df.inner().num_rows();
        assert!(
            n > 2,
            "expected densified vertices (more than start+end), got {n}"
        );

        let pos1_col = df
            .inner()
            .column_by_name(&naming::aesthetic_column("pos1"))
            .unwrap()
            .as_any()
            .downcast_ref::<arrow::array::Float64Array>()
            .unwrap();
        let pos2_col = df
            .inner()
            .column_by_name(&naming::aesthetic_column("pos2"))
            .unwrap()
            .as_any()
            .downcast_ref::<arrow::array::Float64Array>()
            .unwrap();

        assert_eq!(pos1_col.null_count(), 0);
        assert_eq!(pos2_col.null_count(), 0);
    }

    #[cfg(feature = "duckdb")]
    #[test]
    fn test_densified_horizontal_rule_keeps_latitude_constant() {
        use crate::plot::projection::coord::map::clip_boundary_table;
        use crate::reader::{DuckDBReader, Reader};

        let reader = DuckDBReader::from_connection_string("duckdb://memory").unwrap();
        let dialect = reader.dialect();

        for stmt in dialect.sql_spatial_setup() {
            reader.execute_sql(&stmt).unwrap();
        }

        // Create a clip boundary table (simple rectangle in EPSG:4326)
        let boundary_table = clip_boundary_table();
        let create_sql = format!(
            "CREATE TEMP TABLE \"{boundary_table}\" AS \
             SELECT ST_GeomFromText(\
                 'POLYGON ((-90 -60, 90 -60, 90 60, -90 60, -90 -60))'\
             ) AS geom"
        );
        reader.execute_sql(&create_sql).unwrap();

        // A horizontal rule at lat = 20 (pos2 mapped, not pos1)
        let input = format!("SELECT 20.0 AS \"{}\"", naming::aesthetic_column("pos1"),);

        let rule = Rule;
        let mut projection = Projection::map();
        projection.properties.insert(
            "source".to_string(),
            ParameterValue::String("EPSG:4326".to_string()),
        );
        projection.properties.insert(
            "target".to_string(),
            ParameterValue::String("+proj=ortho +lat_0=0 +lon_0=0".to_string()),
        );
        projection
            .properties
            .insert("clip".to_string(), ParameterValue::Boolean(true));

        // Horizontal rule: pos2 is mapped (latitude is the fixed axis)
        let mut mappings = Mappings::new();
        mappings.insert_column("pos2", "pos2");
        let mut partition_by = vec![];

        rule.apply_projection(
            &input,
            &projection,
            dialect,
            &mut mappings,
            &mut partition_by,
            &mut std::collections::HashMap::new(),
        )
        .unwrap();

        // Run expand + densify manually to verify latitude stays constant pre-projection.
        let columns = vec![naming::aesthetic_column("pos1")];
        let has_pos1 = false;
        let bbox_expr = dialect.sql_geometry_bbox("geom", &boundary_table);
        let (expanded, expanded_columns) =
            expand_rule_to_segment(&input, &columns, has_pos1, &bbox_expr);
        let densified = densify_edges(
            &expanded,
            dialect,
            &expanded_columns,
            &[naming::DENSIFY_ID_COLUMN.to_string()],
            Some("__ggsql_vertex__"),
            false,
            1.0,
            360,
        );

        let df = reader.execute_sql(&densified).unwrap();
        let n = df.inner().num_rows();
        assert!(n > 2, "expected densified vertices, got {n}");

        let pos2_col = df
            .inner()
            .column_by_name(&naming::aesthetic_column("pos2"))
            .unwrap()
            .as_any()
            .downcast_ref::<arrow::array::Float64Array>()
            .unwrap();

        // All pos2 (latitude) values should be exactly 20.0
        for i in 0..n {
            let val = pos2_col.value(i);
            assert!(
                (val - 20.0).abs() < 1e-10,
                "row {i}: expected pos2=20.0, got {val}"
            );
        }
    }
}