-
Notifications
You must be signed in to change notification settings - Fork 23
Expand file tree
/
Copy pathmod.rs
More file actions
1389 lines (1223 loc) · 50.2 KB
/
Copy pathmod.rs
File metadata and controls
1389 lines (1223 loc) · 50.2 KB
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
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//! Data source abstraction layer for ggsql
//!
//! The reader module provides a pluggable interface for executing SQL queries
//! against various data sources and returning Polars DataFrames for visualization.
//!
//! # Architecture
//!
//! All readers implement the `Reader` trait, which provides:
//! - SQL query execution → DataFrame conversion
//! - Visualization query execution → Spec
//! - Optional DataFrame registration for queryable tables
//! - Connection management and error handling
//!
//! # Example
//!
//! ```rust,ignore
//! use ggsql::reader::{Reader, DuckDBReader};
//! use ggsql::writer::{Writer, VegaLiteWriter};
//!
//! // Execute a ggsql query
//! let reader = DuckDBReader::from_connection_string("duckdb://memory")?;
//! let spec = reader.execute("SELECT 1 as x, 2 as y VISUALISE x, y DRAW point")?;
//!
//! // Render to Vega-Lite JSON
//! let writer = VegaLiteWriter::new();
//! let json = writer.render(&spec)?;
//!
//! // With DataFrame registration
//! let mut reader = DuckDBReader::from_connection_string("duckdb://memory")?;
//! reader.register("my_table", some_dataframe, false)?;
//! let spec = reader.execute("SELECT * FROM my_table VISUALISE x, y DRAW point")?;
//! ```
use std::collections::HashMap;
use crate::execute::prepare_data_with_reader;
use crate::plot::{CastTargetType, Plot};
use crate::validate::{validate, ValidationWarning};
use crate::{naming, DataFrame, GgsqlError, Result};
// =============================================================================
// SQL Dialect
// =============================================================================
/// SQL type names and functionality in the syntax supported by that backend.
///
/// Default implementations produce portable ANSI SQL.
pub trait SqlDialect {
/// SQL type name for numeric columns (e.g., "DOUBLE PRECISION")
fn number_type_name(&self) -> Option<&str> {
Some("DOUBLE PRECISION")
}
/// SQL type name for integer columns (e.g., "BIGINT")
fn integer_type_name(&self) -> Option<&str> {
Some("BIGINT")
}
/// SQL type name for DATE columns (e.g., "DATE")
fn date_type_name(&self) -> Option<&str> {
Some("DATE")
}
/// SQL type name for DATETIME/TIMESTAMP columns
fn datetime_type_name(&self) -> Option<&str> {
Some("TIMESTAMP")
}
/// SQL type name for TIME columns
fn time_type_name(&self) -> Option<&str> {
Some("TIME")
}
/// SQL type name for STRING/VARCHAR columns
fn string_type_name(&self) -> Option<&str> {
Some("VARCHAR")
}
/// SQL type name for BOOLEAN columns
fn boolean_type_name(&self) -> Option<&str> {
Some("BOOLEAN")
}
/// Get the SQL type name for a cast target type.
fn type_name_for(&self, target: CastTargetType) -> Option<&str> {
match target {
CastTargetType::Number => self.number_type_name(),
CastTargetType::Integer => self.integer_type_name(),
CastTargetType::Date => self.date_type_name(),
CastTargetType::DateTime => self.datetime_type_name(),
CastTargetType::Time => self.time_type_name(),
CastTargetType::String => self.string_type_name(),
CastTargetType::Boolean => self.boolean_type_name(),
}
}
// =========================================================================
// Schema introspection queries (for Connections pane)
// =========================================================================
/// SQL to list catalog names. Returns rows with column `catalog_name`.
fn sql_list_catalogs(&self) -> String {
"SELECT DISTINCT catalog_name FROM information_schema.schemata ORDER BY catalog_name".into()
}
/// SQL to list schema names within a catalog. Returns rows with column `schema_name`.
fn sql_list_schemas(&self, catalog: &str) -> String {
format!(
"SELECT DISTINCT schema_name FROM information_schema.schemata \
WHERE catalog_name = '{}' ORDER BY schema_name",
catalog.replace('\'', "''")
)
}
/// SQL to list tables/views within a catalog and schema.
/// Returns rows with columns `table_name` and `table_type`.
fn sql_list_tables(&self, catalog: &str, schema: &str) -> String {
format!(
"SELECT DISTINCT table_name, table_type FROM information_schema.tables \
WHERE table_catalog = '{}' AND table_schema = '{}' ORDER BY table_name",
catalog.replace('\'', "''"),
schema.replace('\'', "''")
)
}
/// SQL to list columns in a table.
/// Returns rows with columns `column_name` and `data_type`.
fn sql_list_columns(&self, catalog: &str, schema: &str, table: &str) -> String {
format!(
"SELECT column_name, data_type FROM information_schema.columns \
WHERE table_catalog = '{}' AND table_schema = '{}' AND table_name = '{}' \
ORDER BY ordinal_position",
catalog.replace('\'', "''"),
schema.replace('\'', "''"),
table.replace('\'', "''")
)
}
/// Scalar MAX across any number of SQL expressions.
fn sql_greatest(&self, exprs: &[&str]) -> String {
let mut result = exprs[0].to_string();
for expr in &exprs[1..] {
result =
format!("(CASE WHEN ({result}) >= ({expr}) THEN ({result}) ELSE ({expr}) END)");
}
result
}
/// Scalar MIN across any number of SQL expressions.
fn sql_least(&self, exprs: &[&str]) -> String {
let mut result = exprs[0].to_string();
for expr in &exprs[1..] {
result =
format!("(CASE WHEN ({result}) <= ({expr}) THEN ({result}) ELSE ({expr}) END)");
}
result
}
/// Generate a series of integers 0..n-1 as a CTE fragment.
///
/// Returns CTE fragment(s) producing table `__ggsql_seq__` with column `n`.
fn sql_generate_series(&self, n: usize) -> String {
// Uses a cube-root decomposition to avoid deep recursion: only recurses
// ~cbrt(n) times, then cross-joins three copies to cover the full range.
let base_size = (n as f64).cbrt().ceil() as usize;
let base_sq = base_size * base_size;
let base_max = base_size - 1;
format!(
"\"__ggsql_base__\"(n) AS (\
SELECT 0 UNION ALL SELECT n + 1 FROM \"__ggsql_base__\" WHERE n < {base_max}\
),\
\"__ggsql_seq__\"(n) AS (\
SELECT CAST(a.n * {base_sq} + b.n * {base_size} + c.n AS REAL) AS n \
FROM \"__ggsql_base__\" a, \"__ggsql_base__\" b, \"__ggsql_base__\" c \
WHERE a.n * {base_sq} + b.n * {base_size} + c.n < {n}\
)"
)
}
/// Compute a percentile of a column
///
/// Returns a scalar subquery expression that computes the specified percentile
/// of a column within an optional grouping context.
fn sql_percentile(&self, column: &str, fraction: f64, from: &str, groups: &[String]) -> String {
// Uses NTILE(4) to divide data into quartiles, then interpolates between boundaries.
let group_filter = groups
.iter()
.map(|g| {
let q = naming::quote_ident(g);
format!(
"AND {pct}.{q} IS NOT DISTINCT FROM {qt}.{q}",
pct = naming::quote_ident("__ggsql_pct__"),
qt = naming::quote_ident("__ggsql_qt__")
)
})
.collect::<Vec<_>>()
.join(" ");
let lo_tile = (fraction * 4.0).ceil() as usize;
let hi_tile = lo_tile + 1;
let quoted_column = naming::quote_ident(column);
format!(
"(SELECT (\
MAX(CASE WHEN __tile = {lo_tile} THEN __val END) + \
MIN(CASE WHEN __tile = {hi_tile} THEN __val END)\
) / 2.0 \
FROM (\
SELECT {column} AS __val, \
NTILE(4) OVER (ORDER BY {column}) AS __tile \
FROM ({from}) AS \"__ggsql_pct__\" \
WHERE {column} IS NOT NULL {group_filter}\
))",
column = quoted_column
)
}
/// SQL literal for a date value (days since Unix epoch).
fn sql_date_literal(&self, days_since_epoch: i32) -> String {
format!(
"CAST(DATE '1970-01-01' + INTERVAL {} DAY AS DATE)",
days_since_epoch
)
}
/// SQL literal for a datetime value (microseconds since Unix epoch).
fn sql_datetime_literal(&self, microseconds_since_epoch: i64) -> String {
format!(
"TIMESTAMP '1970-01-01 00:00:00' + INTERVAL {} MICROSECOND",
microseconds_since_epoch
)
}
/// SQL literal for a time value (nanoseconds since midnight).
fn sql_time_literal(&self, nanoseconds_since_midnight: i64) -> String {
let seconds = nanoseconds_since_midnight / 1_000_000_000;
let nanos = nanoseconds_since_midnight % 1_000_000_000;
format!(
"TIME '00:00:00' + INTERVAL {} SECOND + INTERVAL {} NANOSECOND",
seconds, nanos
)
}
/// SQL literal for a boolean value.
fn sql_boolean_literal(&self, value: bool) -> String {
if value {
"TRUE".to_string()
} else {
"FALSE".to_string()
}
}
/// Build the DDL statement(s) needed to (re)create a temporary table
/// that holds the result of `body_sql`.
///
/// Column aliases from `WITH t(a, b) AS (...)` are preserved portably by
/// wrapping the body in a named CTE with a column alias list, so the
/// backend never needs to support `CREATE TABLE t(a, b) AS ...` syntax.
///
/// Returned statements must be executed in order via `Reader::execute_sql`.
fn create_or_replace_temp_table_sql(
&self,
name: &str,
column_aliases: &[String],
body_sql: &str,
) -> Vec<String> {
let body = wrap_with_column_aliases(body_sql, column_aliases);
vec![format!(
"CREATE OR REPLACE TEMP TABLE {} AS {}",
naming::quote_ident(name),
body
)]
}
}
/// Wrap a body SQL in a CTE with a column alias list when aliases are present.
/// This is a portable way to rename the body's output columns without relying
/// on `CREATE TABLE t(a, b) AS ...` (which SQLite does not support).
pub(crate) fn wrap_with_column_aliases(body_sql: &str, column_aliases: &[String]) -> String {
if column_aliases.is_empty() {
return body_sql.to_string();
}
let cols = column_aliases
.iter()
.map(|c| naming::quote_ident(c))
.collect::<Vec<_>>()
.join(", ");
format!(
"WITH __ggsql_aliased__({}) AS ({}) SELECT * FROM __ggsql_aliased__",
cols, body_sql
)
}
pub struct AnsiDialect;
impl SqlDialect for AnsiDialect {}
#[cfg(feature = "duckdb")]
pub mod duckdb;
#[cfg(feature = "sqlite")]
pub mod sqlite;
#[cfg(feature = "odbc")]
pub mod odbc;
#[cfg(feature = "odbc")]
pub mod snowflake;
#[cfg(feature = "odbc")]
pub mod exasol;
pub mod connection;
pub mod data;
mod spec;
#[cfg(feature = "duckdb")]
pub use duckdb::DuckDBReader;
#[cfg(feature = "sqlite")]
pub use sqlite::SqliteReader;
#[cfg(feature = "odbc")]
pub use odbc::OdbcReader;
// ============================================================================
// Shared utilities
// ============================================================================
/// Validate a table name for use in SQL statements.
///
/// Rejects empty names and names containing null bytes or newlines.
pub(crate) fn validate_table_name(name: &str) -> Result<()> {
if name.is_empty() {
return Err(GgsqlError::ReaderError("Table name cannot be empty".into()));
}
let forbidden = ['\0', '\n', '\r'];
for ch in forbidden {
if name.contains(ch) {
return Err(GgsqlError::ReaderError(format!(
"Table name '{}' contains invalid character '{}'",
name,
ch.escape_default()
)));
}
}
Ok(())
}
/// Does the SQL statement return rows?
///
/// Looks at the first keyword to decide: `SELECT`, `WITH`, `FROM`,
/// `DESCRIBE`, `SHOW` and `EXPLAIN` produce result sets; everything else
/// (DDL, DML) does not.
pub(crate) fn returns_rows(sql: &str) -> bool {
let first_word = sql.split_whitespace().next().unwrap_or("");
matches!(
first_word.to_ascii_uppercase().as_str(),
"SELECT" | "WITH" | "DESCRIBE" | "SHOW" | "EXPLAIN" | "FROM"
)
}
// ============================================================================
// Spec - Result of reader.execute()
// ============================================================================
/// Result of executing a ggsql query, ready for rendering.
pub struct Spec {
/// Single resolved plot specification
pub(crate) plot: Plot,
/// Internal data map (global + layer-specific DataFrames)
pub(crate) data: HashMap<String, DataFrame>,
/// Cached metadata about the prepared visualization
pub(crate) metadata: Metadata,
/// The main SQL query that was executed
pub(crate) sql: String,
/// The raw VISUALISE portion text
pub(crate) visual: String,
/// Per-layer filter/source queries (None = uses global data directly)
pub(crate) layer_sql: Vec<Option<String>>,
/// Per-layer stat transform queries (None = no stat transform)
pub(crate) stat_sql: Vec<Option<String>>,
/// Validation warnings from preparation
pub(crate) warnings: Vec<ValidationWarning>,
}
/// Metadata about the prepared visualization.
#[derive(Debug, Clone)]
pub struct Metadata {
pub rows: usize,
pub columns: Vec<String>,
pub layer_count: usize,
}
// ============================================================================
// Reader Trait
// ============================================================================
/// Trait for data source readers
///
/// Readers execute SQL queries and return Polars DataFrames.
/// They provide a uniform interface for different database backends.
///
/// # DataFrame Registration
///
/// Readers support registering DataFrames as queryable tables using
/// the [`register`](Reader::register) method. This allows you to query
/// in-memory DataFrames with SQL, join them with other tables, etc.
///
/// ```rust,ignore
/// // Register a DataFrame (takes ownership)
/// reader.register("sales", sales_df, false)?;
///
/// // Now you can query it
/// let result = reader.execute_sql("SELECT * FROM sales WHERE amount > 100")?;
/// ```
pub trait Reader {
/// Execute a SQL query and return the result as a DataFrame
///
/// # Arguments
///
/// * `sql` - The SQL query to execute
///
/// # Returns
///
/// A Polars DataFrame containing the query results
///
/// # Errors
///
/// Returns `GgsqlError::ReaderError` if:
/// - The SQL is invalid
/// - The connection fails
/// - The table or columns don't exist
fn execute_sql(&self, sql: &str) -> Result<DataFrame>;
/// Register a DataFrame as a queryable table (takes ownership)
///
/// After registration, the DataFrame can be queried by name in SQL:
/// ```sql
/// SELECT * FROM <name> WHERE ...
/// ```
///
/// # Arguments
///
/// * `name` - The table name to register under
/// * `df` - The DataFrame to register (ownership is transferred)
/// * `replace` - If true, replace any existing table with the same name.
/// If false, return an error if the table already exists.
///
/// # Returns
///
/// `Ok(())` on success, error if registration fails.
fn register(&self, name: &str, df: DataFrame, replace: bool) -> Result<()>;
/// Unregister a previously registered table
///
/// # Arguments
///
/// * `name` - The table name to unregister
///
/// # Returns
///
/// `Ok(())` on success.
///
/// # Default Implementation
///
/// Returns an error by default. Override for readers that support unregistration.
fn unregister(&self, name: &str) -> Result<()> {
Err(GgsqlError::ReaderError(format!(
"This reader does not support unregistering table '{}'",
name
)))
}
/// Execute a ggsql query and return the visualization specification.
///
/// This is the main entry point for creating visualizations. It parses the query,
/// executes the SQL portion, and returns a `Spec` ready for rendering.
///
/// # Arguments
///
/// * `query` - The ggsql query (SQL + VISUALISE clause)
///
/// # Returns
///
/// A `Spec` containing the resolved visualization specification and data.
///
/// # Errors
///
/// Returns an error if:
/// - The query syntax is invalid
/// - The query has no VISUALISE clause
/// - The SQL execution fails
///
/// # Example
///
/// ```rust,ignore
/// use ggsql::reader::{Reader, DuckDBReader};
/// use ggsql::writer::{Writer, VegaLiteWriter};
///
/// let mut reader = DuckDBReader::from_connection_string("duckdb://memory")?;
/// let spec = reader.execute("SELECT 1 as x, 2 as y VISUALISE x, y DRAW point")?;
///
/// let writer = VegaLiteWriter::new();
/// let json = writer.render(&spec)?;
/// ```
fn execute(&self, query: &str) -> Result<Spec>;
/// Get the SQL dialect for this reader.
///
/// Database-specific SQL type names and SQL generation methods
fn dialect(&self) -> &dyn SqlDialect {
&AnsiDialect
}
}
/// Execute a ggsql query using any reader
///
/// This is the shared implementation behind `Reader::execute()`. Concrete
/// readers delegate to this so the trait stays object-safe (no `Self: Sized`
/// bound on `execute`).
pub fn execute_with_reader(reader: &dyn Reader, query: &str) -> Result<Spec> {
let validated = validate(query)?;
let warnings: Vec<ValidationWarning> = validated.warnings().to_vec();
let prepared_data = prepare_data_with_reader(query, reader)?;
let plot =
prepared_data.specs.into_iter().next().ok_or_else(|| {
GgsqlError::ValidationError("No visualization spec found".to_string())
})?;
let layer_sql = vec![None; plot.layers.len()];
let stat_sql = vec![None; plot.layers.len()];
Ok(Spec::new(
plot,
prepared_data.data,
prepared_data.sql,
prepared_data.visual,
layer_sql,
stat_sql,
warnings,
))
}
#[cfg(test)]
#[cfg(all(feature = "duckdb", feature = "vegalite"))]
mod tests {
use super::*;
use crate::df;
use crate::writer::{VegaLiteWriter, Writer};
#[test]
fn test_execute_and_render() {
let reader = DuckDBReader::from_connection_string("duckdb://memory").unwrap();
let spec = reader
.execute("SELECT 1 as x, 2 as y VISUALISE x, y DRAW point")
.unwrap();
assert_eq!(spec.plot().layers.len(), 1);
assert_eq!(spec.metadata().layer_count, 1);
assert!(spec.layer_data(0).is_some());
let writer = VegaLiteWriter::new();
let result = writer.render(&spec).unwrap();
assert!(result.contains("point"));
}
#[test]
fn test_execute_metadata() {
let reader = DuckDBReader::from_connection_string("duckdb://memory").unwrap();
let spec = reader
.execute(
"SELECT * FROM (VALUES (1, 10), (2, 20), (3, 30)) AS t(x, y) VISUALISE x, y DRAW point",
)
.unwrap();
let metadata = spec.metadata();
assert_eq!(metadata.rows, 3);
// Columns now includes both user mappings (pos1, pos2) and resolved defaults (size, stroke, fill, opacity, shape, linewidth)
// Aesthetics are transformed to internal names (x -> pos1, y -> pos2)
assert!(metadata.columns.contains(&"pos1".to_string()));
assert!(metadata.columns.contains(&"pos2".to_string()));
assert_eq!(metadata.layer_count, 1);
}
#[test]
fn test_execute_with_cte() {
let reader = DuckDBReader::from_connection_string("duckdb://memory").unwrap();
let query = r#"
WITH data AS (
SELECT * FROM (VALUES (1, 10), (2, 20)) AS t(x, y)
)
SELECT * FROM data
VISUALISE x, y DRAW point
"#;
let spec = reader.execute(query).unwrap();
assert_eq!(spec.plot().layers.len(), 1);
assert!(spec.layer_data(0).is_some());
let df = spec.layer_data(0).unwrap();
assert_eq!(df.height(), 2);
}
#[test]
fn test_render_multi_layer() {
let reader = DuckDBReader::from_connection_string("duckdb://memory").unwrap();
let query = r#"
SELECT * FROM (VALUES (1, 10), (2, 20), (3, 30)) AS t(x, y)
VISUALISE
DRAW point MAPPING x AS x, y AS y
DRAW line MAPPING x AS x, y AS y
"#;
let spec = reader.execute(query).unwrap();
let writer = VegaLiteWriter::new();
let result = writer.render(&spec).unwrap();
assert!(result.contains("layer"));
}
#[test]
fn test_polar_project_with_start() {
let reader = DuckDBReader::from_connection_string("duckdb://memory").unwrap();
let query = r#"
SELECT * FROM (VALUES ('A', 10), ('B', 20), ('C', 30)) AS t(category, value)
VISUALISE value AS y, category AS fill
DRAW bar
PROJECT y, x TO polar SETTING start => 90
"#;
let spec = reader.execute(query).unwrap();
let writer = VegaLiteWriter::new();
let result = writer.render(&spec).unwrap();
// Parse the JSON to verify the theta scale range is set correctly
let json: serde_json::Value = serde_json::from_str(&result).unwrap();
// The encoding should have a theta channel with a scale range offset by 90 degrees
// 90 degrees = π/2 radians
let layer = json["layer"].as_array().unwrap().first().unwrap();
let theta = &layer["encoding"]["theta"];
assert!(theta.is_object(), "theta encoding should exist");
// Check that the scale has a range with the start offset
let scale = &theta["scale"];
let range = scale["range"].as_array().unwrap();
assert_eq!(range.len(), 2);
// π/2 ≈ 1.5707963
let start = range[0].as_f64().unwrap();
assert!(
(start - std::f64::consts::FRAC_PI_2).abs() < 0.001,
"start should be π/2 (90 degrees), got {}",
start
);
// π/2 + 2π ≈ 7.8539816
let end = range[1].as_f64().unwrap();
let expected_end = std::f64::consts::FRAC_PI_2 + 2.0 * std::f64::consts::PI;
assert!(
(end - expected_end).abs() < 0.001,
"end should be π/2 + 2π, got {}",
end
);
}
#[test]
fn test_polar_project_default_start() {
let reader = DuckDBReader::from_connection_string("duckdb://memory").unwrap();
let query = r#"
SELECT * FROM (VALUES ('A', 10), ('B', 20), ('C', 30)) AS t(category, value)
VISUALISE value AS y, category AS fill
DRAW bar
PROJECT y, x TO polar
"#;
let spec = reader.execute(query).unwrap();
let writer = VegaLiteWriter::new();
let result = writer.render(&spec).unwrap();
// Parse the JSON
let json: serde_json::Value = serde_json::from_str(&result).unwrap();
// The theta encoding should NOT have a scale with range when start is 0 (default)
let layer = json["layer"].as_array().unwrap().first().unwrap();
let theta = &layer["encoding"]["theta"];
assert!(theta.is_object(), "theta encoding should exist");
// Either no scale, or no range in scale (since default is 0)
if let Some(scale) = theta.get("scale") {
assert!(
scale.get("range").is_none(),
"theta scale should not have range when start is 0"
);
}
}
#[test]
fn test_polar_project_with_end() {
let reader = DuckDBReader::from_connection_string("duckdb://memory").unwrap();
let query = r#"
SELECT * FROM (VALUES ('A', 10), ('B', 20)) AS t(category, value)
VISUALISE value AS y, category AS fill
DRAW bar
PROJECT y, x TO polar SETTING start => -90, end => 90
"#;
let spec = reader.execute(query).unwrap();
let writer = VegaLiteWriter::new();
let result = writer.render(&spec).unwrap();
let json: serde_json::Value = serde_json::from_str(&result).unwrap();
let layer = json["layer"].as_array().unwrap().first().unwrap();
let theta = &layer["encoding"]["theta"];
let range = theta["scale"]["range"].as_array().unwrap();
// -90° = -π/2 ≈ -1.5708, 90° = π/2 ≈ 1.5708
let start = range[0].as_f64().unwrap();
let end = range[1].as_f64().unwrap();
assert!(
(start - (-std::f64::consts::FRAC_PI_2)).abs() < 0.001,
"start should be -π/2 (-90 degrees), got {}",
start
);
assert!(
(end - std::f64::consts::FRAC_PI_2).abs() < 0.001,
"end should be π/2 (90 degrees), got {}",
end
);
}
#[test]
fn test_polar_project_with_end_only() {
// Test using end without explicit start (start defaults to 0)
let reader = DuckDBReader::from_connection_string("duckdb://memory").unwrap();
let query = r#"
SELECT * FROM (VALUES ('A', 10), ('B', 20)) AS t(category, value)
VISUALISE value AS y, category AS fill
DRAW bar
PROJECT y, x TO polar SETTING end => 180
"#;
let spec = reader.execute(query).unwrap();
let writer = VegaLiteWriter::new();
let result = writer.render(&spec).unwrap();
let json: serde_json::Value = serde_json::from_str(&result).unwrap();
let layer = json["layer"].as_array().unwrap().first().unwrap();
let theta = &layer["encoding"]["theta"];
let range = theta["scale"]["range"].as_array().unwrap();
// start=0 (default), end=180° = π
let start = range[0].as_f64().unwrap();
let end = range[1].as_f64().unwrap();
assert!(
start.abs() < 0.001,
"start should be 0 (default), got {}",
start
);
assert!(
(end - std::f64::consts::PI).abs() < 0.001,
"end should be π (180 degrees), got {}",
end
);
}
#[test]
fn test_polar_encoding_keys_independent_of_user_names() {
// This test verifies that polar projections always produce theta/radius encoding keys
// in Vega-Lite output, regardless of what position names the user specified in PROJECT.
// This is critical because Vega-Lite expects specific channel names for polar marks.
let reader = DuckDBReader::from_connection_string("duckdb://memory").unwrap();
// Helper to check encoding keys
fn check_encoding_keys(json: &serde_json::Value, test_name: &str) {
let layer = json["layer"].as_array().unwrap().first().unwrap();
assert!(
layer["encoding"].get("theta").is_some(),
"{} should produce theta encoding, got keys: {:?}",
test_name,
layer["encoding"]
.as_object()
.map(|o| o.keys().collect::<Vec<_>>())
);
// Also verify no x or y keys exist (they should be mapped to theta/radius)
assert!(
layer["encoding"].get("x").is_none(),
"{} should NOT have x encoding in polar mode",
test_name
);
assert!(
layer["encoding"].get("y").is_none(),
"{} should NOT have y encoding in polar mode",
test_name
);
}
// Test case 1: PROJECT y, x TO polar (y as pos1→radius, x as pos2→theta)
let query1 = r#"
SELECT * FROM (VALUES ('A', 10), ('B', 20)) AS t(category, value)
VISUALISE value AS y, category AS fill
DRAW bar
PROJECT y, x TO polar
"#;
let spec1 = reader.execute(query1).unwrap();
let writer = VegaLiteWriter::new();
let result1 = writer.render(&spec1).unwrap();
let json1: serde_json::Value = serde_json::from_str(&result1).unwrap();
check_encoding_keys(&json1, "PROJECT y, x TO polar");
// Test case 2: PROJECT x, y TO polar (x as pos1→radius, y as pos2→theta)
let query2 = r#"
SELECT * FROM (VALUES ('A', 10), ('B', 20)) AS t(category, value)
VISUALISE value AS x, category AS fill
DRAW bar
PROJECT x, y TO polar
"#;
let spec2 = reader.execute(query2).unwrap();
let result2 = writer.render(&spec2).unwrap();
let json2: serde_json::Value = serde_json::from_str(&result2).unwrap();
check_encoding_keys(&json2, "PROJECT x, y TO polar");
// Test case 3: PROJECT TO polar (default radius/angle names)
let query3 = r#"
SELECT * FROM (VALUES ('A', 10), ('B', 20)) AS t(category, value)
VISUALISE value AS angle, category AS fill
DRAW bar
PROJECT TO polar
"#;
let spec3 = reader.execute(query3).unwrap();
let result3 = writer.render(&spec3).unwrap();
let json3: serde_json::Value = serde_json::from_str(&result3).unwrap();
check_encoding_keys(&json3, "PROJECT TO polar");
// Test case 4: PROJECT a, b TO polar (custom aesthetic names)
let query4 = r#"
SELECT * FROM (VALUES ('A', 10), ('B', 20)) AS t(category, value)
VISUALISE value AS a, category AS fill
DRAW bar
PROJECT a, b TO polar
"#;
let spec4 = reader.execute(query4).unwrap();
let result4 = writer.render(&spec4).unwrap();
let json4: serde_json::Value = serde_json::from_str(&result4).unwrap();
check_encoding_keys(&json4, "PROJECT a, b TO polar (custom names)");
}
#[test]
fn test_cartesian_encoding_keys_with_custom_names() {
// This test verifies that cartesian projections produce x/y encoding keys
// even when custom position names are used in PROJECT.
let reader = DuckDBReader::from_connection_string("duckdb://memory").unwrap();
fn check_cartesian_keys(json: &serde_json::Value, test_name: &str) {
let layer = json["layer"].as_array().unwrap().first().unwrap();
assert!(
layer["encoding"].get("x").is_some(),
"{} should produce x encoding, got keys: {:?}",
test_name,
layer["encoding"]
.as_object()
.map(|o| o.keys().collect::<Vec<_>>())
);
// Verify no theta/radius keys exist
assert!(
layer["encoding"].get("theta").is_none(),
"{} should NOT have theta encoding in cartesian mode",
test_name
);
}
// Test case: PROJECT a, b TO cartesian (custom aesthetic names)
let query = r#"
SELECT * FROM (VALUES ('A', 10), ('B', 20)) AS t(category, value)
VISUALISE category AS a, value AS b
DRAW bar
PROJECT a, b TO cartesian
"#;
let spec = reader.execute(query).unwrap();
let writer = VegaLiteWriter::new();
let result = writer.render(&spec).unwrap();
let json: serde_json::Value = serde_json::from_str(&result).unwrap();
check_cartesian_keys(&json, "PROJECT a, b TO cartesian (custom names)");
}
#[test]
fn test_register_and_query() {
let reader = DuckDBReader::from_connection_string("duckdb://memory").unwrap();
let df = df! {
"x" => vec![1i32, 2, 3],
"y" => vec![10i32, 20, 30],
}
.unwrap();
reader.register("my_data", df, false).unwrap();
let query = "SELECT * FROM my_data VISUALISE x, y DRAW point";
let spec = reader.execute(query).unwrap();
assert_eq!(spec.metadata().rows, 3);
// Aesthetics are transformed to internal names (x -> pos1)
assert!(spec.metadata().columns.contains(&"pos1".to_string()));
let writer = VegaLiteWriter::new();
let result = writer.render(&spec).unwrap();
assert!(result.contains("point"));
}
#[test]
fn test_register_and_join() {
let reader = DuckDBReader::from_connection_string("duckdb://memory").unwrap();
let sales = df! {
"id" => vec![1i32, 2, 3],
"amount" => vec![100i32, 200, 300],
"product_id" => vec![1i32, 1, 2],
}
.unwrap();
let products = df! {
"id" => vec![1i32, 2],
"name" => vec!["Widget", "Gadget"],
}
.unwrap();
reader.register("sales", sales, false).unwrap();
reader.register("products", products, false).unwrap();
let query = r#"
SELECT s.id, s.amount, p.name
FROM sales s
JOIN products p ON s.product_id = p.id
VISUALISE id AS x, amount AS y
DRAW bar
"#;
let spec = reader.execute(query).unwrap();
assert_eq!(spec.metadata().rows, 3);
}
#[test]
fn test_execute_no_viz_fails() {
let reader = DuckDBReader::from_connection_string("duckdb://memory").unwrap();
let query = "SELECT 1 as x, 2 as y";
let result = reader.execute(query);
assert!(result.is_err());
}
#[test]
fn test_binned_fill_legend_renders_threshold_scale() {
// End-to-end test for binned fill scale rendering to Vega-Lite
// Verifies that binned material aesthetics use threshold scale type
let reader = DuckDBReader::from_connection_string("duckdb://memory").unwrap();
// Create data with values that span the binned range
// Binned scales use FROM [min, max] for range and SETTING breaks => [...] for explicit breaks
let query = r#"
SELECT * FROM (VALUES
(1, 10, 15.0),
(2, 20, 35.0),
(3, 30, 55.0),
(4, 40, 85.0)
) AS t(x, y, value)
VISUALISE
DRAW point MAPPING x AS x, y AS y, value AS fill
SCALE BINNED fill FROM [0, 100] TO viridis SETTING breaks => [0, 25, 50, 75, 100]
"#;
let spec = reader.execute(query).unwrap();
// Verify spec structure
assert_eq!(spec.plot().layers.len(), 1);
// Note: scales may include auto-generated x/y scales plus the explicit fill scale
assert!(
spec.plot().find_scale("fill").is_some(),
"Should have a fill scale"
);
// Render to Vega-Lite
let writer = VegaLiteWriter::new();
let result = writer.render(&spec).unwrap();
let vl: serde_json::Value = serde_json::from_str(&result).unwrap();
// Verify threshold scale type for fill
let fill_scale = &vl["layer"][0]["encoding"]["fill"]["scale"];
assert_eq!(
fill_scale["type"],
"threshold",
"Binned fill should use threshold scale type. Got: {}",
serde_json::to_string_pretty(&vl["layer"][0]["encoding"]["fill"]).unwrap()
);
// Verify internal breaks as domain (excludes first and last terminals)
// breaks = [0, 25, 50, 75, 100] → domain = [25, 50, 75]