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
use crate::proto::gen::tasks::Variable;
use crate::spec::chart::{ChartSpec, MutChartVisitor};
use crate::spec::data::DataSpec;
use crate::spec::scale::{
    ScaleDataReferenceSort, ScaleDataReferenceSortParameters, ScaleDataReferenceSpec,
    ScaleDataReferencesSpec, ScaleDomainSpec, ScaleSpec, ScaleTypeSpec,
};
use crate::spec::transform::aggregate::AggregateOpSpec;
use crate::task_graph::graph::ScopedVariable;
use crate::task_graph::scope::TaskScope;
use itertools::Itertools;
use std::collections::HashMap;
use vegafusion_common::error::Result;
use vegafusion_common::escape::escape_field;

/// This optimization extracts the intensive data processing from scale.domain.data specifications
/// into dedicated datasets. Domain calculations can't be entirely evaluated on the server, but
/// this approach still allows the heavy data processing to happen on the server, and to avoid
/// serializing the full dataset to send to the client.
pub fn split_domain_data(
    spec: &mut ChartSpec,
) -> Result<HashMap<ScopedVariable, (ScopedVariable, String)>> {
    let task_scope = spec.to_task_scope()?;
    let mut visitor = SplitScaleDomainVisitor::new(&task_scope);
    spec.walk_mut(&mut visitor)?;
    for (scope, data) in visitor.new_datasets {
        if scope.is_empty() {
            spec.data.push(data);
        } else {
            let group = spec.get_nested_group_mut(scope.as_slice())?;
            group.data.push(data);
        }
    }

    Ok(visitor.domain_dataset_fields)
}

#[derive(Debug, Clone)]
pub struct SplitScaleDomainVisitor<'a> {
    pub task_scope: &'a TaskScope,
    pub new_datasets: Vec<(Vec<u32>, DataSpec)>,
    pub domain_dataset_fields: HashMap<ScopedVariable, (ScopedVariable, String)>,
}

impl<'a> SplitScaleDomainVisitor<'a> {
    pub fn new(task_scope: &'a TaskScope) -> Self {
        Self {
            new_datasets: Vec::new(),
            task_scope,
            domain_dataset_fields: Default::default(),
        }
    }
}

impl<'a> MutChartVisitor for SplitScaleDomainVisitor<'a> {
    fn visit_scale(&mut self, scale: &mut ScaleSpec, scope: &[u32]) -> Result<()> {
        if let Some(domain) = scale.domain.clone() {
            match domain {
                ScaleDomainSpec::FieldReference(field_ref) => {
                    self.split_field_reference_domain(scale, scope, &field_ref)?;
                }
                ScaleDomainSpec::FieldsReference(fields_ref) => {
                    self.split_fields_reference_domain(scale, scope, &fields_ref)?;
                }
                _ => {}
            }
        }
        Ok(())
    }
}

impl<'a> SplitScaleDomainVisitor<'a> {
    fn split_fields_reference_domain(
        &mut self,
        scale: &mut ScaleSpec,
        scope: &[u32],
        fields_ref: &ScaleDataReferencesSpec,
    ) -> Result<()> {
        let discrete_scale = scale.type_.clone().unwrap_or_default().is_discrete();
        let (new_datasets, new_dataset_scope, new_domain) = if discrete_scale {
            // Extract sort field and op
            let (sort_field, sort_op) = match &fields_ref.sort {
                Some(ScaleDataReferenceSort::Parameters(sort_params)) => {
                    (sort_params.field.clone(), sort_params.op.clone())
                }
                _ => (None, None),
            };

            // Iterate over data fields
            let mut new_datasets = Vec::new();
            let mut new_dataset_scope = Vec::new();
            let mut new_fields = Vec::new();
            for (field_index, field_ref) in fields_ref.fields.iter().enumerate() {
                let field_name = &field_ref.field;
                let data_name = field_ref.data.clone();
                let scope_suffix = Self::build_scope_suffix(scope);

                let new_data_name = format!(
                    "{}_{}_domain_{}{}_{}",
                    data_name, scale.name, field_name, scope_suffix, field_index
                );

                let new_data = Self::make_discrete_domain_data(
                    &data_name,
                    &new_data_name,
                    field_name,
                    sort_field.clone(),
                    sort_op.clone(),
                )?;
                new_datasets.push(new_data);

                // Compute new domain field
                let mut new_field_ref = field_ref.clone();
                new_field_ref.data = new_data_name.clone();
                new_fields.push(new_field_ref);

                // Compute scope for the original referenced dataset
                let resolved = self
                    .task_scope
                    .resolve_scope(&Variable::new_data(data_name.as_str()), scope)?;
                new_dataset_scope.push(resolved.scope);
            }

            // Create new domain specification that uses the new fields
            let sort = match &fields_ref.sort {
                Some(ScaleDataReferenceSort::Parameters(sort_params)) => Some(
                    ScaleDataReferenceSort::Parameters(ScaleDataReferenceSortParameters {
                        op: Some(AggregateOpSpec::Max),
                        field: Some("sort_field".to_string()),
                        ..sort_params.clone()
                    }),
                ),
                sort => sort.clone(),
            };

            let new_domain = ScaleDomainSpec::FieldsReference(ScaleDataReferencesSpec {
                fields: new_fields,
                sort,
                extra: Default::default(),
            });

            (new_datasets, new_dataset_scope, new_domain)
        } else {
            // Scale type not supported
            return Ok(());
        };

        // Overwrite scale domain with new domain
        scale.domain = Some(new_domain);

        for (new_dataset, scope) in new_datasets.into_iter().zip(new_dataset_scope) {
            // Add new dataset at current scope
            self.new_datasets.push((scope, new_dataset));
        }

        Ok(())
    }

    fn split_field_reference_domain(
        &mut self,
        scale: &mut ScaleSpec,
        scope: &[u32],
        field_ref: &ScaleDataReferenceSpec,
    ) -> Result<()> {
        let discrete_scale = scale.type_.clone().unwrap_or_default().is_discrete();
        let data_name = field_ref.data.clone();
        let data_var = (Variable::new_data(&data_name), Vec::from(scope));
        let field_name = &field_ref.field;

        // Validate whether we can do anything
        if field_name.contains('.') {
            // Nested fields not supported
            return Ok(());
        }

        let scope_suffix = Self::build_scope_suffix(scope);

        let new_data_name = format!(
            "{}_{}_domain_{}{}",
            data_name, scale.name, field_name, scope_suffix
        );
        let new_data_var = (Variable::new_data(&new_data_name), Vec::from(scope));
        self.domain_dataset_fields
            .insert(new_data_var, (data_var, field_name.clone()));

        let (new_data, new_domain) = if discrete_scale {
            // Extract sort field and op
            let (sort_field, sort_op) =
                if let Some(ScaleDataReferenceSort::Parameters(sort_params)) = &field_ref.sort {
                    (sort_params.field.clone(), sort_params.op.clone())
                } else {
                    (None, None)
                };

            let new_data = Self::make_discrete_domain_data(
                &data_name,
                &new_data_name,
                field_name,
                sort_field,
                sort_op,
            )?;

            // Create new domain specification that uses the new dataset
            let sort = match &field_ref.sort {
                Some(ScaleDataReferenceSort::Parameters(sort_params)) => Some(
                    ScaleDataReferenceSort::Parameters(ScaleDataReferenceSortParameters {
                        op: Some(AggregateOpSpec::Max),
                        field: Some("sort_field".to_string()),
                        ..sort_params.clone()
                    }),
                ),
                sort => sort.clone(),
            };

            let new_domain = ScaleDomainSpec::FieldReference(ScaleDataReferenceSpec {
                data: new_data_name,
                field: field_name.clone(),
                sort,
                extra: Default::default(),
            });

            (new_data, new_domain)
        } else if matches!(
            scale.type_.clone().unwrap_or_default(),
            ScaleTypeSpec::Linear
        ) {
            // Create derived dataset that performs the min/max calculations
            let new_data: DataSpec = serde_json::from_value(serde_json::json!(
                {
                    "name": new_data_name,
                    "source": data_name,
                    "transform": [
                        {
                            "type": "formula",
                            "as": field_name,
                            "expr": format!("+datum['{field_name}']")
                        }, {
                            "type": "aggregate",
                            "fields": [field_name, field_name],
                            "ops": ["min", "max"],
                            "as": ["min", "max"],
                             "groupby": []
                        }
                    ]
                }
            ))
            .unwrap();

            // Create new domain specification that uses the new dataset
            let new_domain: ScaleDomainSpec = serde_json::from_value(serde_json::json!([
                {
                    "signal":
                        format!(
                            "(data(\"{}\")[0] || {{}}).min",
                            escape_field(&new_data_name)
                        )
                },
                {
                    "signal":
                        format!(
                            "(data(\"{}\")[0] || {{}}).max",
                            escape_field(&new_data_name)
                        )
                }
            ]))
            .unwrap();

            (new_data, new_domain)
        } else {
            // Unsupported scale type
            return Ok(());
        };

        // Overwrite scale domain with new domain
        scale.domain = Some(new_domain);

        // Add new dataset at same scope as source dataset
        let resolved = self
            .task_scope
            .resolve_scope(&Variable::new_data(data_name.as_str()), scope)?;

        // Add new dataset at current scope
        self.new_datasets.push((resolved.scope, new_data));
        Ok(())
    }

    fn build_scope_suffix(scope: &[u32]) -> String {
        // Build suffix using scope
        let mut scope_suffix = scope.iter().map(|s| s.to_string()).join("_");
        if !scope_suffix.is_empty() {
            scope_suffix.insert(0, '_');
        }
        scope_suffix
    }

    /// Make a Vega dataset that computes the discrete values of an input dataset with
    /// an optional sorting field
    fn make_discrete_domain_data(
        data_name: &str,
        new_data_name: &str,
        field_name: &String,
        sort_field: Option<String>,
        sort_op: Option<AggregateOpSpec>,
    ) -> Result<DataSpec> {
        Ok(if let Some(sort_op) = sort_op {
            // Will sort by the result of an aggregation operation
            let sort_field = sort_field.unwrap_or_else(|| field_name.clone());
            serde_json::from_value(serde_json::json!(
                {
                    "name": new_data_name,
                    "source": data_name,
                    "transform": [
                        {
                            "type": "aggregate",
                            "as": ["sort_field"],
                            "groupby": [field_name],
                            "ops": [sort_op],
                            "fields": [sort_field]
                        }
                    ]
               }
            ))
            .unwrap()
        } else {
            // Will sort by the grouped field values
            serde_json::from_value(serde_json::json!(
                {
                    "name": new_data_name,
                    "source": data_name,
                    "transform": [
                        {
                            "type": "aggregate",
                            "as": [],
                            "groupby": [field_name],
                            "ops": [],
                            "fields": []
                        }, {
                            "type": "formula",
                            "as": "sort_field",
                            "expr": format!("datum['{field_name}']")
                        }
                    ]
               }
            ))
            .unwrap()
        })
    }
}